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

Calculate on historical data before OnBarUpdate

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

    Calculate on historical data before OnBarUpdate

    Hi, I’m wondering what the best approach would be to do some calculations on an extra data series added in code up front, before any bar of my initial dataseries updates?

    For example I add an extra data series with 10000 bars to load in the State.Configure. Now I want to do some calculations using the price information of every bar included in this series. This I do in the State.DataLoaded. The problem here is that only the first index of this dataseries is know which is causing an exception when I try to access the second. So my questions is how is it possible to access these values? The calculated value will act as a constant from then on and be used in a formula for my indicator.

    #2
    Hello Vanderbeke,

    Welcome to the forums!

    The historical data will still have to iterate through OnBarUpdate() to get to the last bar that is loaded in the data series and then transition to realtime. Because of this, I would recommend to perform these calculations in OnBarUpdate() during historical processing for the BarsInProgress for the data series you want to calculate instead of in OnStateChange() when the data is just loaded.

    Code:
    if(BarsInProgress == 1 && State == State.Historical)
    	Print("BIP: " + BarsInProgress + " " + CurrentBar);
    I have included a link to openly available documentation on BarsInProgress.

    BarsInProgress - https://ninjatrader.com/support/help...inprogress.htm

    If you have any additional questions, please don't hesitate to ask.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim, thanks for the feedback!

      How long will the state be historical? Will it be until all bars of the extra added data series are progressed or will it be until it overlaps with the initial data series?

      Comment


        #4
        Hello Vanderbeke,

        The state will remain in historical until all historical bars are processed and the first realtime bar iterates through OnBarUpdate().

        For further reading on NinjaTrader states and NinjaScript lifecycle, please refer to the documentation below.

        NinjaScript Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm

        OnStateChange() - https://ninjatrader.com/support/help...tatechange.htm

        I'll be happy to assist you further if you have any other questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim, thanks again. Everything is clear now.

          Comment


            #6
            NinjaTrader_Jim I'm having some trouble with this. I want to do something similar. That is to calculate on barsinprogress == 1. In my case barsinprogress == 0 is 1 day of renko bars and barsinprogress == 1 is 30 days of 10 minute bars.

            I want to get the SMA on the secondary series. I added another stipulation to the "if" statement above to make sure I had enough bars in the secondary series to perform the calculation . . . .
            if (BarsInProgress == 1 && CurrentBars[1] > CalculationPeriod && State == State.Historical) {

            _volumeAverage[0] = SMA(Volume, CalculationPeriod)[0];}

            However, I keep getting this error "Error on calling 'OnBarUpdate' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

            please help.
            Last edited by kafabla; 11-11-2020, 11:38 AM.

            Comment


              #7
              Hello kafabla,

              You will want to add debugging prints to throughout the OnBarUpdate method to identify the specific line of code that is throwing the error.

              Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

              Once you find that line of code, look for brackets on that line of code since one of these would be where the index that is out of range is at. Please then test again with a separate print for each item in that line that uses brackets. Which print fails?

              If you are getting an error on CurrentBars[1], please make sure AddDataSeries is added in your script and that you have removed and re-added the script before testing again. This will ensure that we will go through a fresh instance of the script that will transition through all states in the lifecycle, and ensures that the AddDataSeries method is called.

              We look forward to assisting.
              JimNinjaTrader Customer Service

              Comment


                #8
                NinjaTrader_Jim Thanks for the quick reply.

                I used debugging prints before I asked for help. The error is throwing on the line of code I provided above. I can pull the barsinprogress == 1 data without any problems. When I comment out the _volumeAverage calculation It prints to the output screen without error. When I add the calculation for SMA back in it gives me the error.

                To this end, I believe AddDataSeries is properly coded in State.Configure. Also, I remove and re-load the script before testing each time as you suggested. Its the SMA calculation that seems to be causing my problems.

                See below for the full code sections we are discussing . . . .

                else if (State == State.Configure)
                {
                Data.BarsPeriod p = new Data.BarsPeriod();
                p.BarsPeriodType = Data.BarsPeriodType.Minute;
                p.Value = 10;
                p.MarketDataType = Data.MarketDataType.Last;
                var minutesToLoad = 20 * 1380; //23 hours a day five days a week
                var barsToLoad = minutesToLoad / 10;
                AddDataSeries(Instrument.FullName, p, barsToLoad, TradingHours.Name, null);

                _volumeAverage = new Series<double>(this, MaximumBarsLookBack.Infinite);
                }
                else if (State == State.DataLoaded)
                {
                _volumeAverage = new Series<double>(this, MaximumBarsLookBack.Infinite);
                }


                protected override void OnBarUpdate()
                {
                if (BarsInProgress == 1 && CurrentBars[1] > CalculationPeriod && State == State.Historical) {

                _volumeAverage[0] = SMA(Volume, CalculationPeriod)[0];

                Print(string.Format("{0}; {1}", CurrentBar, Volume[0]));
                }
                }

                Comment


                  #9
                  Hello kafabla,

                  Please ensure that data series are hardcoded and you are not referencing the Instrument object or BarsPeriod or similar data in State.Configure. It is permissible to use null as the instrument name to inherit the primary data series instrument, but using BarsPeriod, User Input or the Instrument object in State.Configure is not guaranteed. See advisories in AddDataSeries for more details.

                  AddDataSeries - https://ninjatrader.com/support/help...dataseries.htm

                  Please also take steps to export the snippet you are having trouble with in a separate test script. This ensures that you can provide all needed dependencies with your snippet.

                  Exporting as source code - https://ninjatrader.com/support/help...t8/?export.htm

                  I have added your code to a test indicator and have removed CalculationPeriod and have also adapted the AddDataSeries call so it is hardcoded. I can reproduce symptoms if I add the indicator to a chart that does not have sufficient amount of data to load. I.E. I get an error with 5 days, but I do not get an error with 200 days.

                  Since you are using an AddDataSeries overload that specifies BarsToLoad, this would be a new requirement for your script to function properly. You could consider adding some error checking in State.DataLoaded and check ChartBars.Properties.DaysBack or see how many days of data are loaded and ensure that a similar amount is loaded to satisfy the requirement of the added data series.

                  You could also consider using a different AddDataSeries overload which does not specify BarsToLoad.

                  We look forward to assisting.
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    NinjaTrader_Jim I figured it out. My mistake. Declaration of _volumeAverage in State.Configure is NOT "this" but "BarsArray[1]".

                    Thank you for the help. I learned something new about bars loaded on primary versus secondary.
                    Last edited by kafabla; 11-11-2020, 02:37 PM.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by DanielTynera, Today, 01:14 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post DanielTynera  
                    Started by yertle, 04-18-2024, 08:38 AM
                    9 responses
                    40 views
                    0 likes
                    Last Post yertle
                    by yertle
                     
                    Started by techgetgame, Yesterday, 11:42 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post techgetgame  
                    Started by sephichapdson, Yesterday, 11:36 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post sephichapdson  
                    Started by bortz, 11-06-2023, 08:04 AM
                    47 responses
                    1,615 views
                    0 likes
                    Last Post aligator  
                    Working...
                    X