SetState()

View as Markdown

Definition

This method is used for changing the State of any running NinjaScript object.

Notes:

  • Attempting to set a State earlier than the current State will be ignored.
  • Calling SetState() multiple times will be ignored to prevent the object from erroneously setting states unexpectedly.
  • Setting State to State.Terminated is meant as a way to abort the strategy as it is running. Doing this in a Strategy Analyzer backtest will abort the backtest entirely, and no partial backtest results will be shown.
  • After setting State.Terminated, you should return from the calling method to help ensure subsequent logic is not processed asynchronously to OnStateChange().

Method Return Value

This method does not return a value.

Syntax

SetState(State state)

This method should only be called after the State reaches State.DataLoaded.

Parameters

ParameterDescription
stateThe State to be set

Examples

1protected override void OnBarUpdate()
2{
3 // Terminate strategy at 2PM
4 if (ToTime(Time[0]) == 140000)
5 {
6 SetState(State.Terminated);
7 return;
8 }
9}