Exit a Specific Amount of Time Before the Session End Time (Works on Partial Trading Days, Overnight Markets, and Across Time Zones) (Point-and-Click NinjaTrader® Strategy)
Original Trading Strategy
Peer Reviews, Modifications & Responses
Thanks for Sharing!
Trading Strategy Summary
Introduction
This strategy can be used both 1) as a learning tool to help you understand how to create trading logic that flattens a strategy’s open positions a specified number of seconds (like 3600 seconds for 1 hour) before the session end time, and 2) as a source from which to quickly copy and paste this trading logic into your own strategy.
This custom “exit X seconds before the end of the session” trading logic handles partial trading days as well, i.e. you can set the value of an input parameter to exit 3600 seconds (1 hour) before the session’s end time, regardless of whether the session is a regular session or a partial session due to a holiday, and this will cause any open long or short position to be flattened.
It also handles overnight markets like those that open at 5 PM and close at 4 PM the next calendar day, and it wouldn’t have a problem if the end time of the session is past midnight but the 3600 seconds before the session end time is before midnight. This is because this strategy’s custom “exit X seconds before the end of the session” logic does all its calculations not only on the time of day but also on the date part, i.e. its calculations all use Quagensia N Edition’s “Date and Time” data type.
It also works across time zones. For instance if the times in your installation of NinjaTrader® are in the time zone in London, Paris, or Sydney but you are trading a CME future whose session end time is in the Chicago time zone, the session end time may be 4 PM in Chicago time year-round, but in your time zone the session end time will go up and down by one hour when daylight savings time starts and ends in the United States, and will go up and down on other dates as well if your time zone also has its own “daylight savings time” or similar concept where you set your clocks backward or forward on different dates. The custom “exit X seconds before the end of the session” trading logic in this strategy handles these cross-time zone issues as well, i.e. the session end time of the CME futures contract in this example would be the correct time in your time zone, regardless of cross-time zone issues created by daylight savings time and similar concepts in other time zones, and the date and time calculated by subtracting the specified number of seconds from the session end time in your time zone will also be correct.
The custom “exit X seconds before the end of the session” trading logic in this strategy allows a strategy to flatten any open long or short position even at times that are very far from the end of the session, including hours before the session ends, and not consider re-entering until the start of the next session. See the section named “*** PROS AND CONS OF THIS STRATEGY’S CUSTOM “EXIT X SECONDS BEFORE THE END OF THE SESSION” TRADING LOGIC ***” somewhat further down in this “Description & Notes” field to learn more about when you might want to use this strategy’s custom “exit X seconds before the end of the session” trading logic and when you shouldn’t use it.
This custom “exit X seconds before the end of the session” trading logic can be seen at the top of the “When Bar Updates” section, and includes a lot of logic. The logic starts at the very top of the “When Bar Updates” section, starting after the “Comment in Code” component that reads “STEP #1: Handle the Trading Is Allowed Based on Filter ‘Seconds From Session Close To Cease Trading’ Logic” and goes all the way down to directly above the “Comment in Code” component that starts with”END OF STEP #1″.
WANT TO LEARN? Please read the “Description” fields of the input parameters that start with the first one, named “Initiate a Cease in Trading After ‘Seconds From Session Close To Cease Trading'”, and continue down to the one above the one named “Show More Info About Session & Trading Day Start & End Times” in the “Input Parameters” section, and read the “Comment in Code” components in the relatively large amount of trading logic at the top of the “When Bar Updates” section described in the last paragraph to learn more about how this strategy’s custom “exit X seconds before the end of the session” trading logic is implemented.
How to Copy and Paste This Quagensia N Edition Strategy’s Custom “Exit X Seconds Before the End of the Session” Trading Logic into Your Own Quagensia N Edition Strategy
IN A HURRY AND WANT TO USE THE CUSTOM “EXIT X SECONDS BEFORE THE END OF THE SESSION” LOGIC IN YOUR OWN STRATEGY IN THE LEAST AMOUNT OF TIME? You only need to copy and paste trading logic from the top of the “When Bar Updates” section of this Quagensia N Edition Strategy into your own Quagensia N Edition Strategy to do this. The instructions to do this are in the “Comment in Code” text box that starts with “END OF STEP #1: ‘Handle the Trading Is Allowed Based on Filter “Seconds From Session Close To Cease Trading” Logic'” that is most of the way down the When Bar Updates section of this strategy and directly above this strategy’s set of entry conditions.
Pros and Cons of This Strategy’s Custom “Exit X Seconds Before the End of the Session” Trading Logic
This strategy’s custom “exit X seconds before the end of the session” trading logic has some weaknesses and some strengths when compared to the trading logic that can be implemented simply by checking the “Exit on Session Close” check box and entering a number into the “Exit on Session Close Seconds” text box in the “Strategy Details” section of this Quagensia N Edition Strategy. Some of these weaknesses and strengths are mentioned below.
KNOWN WEAKNESS OF THE CUSTOM “EXIT X SECONDS BEFORE THE END OF THE SESSION” TRADING LOGIC: The market order that flattens the long or short position that may exist when the custom “exit X seconds before the end of the session” trading logic is triggered would end up not being able to flatten the position before the end of the session if it is triggered “On bar close” of the last bar of the session, and instead would be unlikely to be able to flatten the position until the start of the next session, leading to holding a potentially highly leveraged position “overnight” or at least until the start of the next session. For historical price bars (including all price bars in Strategy Analyzer), even if the strategy’s “Calculate” field was set to “On each tick” or “On price change” in the NinjaTrader® user interface, historical bars only call the “When Bar Updates” section of this strategy “On bar close”, so if a triggering bar is the last bar of a session and the bar is a historical bar, the market order to flatten the position would show a historical execution on the open of the first bar of the next session. This causes inaccurate historical executions, which would cause the backtests of the strategy in Strategy Analyzer to be very inaccurate if on realtime bars in realtime trading for a strategy whose “Calculate” field is set to “On each tick” or “On price change” in the NinjaTrader® user interface the strategy could flatten its position by the end of the session if there was enough time after the triggering tick or price change to submit a market order to flatten the position (and enough time for it to be filled).
BENEFIT #1 OF THE CUSTOM “EXIT X SECONDS BEFORE THE END OF THE SESSION” TRADING LOGIC: There are *no re-entries* after the “Exit on Session Close Seconds” time. The custom logic doesn’t just flatten all open positions; it also *disallows new orders* from being submitted from the strategy for the remainder of the session. This is *different* than the behavior that can be achieved using the two aforementioned fields in the “Strategy Details” section, because if your strategy depends solely on checking the “Exit on Session Close” check box and entering a number into the “Exit on Session Close Seconds” text box in the “Strategy Details” section, there is always the chance that a new entry order could be submitted for the remainder of the session that occurs *after* the date and time that is equal to the session end time minus the number of seconds specified in the “Exit on Session Close Seconds” field through the session end time. While setting the value of the “Exit on Session Close Seconds” text box to a small number, like 30 seconds, may greatly reduce the odds of entering a new position, the market would still have 30 seconds to potentially move in a way that your strategy would identify as a being a new entry signal. And if an entry order submitted in the last 30 seconds of the session is filled and you are using a lot of leverage, you could end up with a highly leveraged position that is held “overnight”, or at least during the time between a session end time and the next session start time.
BENEFIT #2 OF THE CUSTOM “EXIT X SECONDS BEFORE THE END OF THE SESSION” TRADING LOGIC: The number of seconds entered into the “Seconds Before Session Close On or After Which the Next ‘When Bar Updates’ Should Initiate a Cease in Trading” input parameter of this strategy will also work in back-tests (with the limitation that the bar time used to check against the date and time equal to the session end time minus a number of seconds equal to the value of the input parameter named “Seconds Before Session Close On or After Which the Next ‘When Bar Updates’ Should Initiate a Cease in Trading’ input parameter will be equal to the *bar end time* of historical bars even if this strategy’s “Calculate” field is set to “On each tick” or “On price change” in the NinjaTrader® user interface), whereas the positive number of seconds entered into the “Exit on Session Close Seconds” text box will likely have no impact when the strategy is being run on historical bars. For this reason, the behavior of the custom “exit X seconds before the end of the session” trading logic in back-tests may be closer to this behavior when run on real-time bars.
Copyright © 2025 Quagensia, Inc. All rights reserved.
How to Get the Quagensia Strategies
This point-and-click trading strategy is built into Quagensia N Edition (for NinjaTrader®).
Customize Quagensia Strategies with Your Own Ideas
Tweaking Quagensia Trading Strategies using point-and-click with the Quagensia Desktop Application is easy for non-programmers. If you get stuck you can usually find the answer you need in our online help documentation or you can ask a question in our friendly Discord community.
If you don’t want to tweak a Quagensia Trading Strategy with point-and-click to add your own proprietary trading logic, you can still download a Quagensia Trading Strategy file, open it up in the Quagensia Desktop Application, and generate its code, then backtest and optimize the trading strategy on different instruments, different bar periods (weekly bars, daily bars, hourly bars, 15-minute bars, etc.), or different bar types (time-based bars, volume-based bars, tick-based bars, etc.), and use different start and end dates.
Some tweaks you can make to the Quagensia Trading Strategies you download include:
Modify the entry & exit logic.
- Add more conditions, remove conditions, or change them by choosing from among a very large number of components, including many exotic indicators.
- Add or modify stop losses, trailing stops, and profit targets. Make them tighter, less tight, or based on an entirely different calculation.
- Add or modify time stops. For example, exit after a certain number of bars either unconditionally or only if the post-entry price action did or did not exhibit certain characteristics.
Enhance the output of the strategy to go beyond simply placing orders.
- Draw lines, shapes, and text on the chart. For instance, you can mark times or prices where each entry or exit condition of a multi-condition entry or exit was true, even if all the necessary entry or exit conditions were not true at the same time so an entry or exit did not occur.
- Write information to NinjaTrader®’s NinjaScript® Output window or TradeStation®’s EasyLanguage® Print Log window.
- Write information to a file. You can even output a report that can be opened in Microsoft Excel or consumed by another application that reads comma-delimited, semicolon-delimited, or otherwise character-delimited tabular data files.


