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

Strategy Lifecycle

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

    Strategy Lifecycle

    I am currently confused about the Lifecycle and State transitions of a Strategy, so
    I write up a simple Strategy with this output. For an ordinary guy like me, presuming
    these prints are sequential as they are, the first 3 lines are a little strange. The SetDefault
    is called in the 49806078 instance, apparently before its constructor is called which,
    to most programmers would seem a bit odd; but maybe Reflection is doing things, who knows?

    OrderEntryStrategy SetDefaults.49806078
    OrderEntryStrategy SetDefaults END.
    Constructor count: 1 for 49806078
    OrderEntryStrategy Terminated.49806078
    OrderEntryStrategy Terminated END.49806078
    OrderEntryStrategy SetDefaults.14672690
    OrderEntryStrategy SetDefaults END.
    Constructor count: 2 for 14672690
    OrderEntryStrategy SetDefaults.28031203
    OrderEntryStrategy SetDefaults END.
    Constructor count: 3 for 28031203
    OrderEntryStrategy SetDefaults.13155542
    OrderEntryStrategy SetDefaults END.
    Constructor count: 4 for 13155542
    OrderEntryStrategy Terminated.13155542
    OrderEntryStrategy Terminated END.13155542
    OrderEntryStrategy Terminated.14672690
    OrderEntryStrategy Terminated END.14672690
    OrderEntryStrategy SetDefaults.62063080
    OrderEntryStrategy SetDefaults END.
    Constructor count: 5 for 62063080
    OrderEntryStrategy Configure.62063080
    OrderEntryStrategy Configure END.62063080
    OrderEntryStrategy Terminated.62063080
    OrderEntryStrategy Terminated END.62063080
    OrderEntryStrategy Terminated.28031203
    OrderEntryStrategy Terminated END.28031203

    So, when this Strategy was added to a chart, that was the
    output. And presumably the strategy was activated.

    I'll edit this to post the code; and may be able to attach a zipped
    project, but here's the code in text: [will edit]

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;

    namespace NinjaTrader.NinjaScript.Strategies {
    public class MyStrategy : Strategy {

    static int constructorCallCount = 0; // class static

    static void myPrint(Strategy strategy, string msg) {
    strategy.Print(msg);
    }

    // constructor
    public MyStrategy() {
    ++constructorCallCount;
    myPrint(this, "Constructor count: " + constructorCallCount + " for " + GetHashCode());
    }

    protected override void OnStateChange() {
    try {
    if (State == State.SetDefaults) {
    myPrint(this, "OrderEntryStrategy SetDefaults." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy SetDefaults END.");
    } else if (State == State.Configure) {
    myPrint(this, "OrderEntryStrategy Configure." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy Configure END." + this.GetHashCode());
    } else if (State == State.Active) {
    myPrint(this, "OrderEntryStrategy Active." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy Active END." + this.GetHashCode());
    } else if (State == State.DataLoaded) {
    myPrint(this, "OrderEntryStrategy DataLoaded." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy DataLoaded END." + this.GetHashCode());
    } else if (State == State.Historical) {
    myPrint(this, "OrderEntryStrategy Historical." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy Historical END." + this.GetHashCode());
    } else if (State == State.Transition) {
    myPrint(this, "OrderEntryStrategy Transition." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy Transition END." + this.GetHashCode());
    } else if (State == State.Realtime) {
    myPrint(this, "OrderEntryStrategy Realtime." + this.GetHashCode());
    myPrint(this, "OrderEntryStrategy Realtime END.");
    } else if (State == State.Terminated) {
    // should release ALL resources
    myPrint(this, "OrderEntryStrategy Terminated." + this.GetHashCode());
    myPrint(this,"OrderEntryStrategy Terminated END." + this.GetHashCode());
    } else {
    myPrint(this, "### Unhandled State: " + State);
    }
    } // end try
    catch (Exception ex) {
    myPrint(this, "Exception in OnStateChange state: " + State.ToString());
    }
    }
    }
    }

    Sorry don't know how to prettyprint in the forum.

    If we track the Object HashCodes as individual "clones", and also take note of the
    number of times the constructor is invoked, this is a bit confusing.

    It's possible that actions taken in States might influence the lifecycle evolution, of course,
    in particular in the SetDefaults state.

    Is it normal to have this lifecycle evolution on activation?
    Last edited by Hyper; 09-02-2020, 04:41 PM. Reason: additional observations and typo

    #2
    Hello Hyper,

    One thing to keep in mind is that NinjaTrader will construct many instances of the script for getting the defaults, such as when the Strategies window is opened, when the script is selected, when an instance is added, and when the script is enabled.

    Once a script instance hits State.Configured, that instance is the running enabled instance.

    Below is a link to the help guide on the lifecycle of a NinjaScript.
    https://ninjatrader.com/support/help...fecycle_of.htm

    As well as a link to a forum post that discusses.
    https://ninjatrader.com/support/foru...288#post726288
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks. That sounds logical, so the object that hits the Configure
      state is the 62063080, so that's our object !! But then it's immediately
      given the Terminated State. So how does that fit in? Thanks !

      OrderEntryStrategy SetDefaults.62063080
      OrderEntryStrategy SetDefaults END.
      Constructor count: 5 for 62063080
      OrderEntryStrategy Configure.62063080
      OrderEntryStrategy Configure END.62063080
      OrderEntryStrategy Terminated.62063080
      OrderEntryStrategy Terminated END.62063080

      I thought Terminated was the state when you were done, and
      should be releasing all resources...?

      Last edited by Hyper; 09-02-2020, 04:48 PM. Reason: question

      Comment


        #4
        In a real complex Strategy implementation, I've identified the "clone instance" which proceeds through the entire sequence of states.
        I think this is the one, so my current plan is to allocate resources in the RealTime state (and NinjaTrader does recognize that it is
        requesting that runtime status) for that RealTime instance; and then perhaps just
        ignore the Terminate state (for now; but let all the other clones Terminate) and hold onto resources. I can give more details later.

        Just FYI, I don't think that clone instance is the
        one that first received the Configure state; it was a later clone instance which I believe is my candidate for the full initialization...

        Comment


          #5
          Hello Hyper,

          That should be the case.

          That instance is still enabled and didn't hit an error and disable?

          Do you have prints in OnBarUpdate() showing that instance is still processing data in real-time?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            It's a fluid situation, but once I get clarity; I'll report it as best I can sometime today.
            Thanks for your support; even though I realize this is outside your support envelope...
            I'll be able to confirm its data processing is ongoing, fingers crossed.

            Comment


              #7
              OK, sorry for the delay; I was down a rabbit hole allocating
              a Winforms user interface as well; but now I understand what
              needs to be done.

              So it is definitely NOT the case that the Configure state identifies
              the "clone" which will be allowed to survive.

              The State transition sequence needs to progress beyond Configure, and
              then you've got the one in which you may allocate your significant resources,
              whether that is the DataLoaded state, or any later State; in my
              case I just did it in Realtime State.

              And it is also in that instance where you must terminate/release all
              of your resources, later, of course, by having a clone member
              variable flag to remind you.

              Below shows the start of a Strategy on a Chart, up to the point
              where my WinForms Form user interface is allocated and
              functional; and you can see that it is "Clone #7" which is the first
              one to progress to DataLoaded, then Historical, then Transition
              and then Realtime. It's the 7474387 hash code instance.

              The code is developed in Visual Studio Community 2017 and
              an assembly DLL is generated. I didn't mess with any startup
              properties, except to check "enable" for the Strategy being
              started on the Chart.

              The State transition prints are missing some of the hashcodes,
              but they are just the same as the hashcode immediately above.
              It is these hashcodes which I'm using to identify the various
              clones.

              Hope this helps clear up the confusion that I'm responsible for creating !!! lol

              OrderEntryStrategy CTOR count: 1 hash:34561818
              OrderEntryStrategy Terminated.34561818
              OrderEntryStrategy Terminated END.34561818
              OrderEntryStrategy SetDefaults.51613980
              OrderEntryStrategy SetDefaults END.
              OrderEntryStrategy CTOR count: 2 hash:51613980
              OrderEntryStrategy SetDefaults.37314260
              OrderEntryStrategy SetDefaults END.
              OrderEntryStrategy CTOR count: 3 hash:37314260
              OrderEntryStrategy SetDefaults.54540077
              OrderEntryStrategy SetDefaults END.
              OrderEntryStrategy CTOR count: 4 hash:54540077
              OrderEntryStrategy Terminated.54540077
              OrderEntryStrategy Terminated END.54540077
              OrderEntryStrategy Terminated.51613980
              OrderEntryStrategy Terminated END.51613980
              OrderEntryStrategy SetDefaults.51096172
              OrderEntryStrategy SetDefaults END.
              OrderEntryStrategy CTOR count: 5 hash:51096172
              OrderEntryStrategy SetDefaults.63497805
              OrderEntryStrategy SetDefaults END.
              OrderEntryStrategy CTOR count: 6 hash:63497805
              OrderEntryStrategy Configure.37314260
              OrderEntryStrategy Configure END.37314260
              OrderEntryStrategy Configure.63497805
              OrderEntryStrategy Configure END.63497805
              OrderEntryStrategy SetDefaults.7474387
              OrderEntryStrategy SetDefaults END.
              OrderEntryStrategy CTOR count: 7 hash:7474387
              OrderEntryStrategy Terminated.63497805
              OrderEntryStrategy Terminated END.63497805
              OrderEntryStrategy Configure.7474387
              OrderEntryStrategy Configure END.7474387
              OrderEntryStrategy Terminated.37314260
              OrderEntryStrategy Terminated END.37314260
              OrderEntryStrategy Terminated.51096172
              OrderEntryStrategy Terminated END.51096172
              OrderEntryStrategy DataLoaded.7474387
              OrderEntryStrategy DataLoaded END.7474387
              OrderEntryStrategy Historical.7474387
              OrderEntryStrategy Historical END.7474387
              OrderEntryStrategy Transition.7474387
              OrderEntryStrategy Transition END.7474387
              OrderEntryStrategy Realtime.7474387
              Creating form
              done Created form
              .....running just fine.....

              hyperscalper

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Brevo, Today, 01:45 AM
              0 responses
              3 views
              0 likes
              Last Post Brevo
              by Brevo
               
              Started by aussugardefender, Today, 01:07 AM
              0 responses
              3 views
              0 likes
              Last Post aussugardefender  
              Started by pvincent, 06-23-2022, 12:53 PM
              14 responses
              239 views
              0 likes
              Last Post Nyman
              by Nyman
               
              Started by TraderG23, 12-08-2023, 07:56 AM
              9 responses
              384 views
              1 like
              Last Post Gavini
              by Gavini
               
              Started by oviejo, Today, 12:28 AM
              0 responses
              6 views
              0 likes
              Last Post oviejo
              by oviejo
               
              Working...
              X