Error 130 on OrderModify || order modified few times and then start sending error

 

//****************************************************************************************************************

Transactions from RESULT screen whereby 

49 2020.10.09 18:45 buy 5 0.07 1.18193 1.18051 1.18248 0.00 1006.69

50 2020.10.09 18:45 modify 5 0.07 1.18193 1.18051 1.18322 0.00 1006.69
51 2020.10.09 19:00 modify 5 0.07 1.18193 1.18053 1.18322 0.00 1006.69
54 2020.10.09 19:00 modify 5 0.07 1.18193 1.18053 1.18335 0.00 1006.69
57 2020.10.09 19:15 modify 5 0.07 1.18193 1.18070 1.18335 0.00 1006.69
60 2020.10.09 19:15 modify 5 0.07 1.18193 1.18070 1.18354 0.00 1006.69
63 2020.10.09 19:30 modify 5 0.07 1.18193 1.18086 1.18354 0.00 1006.69
66 2020.10.09 19:30 modify 5 0.07 1.18193 1.18086 1.18369 0.00 1006.69
69 2020.10.09 19:45 modify 5 0.07 1.18193 1.18105 1.18369 0.00 1006.69
72 2020.10.09 19:45 modify 5 0.07 1.18193 1.18105 1.18392 0.00 1006.69
75 2020.10.09 20:00 modify 5 0.07 1.18193 1.18127 1.18392 0.00 1006.69
78 2020.10.09 20:00 modify 5 0.07 1.18193 1.18127 1.18403 0.00 1006.69
81 2020.10.09 20:15 modify 5 0.07 1.18193 1.18145 1.18403 0.00 1006.69
84 2020.10.09 20:15 modify 5 0.07 1.18193 1.18145 1.18405 0.00 1006.69

89 2020.10.09 20:45 close 5 0.07 1.18234 1.18145 1.18405 2.87 1025.04

ND and DtS functions created to NormaliseDouble and DoubleToStr values

void trail_StopLoss()
{
  double pSAR_Value      = get_pSAR(Period_Entry,1);
  double BB_SupportUpper = getBB_SupportUpper(Period_Entry,1);
  double BB_SupportLower = getBB_SupportLower(Period_Entry,1);
  //+---------------------------------------------------------------------------------------------------------------------------+
  //| BUY/LONG Orders: Set Trailing Stop Loss
  //+---------------------------------------------------------------------------------------------------------------------------+
  for(int B = OrdersTotal()-1; B >= 0; B--)
  {
    double trailLoss_Buy = 0;
    if(OrderSelect(B, SELECT_BY_POS, MODE_TRADES))
      if((OrderSymbol() == Symbol()) && (OrderMagicNumber() == ea_MagicNo) && (OrderType() == OP_BUY))
      //+-----------------------------------------------------------------------------------------------------------------------+
      //| Set Trailing Stop Loss Strategy Bollinger
      //+-----------------------------------------------------------------------------------------------------------------------+
      {
        if(OrderComment() == "Bollinger")
        {
          trailLoss_Buy = BB_SupportLower + (3*get_Pips());
        }
      //+-----------------------------------------------------------------------------------------------------------------------+
      //| Set Trailing Stop Loss Strategy MATrendTrading
      //+-----------------------------------------------------------------------------------------------------------------------+
        else if(OrderComment() == "MATrendTrading")
        {
          trailLoss_Buy = pSAR_Value - (2*get_Pips());
        }
      }
      //+-----------------------------------------------------------------------------------------------------------------------+
      //| CHECK for error 1, Minimum allowed price update by broker and modify order
      //+-----------------------------------------------------------------------------------------------------------------------+
      if((trailLoss_Buy - OrderStopLoss()) > (min_PriceUpdate * get_Points()))
      {
        bool res = OrderModify(OrderTicket(),OrderOpenPrice(),ND(trailLoss_Buy),OrderTakeProfit(),0,clrRed);
        if(!res)
           Alert("Error BUY Trail StopLoss [" + DtS(trailLoss_Buy) + "] Order Profit [" + DtS(OrderStopLoss()) +
                 "] OrderNo [" + OrderTicket()+ "] Error Code " + GetLastError());
      }
    } // END of For..Loop to Trail StopLoss on BUY Orders

Log Values error 130 for other orders removed for easy reading [please read  "Order Profit" as "Order Loss" as it was a typo error in Alert Statement]

2020.11.10 11:33:58.282 2020.10.09 20:45:20  iFxTRADE v1.02 EURUSD,M15: close #5 buy 0.07 EURUSD at 1.18193 sl: 1.18145 tp: 1.18405 at price 1.18234

2020.11.10 11:33:58.282 2020.10.09 20:45:10  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18257] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.282 2020.10.09 20:45:10  iFxTRADE v1.02 EURUSD,M15: OrderModify error 130

