Hopefully someone can help.
managing more than one order at a time has been tough for me to get my head around.
Post code by using Alt+S
for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==16384) { if(Minute()==25) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) Print("OrderClose error ",GetLastError()); return; } } if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==16385) { if(Minute()==25) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet)) Print("OrderClose error ",GetLastError()); return; }
try this, not tested!
for(cnt=0; cnt<total; cnt++) { if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==16385) { if(Minute()>=25) { if(OrderType()==OP_BUY) { if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Violet)) Print("OrderClose error ",GetLastError()); return; } if(OrderType()==OP_SELL) { if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Violet)) Print("OrderClose error ",GetLastError()); return; } } } }
Thanks Kenneth,
Ok so how different would the code be if I wanted to close the Buy at minute 25 and the Sell at minute 26?
I used two different numbers for the orders also.
-
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
- For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum - For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE - and check OrderSelect in case later positions were deleted.
What are Function return values ? How do I use them ? - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().
- For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
- No need for two magic numbers — one (and maybe the order's type is sufficient).
If there were quite a few orders open would it also be the same situation.
I will have around 6 positions open at once.
I was trying to simplify my question by only using two orders in my example.
Thanks to everyone that contributed.
I'll continue my hunt for the answer. cheers for pointing me in the right direction.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hopefully someone can help.
managing more than one order at a time has been tough for me to get my head around.
could someone tell me why both of these orders are not closing?
it only closes the first.
i understand that this is not the first time its been asked but all the posts just have random pieces of code which i can never successfully add together without living in errorville.
long and short open simultaneously. i want them both to close on minute 25. I guess something is stopping it loop through them.
Thanks in advance if anyone helps.
<Code deleted as reposted later>