Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Converting to NT8: State.DataLoaded vs OnBarUpdate

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Converting to NT8: State.DataLoaded vs OnBarUpdate

    I'm converting some of my custom indicators from NT7 to NT8. I appreciate that there's been a significant change in the way NT8 scripts are coded. I'm going through the online guides, but I have one nagging question: I'm still having trouble understanding when code should be placed in the (State == State.DataLoaded) section of OnStateChange(), and when it should be put in OnBarUpdate().

    I *get* that any DataSeries that I use need to be converted into Series<double> (assuming I'm working with floating-point values, obviously). And that these need to be declared in the State.DataLoaded code block. But even after examining other code samples, I need some help understanding when calculations should be done in State.DataLoaded, and when they should be placed in OnBarUpdate.


    #2
    Hello pbailey19,

    The DataLoaded state would generally only be used for one time actions that need to be accomplished when the data is sure to be loaded but before you start processing bars.

    The help guide will note any specific times you need to use this with NinjaScript properties or logic. For your own code, you can use this logically how you want but calculations should generally always be done in OnBarUpdate when you are actually processing the bar data. OnDataLoaded you are not processing bar data where OnBarUpdate you are is the main difference here.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks, Jesse. I think I appreciate the obvious "easy" placements: DataSeries declarations go in State.DataLoaded. One-time calculations may go in State.DataLoaded. I need a little help understanding the declaration of new Series (and again, I may be phrasing this incorrectly because I'm still familiarizing myself with NT8).

      Take a stock NT8 indicator ... say, Stochastics. In the State.DataLoaded section, you see several new Series<double> being instantiated. But you also see some new Series being defined using other functions, like MIN(), MAX(), or SMA(). I'm familiar with functional programming, so I assume that's what we are seeing here: new series being defined, using functions. And we won't need to re-calculate new values in OnBarUpdate, because the definition has remained unchanged: once we define smaK as SMA(K, PeriodD), then we won't need to explicitly calculate and add a new element to this series every time another bar closes in OnBarUpdate.

      If I'm understanding this correctly: what's the decision criteria you need to use, when deciding whether you can just define a new Series in Status.DataLoaded, and when you need to calculate each element individually in OnBarUpdate?

      Comment


        #4
        Hello pbailey19,

        Thank you for the reply.

        In this case, I believe you are generalizing the Series too far, the samples you are referring to are specific use cases of Series<T>.

        The stochastics demonstrates a few concepts which include setting indicators to a variable which relates to our demonstration in the AddDataSeries page: https://ninjatrader.com/support/help...=adddataseries

        The series<T> definition "den" in the Stochastics is in DataLoaded because that is where a "new" series needs to be defined specifically. Note that a private variable is used and this is a brand new empty series. This is also different than how a Plot is defined which is also just a Series<double>.

        Another use case for Series<T> involves indicators. You are correct you are seeing a series being provided from a method which is the indicators name MAX for example. There is internal caching happening when you call an indicator if no properties change an existing instance is returned otherwise a new instance to the indicator is created.

        The reason why the stochastics defines the indicators in DataLoaded is for ease of use, the variable names are less messy. it also does not need to later update the indicator with new values.

        You can call indicators from either DataLoaded or from OnBarUpdate. In the stochastics the variable will never change after it has been set so there is no need to keep calling the indicator method, you can just use an instance. In a case where there is a variable input, you would want to instead call the indicator from OnBarUpdate so a new instance can be issued in case of a change in properties. You can still use the same code from OnBarUpdate or set the variable to the indicator from OnBarUpdate, it just depends on the use case.


        One-time calculations may go in State.DataLoaded
        Again you want to move calculations to OnBarUpdate, anywhere you reference the data being used. If you just mean simple math calculations to set variables that do not relate to the actual series of data, sure that is fine to do here or SetDefaults/Configure. DataLoaded is good for one time actions such as the example you provided with the new Series or we can use a button event handler for the sake of this example. An event handler subscription does not use bar data but needs to happen one time before the script starts. DataLoaded would be a good choice to ensure the script is actually ready to start.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kaywai, Today, 06:26 AM
        1 response
        6 views
        0 likes
        Last Post kaywai
        by kaywai
         
        Started by ct, 05-07-2023, 12:31 PM
        6 responses
        205 views
        0 likes
        Last Post wisconsinpat  
        Started by kevinenergy, 02-17-2023, 12:42 PM
        118 responses
        2,780 views
        1 like
        Last Post kevinenergy  
        Started by briansaul, Today, 05:31 AM
        0 responses
        10 views
        0 likes
        Last Post briansaul  
        Started by traderqz, Yesterday, 12:06 AM
        11 responses
        28 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X