OrderModify intermitent error.

 

Howdy everyone,

I've recent started writing my own ea and am running into an error when backtesting my set SL to BE code. The error is intermitent and seemably totally random to where on the compile it sometimes will happen a handful of times, other times its every 5-6 trades it does it. I'm thinking its an error with the backtester, but if there is something I'm missing in the code I'm totally lost as to what it is.

void SetBE()
   { 
   for (int trades=1;trades<=OrdersTotal();trades++)
      {
      if (OrderSelect(trades-1,SELECT_BY_POS)==True)
         {
         if ((OrderMagicNumber()==MAGICNUM) && (OrderSymbol()==Symbol())) 
            {
            if ((OrderType()==OP_BUY) && ((NormalizeDouble(Bid,Digits))>=(OrderOpenPrice()+BreakEven*Point)) && (OrderStopLoss() != OrderOpenPrice()))
               {
                  Alert("Current market price is, ",Bid, " My ticket number of this buy is, ",OrderTicket()," ,and my stop loss is ",OrderStopLoss());
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,0);
                  RefreshRates();
                  return;  
               }
            if ((OrderType()==OP_SELL)&&((NormalizeDouble(Ask,Digits))<=(OrderOpenPrice()-BreakEven*Point)) && (OrderStopLoss() != OrderOpenPrice()))
               {
                  Alert("Current Market Price is, ",Ask, "My ticket number of this sell is, ",OrderTicket()," ,and my stop loss is ",OrderStopLoss());
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,0);
                  RefreshRates();
                  return;
               }
            }
         }
      }
   }
  

And the log file on test 1 ------

01:15:25 1999.10.19 04:00 First Bot4 GBPJPY,H1: open #41 buy 0.10 GBPJPY at 176.123 sl: 175.547 tp: 176.623 ok
01:15:25 1999.10.19 04:13 First Bot4 GBPJPY,H1: Alert: Current market price is, 176.4 My ticket number of this buy is, 41,and my stop loss is 175.547
01:15:25 1999.10.19 04:13 First Bot4 GBPJPY,H1: modify #41 buy 0.10 GBPJPY at 176.123 sl: 176.123 tp: 176.623 ok
01:15:25 1999.10.19 04:14 First Bot4 GBPJPY,H1: Alert: Current market price is, 176.39 My ticket number of this buy is, 41,and my stop loss is 176.123
01:15:25 1999.10.19 04:14 First Bot4 GBPJPY,H1: OrderModify error 1
01:15:25 1999.10.19 04:20 First Bot4 GBPJPY,H1: Alert: Current market price is, 176.386 My ticket number of this buy is, 41,and my stop loss is 176.123
01:15:25 1999.10.19 04:20 First Bot4 GBPJPY,H1: OrderModify error 1
01:15:25 1999.10.19 04:20 First Bot4 GBPJPY,H1: Alert: Current market price is, 176.411 My ticket number of this buy is, 41,and my stop loss is 176.123
01:15:25 1999.10.19 04:20 First Bot4 GBPJPY,H1: OrderModify error 1

--------------------------

This will continue to cycle until SL or TP is hit.

If I run the backtest again without compiling or any changes the log file on test 2 is ----

------------------------------

1:18:03 1999.09.16 21:00 First Bot4 GBPJPY,H1: open #10 buy 0.10 GBPJPY at 170.634 sl: 169.579 tp: 171.134 ok
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: Alert: Current market price is, 170.897 My ticket number of this buy is, 10,and my stop loss is 169.579
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: modify #10 buy 0.10 GBPJPY at 170.634 sl: 170.634 tp: 171.134 ok
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: Alert: Current market price is, 170.929 My ticket number of this buy is, 10,and my stop loss is 170.634
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: OrderModify error 1
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: Alert: Current market price is, 170.962 My ticket number of this buy is, 10,and my stop loss is 170.634
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: OrderModify error 1
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: Alert: Current market price is, 170.954 My ticket number of this buy is, 10,and my stop loss is 170.634
01:18:03 1999.09.16 21:02 First Bot4 GBPJPY,H1: OrderModify error 1

---------------------------------

Sorry for what is likely to be a wall of test when I add the topic, however I'm completely lost as to a possibility to fix this. Also aside from a fix to this if anyone has any constructive critisim on my code I'm glad to hear it.

Thanks,

Kocher

 
Viffer:

Before you send the order modify, you need to check that the price you are modifying to is different to the price it is currently set at. Error 1 means no result because nothing changed.

hth

V


