Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close all open orders & flatten all open positions based on a time filter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Close all open orders & flatten all open positions based on a time filter

    Hello,

    I am currently working on developing an automated trading strategy. My objective is to be able to close all potentially open stopMarket orders and all potentially open positions at a specific point in time to prevent margin calls. Since I trade on tick charts, I think it wouldnt be sufficient to place such a logic under 'protected override void OnBarUpdate()' as sometimes, a lot of time can pass between bars are opening and closing.

    For the orders, is there anything I can do to:
    1. Identify all open stopMarket orders (I only use stopMarket orders to enter trades, nothing else)
    2. Close them all at a specific point in time

    For the positions, is there anything I can do to:
    1. Identify all open positions
    2. Close them all at a specific point in time

    For reference:
    I have tried to use the 'ExitOnSessionClose' thing in the strategy, but I still had a trade that was opened at 10:35PM and closed with 'exit on session close' at 10:59PM. I live in the german time zone if that makes a difference. The settings I have used below.

    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 1200;

    So according to my understanding, this should actually close everything 1200 seconds prior to session close, therefore 20 minutes prior to sessions close. Looking at the official trading times according to CME (I trade MES), there is a daily stop between 4pm and 5pm central time. To get the german time zone, it is basically +7 hours. Then, taking into account that traders are required to close their positions 15min prior to avoid margin calls, I thought I go with 20min just to be extra safe. This does not seem to work during backtesting. So I am a bit lost as to which is the best way to accomplish my objective here.

    Hope this gives a good picture of what I am trying to achieve here.

    Best wishes.

    #2
    Hello arni90,

    For a strategy to close all of its own positions or orders at a specific time you can use a time condition for that. Time conditions are also needed for your entry logic so that after the given time it doesn't try to re enter. Exit on session close won't prevent the strategy from re entering past that point and also does not work the same in backtesting as it does in realtime. For that reason you can skip using the exit on session close feature.

    There is a sample of creating a time condition in the following link



    To exit a position at a specific time you can check the market position and then submit a market exit order: https://ninjatrader.com/support/help...marketposition

    If you have orders open and wanted to cancel them yourself you would need to store the orders as variables and use CancelOrder: https://ninjatrader.com/support/help...ub=cancelorder


    Comment


      #3
      Hello Jesse,

      thanks for the swift reply. I forgot to mention that I have the time condition below in place as well under 'protected override void OnBarUpdate()' which should stop the strategy from submitting any order post that time:
      if(ToTime(Time[0]) >= 224000)
      return;

      As per my understanding, if in addition to the time condition above, I use the 'IsExitOnSessionCloseStrategy', it should close all open positions 20 minutes prior to the official session close.
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 1200;

      So that means...
      • no orders will be submitted post 10:40PM (3:40PM central time)
      • all positions will be closed at 10:40PM (3:40PM central time)
      Now it seems the only case where a trade could slip through is when the strategy places a stop market order before 10:40PM and this gets filled after 10:40PM. So as a consequence, I would have to look for all open stop market orders at 10:40PM and close them. Can you confirm I understand this correctly? Plus, the 'IsExitOnSessionCloseStrategy' doesnt work on backtesting, so it wouldnt show there.

      Comment


        #4
        Hello arni90,

        Your time condition would prevent orders past that time but that also prevents the rest of your OnBarUpdate logic from working because of the return statement.

        The exit on session close would exit the position but does not control your logic or orders at all.

        If you wanted to have this work the same in backtesting and realtime you can skip using exit on session close and instead program your own exit orders based on time conditions. Your entry conditions should include the time condition you had shown instead of using a general return statement so that the exit statements can still work.

        If you have orders that you need to cancel at a specific time you would need to add logic to do that to ensure the orders are cancelled or ensure they are not submitted before you use exit on session close.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, 03-13-2026, 05:17 AM
        0 responses
        86 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        151 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        79 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        53 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        61 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X