• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

The Indicator Club

  • Indicators
    • All Indicators
    • Free Indicators
    • Join the Club!
    • Indicator TImeline
  • Blog
    • All
    • NinjaScript Tutorials
    • Trade Ideas
    • Free Strategies
    • Favorite Posts
  • FAQ
  • My Account
    • Activate License
    • API Keys
    • API Downloads
  • Contact
  • Cart

June 28, 2017 by The Indicator Club Leave a Comment

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.

Filed Under: Tutorial Tagged With: Learn, NinjaScript

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Top Shelf Trader October 6, 2017
  • Use Any Indicator with Our Frameworks: Universal Inputs September 21, 2017
  • NinjaTrader Training: Part 10: Coloring Plots and Backgrounds September 1, 2017
  • NinjaTrader Training: Part 9 – AddPlot and Plot Variables August 31, 2017
  • NinjaTrader Training: Part 8 – Referencing Indicators August 30, 2017

Post Tags

Advance Internals Automated Backtest Day Trading Detrend Forex Trading Free Futures Trading Learn Moving Average MTF NinjaScript NinjaTrader 7 NInjaTrader 8 Oscillator Pivot Points RSI Stock Trading Strategy Vix Trend Volume

Footer

WHO WE ARE

The Indicator Club is made up of normal traders and professional software developers who want to share their indicators for a fraction of the cost it would take to code just one!

The Indicator Club

Purchase & Support

To get access to all of our current and future indicators and strategies, check out our Complete Package.

If you need support please email [email protected], or head over to our contact page.

New Releases

  • NinjaTrader 8 Osc Universal Input Osc Universal Input $497.00
  • NinjaTrader 8 MA Universal Input MA Universal Input $497.00
  • NinjaTrader 8 Bar Orders Bar Orders $297.00
  • NinjaTrader 8 Tick Offset Tick Offset $0.00

Recent Posts

  • Top Shelf Trader
  • Use Any Indicator with Our Frameworks: Universal Inputs
  • NinjaTrader Training: Part 10: Coloring Plots and Backgrounds
  • NinjaTrader Training: Part 9 – AddPlot and Plot Variables

Copyright ©2020 | All Rights Reserved | By Using This Site you Agree to our Terms and Conditions, Risk Disclosures, and Software Agreement

NinjaTrader Disclosure: NinjaTrader® is a registered trademark of NinjaTrader Group, LLC. No NinjaTrader company has any affiliation with the owner, developer, or provider of the products or services described herein, or any interest, ownership or otherwise, in any such product or service, or endorses, recommends or approves any such product or service.

RISK DISCLOSURE: Futures and forex trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or life style. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results.

HYPOTHETICAL PERFORMANCE DISCLAIMER: HYPOTHETICAL PERFORMANCE RESULTS HAVE MANY INHERENT LIMITATIONS, SOME OF WHICH ARE DESCRIBED BELOW. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN; IN FACT, THERE ARE FREQUENTLY SHARP DIFFERENCES BETWEEN HYPOTHETICAL PERFORMANCE RESULTS AND THE ACTUAL RESULTS SUBSEQUENTLY ACHIEVED BY ANY PARTICULAR TRADING PROGRAM. ONE OF THE LIMITATIONS OF HYPOTHETICAL PERFORMANCE RESULTS IS THAT THEY ARE GENERALLY PREPARED WITH THE BENEFIT OF HINDSIGHT. IN ADDITION, HYPOTHETICAL TRADING DOES NOT INVOLVE FINANCIAL RISK, AND NO HYPOTHETICAL TRADING RECORD CAN COMPLETELY ACCOUNT FOR THE IMPACT OF FINANCIAL RISK OF ACTUAL TRADING. FOR EXAMPLE, THE ABILITY TO WITHSTAND LOSSES OR TO ADHERE TO A PARTICULAR TRADING PROGRAM IN SPITE OF TRADING LOSSES ARE MATERIAL POINTS WHICH CAN ALSO ADVERSELY AFFECT ACTUAL TRADING RESULTS. THERE ARE NUMEROUS OTHER FACTORS RELATED TO THE MARKETS IN GENERAL OR TO THE IMPLEMENTATION OF ANY SPECIFIC TRADING PROGRAM WHICH CANNOT BE FULLY ACCOUNTED FOR IN THE PREPARATION OF HYPOTHETICAL PERFORMANCE RESULTS AND ALL WHICH CAN ADVERSELY AFFECT TRADING RESULTS.