Yea, I have that in the code.... && (OrderStopLoss() != OrderOpenPrice()
 

Other suggestions,

*Is there a minimum distance price must change. In other words is there a minimum Gain_Distance move from current price b4 you can set the order modify?

*The Refresh comes after the order_modify,  hmmm maybe put it b4 order_modify.

These are all suggestions, try them and see if any solves your problem. 

 
ubzen:

Other suggestions,

*Is there a minimum distance price must change. In other words is there a minimum Gain_Distance move from current price b4 you can set the order modify?

*The Refresh comes after the order_modify, hmmm maybe put it b4 order_modify.

These are all suggestions, try them and see if any solves your problem.


Hi Ubzen, Yes on these tests the market must move 25pips for it to kick the SL to BE. Also have code in place to make sure the SetBE function will not fire at all if BE is set to 5 or less to make sure price to close to current price shouldnt be an issue.

This problem really boggles me. As I briefly explained in the first post, it is intermitent. I have ran the test several times and have seen it pop the error 4 times in ~1000 trades, and also seen it as frequent as 200+ times in the same 1000 trades, with no changes to the code.

I posted a few lines from the log file on 2 runs and as you can see on the first run it gave me the error on trade 41, on the 2nd run the first error came on trade 10. The ea Alert even recognizes the the stoploss has been changed and is equal to the order open price, however it still fires the code. It shouldnt be firing it if the alert can see it.

*******EDIT****

Just tested moving the RefreshRates above the ordermodify to no avail. First error was on trade 38.

 
kocher:

Yea, I have that in the code.... && (OrderStopLoss() != OrderOpenPrice()

Yeah, I spotted that after I posted.... I can't see anything wrong and you are correct, it shouldn't call the modify if sl = open price. I would call getlasterror() and print it directly after the modify to be 100% that the error is being generated there. You don't need to normalise bid/ask but I doubt that is the issue. Are you offline when you test? the variability suggests spread to me and I think it worth checking that the breakeven is truely 25pips / 250*point (if your 5 digit broker).

V

 
Viffer:

Yeah, I spotted that after I posted.... I can't see anything wrong and you are correct, it shouldn't call the modify if sl = open price. I would call getlasterror() and print it directly after the modify to be 100% that the error is being generated there. You don't need to normalise bid/ask but I doubt that is the issue. Are you offline when you test? the variability suggests spread to me and I think it worth checking that the breakeven is truely 25pips / 250*point (if your 5 digit broker).

V


Tested with 2 ways for get last error( haven't implemented it all into the code yet. Here are the results from the log:

____----------------------

03:34:18 1999.10.14 06:09 First Bot4 GBPJPY,H1: open #38 buy 0.10 GBPJPY at 177.020 sl: 176.310 tp: 177.520 ok
03:34:18 1999.10.14 08:49 First Bot4 GBPJPY,H1: Alert: Current market price is, 177.27 My ticket number of this buy is, 38,and my stop loss is 176.31
03:34:18 1999.10.14 08:49 First Bot4 GBPJPY,H1: modify #38 buy 0.10 GBPJPY at 177.020 sl: 177.020 tp: 177.520 ok
03:34:18 1999.10.14 08:49 First Bot4 GBPJPY,H1: Alert: 0
03:34:18 1999.10.14 08:49 First Bot4 GBPJPY,H1: error(0): no error
03:34:18 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: Current market price is, 177.27 My ticket number of this buy is, 38,and my stop loss is 177.02
03:34:18 1999.10.14 09:03 First Bot4 GBPJPY,H1: OrderModify error 1
03:34:18 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: 0

-------------------

Charts are offline, atleast I have no connection, I haven't gone through all the steps to set up for optimal backtests as I see here https://www.mql5.com/go?link=http://thetrademachine.com/blog/2009/10/01/backtesting-with-metatraders-strategy-tester/ however my connection status is offline.

Also in the log I just posted it shows the entry price of the trade was 177.020. When the code fired to set to BE the market price was 177.25. So it is moving the 25 pips before firing.

 

To be honest, I still get order_modify error number 1 within my own trailing_stop code within the back_tester. The code never have or had a problem in live trading. So, I said good enough. With that said, maybe someone who have advanced knowledge would come in here and shine some light. In the mean time, lets see what we can do for ourselves. Without your entire code, it's hard to tell what you have and what you don't. I'm not telling you to post your entire code but just a suggestion.

Lets break this down so I can understand this once and for all.

for (int trades=1;trades<=OrdersTotal();trades++)

Ok, we got the all purpose loop. But wait, it's an incremental loop with for order modify purpose. Red flag (please don't ask me why) I was told that this is not a good thing so PERHAPS rewriting to

for (int trades=OrdersTotal()-1;trades>=0;trades--)

Would solve the problem. It has something to do with modification reseting the Index position if something closes. So better to count down.

Ok, next line.

if (OrderSelect(trades-1,SELECT_BY_POS)==True)

We already toke care of the the -1 issue so I don't think there's any need for it. Then below should surface.

if (OrderSelect(trades,SELECT_BY_POS)==True)

Next

if ((OrderMagicNumber()==MAGICNUM) && (OrderSymbol()==Symbol())) 

For debugging purposes, I hope the above statement is always true. If not try simplify by putting on one symbol and run one EA with one Magic#.

Next

if ((OrderType()==OP_BUY) && ((NormalizeDouble(Bid,Digits))>=(OrderOpenPrice()+BreakEven*Point)) && (OrderStopLoss() != OrderOpenPrice()))

Do me a favor and print NormalizeDouble(Bid,Digits) vs OrderStopLoss() vs OrderOpenPrice(), I'm interested to see what they look like.

 

**Changed the Loop to count down. I've seen it done both ways and was unsure of the pros and cons of either so jsut did it upwards. Changed now though.

**Order Magic num and ordersymbol are always true for these tests.

** Print is in this error code.

__________---------------

04:05:47 1999.10.14 06:09 First Bot4 GBPJPY,H1: open #38 buy 0.10 GBPJPY at 177.014 sl: 176.310 tp: 177.514 ok
04:05:47 1999.10.14 08:49 First Bot4 GBPJPY,H1: Alert: The bid price is 177.27, The Stop loss is 176.31, The order open price is 177.014
04:05:47 1999.10.14 08:49 First Bot4 GBPJPY,H1: Alert: Current market price is, 177.27 My ticket number of this buy is, 38,and my stop loss is 176.31
04:05:47 1999.10.14 08:49 First Bot4 GBPJPY,H1: modify #38 buy 0.10 GBPJPY at 177.014 sl: 177.014 tp: 177.514 ok
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: The bid price is 177.27, The Stop loss is 177.014, The order open price is 177.014
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: Current market price is, 177.27 My ticket number of this buy is, 38,and my stop loss is 177.014
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: OrderModify error 1
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: The bid price is 177.267, The Stop loss is 177.014, The order open price is 177.014
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: Current market price is, 177.267 My ticket number of this buy is, 38,and my stop loss is 177.014
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: OrderModify error 1

----------------

 
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: The bid price is 177.27, The Stop loss is 177.014, The order open price is 177.014
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: Alert: Current market price is, 177.27 My ticket number of this buy is, 38,and my stop loss is 177.014
04:05:47 1999.10.14 09:03 First Bot4 GBPJPY,H1: OrderModify error 1

 It's clearly still trying to modify the order even though the Stop_Loss and Order Open price is Equal. My guess is it's got something to do with the Points and Digits. But I cannot explain it. So we'll just have to wait til someone more knowledgeable comes along.

One other thing you can try in the meantime is adjusting the digits

cameofx 2010.07.29 10:52
  

I use below functions now, and it's usable across different brokers & instruments. 
double vPoint; 
if(Digits == 2 || Digits == 4) vPoint = Point; else
if(Digits == 3 || Digits == 5) vPoint = Point*10; // I use this for my indies; call once on init() & use where Point supposed to be used in start() 
 

 Put the above code in the int() function at the beginning of the code. Within your code where ever you have *points replace with *vPoint. Also I'll recommend turning the && statements into single arguments.

From

if ((OrderType()==OP_SELL)&&((NormalizeDouble(Ask,Digits))<=(OrderOpenPrice()-BreakEven*Point)) && (OrderStopLoss() != OrderOpenPrice()))

To:

if (OrderType()==OP_SELL)
if (Ask<=(OrderOpenPrice()-BreakEven*vPoint))
if (OrderStopLoss() != OrderOpenPrice())

Good Luck, I wish I could be more helpful. But I'm in the same shoes as you. 

 

The loop direction only matters when you are closing / deleting, because when an order is closed, the indexes of the order pool are changed and your for loop doesn't change accordingly. The pool indexes for all elements higher will change when an element is removed. If you are counting down, it is only changing indexes for those orders that you have already checked so there is no impact. If you are counting up, your for loop is out of sync with the volume of remaining elements and you will miss one.


I'm wondering now where you are calling this function... and whether it is being called multiple times. If it is being called a second time, try refreshrates() before the second call...I don't think the order pool held by the ea reflects the amendments to the server within a tick. It needs to read in new data from the server to update .... but tbh I'm guessing a bit now

V

 
ubzen:

Ok, we got the all purpose loop. But wait, it's an incremental loop with for order modify purpose. Red flag (please don't ask me why) I was told that this is not a good thing so PERHAPS rewriting to

Would solve the problem.

It's because it causes things to mess up when you have two EA's working side by side. If one of the EA's is in the loop and the other EA opens a trade or closes a trade, while the other is still in the loop, the position number of each trade changes, which could cause a trade to be skipped or to be calculated twice.

It is better to work with ticket numbers so that you know the EA will be reliable. Another reason to use ticket numbers is because when you open a trade sometimes there is a lag between when it appears in the trade window and when it was opened, which could cause multiple trades to be opened if not using ticket numbers.

Chris

Reason: