Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Load historical data into a variable independent of current chart timeframe?

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

    Load historical data into a variable independent of current chart timeframe?

    Hi,


    I am trying to code indicators for an intraday chart that are using historical data from the daily chart. I have been experimenting with AddDataSeries, but did not get very far. One of many reasons seems to be that if I needed 200 daily candles, but I am currently on a one minute chart, I would need to load an excessive amount of data (200 days of 1 minute candles).

    Is it possible to manually load a set of historical data into an array independent of the time frame of the current chart?

    I am imagining something like (pseudo-code)
    //loads 200 closes of daily candles (not millions of 1 minute candles..)
    array x = getHistoricalData(currentSymbol, "close", "d", 200);


    and no matter on what chart I am, x[1] would be yesterday's daily close, x[2] the day before and so on..

    Is there anything in NinjaScript that comes close to this code?
    I really appreciate your help!

    #2
    Hello portislander,

    Thanks for opening the thread, and welcome to the forums!

    It is possible to create a data series outside of the indicator or strategy using a BarsRequest, but this approach would be much more complicated than to simply use AddDataSeries().

    Using the following overload, you can assign a a certain number of bars to load for a daily data series:

    AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)

    Documentation and syntax can be found here: https://ninjatrader.com/support/help...urs_window.htm

    When using additional data series, I would suggest to reference the documentation on Multi Series NinjaScripts here: https://ninjatrader.com/support/help...nstruments.htm

    The SampleIntrabarBacktest strategy is a good example of how you can reference and use additional data series. It can be found here: http://ninjatrader.com/support/forum...ead.php?t=6652

    If you would like to take a more advanced approach, you can use a BarsRequest. This approach would require you to synchronize the data series on your own to form your own bars that are consistent with your chart. You will also be subscribing to realtime data and would will need to build historical bars on your own so it can be used for normal calculation. If you would like to continue using a BarsRequest, you can reference the documentation here: https://ninjatrader.com/support/help...arsrequest.htm

    Please let me know if you have any additional questions.
    Last edited by NinjaTrader_Jim; 05-30-2017, 10:49 AM.

    Comment


      #3
      Hi Jim,


      Thank you very much for your quick and helpful response.

      I have two follow-up questions:
      1) If I use the AddDataSeries method to load 200 candles of daily data, does that require me to set the "Days to Load" of my 1 minute chart to 200 days? That would be a problem.

      2) BarsRequest looks really good. However, I am not sure I understand the requirement to synchronize the data series when using it.

      I would like to calculate my indicator on each bar update (1 or 5 minute bar) using a mix of the daily data and the last close price. Let me give you a simplified example:

      Market opens, first 5min candle closes at 10 USD at 9:35am.
      Indicator should print at an average over (199 daily closes + 10) on that bar.

      Second 5 min candle closes at 12 USD at 9:40am.
      Indicator should print at an average over (199 daily closes + 12) on that bar.

      Ideally, I would load the 199 daily closes only once for the first bar and have them stored in memory for the following.

      Where would the synchronization come into play in this case?

      Comment


        #4
        Hello portislander,

        Thanks for the reply.

        1) If I use the AddDataSeries method to load 200 candles of daily data, does that require me to set the "Days to Load" of my 1 minute chart to 200 days? That would be a problem.
        AddDataSeries() will work independently from the primary data series. You can load as much or as little data as you tell it.

        2) BarsRequest looks really good. However, I am not sure I understand the requirement to synchronize the data series when using it.
        BarsRequests can be powerful, but they require more effort to use. I believe the BarsRequest would veer away what you are trying to accomplish. However, if you would like to view an example of how you can synchronize a BarsRequest, you can see here:


        If you would like to calculate an indicator based off of a combination of daily data and intra day data, I would suggest to create your own Series<double> to hold the data that you wish to calculate for your indicators. With each OnBarUpdate(), you can update the custom Series<double> with the 199 previous daily bars, and then you can sub in the last bar with whatever data you consider pending for that day. This Series can then be placed directly into an indicator for calculation.

        Information on using a custom Series object can be found here: https://ninjatrader.com/support/help...s/?seriest.htm

        Comment


          #5
          Hello Jim,


          Yes, a custom Series object sounds exactly what I need. Thank you very much!
          So your recommendation would be to load the data once with AddDataSeries(), store it in a series object and then perform calculations OnBarUpdate()?

          Please correct me if I am wrong.
          I will give this a try this week and get back to you (either mark this solved or post a follow-up question).

          Thanks!

          Comment


            #6
            Hello,

            Yep, you understand correctly.

            If you have any additional questions, please don't hesitate to write back.

            Comment


              #7
              Hi Jim,

              I have tried it out, but I am stuck:
              First, I am loading 200 daily bars with "AddDataSeries".

              Code:
              if (State == State.Configure) {
                BarsPeriod bp = new BarsPeriod();
                bp.BaseBarsPeriodType = BarsPeriodType.Day;
                bp.BaseBarsPeriodValue = 1;        
                bp.MarketDataType = MarketDataType.Last;
                AddDataSeries(Instrument.ToString(), bp, 200, "US Equities RTH", false);
              }
              Next, in State.DataLoaded I want to load the "close" data into my Series<double> one by one with a for loop. I assume (incorrectly?) the values can be accessed with Closes[1][x].
              This failed, so I tried printing the first 20 values of the series to the console instead:

              Code:
              if (State == State.DataLoaded) {                
                 for (int x = 0; x<=20; x++)    {                    
                    Print(Closes[1][x]);
                 }
              }
              I notice two issues:
              1) The values are not the close values of the daily chart. They are mostly static at one price, but I cannot find them in my primary data series (5min chart) either.
              2) After printing 15 values, I get an error:
              Indicator 'MyIndicator': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

              Do you have an idea what I am doing wrong?
              Thanks!

              Comment


                #8
                Well, you would not want to be referencing a price object in OnStateChange() because the bars are not formed yet. You will want to place this logic in OnBarUpdate().

                I forgot to mention there is a sample for storing data to a a Series or DataSeries for calculations. You can check out that sample here: http://ninjatrader.com/support/forum...ead.php?t=7299

                In addition to the samples noted, we have a tips section that covers some good ways to debug NinjaScripts and fix common issues. You can check those out here: http://ninjatrader.com/support/forum...splay.php?f=31

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                576 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X