Obtain profit from last closed deal (closed) - page 2

 

this is not working with me at all!!

MT4 works perfect!!

  if(OrdersTotalT(OP_BUY)==0 && c==true && check(OP_SELL))
double check(ENUM_ORDER_TYPE type)
{
double yes=false;
int last_trade=HistoryTotal();
if(last_trade>0)
  {
   if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
     {
      if(OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==MagicNumber)
         {
yes=true;
         }
     }
  }
else yes=EMPTY_VALUE;
return yes;
}

Mt5

if(smain && Enable_Trading==true && LastPositionWon(DEAL_TYPE_SELL))
double LastPositionWon(ENUM_DEAL_TYPE Type)
{  
   HistorySelect(0, 0); // Problem here!
   int l_deals = HistoryDealsTotal();
double yes=false;
if(l_deals>0)
  {
 for (int i = 0; i <= (l_deals-1); i++)
 //   for (int i = 0; i < l_deals; i++)
   { 
      ulong l_ticket =  HistoryDealGetTicket(i);  
      int l_magic = (int) HistoryDealGetInteger(l_ticket, DEAL_MAGIC);
      ENUM_DEAL_TYPE l_type = (ENUM_DEAL_TYPE) HistoryDealGetInteger(l_ticket, DEAL_TYPE);
      string l_comment = HistoryDealGetString(l_ticket, DEAL_COMMENT);
      ENUM_DEAL_ENTRY entry_type = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(l_ticket, DEAL_ENTRY);
      // Evaluate and return
      if(l_type == Type && l_comment == comment && l_magic == MagicNumber)
   if(HistoryOrderSelect(l_deals-1)==true)
yes=true;
   }
     }
else yes=EMPTY_VALUE;


   return(yes);     
}

i tried the first code.. but still it still opens order if previous trades are losses

 for (int i = 0; i <= (l_deals-1); i++)
 //   for (int i = 0; i < l_deals; i++)
bool LastPositionWon(ENUM_DEAL_TYPE Type)
{  
   HistorySelect(0, 0); // Problem here!
   int l_deals = HistoryDealsTotal();
   for (int i = 0; i < l_deals; i++)
   { 
      // Ticket
      ulong l_ticket =  HistoryDealGetTicket(i);  
      
      // Magic number 
      int l_magic = (int) HistoryDealGetInteger(l_ticket, DEAL_MAGIC);
      
      // Type
      ENUM_DEAL_TYPE l_type = (ENUM_DEAL_TYPE) HistoryDealGetInteger(l_ticket, DEAL_TYPE);
      
      // Comment
      string l_comment = HistoryDealGetString(l_ticket, DEAL_COMMENT);
      
      // Entry type
      ENUM_DEAL_ENTRY entry_type = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(l_ticket, DEAL_ENTRY);
      
      // Evaluate and return
      if(entry_type == DEAL_ENTRY_OUT && l_type == Type && l_comment == comment && l_magic == MagicNumber)
      {
         double profit = HistoryDealGetDouble(l_ticket, DEAL_PROFIT);
         if(profit > 0)
         {
            return(true); 
         } else { 
            return(false);
         }
      }
   }
   return(true);     
}
 

i tried converting the mt4 script to mt5 tried different codes!!

with last opened position it works fine but not history position

double check(ENUM_ORDER_TYPE type)
{
double yes=false;
int last_trade=HistoryOrdersTotal();
if(last_trade>0)
  {
   if(HistoryOrderSelect(last_trade-1)>0)
     {
      if(OrderGetString(ORDER_SYMBOL)==Symbol() && HistoryDealGetTicket(ORDER_TYPE)==type && OrderGetInteger(ORDER_MAGIC)==MagicNumber)
yes=true;
     }
  }
else yes=EMPTY_VALUE;
return yes;
}

getting last position works well

double check(ENUM_POSITION_TYPE type)
{
double yes=false;
int last_trade=PositionsTotal();
if(last_trade>0)
  {
   if(OrderSelect(last_trade-1)==true)
     {
      if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetTicket(POSITION_TYPE)==type && PositionGetInteger(POSITION_MAGIC)==MagicNumber)
yes=true;
     }
  }
else yes=EMPTY_VALUE;
return yes;
}