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

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 aa731, Today, 02:54 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post aa731
                  by aa731
                   
                  Started by thanajo, 05-04-2021, 02:11 AM
                  3 responses
                  470 views
                  0 likes
                  Last Post tradingnasdaqprueba  
                  Started by Christopher_R, Today, 12:29 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post Christopher_R  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  166 responses
                  2,237 views
                  0 likes
                  Last Post sidlercom80  
                  Started by thread, Yesterday, 11:58 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post thread
                  by thread
                   
                  Working...
                  X