EOD flatten on RTH intermittently misses and exits next day

Hi Everyone,

I have a strategy that is suppose to flatten at 12:50 on a 5‑minute ES chart(basically 10 min before close). I’m in Pacific time (market 6:30 AM–1:00 PM local). I’ve attached three screenshots:

Behavior: On some days it skips the EOD exit and exits at the next day’s first bar.

EOD code (bar-close flatten at 12:50):

// in OnBarUpdate()
int currentTime = ToTime(Time[0]);
int endTimeExitSec  = (endTimeExit / 100) * 10000 + (endTimeExit % 100) * 100; // endTimeExit = 1250

if (currentTime >= endTimeExitSec && !eodFired && Position.MarketPosition != MarketPosition.Flat)
{
    if (Position.MarketPosition == MarketPosition.Long)
        ExitLong("EOD_1250_Exit_Market");
    else if (Position.MarketPosition == MarketPosition.Short)
        ExitShort("EOD_1250_Exit_Market");
    eodFired = true;
    Print($"EOD 12:50 market exit @ {Time[0]}");
    return;
}

Question:

Based on the attached settings and the code above, is this intermittent miss most likely a code issue or a platform/session configuration issue, and what specific change would you recommend to make the 12:50 bar‑close flatten reliable on a 5‑minute chart?
Thanks very much for your guidance!

Everything looks fine. When it’s missing, this never returns true?

if (currentTime >= endTimeExitSec && !eodFired && Position.MarketPosition != MarketPosition.Flat)

or does it just fail to flatten? What about the place where you reset the eodFired flag back to false?

1 Like

Thanks! Here’s what I have and what I’ll log to verify.

eodFired reset placement (runs before any early returns):

    // OnBarUpdate() – very top
    if (Bars.IsFirstBarOfSession)
    {
        eodFired = false;
        if (Position.MarketPosition == MarketPosition.Long)
            ExitLong("EOD_SessionOpen_ForceFlat");
        else if (Position.MarketPosition == MarketPosition.Short)
            ExitShort("EOD_SessionOpen_ForceFlat");
    }

EOD flatten (bar-close at 12:50):

int currentTime = ToTime(Time[0]);
int endTimeExitSec = (endTimeExit / 100) * 10000 + (endTimeExit % 100) * 100; // endTimeExit = 1250

if (currentTime >= endTimeExitSec && !eodFired && Position.MarketPosition != MarketPosition.Flat)
{
    if (Position.MarketPosition == MarketPosition.Long)
        ExitLong("EOD_1250_Exit_Market");
    else if (Position.MarketPosition == MarketPosition.Short)
        ExitShort("EOD_1250_Exit_Market");
    eodFired = true;
    Print($"EOD 12:50 market exit @ {Time[0]}");
    return;
}

Instrumentation I’m adding to confirm exactly what happens on missed days:

// Heartbeat every bar
Print($"HB {Time[0]} currentTime={currentTime} end={endTimeExit} eodFired={eodFired} pos={Position.MarketPosition}");

// Right before the if:
Print($"CHK {Time[0]} ct>={endTimeExitSec}? {currentTime >= endTimeExitSec}, eodFired={eodFired}, flat={Position.MarketPosition == MarketPosition.Flat}");

If this reset location and check sequence look correct, I’ll post the 12:45–13:00 logs from a day it misses so we can see whether the condition never turns true or it fails to flatten after it does. Thanks again for the guidance.

For anyone who reads this and is dealing with the same issue. The fix is very simple, all you have to do is check the box in the Strategies that says “Exit on Session Close”…

Regardless of that option, shouldn’t your code have done that manually?

Yes, you are correct. It is still a mystery why it’s not exiting manually 10 minutes before market close, but at least there is this option to get out at the end of the session.