OnStateChange()
OnStateChange()
Definition
An event driven method which is called whenever the script enters a new State. The OnStateChange() method can be used to configure script properties, create one-time behavior when going from historical to real-time, as well as manage clean up resources on termination.
Notes:
- Viewing any UI element which lists NinjaScript classes (such as the Indicators or Strategies window, a chart’s Chart Style dropdown menu, etc.) will initialize all classes of that Type when it is opened, which causes each script to enter State.SetDefaults, even if it is not actively configured or running in any window. It is important to keep this in mind when adding logic within State.SetDefaults in OnStateChange(), as this logic will be processed each time the script is initialized. For example, opening the Indicators window will trigger State.SetDefaults for all indicators in the system, and closing the Indicators window will trigger State.Terminated for all Indicators. In addition, disconnecting or connecting to a data provider can cause State transitions for any currently active scripts. Further discussion of this aspect of the state change model can be found via Understanding the lifecycle of your NinjaScript objects.
- When an indicator is configured on a chart while a Compile is taking place in the NinjaScript Editor, it can appear that the script passes through State.Terminated. However, this is the result of a copy of the script being initialized at compile-time, NOT the result of the indicator on the chart being disabled and re-initialized.
Related Methods and Properties
Method Return Value
This method does not return a value.
Syntax
See example below. The NinjaScript wizards automatically generate the method syntax for you.
Possible states are:
Active States vs Data Processing States
After State.Configure, each type of NinjaScript type has its own state management system which can be classified under two categories:
-
Active state: State.Active
-
Data Processing states: State.DataLoaded, State.Historical, State.Transition, State.Realtime
The table below lists each NinjaScript type and its designed state management system:
Tips:
- Resources created in State.Configure and not disposed of immediately will be kept and utilized if the NinjaScript object resides in grids (e.g. Strategy tab on Control Center), even if it is not enabled. Try to create resources in State.Historical or State.DataLoaded instead, if possible.
- State.Historical is called multiple times when running a backtest optimization on a strategy and the property “IsInstantiatedOnEachOptimizationIteration” is true (default behavior).
- Embedded scripts within a calling parent script should not use a different Calculate property since it is already utilizing the Calculate property of the parent script (i.e. the strategy your indicator is called from).
- Since the parent NinjaScript therefore governs this setting, it should be set to the highest needed setting satisfying all its children.
- When instantiating indicators in a Multi-Series script in OnStateChange, the input any hosted indicator is running on should be explicitly stated (since a specific BarsInProgress is not guaranteed)

