Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing previous days bars

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

    Accessing previous days bars

    I am trying to calc the average of the last 20 days 1-min bars excluding todays RT data. I'm not interested in displaying this avg, just need it for further calcs. Here is my code snippet.

    // if first bar of today's session, calc last 20 days average
    if(Bars.FirstBarOfSession)
    {
    int barsCounted = 0;
    for(int i = CurrentBar-1; i>=0; i--)
    {
    nDaysAvg += ((High[i] + Low[i] + Close[i]) / 3);
    barsCounted++;
    }
    nDaysAvg = nDaysAvg / barsCounted;
    }

    My Questions:
    1) Am I iterating the Bars collection backwards correctly
    2) What is the best way to determine if I've processed 20 days. Currently, I'm creating a chart for 20 days and stopping when no more bars (i == 0).
    3) Since I am only interested in displaying only todays 1min bars, how can I retrieve the last 20 days 1min bars used to calc the avg without having to display their bars.

    thx

    #2
    Your style should be fine at a glance over. You will not be able to process past 20 days without loading 20 days onto the chart.

    You could detect if you have processed 20 bars by using GetBar() and passing in a DateTime of market open of 20 days ago. Then you will have the number of bars ago to use instead of CurrentBar - 1.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh,

      just to clarify, I used CurrentBar-1 to get the "last" bar of yesterdays session, then Iterate backwards for 20 days starting from this "last" bar. Is this the correct way to get this last bar?

      I like the GetBar() idea. The only issue is how to calculate the date for 20 previous "trading" days to pass into GetBar()

      Also, is this the only developer guide available for NT

      Comment


        #4
        That is where you can find the language reference. For examples and such please see the reference samples section of the forums.

        Calculating 20 days would be up to you. You can try some DateTime manipulations. Check the DayOfWeek and then do the math to get you 20 trading days from there. Then create your DateTime to have the proper date.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          will do, thanks

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 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
          548 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X