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

OnStateChange() is called from "inactive" Strategy

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

    OnStateChange() is called from "inactive" Strategy

    I have several independent Strategies defined in separate code files. They are unrelated and all Strategies have their own OnStateChange() defined. Each OnStateChange() may do different things in their individual Strategies. Now I noticed OnStateChange() in all Strategies will be called and executed when I start Strategies before enabling individual Strategy.
    Question 1: If this behavior is expected, how do I avoid this?
    Question 2: If a private variable is defined and used OnStateChange() in one Strategy class, this variable will be changed by all other Strategies as they all call OnStateChange()?

    Thanks.

    #2
    Hello localappleseed,

    Thanks for your post.

    Private variables are not accessible in other classes. Each strategy is its own class and each strategy created is a new instance of that class. If you have "StrategyA" utilize a private variable, and create 2 instances of the strategy, these properties will be unique.

    OnStateChange does occur multiple times throughout a scripts life. It is suggested to use State.DataLoaded for operations that should be performed only once when the script starts up. State.SetDefaults and State.Configure will be seen during the NinjaScritp LifeCycle when the script is listed in the Strategies dialog and when properties are listed in the property grid.

    NinjaScript Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm

    OnStateChange - https://ninjatrader.com/support/help...tatechange.htm

    Let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by WHICKED, Today, 12:56 PM
    2 responses
    15 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Felix Reichert, Today, 02:12 PM
    0 responses
    1 view
    0 likes
    Last Post Felix Reichert  
    Started by Tim-c, Today, 02:10 PM
    0 responses
    1 view
    0 likes
    Last Post Tim-c
    by Tim-c
     
    Started by cre8able, Today, 01:16 PM
    2 responses
    9 views
    0 likes
    Last Post cre8able  
    Started by chbruno, 04-24-2024, 04:10 PM
    3 responses
    49 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X