Can anyone help me about order select?

BINTARI Uchiha  

Dear the mql5 master coder.

I need help with coding mql5 that doesn't work.
Here is the code.
Mql4 work smoothly:

  double LOSS(int m)
{ double a=0;
 
   for (  int cnt = OrdersHistoryTotal()-1 ; cnt >= 0; cnt--) 
   { 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)) 
      if (OrderSymbol() == Symbol() && OrderMagicNumber() ==  m  ) 
      {  
      
         if(OrderType()<=1){
          if(OrderProfit()>=0)a--;else 
         a++;    
          if(StringFind(OrderComment(),"-0",0)!= -1)break; 
         }
      } 
   } 
   
   
   
  for(int x=0;x<1000;x++){
  if(a<MaxTrades)break;
  a-=MaxTrades;
  }
   
   
  if(LOSSP(Magic)>=0)a=0;
   
   return(a);
} 

Mql5 not work????

double LOSS(int m)
  {
   double a=0;
   int tk;
HistorySelect(0,TimeCurrent()); 
   for(int cnt = HistoryDealsTotal()-1 ; cnt >= 0; cnt--)
     {
     tk=HistoryDealGetTicket(cnt);
      //if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY))
         if(HistoryDealGetString(tk,DEAL_SYMBOL) == Symbol() && HistoryDealGetInteger(tk,DEAL_MAGIC) ==  m && HistoryDealGetInteger(tk,DEAL_ENTRY)==DEAL_ENTRY_OUT)
           {

            //if(HistoryDealGetInteger(DEAL_TYPE_BUY)<=1)
            //if(PositionGetInteger(POSITION_TYPE)<=1)
            //Print(cnt," ",HistoryDealGetDouble(tk,DEAL_PROFIT));
              {
               if(HistoryDealGetDouble(tk,DEAL_PROFIT)>=0)
                  a--;
               else
                  a++;
               if(StringFind(HistoryDealGetString(tk,DEAL_COMMENT),"-0",0)!= -1)
                  break;
              }
           }
     }



   for(int x=0; x<1000; x++)
     {
      if(a<MaxTrades)
         break;
      a-=MaxTrades;
     }

What can I replace to fix the mql5 code?

For your help, I thank you very much

Vladimir Karputov  
BINTARI Uchiha :

Dear the mql5 master coder.

I need help with coding mql5 that doesn't work.
Here is the code.
Mql4 work smoothly:

Mql5 not work????

What can I replace to fix the mql5 code?

For your help, I thank you very much

HistorySelect Code example [data folder]\MQL5\Experts\Examples\Moving Average\Moving Average.mq5

//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double TradeSizeOptimized(void)
  {
   double price=0.0;
   double margin=0.0;
//--- select lot size
   if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))
      return(0.0);
   if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))
      return(0.0);
   if(margin<=0.0)
      return(0.0);

   double lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*MaximumRisk/margin,2);
//--- calculate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      //--- select history for access
      HistorySelect(0,TimeCurrent());
      //---
      int    orders=HistoryDealsTotal();  // total history deals
      int    losses=0;                    // number of losses orders without a break

      for(int i=orders-1;i>=0;i--)
        {
         ulong ticket=HistoryDealGetTicket(i);
         if(ticket==0)
           {
            Print("HistoryDealGetTicket failed, no trade history");
            break;
           }
         //--- check symbol
         if(HistoryDealGetString(ticket,DEAL_SYMBOL)!=_Symbol)
            continue;
         //--- check Expert Magic number
         if(HistoryDealGetInteger(ticket,DEAL_MAGIC)!=MA_MAGIC)
            continue;
         //--- check profit
         double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         if(profit>0.0)
            break;
         if(profit<0.0)
            losses++;
        }
      //---
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//--- normalize and check limits
   double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
   lot=stepvol*NormalizeDouble(lot/stepvol,0);

   double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
   if(lot<minvol)
      lot=minvol;

   double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
   if(lot>maxvol)
      lot=maxvol;
//--- return trading volume
   return(lot);
  }
Reason: