Trading daily close on daily timeframe
You may check if market is closed before trying to open trade.
https://www.mql5.com/en/forum/373227
- 2021.07.13
- www.mql5.com
IMHO this is typical issue for auto trading. This issue usually is not spotted by developers, and EA loosing effeciency.
The problem occurs not only when ordering, but also when closing. If the positions requires closing at a specific time/condition, it obviously loses performance when not secured.
This is how I deal with it.
1. In the function responsible for creating the order, I check the result
void Buy(double paLotSize){ Trade.Buy(paLotSize, _Symbol, NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK), _Digits), 0, 0); if(Trade.ResultRetcode() == TRADE_RETCODE_MARKET_CLOSED){ BuyOrder.MarketClosed = true; BuyOrder.Lots = paLotSize; } else{ BuyOrder.MarketClosed = false; } }
2. In On Tick I check the flags and repeat the order until it is completed. Most often, after a dozen or so ticks, the order is already created.
void OnTick(){ if(SellPosition.MarketClosed || BuyPosition.MarketClosed) CloseAllPositions(); if(BuyOrder.MarketClosed) Buy(BuyOrder.Lots); if(SellOrder.MarketClosed) Sell(SellOrder.Lots); ClosePositionsByProfit(); if(!NewBar(inpTimeFrame)) return; CheckToOpen(); }
and simple structure for storing info
struct MarketClosed{ bool MarketClosed; double Lots; }; MarketClosed SellOrder, BuyOrder, SellPosition, BuyPosition;
I'm trying to implement a strategy that trades on the daily timeframe based on the daily close. The problem, of course, is that the instant the daily candle closes, the market is closed. So I need to catch the next open (not necessarily NYC - in fact, don't want to wait on that).
It seems to me in theory that since OnTick() is triggered on the next available price change, that should work. But I end up getting some trades, and missing others. This is an example of what shows in the tester log, although other trades were made (these weren't retried).
I don't want to have to try to code around knowing when the market opens are, because that doesn't port well between brokers, or even different asset classes. So I thought I'd use a timer and put in a timer loop:
void ExecuteBuyOrder() { trade.SetExpertMagicNumber(MagicNumber); double stopLoss = ATRStopLossMultiplier > 0.0 ? atrValue * ATRStopLossMultiplier : 0.0; double buyPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); int retryIntervalInSeconds = 60; // Retry every minute bool orderExecuted = false; while (!orderExecuted) { if (stopLoss > 0.0) { double stopLossPrice = NormalizeDouble(buyPrice - stopLoss, _Digits); if(trade.Buy(LotSize, NULL, 0, stopLossPrice, 0, "Buy signal (test order)")) { orderExecuted = true; } } else { if(trade.Buy(LotSize, NULL, 0, 0, 0, "Buy signal (test order)")) { orderExecuted = true; } } if (!orderExecuted) { Print("Order execution failed, will retry in ", retryIntervalInSeconds, " seconds."); Sleep(retryIntervalInSeconds * 1000); // Sleep for retry interval (milliseconds) } } Print("Buy order executed successfully."); }Which seems to work in a single test (haven't checked rigorously), but it makes it not work in an optimization. The sleep function breaks it:
So, how am I supposed to trade the daily close? Am I just missing something, or is my approach completely wrong?
P.S. My broker doesn't support market-on-open orders, so that approach won't work either.