Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get the previous N bar data of first bar of chart

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

    How to get the previous N bar data of first bar of chart

    Hi All,


    I have a strategy that needs 1500 bars to calculate the algorithm properly. May I know how to get the previous N bar data of first bar of chart?


    Thank you in advance.

    #2
    Hello matrixxx,

    You would need to increase the Days to load in the Data Series parameters to cover the period, and then ensure that CurrentBar is greater than 1500 before processing begins in OnBarUpdate.

    Below is a public link to the help guide on CurrentBar.


    Or you can do an out of sync bars request.
    Below is a public link to the help guide.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I found the function AddDataSeries() have barsToLoad. Can it solve my problem? I try to use the following code but no any output.

      AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Week, Value = barPeriod }, barsToLoad, Instrument.MasterInstrument.TradingHours.Name, null);

      Comment


        #4
        Hello matrixxx,

        The issue with this is that bars from non-primary series are looked up in the array of bars from 'earlier' BIPs. The lookup is done by a matching logic using these criteria:
        .Instrument
        .IsTickReplay
        .IsResetOnNew TradingDay
        .BarsPeriod

        This means if you are using the same bar type and same instrument, then it its going to used the cached series and you will not get the requested number of bars.

        (If you change the bar type or interval, however, this should allow for the new bar series to generate and will give the extra bars)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,


          Do you have sample strategy for this case? I'm not understand how to use the function. Thanks.

          Comment


            #6
            Hello matrixxx,

            Thank you for your response.

            I think we ran with the idea that you are using multiple series in your script which may not actually be the case.

            Can you describe how you are currently trying to load the number of bars? Are you using multiple bar series through AddDataSeries() in your script?

            Based on your initial question below, can you detail if this is the primary bar series or an additional bar series that needs 1500 bars?
            I have a strategy that needs 1500 bars to calculate the algorithm properly. May I know how to get the previous N bar data of first bar of chart?
            I look forward to your response.

            Comment


              #7
              Hi Patrick,

              My primary bar series is 1 min. I'm added the AddDataSeries(BarsPeriodType.Week, 1) for calculate average 10 weeks high/low by SMA(Highs[1], 10) and SMA(Lows[1], 10).


              Thanks.

              Comment


                #8
                Hello matrixxx,

                Thank you for your response.

                Essentially you need to increase the primary bar series (the bars on the chart) in order to increase the added bar series (the 1 week) as it adopts it's barsToLoad from the primary bar series as the instrument is the same between both of them.

                To increase the primary bar series you will need to right click in the chart > select Data Series > for the 'Load data based on' setting either choose the Days or Bars setting > then set the 'Bars to load' or 'Days to load' to far enough back that 1500 weekly bars would load.

                An example would be that there are 10,080 1 minute bars in a weekly bar. so you would likely need 15, 120, 000 1 minute bars to go back 1500 weekly bars.

                Please let me know if you have any questions.

                Comment


                  #9
                  Thanks to all on this thread.

                  I found this very helpful. Thanks to all for working on this!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Today, 05:17 AM
                  0 responses
                  43 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  124 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  65 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  42 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  46 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X