2020.11.10 11:33:58.282 2020.10.09 20:45:00  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18257] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.282 2020.10.09 20:44:59  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.282 2020.10.09 20:43:42  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.282 2020.10.09 20:43:36  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.282 2020.10.09 20:43:30  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:43:18  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:43:12  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:43:00  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:54  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:53  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:51  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:50  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:48  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:46  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:45  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:43  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:40  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:38  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:36  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:35  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:33  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:31  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:30  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:28  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:27  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:26  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:25  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:23  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:21  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:20  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:18  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:16  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:15  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:13  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130

2020.11.10 11:33:58.281 2020.10.09 20:42:10  iFxTRADE v1.02 EURUSD,M15: Alert: Error BUY Trail StopLoss [1.18259] Order Profit [1.18145] OrderNo [5] Error Code 130


 
ERR_NO_RESULT
You Server
Change the SL to X It is at X!
Change the SL to X It is at X!
Change the SL to X You are insane
Insanity: doing the same thing over and over again and expecting different results.
          Unknown

Compute the new value, then check that you are moving the existing value at least a tick.

PIP, Point, or Tick are all different in general.
          What is a TICK? - MQL4 programming forum 2014.08.03

 
William Roeder:
ERR_NO_RESULT
You Server
Change the SL to X It is at X!
Change the SL to X It is at X!
Change the SL to X You are insane

Compute the new value, then check that you are moving the existing value at least a tick.

PIP, Point, or Tick are all different in general.
          What is a TICK? - MQL4 programming forum 2014.08.03

Dear William

thanks for a pointer in right direction. I realised that i was calculating update values on completion of previous candle, while trying to update them on each tick!!! So for the whole of current bar, with each tick it was trying to update with the same value. just moving my check function from Tick to NewBar, seems to solved it out.

 
Anil Varma:

Dear William

thanks for a pointer in right direction. I realised that i was calculating update values on completion of previous candle, while trying to update them on each tick!!! So for the whole of current bar, with each tick it was trying to update with the same value. just moving my check function from Tick to NewBar, seems to solved it out.

Dear William

Hope I am not bothering you too much.

I am getting Error 130 again while running Trailing Take Profit code. This code is almost similar to my TrailStopLoss code, which is working perfectly fine (Highlighted on Log). I have made sure to check update is on a new candle only and updateTakeProfit != currentTakeProfit. You will notice few updates with -tive figures, which is intentional to follow Bollinger Band as TakeProfit target.

The fact is not a single update has worked on TrailinTakeProfit code. All are showing error.

The log for Order#1 is as below. Looking forward to hear from you, what I am doing wrong. Thanks in advance.

2 18:47:48.595 2020.01.15 20:00:00  iFxTRADE v1.01 EURUSD,H1: open #1 buy 0.05 EURUSD at 1.11577 sl: 1.11381 tp: 1.11849 ok

0 18:47:48.596 2020.01.15 21:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11866] Diff Points [17] ErrorCode 130

0 18:47:48.596 2020.01.15 22:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11874] Diff Points [25] ErrorCode 130

0 18:47:48.597 2020.01.15 23:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11884] Diff Points [35] ErrorCode 130

0 18:47:48.598 2020.01.16 00:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11888] Diff Points [39] ErrorCode 130

0 18:47:48.599 2020.01.16 01:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11892] Diff Points [43] ErrorCode 130

0 18:47:48.599 2020.01.16 02:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11892] Diff Points [43] ErrorCode 130

0 18:47:48.600 2020.01.16 03:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11895] Diff Points [46] ErrorCode 130

0 18:47:48.600 2020.01.16 04:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11897] Diff Points [48] ErrorCode 130

0 18:47:48.601 2020.01.16 05:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11893] Diff Points [44] ErrorCode 130

0 18:47:48.601 2020.01.16 06:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11891] Diff Points [42] ErrorCode 130

