What I am trying to do below is when the EA places an order to then check the close of the subsequent bar and see if it is higher or lower than the OrderOpenPrice() depending if it is a long or short position respectively. I use a bool called "check" when an order is initiated to start the check cycle then after the first bar reset it so that it stops checking after subsequent first bar. If the subsequent bar closes in the wrong direction then close the order.
The problem I'm running into is how to tell it to check after the subsequent bar closes or even if the below is the right approach. Right now it is looking at the bar behind it...
Thanks for any help
A few points. You need to check the order closed before setting your flag.
You need to break out of the for cnt as soon as you close the position since OrdersTotal has now changed.
Run this test at the start of the new bar, then using Close[1] will be correct.
Do you mean to use break; if one of the conditions is met like below?
I added a time check and that puts me on the correct bars. The problem I am having now is that it skips past the if(OrderType()==OP_SELL) statement everytime. If I comment out the OP_BUY statement before it then it works fine. I'm not sure why the second set is skipped completely...
if(OrdersTotal()>0 && check==true) { int total=OrdersTotal(); for(int cnt=0;cnt<total;cnt++) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderSymbol()==Symbol()) { if(OrderOpenTime()<Time[1]) { if(OrderType()==OP_BUY) { if(Close[2]<Close[1]) { check=false; break; } if(Close[2]>Close[1]) { OrderClose(OrderTicket(),Lots,Bid,3,PaleGreen); check=false; break; } } if(OrderType()==OP_SELL) { if(Close[2]>Close[1]) { check=false; break; } if(Close[2]<Close[1]) { OrderClose(OrderTicket(),Lots,Ask,3,PaleGreen); check=false; break; } } } } } }
You need to break out of the for cnt as soon as you close the position since OrdersTotal has now changed.
for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair. ){
Do you mean to use break; if one of the conditions is met like below?
I added a time check and that puts me on the correct bars. The problem I am having now is that it skips past the if(OrderType()==OP_SELL) statement everytime. If I comment out the OP_BUY statement before it then it works fine. I'm not sure why the second set is skipped completely...
No. That is wrong. Re-read this
https://docs.mql4.com/basis/operators/break
what I said was
"You need to break out of the for cnt as soon as you close the position since OrdersTotal has now changed."
And I meant exactly that and not anything like "break after any old random test".
You are in a for loop. When you are done you use a break and it quits executing the for loop immediately. I am presupposing you only have one order on for the current symbol. If you have more than one then you must do as WHR has said immediately above and count down not up in the for loop. So you had 6 open orders and you delete one. What happens to the order count. Now 5. You were on an order in position 3. You delete that. An order shuffles back into position 3 to fill the gap. Hence WHR suggests counting down, a very sensible approach.
Much appreciated (still under a year of MQL experience and no real programming background). I've changed it to count down instead.
You must count down in the presence of other orders on other charts (other EAs) If the Other EA closes an order while you are looping, you'll possibly miss your order. Count down ALWAYS.
It's only an issue if one of the EAs is being tasked with closing Orders for both EAs. I don't think that is the case in the context of this thread.
No. The topic of conversation/debate is simply the length of OrdersTotal(). Suppose one EA has 10 orders on and so does the other. Each order has a magic number appropriate to its commanding EA. Both EAs read OrdersTotal() and start looping through the orders by position. Fast EA closes its 10 positions whilst Slow EA is having trouble closing one. It doesn't matter if the list is being counted backward or forwards, Slow EA thinks there are 20 items in the list and now there are only 10. It is now indexing non-existent locations with the OrderSelect() function. Hopefully that won't actually crash the system.
So Slow EA closes its position, there now being 9 open positions. FastEA now opens 10 positions in the blink of an eye because its Symbol is fast moving. We now have to rely on these new orders being placed at the end of the list. If it decides to put them lower down the list, slow EA is not indexing invalid positions but misses its trades.
I have been working with OrderHistory recently and frankly the order of the trades in the history seems to be some weird polynomial function of unknown variables. I was expecting them to go into the history in the order they were closed. No such luck. I ended up having to load the appropriate (by magic number) trades into an array and sort it to find out which were the last ones closed. I would worry about what the ordering algorithm was in the (undocumented) OrderSelect by Position function, and would be unhappy using this undocumented feature in case it later broke by software upgrade or different broker. That was the point I was trying to make and is another reason why I would prefer each EA to have its own account. (I have 5 accounts with the same broker). It is much easier to evaluate strategies; you literally look at the bottom line!
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
What I am trying to do below is when the EA places an order to then check the close of the subsequent bar and see if it is higher or lower than the OrderOpenPrice() depending if it is a long or short position respectively. I use a bool called "check" when an order is initiated to start the check cycle then after the first bar reset it so that it stops checking after subsequent first bar. If the subsequent bar closes in the wrong direction then close the order.
The problem I'm running into is how to tell it to check after the subsequent bar closes or even if the below is the right approach. Right now it is looking at the bar behind it...
Thanks for any help