Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to terminate/stop the strategy within the script

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

    How to terminate/stop the strategy within the script

    How to terminate/stop the strategy within the script ?

    In the event of unknown exception/error occurs, I would like to call an emergency terminate function as risk control

    1. terminate the strategy
    2. flatten everything (cancel all orders, and close all open position)

    Last edited by ppau2004; 10-29-2020, 12:42 AM.

    #2
    Hello ppau2004,

    Thanks for your post.

    You can call CloseStrategy(). https://ninjatrader.com/support/help...sestrategy.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello,
      the definition of the CloseStrategy method says: "Cancels all working orders, closes any existing positions, and finally disables the strategy".
      It means that after calling the CloseStrategy method (and exiting the calling method), code will no longer be processed in the script, because the strategy is disabled?
      Thanks
      Last edited by cls71; 11-12-2020, 06:02 AM.

      Comment


        #4
        Hello cls71,

        Thanks for your post.

        Correct, once a strategy is disabled it is no longer running and would need to be manually re-enabled to run again.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hello Paul,

          I'm not entirely sure that is so.

          Inside the OnOrderUpdate I have called the CloseStratregy method, but still I have continued to receive several updates from the OnOrderUpdate method.
          Is possible that the OnOrderUpdate method can be executed several more times, after calling CloseStrategy ()?

          Thanks

          Comment


            #6
            Hello cls71,

            Thanks for your reply.

            Yes, that is possible but these would happen between the time the method is called and when the strategy actually is disabled. Keep in mind what the help guide advises: "Cancels all working orders, closes any existing positions, and finally disables the strategy" meaning that there is a sequence it follows and in that sequence, you could/would get OnOrderUpdates()

            Reference: https://ninjatrader.com/support/help...sestrategy.htm

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thanks Paul,

              this behavior should be noted in the documentation with a very big warning: the CloseStrategy method does not disable the strategy immediately and order event handling methods can continue to be executed afterwards.

              Comment


                #8
                Hello cls71,

                Thanks for your reply.

                "... the CloseStrategy method does not disable the strategy immediately and order event handling methods can continue to be executed afterwards.", just to clarify your comment, you mean that the order handling methods, which the system uses to Close positions and cancel orders continues to operate until the strategy is disabled. Once disabled the strategy cannot run.

                Your original statement leads one to believe you are saying the strategy continues to run after it has been disabled. Can you clarify that is the case?
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  No Paul, what I'm saying is that after calling the standard function CloseStrategy () and exiting the method from where it is called (in my specific case the OnOrderUpdate after a Rejected), surprisingly the OnOrderUpdate method continues to execute several more times.

                  That is, the call to CloseStrategy does not instantly disable the strategy. The strategy continues to receive order updates when it supposedly should have been disabled after the CloseStrategy call.

                  Obviously the internal code of the CloseStrategy method will cancel orders and close the position before disabling the strategy, and this will consume some time, but what I did not expect is that NinjaScript code would continue to be executed in my strategy through the OnOrderUpdate method.

                  I suspect that if there are still pending order events in the queue, they are processed even after the CloseStrategy has been called, and it shouldn't be. If I call CloseStrategy it is because I have detected an unexpected scenario that I don't want to handle it and decide to end it. I don't care what comes next (more OnOrderUpdates, OnExecutionUpdates, etc) and I don't want to process any more Ninjascript code. I just want to terminate everything after calling CloseStratey

                  Thanks.

                  Comment


                    #10
                    Hello cls71,

                    Thanks for your reply.

                    I've modified the public strategy SampleOnOrderUpdate() to place a pending order and then call CloseStrategy(). In the OnOrderUpdate() and OnExecutionUpdate() methods I placed a print statement along with an EnterLong() order.

                    I do not see the EnterLong orders, I do not see the print statements which indicates the methods are not being called after CloseStrategy(). The pending order does get canceled. I also tested this when the strategy was in a position and again do not see OnOrderUpdate() or OnExecutionUpdate(). I was incorrect in advising that "...there is a sequence it follows and in that sequence, you could/would get OnOrderUpdates()" in post #6 and will correct that post later.

                    I've attached the example strategy for you to test and confirm. You are welcome to further modify to demonstrate the issue you are seeing.

                    CLS71SampleOnOrderUpdate.zip
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Paul,

                      the problem happened in real account, with partial filleds, and after receiving a Rejected from the broker. This specific scenario cannot be simulated in demo.

                      In summary, 1 market buy order of 2 contracts was sent, which had 2 partial fills. And 2 limit sell orders were submitted above with 2 contracts each. For some inexplicable reason not resolved by Ninjatrader Brokerage for now, the second limit sell order was rejected for lack of guarantees. The strategy received the Rejected in the OnOrderUpdate and the CloseStrategy was called. And here came the error. I expected that after exiting the OnOrderUpdate method no more code would be executed. Well no, the OnOrderUpdate method was executed two more times, and the CloseStrategy was mistakenly called another two times.
                      But one might think that it doesn't matter how many times the CloseStrategy is called because the last call should leave all FLAT. Well no, the position remained open but with 4 shorts.

                      I can assure you that after calling the CloseStrategy method, the OnOrderUpdate method was executed several more times.

                      Comment


                        #12
                        Hello cls71,

                        Thanks for your reply.

                        Just a quick note to let you know we are investigating further and will update this thread when we have something to advise or if further information is needed.

                        Thanks in advance for your patience.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello cls71,

                          Thanks for your patience.

                          I've attached a short video demonstrating a rejected order scenario with CloseStrategy and used prints in OnOrderUpdate() that show it is not called after close strategy and close strategy is not called more than once.

                          Video: https://Paul-ninjaTrader.tinytake.co...Nl8xNTI4OTY4Nw

                          Test file: CLS71Testing2.zip

                          Using the test here, I am not seeing order updates for the strategy occur after the strategy has called CloseStrategy. I am on version 23 and here is a test script and video demonstration showing this in action.

                          However, focusing on OrderUpdates occurring after the strategy is disabled should not be our concern. (It would be possible for orderupdates to come through as a matter of timing when the strategy disables.)

                          The concern would be how the logic has allowed CloseStrategy to be called multiple times. If you look at the example here, it would not be possible for CloseStrategy to be called more than once since it is called when stopOrder is not null, when stopOrder gets the order rejected update, and we set the stopOrder object to null here since the order is terminal.

                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello cls71,

                            I believe you are working with a mutual client of ours and I have asked this client to have their developer reach out to me directly for what happened with their strategy.

                            What is described here sounds similar to what the mutual client was reporting and I would like to make sure all ends our tied.

                            Could you reach out to me at platformsupport [at] ninjatrader [dot] com if this is you? Please include the text "Attn Jim" and a link to this thread.
                            JimNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by YevhenShynkarenko, Today, 12:16 AM
                            1 response
                            31 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by JChien, Today, 06:26 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_PatrickG  
                            Started by dastaan, Today, 06:24 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post dastaan
                            by dastaan
                             
                            Started by manueldecastro, Today, 04:57 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post manueldecastro  
                            Started by pthomas, 08-31-2020, 05:15 PM
                            16 responses
                            160 views
                            0 likes
                            Last Post peregrine  
                            Working...
                            X