0 18:47:48.601 2020.01.16 07:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11862] Diff Points [13] ErrorCode 130

0 18:47:48.602 2020.01.16 08:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11828] Diff Points [-21] ErrorCode 130

0 18:47:48.602 2020.01.16 09:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11766] Diff Points [-83] ErrorCode 130

0 18:47:48.603 2020.01.16 10:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11741] Diff Points [-108] ErrorCode 130

0 18:47:48.605 2020.01.16 11:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11747] Diff Points [-102] ErrorCode 130

0 18:47:48.606 2020.01.16 12:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11721] Diff Points [-128] ErrorCode 130

0 18:47:48.607 2020.01.16 13:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11728] Diff Points [-121] ErrorCode 130

2 18:47:48.608 2020.01.16 14:00:00  iFxTRADE v1.01 EURUSD,H1: modify #1 buy 0.05 EURUSD at 1.11577 sl: 1.11468 tp: 1.11849 ok [Note: StopLoss Modified with same code structure]

0 18:47:48.608 2020.01.16 14:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.1174] Diff Points [-109] ErrorCode 130

2 18:47:48.610 2020.01.16 15:00:00  iFxTRADE v1.01 EURUSD,H1: modify #1 buy 0.05 EURUSD at 1.11577 sl: 1.11513 tp: 1.11849 ok [Note: StopLoss Modified with same code structure]

0 18:47:48.610 2020.01.16 15:00:00  iFxTRADE v1.01 EURUSD,H1: Alert: REJECTED MA_Cross: BUY Trail_TP Order# [1] OrderTP [1.11849] TrailTP [1.11776] Diff Points [-73] ErrorCode 130

0 18:47:48.612 2020.01.16 15:56:10  Tester: stop loss #1 at 1.11513 (1.11513 / 1.11515)

//+--------------------------------------------------------------------------------------+
//| MACD CrossOver - TRAILING TAKE PROFIT: trailTakeProfit_MACross()
//| Maximum price allowed to Close Trade on REVERSE CrossOver, TP not expected to reach
//+--------------------------------------------------------------------------------------+
void trailTakeProfit_MACross()
  {
    bool   ticket;
    double trail_TakeProfit;
    double bb_SupportUpper = getBB_SupportUpper(chart_Entry,shift1);
    double bb_SupportLower = getBB_SupportLower(chart_Entry,shift1);
  //+------------------------------------------------------------------------------------+
  //| MACD CrossOver: process BUY Trade Trailing Take Profit
  //+------------------------------------------------------------------------------------+
    for(int B = OrdersTotal()-1; B >= 0; B--)
      {
        // BUY Position: Calculate Trailing TakeProfit
        {
          trail_TakeProfit = NormalizeDouble((bb_SupportUpper + (10*get_Pips())),Digits);
        }
        // BUY Position: Modify Trailing TakeProfit
        if(OrderSelect(B, SELECT_BY_POS, MODE_TRADES))
          if((OrderMagicNumber() == EA_MagicNo) && (OrderSymbol() == Symbol()))
            if(OrderType() == OP_BUY)
              if(OrderComment() == "MA_Cross")
                if(trail_TakeProfit != OrderTakeProfit())
                  {
                    ticket = OrderModify(OrderTicket(),OrderOpenPrice(),trail_TakeProfit,
                                         OrderTakeProfit(),0,clrRed);
                    if(!ticket)
                      Alert("REJECTED MA_Cross: BUY Trail_TP Order# [" + OrderTicket() + "] " +
                            "OrderTP [" + OrderTakeProfit() + "] " +
                            "TrailTP [" + DTS(trail_TakeProfit) + "] " +
                            "Diff Points [" + DoubleToStr(((trail_TakeProfit - OrderTakeProfit())/get_Points()),0) + "] " +
                            "ErrorCode " + GetLastError());
                      return;
                  } // END of Check Condition
      } // END of For..Loop to Trail TakeProfit on BUY Orders
  //+------------------------------------------------------------------------------------+
  //| MACD CrossOver: process SELL Trade Trailing Take Profit
  //+------------------------------------------------------------------------------------+
    for(int S = OrdersTotal()-1; S >= 0; S--)
      {
        //SELL Position: Calculate Trailing TakeProfit
          {
            trail_TakeProfit = NormalizeDouble((bb_SupportLower - (10*get_Pips())),Digits);
          }
        // SELL Position: Modify Trailing TakeProfit
        if(OrderSelect(S, SELECT_BY_POS, MODE_TRADES))
          if((OrderMagicNumber() == EA_MagicNo) && (OrderSymbol() == Symbol()))
            if(OrderType() == OP_SELL)
              if(OrderComment() == "MA_Cross")
                if(trail_TakeProfit != OrderTakeProfit())
                  {
                    ticket = OrderModify(OrderTicket(),OrderOpenPrice(),trail_TakeProfit,
                                         OrderTakeProfit(),0,clrRed);
                    if(!ticket)
                      Alert("REJECTED MA_Cross: SELL Trail_TP Order# [" + OrderTicket() + "] " +
                            "OrderTP [" + OrderTakeProfit() + "] " +
                            "TrailTP [" + DTS(trail_TakeProfit) + "] " + 
                            "Diff Points [" + DoubleToStr(((OrderTakeProfit() - trail_TakeProfit)/get_Points()),0) + "] " +
                            "ErrorCode " + GetLastError());
                      return;
                  } // END of Sell Order Check condition
      } // END of For..Loop to Trail Take Profit on SELL Orders
  } // END of trailTakeProfit_MACross()
 

