in trailing stop this fuction is correct or not

 
//+------------------------------------------------------------------+
//|                                       Trailing Stop v_tdavid.mq4 |
//|                              transport_david , David W Honeywell |
//|                                        transport.david@gmail.com |
//+------------------------------------------------------------------+
#property copyright "transport_david , David W Honeywell"
#property link      "transport.david@gmail.com"

extern int TrailingStop =  15;              // trades in profit will move to entry price + 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
   int cnt,total=OrdersTotal();
   
   for(cnt = total-1; cnt >= 0; cnt--)
    {
     RefreshRates();
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol() == Symbol())
      {
       if ((TrailingStop > 0) &&
           (MathAbs(OrderClosePrice() - OrderOpenPrice()) > TrailingStop*Point     ) &&
           (OrderProfit() > 0.00))
        {
         //if (  ((OrderType() == OP_BUY) && (OrderClosePrice() - TrailingStop*Point  - BreakEven*Point) > OrderStopLoss()) ||
              // ((OrderStopLoss() == 0) && (OrderClosePrice() - TrailingStop*Point) >= OrderOpenPrice())  )
            // OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()-TrailingStop*Point  - BreakEven*Point,OrderTakeProfit(),0,White);
         
         if (  ((OrderType() == OP_SELL) && (OrderClosePrice() + TrailingStop*Point  ) < OrderStopLoss()) ||
               ((OrderStopLoss() == 0) && (OrderClosePrice() + TrailingStop*Point  ) <= OrderOpenPrice())  )
             OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()+TrailingStop*Point  ,OrderTakeProfit(),0,White);
        }
      }
    }
   

Sleep(1000);


//----
   return(0);
  }
//+------------------------------------------------------------------+
 
in thisi ea fuction

int cnt,total=OrdersTotal();

for(cnt = total-1; cnt >= 0; cnt--)

is correct or i put cnt++ in place of cnt--

plece any body tell me waht is correct
     
    of course cnt-- you kidding aha ?
     
    for(cnt = total-1; cnt >= 0; cnt--)

    Count down from total.


    for(cnt = 0; cnt >= total; cnt++)

    Count up from zero.

     

    Don't count up if you also close or delete trades inside your loop

    because of that it is preffered to count down checking the trades

    for an example how you can do a trailingstop see your EA MACD Sample

    on a lot of Symbols it makes no sense TrailingStop 1.5 pip Spread is on lot of Symbols bigger

    no checking returnvalues in your code ....

    why do you not use magicnumber

    why not select separatly OrderType

    why not use OrderStopLoss value to check for breakeven....

     
    deysmacro:


    Count up from zero.


    for(cnt = 0; cnt < total; cnt++)
     
    angevoyageur:


    Sorry. Its been long since I used that count method.
     
    amitkk:

    You may want to think it in more detail. This is the same but a bit improved ( I hope ) :

    //+------------------------------------------------------------------+
    //|                                       Trailing Stop v_tdavid.mq4 |
    //|                              transport_david , David W Honeywell |
    //|                                        transport.david@gmail.com |
    //+------------------------------------------------------------------+
    #property copyright "transport_david , David W Honeywell"
    #property link      "transport.david@gmail.com"
    
    extern int TrailingStop=15;
    input  int MagicNumber=1234567; //choose whatever value you want to make it unique             
    //+------------------------------------------------------------------+
    //| expert initialization function                                   |
    //+------------------------------------------------------------------+
    int init()
      {
    //----
    
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    //| expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    int deinit()
      {
    //----
    
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    //| expert start function                                            |
    //+------------------------------------------------------------------+
    int start()
      {
    //----
       int try=0;
       int step=10;
       int cnt,total=OrdersTotal();
       double pips=Point;
       if(Digits==3 || Digits==5)pips=Point*10;
    //--- exit if no trailing required
       if(TrailingStop<=0)return(0);
    //---
       else
         {
          for(cnt=total-1; cnt>=0; cnt--)
            {
             if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
               {
                Print("OrderSelect() has failed with error : "+(string)GetLastError());
                break;
               }
             else
               {
                if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                  {
                   if(OrderType()==OP_BUY && OrderProfit()>0.0)
                     {
                      if(Bid-OrderStopLoss()>(TrailingStop+step)*pips)
                        {
                         bool modified=false;
                         try=30;
                         while(modified==false && try>0)
                           {
                            RefreshRates();
                            double newStop=NormalizeDouble(Bid-TrailingStop*pips,Digits);
                            if(newStop>OrderStopLoss())
                              {
                               int ticket=OrderTicket();
                               if(IsTradeAllowed()==false)
                                 {
                                  Print("Trade context busy or trade not allowed ! Please wait .");
                                  Sleep(100);
                                 }
                               else
                                 {
                                 modified=OrderModify(ticket,OrderOpenPrice(),newStop,OrderTakeProfit(),0,White);
                                  if(!modified)
                                    {
                                     Print("Failed to modify stop for order with ticket : "+(string)ticket+" error : "+(string)GetLastError());
                                     try--;
                                    }
                                  else
                                    {
                                     Print("Stop loss was modified for order with ticket : "+(string)ticket);
                                    }
                                 }
                              }
                           }
                        }
                     }//end if OP_BUY
                  //---
                  if(OrderType()==OP_SELL && OrderProfit()>0.0)
                     {
                      if(OrderStopLoss()-Ask>(TrailingStop+step)*pips)
                        {
                         modified=false;
                         try=30;
                         while(modified==false && try>0)
                           {
                            RefreshRates();
                            newStop=NormalizeDouble(Ask+TrailingStop*pips,Digits);
                            if(newStop>OrderStopLoss())
                              {
                               ticket=OrderTicket();
                               if(IsTradeAllowed()==false)
                                 {
                                  Print("Trade context busy or trade not allowed ! Please wait .");
                                  Sleep(100);
                                 }
                               else
                                 {
                                 modified=OrderModify(ticket,OrderOpenPrice(),newStop,OrderTakeProfit(),0,White);
                                  if(!modified)
                                    {
                                     Print("Failed to modify stop for order with ticket : "+(string)ticket+" error : "+(string)GetLastError());
                                     try--;
                                    }
                                  else
                                    {
                                     Print("Stop loss was modified for order with ticket : "+(string)ticket);
                                    }
                                 }
                              }
                           }
                        }
                     }//end if OP_SELL
                  }
               }
            }// enf for loop
         }
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    

    If you have any questions or want to discuss in more detail, ask.

    Hope it helps

    Cheers

    Reason: