Any ideas,tricks or scripts on how to group all open trades in a fill or kill type order would be greatly appreciated.
You could use MQL5 because it only allows one open position per symbol.
Otherwise it is messy. Suppose you have 5 open buy orders. MQL4 will only allow them to be closed individually. So I think you would need to sum up the lots in all open positions and put on a reverse position. That has effectively closed all of them at once. You can now use OrderCloseBy to get rid of the individual positions at your leisure. But I bet, since you are bothered by FIFO you are also the victim of non-hedging. In which case I see no solution.
ashlandorjohn:
This leaves open the possibility that some of the open trade positions will not close when the cumulative profit target is reached. THIS IS A MAJOR PROBLEM FOR MY STRATEGY. It makes the cummulative profit measurement worthless, because FIFO makes the oldest orders close first, which are often still in a loss position.
- Some brokers, like IBFX, implemented FIFO in the back office so the mt4 model is unaffected
- Otherwise, modify your closeAll to repeat.
bool CloseOrder(int ticket=EMPTY, double size=INF){ // INF == entire. if (ticket == EMPTY) ticket = OrderTicket(); else if (!OrderSelect(ticket, SELECT_BY_TICKET)){ Alert("OrderSelect(",ticket,",ticket) failed: ", GetLastError()); return(false); } double minLot = MarketInfo(chart.symbol, MODE_MINLOT), lotStep = MarketInfo(chart.symbol, MODE_LOTSTEP), sizeCurr = OrderLots(), sizeClose = MathRound(size/lotStep)*lotStep, sizeRem = sizeCurr - sizeClose; if (sizeClose < minLot) return(false); if (sizeRem < minLot){ sizeClose = sizeCurr; // Close all color op.color = IfI(Color.Buy, Color.Sell); } else op.color = Aqua; if (GetTradeContext() < TC_LOCKED) return(false); if (OrderClose( ticket, sizeClose, OrderClosePrice(), Slippage.Pips*pips2points, op.color )){ RelTradeContext(); return(true); } Alert("OrderClose(ticket=", ticket, ", ...) [1] failed: ", GetLastError()); RelTradeContext(); // After GetLastError return(false); } void CloseAllOrders(int op = -1){ bool repeatOnError = true; while(repeatOnError){ repeatOnError = false; 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, && (op < 0 || op == OrderType()) // and wanted type. ){ // Don't combine with &&'s Compiler bug. if (!CloseOrder()) repeatOnError = true; }
Thanks for your assistance. I am just getting back to this challenge after some time away from the project.
One quick question about this code. Does it assure that all the tickets will be closed within a specified price range if one of the tickets is closed, but the price is no longer available by the time the close order repeats and executes? This is the heart of the matter, since I cannot close the position by just placing an equal size opposite order of a specific size.
Thanks again,
Ashlandorjohn
- Some brokers, like IBFX, implemented FIFO in the back office so the mt4 model is unaffected
- Otherwise, modify your closeAll to repeat.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have been writing EA's for about a year in FXCM's strategy trader. I am new to Mql4.
I am trying to duplicate a function I have in my Strategy trader EA, which is to specify a close order (fill or kill) in the total lot volume of open trades in the currency pair chart to which the EA is attached.
In Mql4, I have not been able to figure out if it is possible to specify that all of my previous open trades close at a specified cumulative profit target from all open positions.
When I have attempted this on my demo account, the open trades will close, but they will close individually. This leaves open the possibility that some of the open trade positions will not close when the cumulative profit target is reached. THIS IS A MAJOR PROBLEM FOR MY STRATEGY. It makes the cummulative profit measurement worthless, because FIFO makes the oldest orders close first, which are often still in a loss position.
Any ideas,tricks or scripts on how to group all open trades in a fill or kill type order would be greatly appreciated.
AshlandorJohn