OnStateChange with NinjaScript
Brief Introduction
We’ve put together a series of posts to help you in your journey to learn how to program from scratch, or edit existing NinjaTrader indicators and strategies. All of our posts are geared toward the non-programmer, so even though we will present a lot of information, for all of you more technical people out there, we will not go into all the technicalities of C#, NinjaTrader, or NinjaScript. It’s also important to note that even though we show you how to do something in NinjaTrader, that is by no way the only way to do it. We will present what has worked best for us and what will be most easily understood by someone learning how to program. Finally, for the more advance users, we are going to leave things out and over simplify certain parts because we are focused on learning what we need to so we can get started on our NinjaScript journey.
The State variable is a an enum value, which means State can only represent a certain (defined) number of variables. We can check when State changes its value in OnStateChange. When State changes and enters a certain State, that is when we will perform certain actions such as setting default values, or creating a new plot or line.
OnStateChange Basics
The best way to learn OnStateChange is to see lots of examples and have a good understanding of what each State means, so let’s start with a description of each State:
- State.SetDefaults: Think of this State as when you want to add an indicator to a chart and more specifically, right when the window opens to add a new indicator to that chart. This is where we will define plots, lines, and set default values for variables.
- State.Configure: Think of this State as when you add a new indicator to a chart and click the OK or APPLY button. This is where we will add additional data-series (MTF) and declare custom resources.
- State.Active: We will not focus on this State since we won’t use this State in developing our indicators. Also, at our current level of understanding (Part 4), this State makes things more confusing.
- State.DataLoaded: Think of this State as just after you click OK or APPLY and before bars are drawn on your chart. PRIOR to entering this this State, NinjaTrader will download, or read data from your database to make sure NinjaTrader has the data it needs to build the bars (or additional MTF bars) for your chart and indicator. AFTER all the data for your chart and indicators are loaded, NinjaTrader will enter this State, where we will perform activities that may need to reference instrument data such as TickSize, or initializing Series<T>.
- State.Historical: Think of this State as being after the DataLoaded State is finished (i.e. NinjaTrader has gathered all the data it needs to build the bars on your chart). During this State NinjaTrader will start processing historical data and drawing bars on your chart (starting from the left most bar and adding 1 bar, at a time, to the right).
- State.Transition: Think of this State as after all the historical data was processed (State.Historical) and BEFORE real-time / live data starts being processed.
- State.Realtime: Think of this State as when NinjaTrader has finished processing historical data and begins to process real-time / live data.
- State.Terminated: Think of this State as when you close a chart by clicking the X button, but right before the chart closes / disappears. Also, think of when you remove an indicator from a chart, but right before the indicator is removed / disappears.
OnStateChange Examples
In the below example I have included each phase of State with comments in each section. This way you can familiarize yourself with how OnStateChange looks in NinjaScript code. When you code your own indicator, you do not want to include each State in OnStateChange; instead, you only want to include the States you need to use.
I would like to take this opportunity to introduce another programming concept, Switch. When you have a variable with defined values (e.g. we always know what values State can take on), instead of creating a very long If Else If statement, we can utilize a Switch Statement (you will also note we can use a default value with Switch, so all conditions don’t necessarily need to be known).
It’s As Simple As That
State and OnStateChange is as simple as that. Now it’s time to checkout Part 5 – Default Options: State.SetDefaults.
Leave a Reply