What is wrong with my code?

 

Dear All,


I trying to write a code to Trail Stop Loss.


///////////////////////////////Trailing Stoploss//////////////////////////////////////////////////
   if(OrdersTotal() >= 1 && startModify)
   {
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
      {
         double currentPrice = 0;
         if(OrderType()==OP_BUY) { currentPrice = Bid; OrderSL = OrderStopLoss() + SL * Point; }
         else if(OrderType()==OP_SELL) { currentPrice = Ask; OrderSL = OrderStopLoss() - SL * Point; }
         OrderSL = NormalizeDouble(OrderSL,Digits);
         if(MathAbs(OrderOpenPrice() - currentPrice) >= ExpectedProfit*Point)
         {
            if(OrderModify(OrderTicket(),0,OrderSL,0,0,clrViolet))
               {
                  OrderPrint();
                  if(OrderType()==OP_BUY) ExpectedProfit = ExpectedProfit + SL;// * Point;
                  else if(OrderType()==OP_SELL) ExpectedProfit = ExpectedProfit - SL;// * Point;
               }
            else
               {
                  Print("Last Error #" + GetLastError());
                  Print("#ticket number; open time; trade operation; amount of lots; symbol; open price; Stop Loss;");
                  OrderPrint();
                  Print("Order SL - " + OrderSL);
                  startModify = false;
               }
         }
      }
   }   
/////////////////////////////////////////////////////////////////////////////////

In Opening the order,

startModify = true;
ExpectedProfit = 25;

and SL = 20;

2020.06.17 20:09:37.403 EURUSD,M15: 879905 tick events (1720 bars, 880905 bar states) processed in 0:00:13.063 (total time 0:00:24.766)
2020.06.17 20:09:37.387 2020.06.10 23:59:56  Tester: order #18 is closed
2020.06.17 20:09:37.247 2020.06.10 17:00:00  ADX_Trailing_SL EURUSD,M15: open #18 buy 0.20 EURUSD at 1.1375 sl: 1.1355 ok
2020.06.17 20:09:37.090 2020.06.10 14:00:38  Tester: stop loss #17 at 1.1351 (1.1351 / 1.1352)
2020.06.17 20:09:37.090 2020.06.10 14:00:00  ADX_Trailing_SL EURUSD,M15: open #17 buy 0.20 EURUSD at 1.1371 sl: 1.1351 ok
2020.06.17 20:09:36.997 2020.06.10 10:55:03  Tester: stop loss #16 at 1.1353 (1.1353 / 1.1354)
2020.06.17 20:09:36.856 2020.06.10 07:18:30  ADX_Trailing_SL EURUSD,M15: #16 2020.06.10 06:15:00 buy 0.20 EURUSD 1.1353 1.1333 0.0000 1.1379 0.00 0.00 52.00 Buy Trade 0
2020.06.17 20:09:36.856 2020.06.10 07:18:30  ADX_Trailing_SL EURUSD,M15: modify #16 buy 0.20 EURUSD at 1.1353 sl: 1.1353 tp: 0.0000 ok
2020.06.17 20:09:36.825 2020.06.10 06:15:00  ADX_Trailing_SL EURUSD,M15: open #16 buy 0.20 EURUSD at 1.1353 sl: 1.1333 ok
2020.06.17 20:09:36.153 2020.06.09 03:45:38  Tester: stop loss #15 at 1.1267 (1.1266 / 1.1267)
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: Order SL - 1.1247
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: #15 2020.06.09 00:30:00 sell 0.20 EURUSD 1.1287 1.1267 0.0000 1.1261 0.00 0.00 52.00 Sell Trade 0
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: #ticket number; open time; trade operation; amount of lots; symbol; open price; Stop Loss;
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: Last Error #130
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: OrderModify error 130
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: #15 2020.06.09 00:30:00 sell 0.20 EURUSD 1.1287 1.1287 0.0000 1.1262 0.00 0.00 50.00 Sell Trade 0
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: modify #15 sell 0.20 EURUSD at 1.1287 sl: 1.1267 tp: 0.0000 ok
2020.06.17 20:09:36.153 2020.06.09 03:41:27  ADX_Trailing_SL EURUSD,M15: #15 2020.06.09 00:30:00 sell 0.20 EURUSD 1.1287 1.1307 0.0000 1.1261 0.00 0.00 52.00 Sell Trade 0
2020.06.17 20:09:36.153 2020.06.09 03:41:27  ADX_Trailing_SL EURUSD,M15: modify #15 sell 0.20 EURUSD at 1.1287 sl: 1.1287 tp: 0.0000 ok
2020.06.17 20:09:36.059 2020.06.09 00:30:00  ADX_Trailing_SL EURUSD,M15: open #15 sell 0.20 EURUSD at 1.1287 sl: 1.1307 ok

