NinjaScript diagnostics

3 min read

Why NinjaTrader calls OnStateChange() when you open Indicators

The Output window can print State.Terminated while your indicator keeps plotting. The chart and the Indicators window each have their own copy of the script.

Illustration of two translucent button outlines beside one wired orange pushbutton, representing display copies and a configured indicator.

Quick answer

NinjaTrader calls OnStateChange() for each indicator instance. Opening the Indicators window creates temporary instances so it can display their defaults. Closing the window can send those copies to State.Terminated while the chart’s indicator keeps running. Each instance should release only the resources it acquired.

Follow one indicator from the list to the chart

To build the Available list, NinjaTrader creates a copy of each installed indicator and reads its name in State.SetDefaults. Adding an indicator to the Configured list creates another copy for editing its properties. Clicking Apply or OK creates the instance that processes data on the chart.

Compiling in the NinjaScript Editor can also create a temporary copy of an indicator already on a chart. A termination message that contains just the indicator’s name leaves the identity of the copy open.

Choose the state for each setup step

  • State.SetDefaults runs even when someone is just browsing. Set visible defaults here, such as the indicator name and its lookback period. Opening a file here would open it for the list copy, too.
  • Use State.Configure to add an extra data series. Wait until State.DataLoaded to read BarsPeriod or TradingHours, build a custom Series, or initialize a child indicator that depends on loaded data.
  • State.Historical marks the start of historical processing. State.Transition comes after history finishes and before live processing begins. Use State.Realtime for work that should start when the indicator begins processing real-time data.

Identify which copy wrote to the Output window

While diagnosing the message, import System.Runtime.CompilerServices and add these two lines inside your existing OnStateChange() method. They print the script type, an object hash and the state together.

Compare the tag on State.Terminated with the tag on State.Realtime. Different tags establish that different objects wrote the two lines. Matching tags alone are inconclusive because two objects can share a hash.

int tag = RuntimeHelpers.GetHashCode(this);
Print(GetType().Name + " #" + tag + " " + State);

Remove a chart button only from the instance that added it

Suppose an indicator adds a toolbar button in State.Historical. Keep the button reference on that instance and set a private flag once it has attached the button and its click handler. In State.Terminated, check the flag before detaching the handler and removing the button. Clear the flag after cleanup.

Reopening the settings can clone a configured indicator back into the dialog. Even a UI copy can have a ChartControl reference, so checking that reference alone can let the wrong copy remove the live button. Make changes to chart controls on their UI thread, using the chart’s dispatcher when the callback runs on another thread.

Official sources

Platform monitoring

Get notified when NinjaTrader goes offline

Ninja Watchdog monitors NinjaTrader on your Windows desktop or VPS and can email you when it detects a platform interruption.

Try Ninja Watchdog