Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot execute code in State == State.Terminated

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

    Cannot execute code in State == State.Terminated

    In NT7 it was possible to execute a piece of code when a strategy was disabled. But in NT8 it doesn't seem to be possible.

    All code in State == State.Terminated is not executed.

    Is it possible to execute any code when disabling a strategy?

    #2
    Hello guillembm,

    Thank you for the post.

    In NT7 the OnTermination would be the equivalent of NT8's OnStateChange -> State.Terminated.
    From the code breaking changes guide:
    Implementation General OnTermination() OnStateChange() Reimplemented as State.Terminated Details
    https://ninjatrader.com/support/help...ng_changes.htm


    What code are you trying to execute?
    JesseNinjaTrader Customer Service

    Comment


      #3
      The error came when trying to access Position.AveragePrice when OnStateChange -> State.Terminated.

      Is it an error? Can I access Position.AveragePrice from State.Terminated?

      Comment


        #4
        Hello guillembm,

        What was the error?

        State.Terminated is called more than once through a scripts lifecycle, depending on the error you may need to add logic to make sure the script was running before accessing that. Alternatively you can create your own class level double variable and assign the value for the average price from OnBarUpdate then use the variable in State.Termianted.

        JesseNinjaTrader Customer Service

        Comment


          #5
          What you propose will be the best option. Accessing Position.AveragePrice gives an error in State.Termianted

          Comment


            #6
            Hello guillembm,

            You have not provided the error so I can't tell you what to do next, we would need to know what error you are seeing to know what to do.

            You can possibly use either of the items I had previously recommended, that depends on what the problem is that you are seeing.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Originally posted by guillembm View Post
              What you propose will be the best option. Accessing Position.AveragePrice gives an error in State.Termianted
              You might need to track it in OnBarUpdate and assign to local variable, which should be available in Terminated state

              Comment


                #8
                The error appears when executing this code:

                if (Position.AveragePrice != 0)
                {
                //Do this

                }

                Comment


                  #9
                  Hello guillembm,

                  What is the error that you see? Does the error have text or a message? If so please provide that or a screenshot. We would also need to know when you see the error, without those kind of details we can't really comment on what the solution may be.

                  If you are trying to use strategy based properties inside State.Terminated you would also need to make sure that the strategy was actually running before using that code, the strategy terminated state gets called for many reasons. I mentioned this in post 4 and you have two options on how to know the script was running.

                  You would need to either add logic such as setting a user variable to true from State.DataLoaded or use your own double variable which gets updated from OnBarUpdate. If you use the Position object directly from Terminated you need to make sure the script had run for that use case to be valid. Terminated is called when you open menus so that would be a case where the strategy had not run and then that logic is called at an invalid time.



                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Segwin, 05-07-2018, 02:15 PM
                  14 responses
                  1,789 views
                  0 likes
                  Last Post aligator  
                  Started by Jimmyk, 01-26-2018, 05:19 AM
                  6 responses
                  837 views
                  0 likes
                  Last Post emuns
                  by emuns
                   
                  Started by jxs_xrj, 01-12-2020, 09:49 AM
                  6 responses
                  3,293 views
                  1 like
                  Last Post jgualdronc  
                  Started by Touch-Ups, Today, 10:36 AM
                  0 responses
                  13 views
                  0 likes
                  Last Post Touch-Ups  
                  Started by geddyisodin, 04-25-2024, 05:20 AM
                  11 responses
                  63 views
                  0 likes
                  Last Post halgo_boulder  
                  Working...
                  X