Trailing stop ?

 

There is Errors appear on the Strategy Tester journal , I didn’t understand them , 
The image :

 

And this is  the code of the trailing stop function

void TrailingStop() 
{
   for( int b = OrdersTotal()-1; b>=0; b-- )
   
      if(OrderSelect( b, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber() == magicNumber)
            if(OrderSymbol() == Symbol())
               if(OrderType() == OP_BUY)
                  if(Bid - OrderOpenPrice()>WhenToTrail*pips)
                     if(OrderStopLoss()<Bid - TrailAmount*pips)
                        result=OrderModify( OrderTicket(), OrderOpenPrice(), Bid - TrailAmount*pips, OrderTakeProfit(), 0, CLR_NONE );
                        if (!result) 
                           Alert("Buy Order Modify Error n° ", GetLastError());
   
   for( int s = OrdersTotal()-1;s>=0; s-- )
      if(OrderSelect( s, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber() == magicNumber)
            if(OrderSymbol() == Symbol())
               if(OrderType() == OP_SELL)
                  if(OrderOpenPrice() - Ask >WhenToTrail*pips)
                     if(OrderStopLoss() >Ask + TrailAmount*pips || OrderStopLoss() == 0)
                        result=OrderModify( OrderTicket(), OrderOpenPrice(), Ask + TrailAmount*pips, OrderTakeProfit(), 0, CLR_NONE );
                        if (!result) 
                           Alert("Sell Order Modify Error n° ", GetLastError());  
}

Some help to solve this problem 

 


 
yassin.mokni:

There is Errors appear on the Strategy Tester journal , I didn’t understand them , 
The image :

 

And this is  the code of the trailing stop function

Some help to solve this problem 

 


Print the values of OrderStoploss() and Bid-TrailAmount*pips/Ask+TrailAmount*pips.

 
This is a piece from the journal 
TS : trailing stop
BE : break even


2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: modify #23 buy 0.10 EURUSD at 1.30751 sl: 1.31388 tp: 1.32710 ok
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: modify #23 buy 0.10 EURUSD at 1.30751 sl: 1.31387 tp: 1.32710 ok
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify TS Error n° 0 . Ask + TrailAmount*pips = 1.3241 . The Ask = 1.3191
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify TS Error n° 1 . Bid - TrailAmount*pips = 1.3139 . The Bid = 1.3189
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: OrderModify error 1
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify TS Error n° 0 . Ask + TrailAmount*pips = 1.324 . The Ask = 1.3191
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify TS Error n° 0 . Bid - TrailAmount*pips = 1.3138 . The Bid = 1.3188
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify TS Error n° 0 . Ask + TrailAmount*pips = 1.324 . The Ask = 1.319
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify TS Error n° 0 . Bid - TrailAmount*pips = 1.3138 . The Bid = 1.3188
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify TS Error n° 0 . Ask + TrailAmount*pips = 1.324 . The Ask = 1.3191
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify TS Error n° 0 . Bid - TrailAmount*pips = 1.3138 . The Bid = 1.3188
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify TS Error n° 0 . Ask + TrailAmount*pips = 1.324 . The Ask = 1.319
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify TS Error n° 0 . Bid - TrailAmount*pips = 1.3138 . The Bid = 1.3188
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify BE Error n° 0
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Sell Order Modify TS Error n° 0 . Ask + TrailAmount*pips = 1.3241 . The Ask = 1.3191
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: Buy Order Modify TS Error n° 1 . Bid - TrailAmount*pips = 1.3139 . The Bid = 1.3189
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: OrderModify error 1
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: modify #23 buy 0.10 EURUSD at 1.30751 sl: 1.31385 tp: 1.32710 ok
2014.12.27 00:15:53.291 2013.04.16 18:25  PBD_EA_test EURUSD,H4: modify #23 buy 0.10 EURUSD at 1.30751 sl: 1.31383 tp: 1.32710 ok

 
yassin.mokni:

There is Errors appear on the Strategy Tester journal , I didn’t understand them , 
The image :

 

And this is  the code of the trailing stop function

Some help to solve this problem 

 


Nested "if" statement is a bad coding practice, which leads to error and lost of time, as in your case.

  if(OrderStopLoss()<Bid - TrailAmount*pips)
    result=OrderModify( OrderTicket(), OrderOpenPrice(), Bid - TrailAmount*pips, OrderTakeProfit(), 0, CLR_NONE );
  if (!result)                                              <==== this 'if' is not inside the previous one
    Alert("Buy Order Modify Error n° ", GetLastError());

Your last if can be executed even if there is no error. This last 'if' statement is not even inside your loop.

It should be:

  if(OrderStopLoss()<Bid-TrailAmount*pips)
    {
      result=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailAmount*pips,OrderTakeProfit(),0,CLR_NONE);
      if(!result)
      Alert("Buy Order Modify Error n° ",GetLastError());
    }

Or better :

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()==magicNumber && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)
           {
            if(Bid-OrderOpenPrice()>WhenToTrail*pips && OrderStopLoss()<Bid-TrailAmount*pips)
              {
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailAmount*pips,OrderTakeProfit(),0,CLR_NONE))
                  Alert("Buy Order Modify Error n° ",GetLastError());
              }
           }
         if(OrderType()==OP_SELL)
           {
            if(OrderOpenPrice()-Ask>WhenToTrail*pips && (OrderStopLoss()>Ask+TrailAmount*pips || OrderStopLoss()==0))
              {
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailAmount*pips,OrderTakeProfit(),0,CLR_NONE))
                  Alert("Sell Order Modify Error n° ",GetLastError());
              }
           }
        }
     }
 
angevoyageur:

Nested "if" statement is a bad coding practice, which leads to error and lost of time, as in your case.

Your last if can be executed even if there is no error. This last 'if' statement is not even inside your loop.

It should be:

Or better :

Thanks for your reply Mr.Alain , you have a wonderful programming method.
 

The BE errors fixed , but the TS errors did not , look at the image :


an OrderSend error also :) 

 

what do you think the problem is ? 

 
yassin.mokni:

The BE errors fixed , but the TS errors did not , look at the image :


an OrderSend error also :) 

 

what do you think the problem is ? 

Error 1 means you are trying to change the current Stoploss with the same value. See this topic to understand why.

Error 130 means your stoploss are too close to market price (Bid/Ask).

Can price != price ? - MQL4 forum
  • www.mql5.com
Can price != price ? - MQL4 forum
 
angevoyageur:

Error 1 means you are trying to change the current Stoploss with the same value. See this topic to understand why.

Error 130 means your stoploss are too close to market price (Bid/Ask).

Thanks Mr.Alain

Errors fixed by organizing the code ,add 0.5 pips to the TrailAmount and WhenToTrail , adjust the SL and TP if they are too close to the market .


Should we always adjust the slippage ? for the 5 digits broker . 

Reason: