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

Perform Initial Calculations on Historical Data Prior to First OnBarUpdate Call?

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

    Perform Initial Calculations on Historical Data Prior to First OnBarUpdate Call?

    I have an indicator which calculates an EMA on a secondary series initialized in the DataLoaded state:

    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(BarsArray[1], 100);
    ....

    Is there any way to access the EMA value from the last historical bar prior to the first call of OnBarUpdate? The reason I ask is I am trying to set the initial state of my indicator visuals based on the last value of the EMA before OnBarUpdate is called. This is a problem because after hours with no data updates the indicator remains in this state of initialization since there are no OnBarUpdate calls. I would like to be able to look through charts and have them be correct based on the last bar of data regardless of incoming data to trigger the OBU.

    Thanks.

    #2
    Hello RandomTask,

    Thanks for your post.

    I am not sure I am following. Unless you are skipping historical processing with if (State == State.Historical) return; the script will process the historical data in each data series through OnBarUpdate before we start processing realtime data. Your question comes off as "I want to process historical values before OnBarUpdate is called." You will already have historically calculated values as the script processes historical data in OnBarUpdate, so this would be redundant.

    It is possible to loop through the historical bars in State.DataLoaded with BarsArray[0].GetValueAt() before the script starts processing historical data, but I am not sure why you would want to do this instead of just letting the indicator calculate the historical data in OnBarUpdate. Note that we could not use BarsAgo indexes or use Series objects here, because this would be before the data is processed bar by bar in OnBarUpdate.

    Even when enabled after hours, the script will process the historical data and wait for realtime data to come in to complete new bars.

    Could you maybe explain a problem scenario you are experiencing and we can share our perspective?

    We look forward to assisting.
    Last edited by NinjaTrader_Jim; 11-17-2020, 08:00 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Ok. Misunderstanding on my part. I do see that OBU is being called during State.Historical.

      Since I am using WPF elements and a construct like:

      if (State >= State.Realtime)
      {
      Dispatcher.InvokeAsync((() =>

      Do some WPF work here......

      My coloring function doesn't get hit until State.Realtime is achieved hence no display update until the first incoming tick of data.

      Is there any harm in changing that to >= State.Historical for the Async WPF functions? I do recall seeing a thread where this was discussed but can't find it at present.

      Thanks.

      Comment


        #4
        That seems to have done the trick with no ill effects thus far. Thanks for your assist.

        Comment


          #5
          Hello RandomTask,

          Thanks for clarifying.

          Our examples in the HelpGuide suggest dispatching UI modifications in State.Historical, but you can also do this in State.DataLoaded.

          My colleague Chelsea's examples do these modifications in State.DataLoaded.

          If you need to update the UI with the latest historical information before there is any realtime data, you may wish to consider tracking some items you want to push to the UI in private variables, and then in OnBarUpdate, you could fire a method to update your UI elements on the last historical bar. Something like:

          Code:
          if (State == State.Historical && BarsInProgress == 0 && CurrentBar == Bars.Count-2)
          {
              // Do Stuff
          }
          We look forward to assisting.
          JimNinjaTrader Customer Service

          Comment


            #6
            NinjaTrader_Jim

            Im doing some processing with onbarupdate and loading in 8000 5min bars.

            The current window has 10 days loaded.

            OnBarUpdate is never called for 6000 bars. Only the last 2000 bars are processed depending on the window and how many days are loaded. If 10 days are loaded.

            This means I can't process the last 6000 bars in the same fashion.

            Why would this be?

            Comment


              #7
              " Unless you are skipping historical processing with if (State == State.Historical) return; the script will process the historical data in each data series through OnBarUpdate before we start processing realtime data. Your question comes off as "I want to process historical values before OnBarUpdate is called." You will already have historically calculated values as the script processes historical data in OnBarUpdate, so this would be redundant."

              ^ This doesnt seem to be true. I get no OnBarUpdate for all of the historical bars depending on the window days loaded.

              Comment


                #8
                Hello morpheus0,

                Thank you for your post.

                What instrument are you running this chart on?

                When you look at the chart, are all 10 days worth of 5 minute bars loaded/visible?

                I recommend printing the Time[0] within OnBarUpdate() and writing this to a text file so we can see all the bars the script is processing.

                Below is a sample script demonstrating writing to a text file.



                Please let me know if I can assist further.
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Please update the link it does not work. Thank you

                  Comment


                    #10
                    Hello morpheus0,

                    Please see the link below:

                    Gaby V.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by carnitron, Today, 08:42 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post carnitron  
                    Started by strategist007, Today, 07:51 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post strategist007  
                    Started by StockTrader88, 03-06-2021, 08:58 AM
                    44 responses
                    3,974 views
                    3 likes
                    Last Post jhudas88  
                    Started by rbeckmann05, Today, 06:48 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post rbeckmann05  
                    Started by rhyminkevin, Today, 04:58 PM
                    4 responses
                    58 views
                    0 likes
                    Last Post dp8282
                    by dp8282
                     
                    Working...
                    X