Issue with my trailing stop EA

for ( int TrC = OrdersTotal() - 1; TrC >= 0; TrC -- )
   {  
      if ( !OrderSelect( TrC, SELECT_BY_POS,MODE_TRADES ) ) { Print( "OrderSelect Error #:", GetLastError());  continue;  }
      if ( OrderSymbol() != symb || OrderMagicNumber() != mgn ) continue;       //symb is desired symbol and mgn is desired magic number for filtering opened orders
      if ( OrderType() != OP_BUY && OrderType() != OP_SELL ) continue;          //selectonly buy or sell order
      SL=0;TP=0;                //defined as doubke type data in global variable
      if ( OrderProfit()> 0 )
      if ( trailslpoint > 0 )           //int trailspoint ( no. of point used for trailing
        {
         SL=ntrailsl(Symbol(),OrderType(), OrderStopLoss(), OrderOpenPrice() ,trailslpoint, PERIOD_CURRENT);            //it returns the stoploss price to modify by trailing it is userdefined function for buy or sell type position
         }
      if (SL==OrderStopLoss() ) continue;               
      if ( OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)==true ) Print("Modified by "+strategy_code); // CE10089 (" ","Error in modofying SL for trailing of ticket#"+(string)OrderTicket()+" Error#"+(string)GetLastError()," ")  ;
   }

the problem is that it is working only with opened order of buy type, not sell

I my point of view it must work in both buy and sell type of orders

Can anybody help me with this.

Thanks in advance.

Looks like your problem is in your ‘ntrailsl()’ function, which you did not include here.

double ntrailsl(string SymboL,int position, double pos_sl, double pos_open_price,int trail, int Time_Frame)
{
   double new_sl=pos_sl;
   double price=0;
   double point    =        MarketInfo(SymboL, MODE_POINT);
   int    digit    =  (int) MarketInfo(SymboL, MODE_DIGITS);
   if      (position==OP_BUY)  price=MarketInfo(SymboL, MODE_BID);
   else if (position==OP_SELL) price=MarketInfo(SymboL, MODE_ASK);
   else new_sl=pos_sl;
   if (position==OP_BUY)
      if (price-pos_open_price>trail*point)
         if ( (price-(trail*point)>pos_sl) ) new_sl=price-(trail*point);
   if (position==OP_SELL)
      if (pos_open_price-price>trail*point)
         if ( (price+(trail*point)<pos_sl) ) new_sl=price+(trail*point);
   if ( MathAbs(new_sl-pos_sl)<10*point ) new_sl=pos_sl;
   if (new_sl < 0) new_sl = pos_sl;
   return (NormalizeDouble(new_sl,digit));
}

here is the ‘ntrailsl()’ function

Your code looks fine (except the missing {} as highlighted below, but no big deal), so I decided to test run it, and everything works! - but of course,with my own “guesses” of what the other parts of your code looks like (e.g. your data declarations, whether you have other supporting functions, etc.).

   if ( OrderProfit()> 0 )
{
      if ( trailslpoint > 0 )           //int trailspoint ( no. of point used for trailing
        {
         SL=ntrailsl(Symbol(),OrderType(), OrderStopLoss(), OrderOpenPrice() ,trailslpoint, PERIOD_CURRENT);            //it returns the stoploss price to modify by trailing it is userdefined function for buy or sell type position
         }
      if (SL==OrderStopLoss() ) continue;               
      if ( OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)==true ) Print("Modified by "+strategy_code); // CE10089 (" ","Error in modofying SL for trailing of ticket#"+(string)OrderTicket()+" Error#"+(string)GetLastError()," ")  ;

So I suggest you post your entire code if you want a thorough testing done :slight_smile:

After applying your suggestion, I did testing and couldnot got solved.

finally i came to know that my function ‘ntrailsl()’ return value for sell position is 0, I could not recognize where is the error that returns value 0 for sell position.

double ntrailsl(string SymboL,int position, double pos_sl, double pos_open_price,int trail, int Time_Frame)
{
   double new_sl=pos_sl;
   double price=0;
   double point    =        MarketInfo(SymboL, MODE_POINT);
   int    digit    =  (int) MarketInfo(SymboL, MODE_DIGITS);
   if      (position==OP_BUY)  price=MarketInfo(SymboL, MODE_BID);
   else if (position==OP_SELL) price=MarketInfo(SymboL, MODE_ASK);
   else new_sl=pos_sl;
   if (position==OP_BUY)
      if (price-pos_open_price>trail*point)
         if ( (price-(trail*point)>pos_sl) ) new_sl=price-(trail*point);
   if (position==OP_SELL)
      if (pos_open_price-price>trail*point)
         if ( (price+(trail*point)<pos_sl) ) new_sl=price+(trail*point);
   if ( MathAbs(new_sl-pos_sl)<10*point ) new_sl=pos_sl;
   if (new_sl < 0) new_sl = pos_sl;
   return (NormalizeDouble(new_sl,digit));
}

That’s weird, because without the braces, you can get zero (in fact when profit is less than zero, all stoplosses are set to zero according to your code, so subsequent call to ‘nTrailsl()’ will also give you zero.

However, with the braces in place I don’t get zero on my end…

2       2019.04.12 00:00        sell    2       0.01    1.12515 1.14515 0.00000 0.00    10000.00
:
32      2019.04.18 15:30        modify  2       0.01    1.12515 1.12509 0.00000 0.00    10002.82
33      2019.04.18 15:30        modify  2       0.01    1.12515 1.12498 0.00000 0.00    10002.82
34      2019.04.18 15:30        modify  2       0.01    1.12515 1.12488 0.00000 0.00    10002.82
35      2019.04.18 16:03        modify  2       0.01    1.12515 1.12478 0.00000 0.00    10002.82
36      2019.04.18 16:24        s/l     2       0.01    1.12478 1.12478 0.00000 0.38    10003.20

when i printed out I got this for sell position

image__19

and for buy position

i printed out by this

 
  if ( OrderProfit()> 0 )
{
      if ( trailslpoint > 0 )           //int trailspoint ( no. of point used for trailing
        {
         SL=ntrailsl(Symbol(),OrderType(), OrderStopLoss(), OrderOpenPrice() ,trailslpoint, PERIOD_CURRENT);            //it returns the stoploss price to modify by trailing it is userdefined function for buy or sell type position
        Print("new stop loss "+DoubleToStr(SL)+" for "+"OrderType "+(string)OrderType()+"Profit"+(string)OrderProfit());
         }
      if (SL==OrderStopLoss() ) continue;               
      if ( OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)==true ) Print("Modified by "+strategy_code); 
}

Did you put stoploss during execution of sell.

mine was with no tp and no sl.

is that the reason.

I did :).

Looks like you found the problem! Because of this line " if ( (price+(trailpoint)<pos_sl) ) new_sl=price+(trailpoint);" - condition will never be true with a zero initial sell stop loss.

Yeah, thanks a lot for your response.