Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close all open positions automatically at 3:58 pm

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

    Close all open positions automatically at 3:58 pm

    Hi,

    Is there a way to have NinjaTrader automatically close all open positions at 3:58 pm ET (two minutes before the market closes)? I am entering positions during the day by writing oif files (from outside of NinjaTrader), but would like to automatically have them all closed at 3:58 pm, if they haven't been closed out before.

    Thanks for any help,

    Tom

    #2
    This can only be done from a NinjaScript strategy in closing the strategy's particular positions.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      OK, thanks, I figured out how to do this by writing a FLATTENEVERYTHING command to an oif file at 3:58 pm.

      Tom

      Comment


        #4
        What is the code to accomplish this? I tried

        private int endofsession = 135800;
        if (ToTime(Time[0]) <= endofsession)
        {FlattenEverything;}

        and got the error that FlattenEverything does not exist in this context.

        I'm trying this because I couldn't get

        ExitOnClose = true; // Exit position at the end of the trading day
        ExitOnCloseSeconds = 36120; // 10 hours 120 seconds before close. End of session is 12:00 AM

        to work.

        Thanks, Tim

        Comment


          #5
          FlattenEverything is not an actual property for NinjaScript.

          If you want to flatten everything you would need to go through the logic of closing all your positions. This means using things like ExitLong() if you are long or ExitShort() if you are short. You would also want to cancel any pending orders.

          Ultimately though you are using two competing techniques. You should only choose one. If you are trying to close it via code logic then you can use ExitLong() and ExitShort(). If you are trying to use the ExitOnClose feature, just setting that to true will do the trick. You do not need to add any additional logic.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            The session close is 12:00 AM, so ExitOnClose doesn't exit my position when I want. I want to keep it that way so my indicators use 24 hour data. I'm running this strategy on a Futures contract. However, I want to close all open positions before 2:00 PM local time. Why isn't ExitOnCloseSeconds working?

            ExitOnClose = true; // Exit position at the end of the trading day
            ExitOnCloseSeconds = 36120; // 10 hours 120 seconds before close. End of session is 12:00 AM

            Thanks, Tim

            Comment


              #7
              Tim, please set TraceOrders = true in the Initalize() section of the code and then open up the output window (Tools -> Output Window) and all the order information will be presented there. It will also include descriptions of why an order could've been ignored. You can run your strategy as a backtest or real-time, and every bit of order information will be printed.
              AustinNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by chbruno, Today, 04:10 PM
              0 responses
              3 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              6 views
              0 likes
              Last Post FAQtrader  
              Started by rocketman7, Today, 09:41 AM
              5 responses
              19 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by frslvr, 04-11-2024, 07:26 AM
              9 responses
              127 views
              1 like
              Last Post caryc123  
              Working...
              X