How can I check the last closed trade was open on candle ID 0?
-
I need to identify those trades that were open and closed within candle ID 0. I'm not sure why this config is not working:

Is the comparison searching for the exact UNIX time the current candle was open, maybe? The trade can be closed at any moment AFTER the current candle was open. It doesn't need to be open at the first tick of the current candle, as this setting is apparently doing.
Am I missing something?
-
Try this:

-
I'm afraid it didn't work.

-
@l-andorrà This is what ChatGPY created.
//+------------------------------------------------------------------+
//| Function to check if trade was opened and closed in same candle |
//+------------------------------------------------------------------+
bool IsTradeClosedInSameCandle(ulong ticket)
{
// Get trade history by ticket
HistorySelect(0, TimeCurrent());
ulong history_ticket = HistoryOrderGetTicket(ticket);if(history_ticket == 0)
return false;// Get trade open and close times
datetime open_time = (datetime)HistoryOrderGetInteger(history_ticket, ORDER_TIME_OPEN);
datetime close_time = (datetime)HistoryOrderGetInteger(history_ticket, ORDER_TIME_DONE);if(open_time == 0 || close_time == 0)
return false;// Get current timeframe in seconds
ENUM_TIMEFRAMES current_tf = Period();
int tf_seconds = PeriodSeconds(current_tf);// Calculate candle start times
datetime open_candle_start = open_time - (open_time % tf_seconds);
datetime close_candle_start = close_time - (close_time % tf_seconds);// Check if both open and close are in same candle
return (open_candle_start == close_candle_start);
}How could I make it work below a 'For each position' block?
-
I assume this did what was needed, I use ChatGPT for this type of code, the problem is that the FX trade time returns as a string, not a number. This is MT4 but will show you what I mean:
-
Interesting. I don't understand why it doesn't work on MT5.