Change the code like this. You may not have sent the value of OrderSymbol to the ND function. Add your ND function to the form for a more precise solution.

trailLoss_Buy = (NormalizeDouble(BB_SupportLower,MarketInfo(OrderSymbol(),MODE_DIGITS))
           + 
           (3* NormalizeDouble(get_Pips(),MarketInfo(OrderSymbol(),MODE_DIGITS)));
          //
          if(OrderStopLoss()!=trailLoss_Buy) bool res = OrderModify(OrderTicket(),OrderOpenPrice(),trailLoss_Buy,OrderTakeProfit(),0,clrRed);
          
 
Mehmet Bastem:

Change the code like this. You may not have sent the value of OrderSymbol to the ND function. Add your ND function to the form for a more precise solution.

Thanks Mehmet

I have tried but this did not work too.

I have used my TrailStopLoss function (which is working well) and changed it to modify both SL & TP. now struggling with Error 0 and Error 1 :)

just curious to know, is MQL4 is stable language to do serious programming !!!

regards

 
Anil Varma:

Thanks Mehmet

I have tried but this did not work too.

I have used my TrailStopLoss function (which is working well) and changed it to modify both SL & TP. now struggling with Error 0 and Error 1 :)

just curious to know, is MQL4 is stable language to do serious programming !!!

regards

If you are getting errors 0 and 1 then it is almost certainly due to your coding.

You are probably trying to modify with the same SL and TP values that are the same as the existing SL and TP.

I believe that you can do serious programming with MQL4 as long as your coding skills are adequate.

 
Anil Varma:

Thanks Mehmet

I have tried but this did not work too.

I have used my TrailStopLoss function (which is working well) and changed it to modify both SL & TP. now struggling with Error 0 and Error 1 :)

just curious to know, is MQL4 is stable language to do serious programming !!!

regards


Error 1 is due to SL or TP values being the same. ie EU SL: 1.82256 and the SL you want to replace is 1.82256, an error occurs. Remedy for this


if(OrderStopLoss()!=trailLoss_Buy) 
 
Mehmet Bastem:


Error 1 is due to SL or TP values being the same. ie EU SL: 1.82256 and the SL you want to replace is 1.82256, an error occurs. Remedy for this


Thanks Mehmet for prompt reply.

Already have this check in my code, still getting the error. GOT IT SORTED OUT :) Seems my Alert Statement, was on a wrong position. With the above two separate Alert Statement, it is working now.

Another CHALLANGE facing is how to code, Trail StopLoss to START only if it greater than 10 Pips from the StopLoss at the time of placing the order !!!

example:

OrderOpenPrice = 1.15000

OrderStopLoss   = 1.14700 [30 pips]

first update       = 1.14910 [9 pips] not to be modified

second update   = 1.14850 [15 pips] to be modified

third update      = 1.14800 [20 pips] to be modified and so on ...

