Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ChartBars.Properties.DaysBack , ensuring enough historical bars have been loaded

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

    ChartBars.Properties.DaysBack , ensuring enough historical bars have been loaded

    I'm writing an indicator which builds averages from previous bars (historical bars), using complex calculations of contrived measurements,
    and I want to determine that the chart has enough historical bars loaded in the Data Series, to ensure the averages being accumulated by the indicator will be reliable.

    for example, with a live data connection, I open Ninjatrader, open a chart for a instr, and the data series Type : Minute, Value : 1

    Time Frame has
    " Load data based on : Days"
    " Days to load : 1 "


    but my indicator would work better if it had 3 days of historical data loaded.

    My understanding is, NinjaTrader will process the historical bars and my indicator will perform it's calculations and build up the averages.
    But this is not as plain and simple as using if CurrentBar < 20 etc.

    If the current session on the active day doesn't have the calculated data I made, I make it look at the dictionary from the previous day to see if it has the data I want.
    When the settings of the chart are not loading enough data to build up these collections of previous days of dictionaries, I want to display a warning on the chart.



    So I try in State.DateLoaded
    Code:
    if (ChartBars.Properties.DaysBack < 2)
                    {
                        Print("number of days of DaysBack = "+  ChartBars.Properties.DaysBack );
                        Draw.TextFixed(this, "NinjaScriptInfo", "Adjust Data Set Days Back to grab at least 2 days worth of historical data, so this can collect avgs for each session", TextPosition.BottomRight);
                    }
    Is this the proper method to test / catch this case?



    And if I am testing using Playback connection, how does this affect the value?

    i.e. if my playback , Market Replay Start = 04/27/2020 , End = 05/01/2020
    and I position my GoTo to 04/29/2020 12:00:00 pm
    there will be more than 1 days worth of 1 minute bars loaded on the chart, and the above code block will still show the warning message.

    I intend to make this work on a live data connection, but I need to understand if this is the best practice approach for this while testing using a Playback Connection ?


    Thank you

    #2
    Hello balltrader,

    Thanks for your question.

    If your goal is to advise the user if they have loaded enough data for the indicator to start plotting, I would suggest checking Bars.Count in State.DataLoaded to see if Bars.Count-2 is less than the number of bars needed to start plotting.

    Please see the example and screenshots attached.

    We look forward to assisting.
    Attached Files

    Comment


      #3
      HI, Sorry that was not my goal.
      Is there anything in my original question that you didn't understand what I was asking, that I can elaborate on, to try to explain it in more detail?

      Comment


        #4
        Hello balltrader,

        Your approach for checking DaysBack would check the number of days to load entered in the Data Series window. This looks back calendar days and would not necessarily mean that we have that many data of data loaded. For example, if we request data on Monday, weekends would be included here. This property is simply what is used in the Data Series window and does not increase as more data loads.

        My suggestion to check the number of bars loaded would be more relevant to having enough data to drive your calculations.

        To see the number of days of data that get loaded, you can compare the timestamps of the bars that get loaded in State.DataLoaded. For example:

        Code:
        Print(Bars.GetTime(Bars.Count-2).Subtract(Bars.GetTime(0)).Days);
        If this does not satisfy your need, I may need some clarity on how it does not meet your requirement.

        I look forward to assisting.

        Comment


          #5
          Aha, Ok I understand now, thanks. DaysBack property was the best I could find searching the forums. I also did not realize it was calendar days and not actual trading days defined by session EOD flags.



          Bars Represents the data returned from the historical data repository.

          Bars.Count (although not listed as a property in online docs) give the count of the number of bars loaded.



          My new code in : else if (State == State.DataLoaded)
          Code:
          int NumberOfDaysOfHistoricalDataLoadedAtStateDataLoaded = Bars.GetTime(Bars.Count-2).Subtract(Bars.GetTime(0)).Days ;
          if (NumberOfDaysOfHistoricalDataLoadedAtStateDataLoaded < 2) 
          {
             Draw.TextFixed(this, "NinjaScriptInfo",
                       "Adjust Data Series property 'Days Back' to grab at least 2 trading days worth of historical data, so this can collect avgs for each session over a period of 2 trading days",
                        TextPosition.BottomRight);
           }

          and to clarify my understanding further,

          1. with live data connection, Chart > Data Series > Days to load, this value should be 2 more than the minimum amount of days you want, to account for weekends, and the historical data will be loaded from that date?

          2. with playback connection, you must have downloaded data from 2 trading days before the date you set as your GOTO date, and the start date is the earliest date, starting at midnight, that your historical replay data will be loaded from? and "playback current day only" must be unchecked to have the previous 2 days worth of historical data loaded ?

          Comment


            #6
            Hello balltrader,

            1. with live data connection, Chart > Data Series > Days to load, this value should be 2 more than the minimum amount of days you want, to account for weekends, and the historical data will be loaded from that date?
            Holiday weekends would also need to be considered. If you load 5 days of data this should be fine for most cases.

            2. with playback connection, you must have downloaded data from 2 trading days before the date you set as your GOTO date, and the start date is the earliest date, starting at midnight, that your historical replay data will be loaded from? and "playback current day only" must be unchecked to have the previous 2 days worth of historical data loaded ?
            Historical data (not to be confused with Market Replay data) will still be added to the chart to satisfy the data request in the Data Series window, but the time Market Replay data would be used would be from the Start Date of the Playback Controller.

            When Playback Current Day Only is selected, only the current day will be played back from Market Replay when dragging the slider between multiple days, historical data is loaded for the past days which is faster to fast forward through since those bars would not be recreated with Market Replay data.

            Let us know if there is anything else we can do to help.

            Comment


              #7
              Historical data (not to be confused with Market Replay data) will still be added to the chart to satisfy the data request in the Data Series window, but the time Market Replay data would be used would be from the Start Date of the Playback Controller.
              I just tested a scenario using market playback.
              I set the playback Start value to 04/22/2020, but there is no replay data downloaded from that date. The downloaded replay data starts on 04/27/2020.
              I set the GOTO value to 04/27/2020 12:00:00 PM.

              So even if historical data is attempted to be added to the chart for the Data Series windows settings, if there is no data available to the playback connection, those days won't have any bars, therefore the bars count subtract .Days property is still valid bcuz it will only count the bars that are loaded.



              I do get an error using your code sample,
              Code:
              Bars.GetTime(Bars.Count-2)
              when using Playback, and adjusting the GOTO.
              When the playback starts, the Bars.Count will be 0, because those bars have not been loaded / processed yet. So using the Bars.Count-2 results in an index of negative 2, which throws the typical "out-of-range" error.

              If I wait for the playback to finish loading all it's data, and then press F5 to refresh the chart, then I do not have that error.

              I don't think it will matter on a live data connection, I am just using replay while debugging and testing my indicator code, so I wanted to mention that for future readers of this thread if they encounter that error during replay initial load.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              648 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              369 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              108 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              572 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              574 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X