Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Market On CLose

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

    Market On CLose

    Hi. I am running NT 7.0.1000.2

    Is there a command for a Market On Close order. I have been searching around for it but am unable to find one.

    I am running a strategy on daily bars and when the strategy signals for me to exit a position I would like to do it as close to the close as possible.

    The strategy is currently exiting with a market order and this sometimes causes the strategy to open and close numerous positions when the security is trading right at the exit signal. I would like the strategy to remain in the current position, determine whether the exit criteria still exist at the end of the day. If it does i would then like it to initiate a market order as close to the close as possible.

    Does this sound like something that can be done?

    Thanks

    #2
    Hello Mannygags,

    Strategies have an ExitOnClose property you can set to true. When true, it will close any open positions based on the Exit on close seconds. It will use your session definitions to mark the close boundary.

    If entry conditions continue to evaluate true after this position is closed with this method, it will still submit orders.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      So I can just replace the ExitLong or ExitShort command with ExitOnClose?

      The strategy will need to hold positions over night as well. This will not cause it to exit a position even though the strategy dictates that it should not?

      Thanks

      Comment


        #4
        ExitOnClose is an option you set when running the strategy. You could use it in replace of or in addition to ExitLong(), ExitShort(). If there is not an open position when you call these order methods, then it will be ignored.

        It will exit positions at the close of sessions so not sure what you mean by the statement below. Can you please clarify?

        The strategy will need to hold positions over night as well. This will not cause it to exit a position even though the strategy dictates that it should not?
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Just that sometimes I will need to hold a position for multiple days. I only want to exit on the close when the strategy signals it is time to get out. I do not want to be flat every night, only when the strategy says so.

          Thanks

          Comment


            #6
            I see. Thanks for the reply. In this case, ExitOnClose may not be the best option for you. It's set at strategy runtime, and can't be dynamically enabled / disabled. If you want to selectively close your positions at session end, then this must be custom coded in your exit conditions.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Any thoughts on how I might do that? I have been unable to figure it out????

              Thanks

              Comment


                #8
                There is unfortunately no "endofSession" event you can monitor for. You can use Time[0] to access the bar's time stamp or DateTime.Now to access your computer's system clock. DateTime.Now won't give you useful results when used historically (in a backtest)

                When using Time[0], it's good to be aware that the time represents when the bar will end. You can check for exact equality or a range of times.

                Here is an example that checks bar's time stamps for the range of times between 16:00 and 16:15.

                if (ToTime(Time[0]) > 160000 && ToTime(Time[0]) < 161500)

                Additional help for working with DateTime objects is available here:
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  I had replaced the market orders with the ExitOnClose command. ExitOnCloseSeconds = 30. I was running the strategy in Sim mode at the end of the day. 3 of the 4 positions closed out as expected and the 4th position remained open and I am now carrying over night (as expected). So it seems to work...

                  The one issue that occurred was that the strategy sent two orders to close out one of the positions resulting in a position reversal. NT recognized this and sent another order to get the position flat. Not sure what would have caused this, but I will continue to monitor. perhaps just a glitch?

                  Thanks

                  Comment


                    #10
                    For ExitOnClose to work, there must be a trade that takes place in between the ExitOnCloseSeconds time and the actual market close. For illiquid markets you should consider increasing this beyond 30 so you can make sure a trade takes place.

                    Market replay is a good place to verify ExitOnClose functionality as you don't have to wait once per day to check it out. If you identify any behavior that's inconsistent with what we've been discussing, please let us know how to reproduce.

                    For the scenario you ran into, we'd need to look at your log and trace files to investigate. You can submit by using the Help > Mail to support feature - Attn: RyanM
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      OK, this may not be working as well as I had hoped. When I restarted the strategies today, the positions that were closed out yesterday reappeared. It's as if the exits from the previous day are ignored. Is this happening because the strategy wants to exit intra-bar but when it looks at the historical data to calculate the position it only sees Open-High-Low-Close?

                      When I run my original strategy that utilizes market orders to exit the positions, the strategy calculates the correct position. Not sure what is causing the discrepancy?

                      Comment


                        #12
                        Not sure what may have happened. Can you please share a code snippet, strategy settings used and a description of what you expect to happen with this?
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi. I apologize in advance for the long winded post, but... I was able to get my positions to exit at or near the close by adding ToTime(DateTime.Now) > 155930 as one of my exit criteria. This seems to work fine in simulation. I would attempt to carry positions overnight. The one issue that I continued to have was when I restarted my strategies the next day. The strategy would calculate the positions incorrectly because my exit is dependent upon the ToTime parameter. I was able to get around this by telling the strategy when it was looking at historical data to ignore the ToTime parameter. I did this by using if (Historical) and then regenerating my strategy using market orders only. This seems to calculate the correct positions.

                          My problem again arises at startup though when I am carrying a position over night. I should probably mention that I am only telling my strategy to operate during 9:30 - 16:00 ET by using the ToTime function.

                          What happens. I turn on NT and connect. All of my positions are there as well as my strategies that were running the day before. I have "submit live working orders" and sync on start = false. If I enable the strategies before 9:30 the strategies sit in a suspened state (which I expect) showing no positions. Then at 9:30 it calculates the correct position but it submits live orders which then double my account positions. If I would wait until just after 9:30 it would seem to work as I would hope, the strategy calculates a position that is in sync with my account and does not submit an order that skews my account position. A bit of a hassle in that I needed to wait to turn on my strategy but I figured I could live with it. Unfortunately this still is not a good work around.

                          If I am carrying a position overnight and at startup the strategy signals an exit, it submits the market order at that instant. If the exit signal were to come soon after the strategy was started, the strategy would wait until 15:59, as expected. So I am a bit stuck on when I can start up my strategies sometimes it needs to be pre-market, sometimes right after the open.

                          What I want to do is be able to wake up in the morning turn on my strategies and let them operate. I don't want to have to try and figure out when I should be starting them. I just want my strategies and account positions to be in sync without having to manipulate, When I have changed my setting to 'sync on start = true', I observed the strategy zeroing out my position to get flat and then submitting and order to get my account to sync to the strategy. I don't want this excessive churn either.

                          Their must be an easy way to get both in sync and have my strategy execute at the proper times. I am very junior when it comes to programming, so perhaps their is something simple that I am missing, but I have been struggling with these issues considerably and need some help. Any suggestions you could offer would be much apprecciated.

                          Thanks

                          Comment


                            #14
                            Thank you for the reply, Manny. I believe you have a good handle on how it works, and unfortunately there is no easy option to match a prior strategy run with a new one. There is no persistence from one strategy to another. When the new strategy is run, it looks at the historical (virtual) executions and then compares these to any actual ones that were placed by the strategy earlier. Since your strategy uses real time conditions based on your PC clock, there will never be a match between the two.

                            I would consider either 1) running your strategy overnight or 2) manage any overnight positions manually, and use a wait until flat setting for your strategy.
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks for the reply.

                              Closing the positions manually can be an option, although not always the best, as I would need to ensure that I am around a computer for the close, which is not always possible for me.

                              Also, I currently use IB for my broker and data feeds. I have read numerous times that TWS needs to come down every day. Won't this disable my strategies, causing them to stop running anyway and preventing me from keeping them running overnight? If so, is there a work around?

                              I almost need a 'pause' button for the strategy instead if 'disable.'

                              Any further thoughts would be much appreciated.

                              Thanks again.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X