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

Order of State Changes in Strategy

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

    Order of State Changes in Strategy

    I've been reviewing this page on State changes: https://ninjatrader.com/support/helpGuides/nt8/en-us/

    And trying to understand the right places to initialize custom variables for historical backtests. I'm running into some cases where my variables seem to lose values. I'm sure this is just because I don't understand the order of state changes and where I need to set things.

    So I added this code to OnStateChange in a strategy:

    Code:
    Print(DateTime.Now + ": Current State is State." + State);
    Then I execute the strategy on a chart to get the following output:

    Code:
    8/24/2018 11:55:39 PM: Current State is State.SetDefaults
    8/24/2018 11:55:39 PM: Current State is State.SetDefaults
    8/24/2018 11:55:39 PM: Current State is State.Terminated
    Disabling NinjaScript strategy 'Barz - BuyDay SellDay/146636207'
    8/24/2018 11:55:39 PM: Current State is State.Configure
    8/24/2018 11:55:39 PM: Current State is State.Terminated
    8/24/2018 11:55:39 PM: Current State is State.DataLoaded
    8/24/2018 11:55:39 PM: Current State is State.Historical
    8/24/2018 11:55:39 PM: Current State is State.Transition
    Enabling NinjaScript strategy 'Barz - BuyDay SellDay/146636207' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
     8/24/2018 11:55:39 PM: Current State is State.Realtime
    I'm really confused about what's happening here. I think that the existing strategy is shutting down and a new one is being created, but the order of things makes no sense to me.

    Questions:

    * Why does the Terminated state occur after Configure and before DataLoaded? How should I understand the Terminated state? Is it when the object is shutting down or when the strategy is shutting down? And if the latter then why does it run before DataLoaded?

    * In which state should I initialize custom variables for backtests so that they get reset with each new backtest?

    #2
    OK. I'll attempt an answer my own question after some further research.

    I added a unique instance ID for every instance of the strategy class so that we can see which states are associated with the same instance of each class. We get the following:

    Code:
    8/25/2018 2:00:23 PM State.SetDefaults 597e85c883c04632892c1b167023b048
    8/25/2018 2:00:23 PM State.SetDefaults a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Terminated 517fcf066e664294a31b98788316e9dc
    Disabling NinjaScript strategy 'Barz - BuyDay SellDay/146636207'
    8/25/2018 2:00:23 PM State.Configure a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Terminated 597e85c883c04632892c1b167023b048
    8/25/2018 2:00:23 PM State.DataLoaded a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Historical a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Transition a80c17195209414aaed3951db56618fb
    Enabling NinjaScript strategy 'Barz - BuyDay SellDay/146636207' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
    8/25/2018 2:00:23 PM State.Realtime a80c17195209414aaed3951db56618fb
    As you can see we still have the same tangle of states, but my concern that State.Terminated was coming between State.Configure and State.DataLoaded is a byproduct of multiple instances, not just one.

    If I clean up the output a bit by grouping instances we get this:

    Code:
    8/25/2018 2:00:23 PM State.SetDefaults 597e85c883c04632892c1b167023b048
    8/25/2018 2:00:23 PM State.Terminated 597e85c883c04632892c1b167023b048
    
    8/25/2018 2:00:23 PM State.Terminated 517fcf066e664294a31b98788316e9dc
    
    8/25/2018 2:00:23 PM State.SetDefaults a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Configure a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.DataLoaded a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Historical a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Transition a80c17195209414aaed3951db56618fb
    8/25/2018 2:00:23 PM State.Realtime a80c17195209414aaed3951db56618fb
    This is a little less confusing.

    One of the first two State.Terminated states is the previous instance of the strategy shutting down. I'm not sure what the other is for, but I'll assume it's used internally to get default values for the UI or something.

    What's evident though is that it's the last instance I care about ("a80c..."). And its order of states is exactly what I would expect.

    But I'm still left wondering why my custom variables aren't retaining their values.

    A few more print statements and the issue revealed itself.

    I was assigning a default value for my custom variable in the constructor. And then assigning the desired value in State.SetDefaults.

    Code:
    public BarzBuyDaySellDay: base()
    {
       this.MyVariable = 0;
    }
    
    protected override void OnStateChange()
    {
       Print(DateTime.Now + " State." + State + " " + _InstanceId);
       if (State == State.SetDefaults)
       {
          this.MyVariable = 25;
       }
    }
    What's clear now is that custom variables should NOT be set in a constructor.

    The base Strategy constructor raises the SetDefaults state and only after that does the child constructor execute.

    So, instead of the desired value of 25, the actual value was 0 because my constructor code ran _after_ the SetDefaults state.

    The real problem here is that I was doing something in the constructor. I'll be sure not to do that again.

    Any of the other states work fine for setting the value.

    This has been a good exercise in understand how states work and I hope it helps
    someone else.

    NOTE: If someone is trying to replicate this I'm running the strategy on a chart. I'm then re-running using F5.

    Comment


      #3
      Hello BarzTrading,

      Thank you for your note.

      Glad you resolved the issue.

      Please let us know if you need further assistance.
      Alan P.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      6 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Started by alifarahani, Today, 09:40 AM
      6 responses
      41 views
      0 likes
      Last Post alifarahani  
      Working...
      X