Announcement

Collapse
No announcement yet.

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 Anthony_0709, Today, 01:29 PM
    2 responses
    7 views
    0 likes
    Last Post Anthony_0709  
    Started by kenbun, Today, 12:21 PM
    0 responses
    9 views
    0 likes
    Last Post kenbun
    by kenbun
     
    Started by WNorton, Today, 11:23 AM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Spencer  
    Started by stafe, Today, 10:43 AM
    0 responses
    12 views
    0 likes
    Last Post stafe
    by stafe
     
    Started by UltraNIX, Today, 09:13 AM
    3 responses
    18 views
    0 likes
    Last Post UltraNIX  
    Working...
    X