any guidance / suggestion will be highly appreciated.

                    if((OrderStopLoss() != trailStopLoss_Buy) && (OrderTakeProfit() != trailTakeProfit_Buy))
                      {
                        ticket = OrderModify(OrderTicket(),OrderOpenPrice(),ND(trailStopLoss_Buy),
                                             ND(trailTakeProfit_Buy),0,clrRed);
                        if(!ticket)
                        Alert("REJECTED MA_Cross: BUY Trail_SL update Order# [" + OrderTicket() + "] " +
                              "Price [" + OrderOpenPrice() + "] " +
                              "OrderSL [" + OrderStopLoss() + "] TrailSL [" + DTS(trailStopLoss_Buy) + "] " +
                              "SL Pips [" + DoubleToStr(((trailStopLoss_Buy - OrderStopLoss())/get_Pips()),1) + "] " +
                              "OrderTP [" + OrderTakeProfit() + "] TrailTP [" + DTS(trailTakeProfit_Buy) + "] " +
                              "TP Pips [" + DoubleToStr(((trailTakeProfit_Buy - OrderTakeProfit())/get_Pips()),1) + "] " +
                              "ErrorCode " + GetLastError());
                      }
                    // TakeProfit satisfy update condition
                    else if(OrderTakeProfit() != trailTakeProfit_Buy)
                      {
                        ticket = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),
                                             ND(trailTakeProfit_Buy),0,clrRed);
                        if(!ticket)
                        Alert("REJECTED MA_Cross: BUY Trail_SL update Order# [" + OrderTicket() + "] " +
                              "Price [" + OrderOpenPrice() + "] " +
                              "OrderTP [" + OrderTakeProfit() + "] TrailTP [" + DTS(trailTakeProfit_Buy) + "] " +
                              "TP Pips [" + DoubleToStr(((trailTakeProfit_Buy - OrderTakeProfit())/get_Pips()),1) + "] " +
                              "ErrorCode " + GetLastError());
 
Anil Varma:

Thanks Mehmet for prompt reply.

Already have this check in my code, still getting the error. GOT IT SORTED OUT :) Seems my Alert Statement, was on a wrong position. With the above two separate Alert Statement, it is working now.

Another CHALLANGE facing is how to code, Trail StopLoss to START only if it greater than 10 Pips from the StopLoss at the time of placing the order !!!

example:

OrderOpenPrice = 1.15000

OrderStopLoss   = 1.14700 [30 pips]

first update       = 1.14910 [9 pips] not to be modified

second update   = 1.14850 [15 pips] to be modified

third update      = 1.14800 [20 pips] to be modified and so on ...

any guidance / suggestion will be highly appreciated.

// Checking SL and TP at the same time is faulty. You can reset it first and then update SL and TP. Remote assistance is a little tricky without the whole piece of code.
ticket = OrderModify(OrderTicket(),OrderOpenPrice(),0,0,0,clrRed);// set SL=0,TP=0
ticket = OrderModify(OrderTicket(),OrderOpenPrice(),ND(trailStopLoss_Buy), ND(trailTakeProfit_Buy),0,clrRed);

if(GetLastError()!=0) 
{
                       Alert("REJECTED MA_Cross: BUY Trail_SL update Order# [" + OrderTicket() + "] " +
                              "Price [" + OrderOpenPrice() + "] " +
                              "OrderSL [" + OrderStopLoss() + "] TrailSL [" + DTS(trailStopLoss_Buy) + "] " +
                              "SL Pips [" + DoubleToStr(((trailStopLoss_Buy - OrderStopLoss())/get_Pips()),1) + "] " +
                              "OrderTP [" + OrderTakeProfit() + "] TrailTP [" + DTS(trailTakeProfit_Buy) + "] " +
                              "TP Pips [" + DoubleToStr(((trailTakeProfit_Buy - OrderTakeProfit())/get_Pips()),1) + "] " +
                              "ErrorCode " + GetLastError());
                      }
 
Mehmet Bastem:

Thanks again for prompt reply Mehmet.

attached is the final version of full code. It is working fine (no Error 130, 1 or 0) except when more then one order is opened, it update only the latest one. So while i was testing for three years data, realised that when order 2 opened, it modified it. However after simultaneous opening of order 3, it did modified order 3 but not 2. When order 3 was closed earlier then 2, it again modified order 2.

Also let me know the relevance or importance of having RETURN (highlighted) just before the close of For Loop ... It seems this is the one causing problem to update multiple records at same time!!!