Please tell me what should correct here. Thanks in advance.

 
What is the problem ? 
 
Marco vd Heijden:
What is the problem ? 

The order is modified 2 times successfully. On 3rd time it shows an error 130. OrderSL is the new stoploss value. 

2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: Last Error #130
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: OrderModify error 130
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: #15 2020.06.09 00:30:00 sell 0.20 EURUSD 1.1287 1.1287 0.0000 1.1262 0.00 0.00 50.00 Sell Trade 0
2020.06.17 20:09:36.153 2020.06.09 03:41:28  ADX_Trailing_SL EURUSD,M15: modify #15 sell 0.20 EURUSD at 1.1287 sl: 1.1267 tp: 0.0000 ok
2020.06.17 20:09:36.153 2020.06.09 03:41:27  ADX_Trailing_SL EURUSD,M15: #15 2020.06.09 00:30:00 sell 0.20 EURUSD 1.1287 1.1307 0.0000 1.1261 0.00 0.00 52.00 Sell Trade 0
2020.06.17 20:09:36.153 2020.06.09 03:41:27  ADX_Trailing_SL EURUSD,M15: modify #15 sell 0.20 EURUSD at 1.1287 sl: 1.1287 tp: 0.0000 ok
2020.06.17 20:09:36.059 2020.06.09 00:30:00  ADX_Trailing_SL EURUSD,M15: open #15 sell 0.20 EURUSD at 1.1287 sl: 1.1307 ok
 
modify #16 buy 0.20 EURUSD at 1.1353 sl: 1.1353 tp: 0.0000 ok
 
Marco vd Heijden:
modify #16 buy 0.20 EURUSD at 1.1353 sl: 1.1353 tp: 0.0000 ok

Yes. Modifying SL to Open Price. Is it wrong? But the modify order is OK, right?

 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.

Fine. Can you help me here?

 
         if(OrderType()==OP_BUY) { currentPrice = Bid; OrderSL = OrderStopLoss() + SL * Point; }
         else if(OrderType()==OP_SELL) { currentPrice = Ask; OrderSL = OrderStopLoss() - SL * Point; }
The new SL is not the current SL ± n
 
William Roeder:
The new SL is not the current SL ± n

Why not? I am trying to move the SL in the direction of the market. Can you explain why New SL is not Current SL +- n?

 
Gopalakrishna R: I am trying to move the SL in the direction of the market.
That's what you want, not what you are doing. When in doubt, THINK, or Use the debugger or print out your variables, including _LastError and prices and find out why.
  1. SL=1.0000 Bid=1.0040
  2. SL=1.0010 Bid=1.0041
  3. SL=1.0020 Bid=1.0040
  4. SL=1.0030 Bid=1.0041
  5. SL=1.0040 Bid=1.0040 invalid SL
 
William Roeder:
That's what you want, not what you are doing. When in doubt, THINK, or Use the debugger or print out your variables, including _LastError and prices and find out why.
  1. SL=1.0000 Bid=1.0040
  2. SL=1.0010 Bid=1.0041
  3. SL=1.0020 Bid=1.0040
  4. SL=1.0030 Bid=1.0041
  5. SL=1.0040 Bid=1.0040 invalid SL

Thank you. I fixed it finally. 

Reason: