Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Does pressing F5 in the chart immediately terminate the strategy?

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

    Does pressing F5 in the chart immediately terminate the strategy?

    Hello,

    When each bar arrives, the strategy will go through onbarupdate again.
    Suppose the strategy is very complex and takes a long time. For example, it takes 5 seconds to complete the logic in the onbarupdate.
    During the 5-second calculation, I'm pressing F5 from the chart.
    I want to know whether the strategy waits for the onbarupdate to complete before terminating, or Terminate immediately (for example, when a statement inside onbarupdate is run, it is terminated immediately).

    Because I have txt file output as shared variable between charts, I am worried that there will be problems with F5. For example, if a part of file is output, I don't know what will happen.

    Thankyou
    Last edited by jetheil2; 02-20-2022, 06:58 PM.

    #2
    Hello jetheil2,

    Once OnBarUpdate() is run, there is no abort in this. Disabling a script will prevent the next OnBarUpdate cycle and begin cloning and disposing once all current market / order events have completed.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello ChelseaB,

      The post below:

      https://ninjatrader.com/support/foru...ate#post451254
      https://ninjatrader.com/support/foru...s-on-f5-repeat


      It seems to say that OnBarUpdate() terminates immediately when F5 is pressed.
      He said that sometimes, I haven't tested in detail
      But this was many years ago.


      Thank you for your help.
      Last edited by jetheil2; 02-21-2022, 07:29 AM.

      Comment


        #4
        Hello jetheil2,

        The first post is describing how the script will start disposing resources while OnBarUpdate() is still running as the State of script becomes Terminated. It does not abort that pass of OnBarUpdate() which does finish running.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello ChelseaB,

          What I should consider is:

          1.
          Disposing resources when Terminated (when I press F5 on the chart), xxx1.dispose, xxx2 = null will happen
          ( If I write "xxx1.dispose, xxx2 = null" in (State == State.Terminated){ xxx1.dispose , xxx2= null } , )
          While OnBarUpdate() is executing the last time, A null pointer exception will occur.

          2.
          But if I don't write anything in State == State.Terminated{ }, will it still generate a null pointer exception (or the resource is released early)?


          Is that right?

          Thankyou
          Last edited by jetheil2; 02-21-2022, 10:36 AM.

          Comment


            #6
            Hello jetheil2,

            A null pointer exception would only occur if you try and use a variable that has a null value. Checking the variable is not null first can avoid that error.

            if (myObject != null)
            {
            // do something with myObject)
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello ChelseaB,
              thank you for your help.

              I was thinking that when F5 is pressed, OnStateChange will be called and will go to on(State == State.Terminated), which may Disposing resources early and empty some variables,
              Is what I said in item 2 of the above post correct?

              Comment


                #8
                Hello jetheil2,

                Only NinjaTrader resources would be disposed.

                The script would run OnStateChange() as the next instruction from the script thread.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello ChelseaB,

                  "Only NinjaTrader resources would be disposed."
                  What does NinjaTrader resources refer to?



                  "The script would run OnStateChange() as the next instruction from the script thread."

                  Sorry, I didn't understand. Can you explain briefly? Thank you

                  Comment


                    #10
                    Hello jetheil2,

                    As an example, if you have your own custom class and an object instantiated that implements the Dispose method, that object would not be disposed automatically unless the entire script instance is disposed which would be after State.Terminated has finished.

                    An example class I can think of would be StreamReader / StreamWriter.


                    NinjaTrader resources would be things like orders, the renderTarget, market data, etc. Things that are part strategy class that are not held until the end of State.Terminated that you would not explicitly call Dispose on.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello jetheil2,

                      As an example, if you have your own custom class and an object instantiated that implements the Dispose method, that object would not be disposed automatically unless the entire script instance is disposed which would be after State.Terminated has finished.

                      An example class I can think of would be StreamReader / StreamWriter.


                      NinjaTrader resources would be things like orders, the renderTarget, market data, etc. Things that are part strategy class that are not held until the end of State.Terminated that you would not explicitly call Dispose on.

                      Thank you for the explanation.
                      I would use File.XXX, try to avoid StreamReader/StreamWriter, they need to be dispose manually.


                      ".....object would not be disposed automatically unless the entire script instance is disposed which would be after State. Terminated has finished."

                      if OnBarUpdate() is running, the entire script instance will not disposed, although State.Terminated may have finished.
                      am i right?

                      Thank you

                      Comment


                        #12
                        Hello jetheil2,

                        The strategy would not reach State.Terminated until after the in-progress pass of OnBarUpdate().

                        The script instance will not be disposed until after OnStateChange() has completed it's pass of State.Terminated. (or in some cases, will not be disposed of at all)
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello ChelseaB,
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello jetheil2,

                          The strategy would not reach State.Terminated until after the in-progress pass of OnBarUpdate().
                          If the OnBarUpdate() is still running and i press F5 in the chart, won't the strategy reach State.Terminated?

                          Comment


                            #14
                            Hello jetheil2,

                            It will and OnStateChange() will update with the Terminated state once any in progress methods finish their pass and the script is ready for teardown.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by llanqui, Today, 03:53 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post llanqui
                            by llanqui
                             
                            Started by burtoninlondon, Today, 12:38 AM
                            0 responses
                            10 views
                            0 likes
                            Last Post burtoninlondon  
                            Started by AaronKoRn, Yesterday, 09:49 PM
                            0 responses
                            15 views
                            0 likes
                            Last Post AaronKoRn  
                            Started by carnitron, Yesterday, 08:42 PM
                            0 responses
                            11 views
                            0 likes
                            Last Post carnitron  
                            Started by strategist007, Yesterday, 07:51 PM
                            0 responses
                            14 views
                            0 likes
                            Last Post strategist007  
                            Working...
                            X