//+--------------------------------------------------------------------------------------+
//| MACD CrossOver - TRAILING TakeProfit & StopLoss together: trailProfitLoss_MACross()
//+--------------------------------------------------------------------------------------+
void trailProfitLoss_MACross()
  {
    bool   ticket;
    double atr_Value;
    double ema_Medium;
    double stopLoss_ToStart;
  //+------------------------------------------------------------------------------------+
  //| MACD CrossOver: process Modifying BUY Trade Trailing Stop Loss & Take Profit
  //+------------------------------------------------------------------------------------+
    for(int B = OrdersTotal()-1; B >= 0; B--)
      {
        double atrStopLoss_Buy;
        double trailStopLoss_Buy;
        double trailTakeProfit_Buy;
      //+--------------------------------------------------------------------------------+
      //| MACD CrossOver: "CALCULATE" BUY Trade Trailing Take Profit & Stop Loss
      //+--------------------------------------------------------------------------------+
        // Calculate trailStopLoss
        {
          atr_Value          = get_ATR(chart_Entry,shift1);
          atrStopLoss_Buy    = Bid - (atr_Value * ATR_MultiplierSLoss);
          ema_Medium         = getEMA_Medium(chart_Entry,shift1);
          trailStopLoss_Buy  = MathMax(ema_Medium,atrStopLoss_Buy);
          // Check trail StopLoss not less then or equal to Order StopLoss
          if(trailStopLoss_Buy <= OrderStopLoss())
            trailStopLoss_Buy = OrderStopLoss();
        }
        // Calculate trailTakeProfit
        {
          double bb_SupportUpper = getBB_SupportUpper(chart_Entry,shift1);
          trailTakeProfit_Buy    = bb_SupportUpper + (5*get_Pips());
        } // END Of Calculate Buy TakeProfit & Stop Loss
      //+--------------------------------------------------------------------------------+
      //| MACD CrossOver: "MODIFY" BUY Trade Trailing Take Profit & Stop Loss
      //+--------------------------------------------------------------------------------+
        if(OrderSelect(B, SELECT_BY_POS, MODE_TRADES))
          if((OrderMagicNumber() == EA_MagicNo) && (OrderSymbol() == Symbol()))
            if(OrderType() == OP_BUY)
              if(OrderComment() == "MA_Cross")
                if(check_OrderStopLoss(OrderType(),trailStopLoss_Buy) == true)
                if(check_OrderTakeProfit(OrderType(),trailTakeProfit_Buy) == true)
                    // TakeProfit & StopLoss both satisfy update condition
                    if((OrderStopLoss() != trailStopLoss_Buy) && (OrderTakeProfit() != trailTakeProfit_Buy))
                      {
                        ticket = OrderModify(OrderTicket(),OrderOpenPrice(),ND(trailStopLoss_Buy),
                                             ND(trailTakeProfit_Buy),0,clrRed);
                        if(!ticket)
                        Alert("REJECTED MA_Cross: BUY Trail_SL update Order# [" + OrderTicket() + "] " +
                              "Price [" + OrderOpenPrice() + "] " +
                              "OrderSL [" + OrderStopLoss() + "] TrailSL [" + DTS(trailStopLoss_Buy) + "] " +
                              "SL Pips [" + DoubleToStr(((trailStopLoss_Buy - OrderStopLoss())/get_Pips()),1) + "] " +
                              "OrderTP [" + OrderTakeProfit() + "] TrailTP [" + DTS(trailTakeProfit_Buy) + "] " +
                              "TP Pips [" + DoubleToStr(((trailTakeProfit_Buy - OrderTakeProfit())/get_Pips()),1) + "] " +
                              "ErrorCode " + GetLastError());
                      }
                    // TakeProfit satisfy update condition
                    else if(OrderTakeProfit() != trailTakeProfit_Buy)
                      {
                        ticket = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),
                                             ND(trailTakeProfit_Buy),0,clrRed);
                        if(!ticket)
                        Alert("REJECTED MA_Cross: BUY Trail_SL update Order# [" + OrderTicket() + "] " +
                              "Price [" + OrderOpenPrice() + "] " +
                              "OrderTP [" + OrderTakeProfit() + "] TrailTP [" + DTS(trailTakeProfit_Buy) + "] " +
                              "TP Pips [" + DoubleToStr(((trailTakeProfit_Buy - OrderTakeProfit())/get_Pips()),1) + "] " +
                              "ErrorCode " + GetLastError());
                      }
        return;
      } // END Of For..Loop for BUY Trailing StopLoss
  //+------------------------------------------------------------------------------------+
  //| MACD CrossOver: process SELL Trade Stop Loss 
  //| LOWER [minimise stop loss] of BB Upper Candle1 and pSAT Candle1
  //+------------------------------------------------------------------------------------+
    for(int S = OrdersTotal()-1; S >= 0; S--)
      {
        double atrStopLoss_Sell;
        double trailStopLoss_Sell;
        double trailTakeProfit_Sell;
      //+--------------------------------------------------------------------------------+
      //| MACD CrossOver: "CALCULATE" SELL Trade Trailing Take Profit & Stop Loss
      //+--------------------------------------------------------------------------------+
        // Calculate trailStopLoss
        {
          atr_Value            = get_ATR(chart_Entry,shift1);
          atrStopLoss_Sell     = Ask + (atr_Value * ATR_MultiplierSLoss);
          ema_Medium           = getEMA_Medium(chart_Entry,shift1);
          trailStopLoss_Sell   = MathMin(ema_Medium,atrStopLoss_Sell);
          // Check trail StopLoss not higher or equal to Order StopLoss
          if(trailStopLoss_Sell >= OrderStopLoss())
            trailStopLoss_Sell = OrderStopLoss();
        }
        // Calculate trailTakeProfit, only if !!!
        {
          double bb_SupportLower = getBB_SupportLower(chart_Entry,shift1);
          trailTakeProfit_Sell   = bb_SupportLower - (5*get_Pips());
        } // END Of Calculate Sell TakeProfit & Stop Loss
      //+--------------------------------------------------------------------------------+
      //| MACD CrossOver: "MODIFY" SELL Trade Trailing Take Profit & Stop Loss
      //+--------------------------------------------------------------------------------+
        if(OrderSelect(S, SELECT_BY_POS, MODE_TRADES))
          if((OrderMagicNumber() == EA_MagicNo) && (OrderSymbol() == Symbol()))
            if(OrderType() == OP_SELL)
              if(OrderComment() == "MA_Cross")
                if(check_OrderStopLoss(OrderType(),trailStopLoss_Sell) == true)
                if(check_OrderTakeProfit(OrderType(),trailTakeProfit_Sell) == true)
                  // TakeProfit & StopLoss both satisfy update condition
                  if((OrderStopLoss() != trailStopLoss_Sell) && (OrderTakeProfit() != trailTakeProfit_Sell))
                    {
                      ticket = OrderModify(OrderTicket(),OrderOpenPrice(),ND(trailStopLoss_Sell),
                                           ND(trailTakeProfit_Sell),0,clrRed);
                      if(!ticket)
                      Alert("REJECTED MA_Cross: SELL Trail_SL update Order# [" + OrderTicket() + "] " +
                            "Price [" + OrderOpenPrice() + "] " +
                            "OrderSL [" + OrderStopLoss() + "] TrailSL [" + DTS(trailStopLoss_Sell) + "] " +
                            "SL Pips [" + DoubleToStr(((OrderStopLoss() - trailStopLoss_Sell)/get_Pips()),1) + "] " +
                            "OrderTP [" + OrderStopLoss() + "] TrailTP [" + DTS(trailTakeProfit_Sell) + "] " +
                            "TP Pips [" + DoubleToStr(((OrderTakeProfit() - trailTakeProfit_Sell)/get_Pips()),1) + "] " +
                            "ErrorCode " + GetLastError());
                    }
                  // TakeProfit only satisfy update condition
                  else if(OrderTakeProfit() != trailTakeProfit_Sell)
                    {
                      ticket = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),
                                           ND(trailTakeProfit_Sell),0,clrRed);
                      if(!ticket)
                      Alert("REJECTED MA_Cross: SELL Trail_SL update Order# [" + OrderTicket() + "] " +
                            "Price [" + OrderOpenPrice() + "] " +
                            "OrderTP [" + OrderStopLoss() + "] TrailTP [" + DTS(trailTakeProfit_Sell) + "] " +
                            "TP Pips [" + DoubleToStr(((OrderTakeProfit() - trailTakeProfit_Sell)/get_Pips()),1) + "] " +
                            "ErrorCode " + GetLastError());
                    }
        return;
      } // END Of For..Loop for Sell Trailing StopLoss
  } // END Of trailProfitLoss_MACross()