Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using BarsArray to get data from a difference data series.

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

    Using BarsArray to get data from a difference data series.

    Hello,

    I have added an additional data series.

    [0] = 1 minute
    [1] = 60 minute

    When the [1] series changes, say from hour 13 to hour 14, I want to grab specific data points from historical bars. I am doing this utilizing the following method:
    Code:
    if (BarsArray[1].GetTime(CurrentBars[1]).Hour == 14)
    While the above works, it is specific to the 14th hour. I would like to have this be dynamic and process every time the [1] series bar changes. If the [1] series hour is 14, than get historical data points from when it was 14. When it changes to 15, get historical data points only when it was 15. etc, etc.

    I have tried a few things but am not getting it to do what I want, any suggestions.

    #2
    Hello ChrisR,

    The hour series is BarsArray[1] correct?

    The hour of the currently updated hour bar would be Times[1][0].Hour if this what you are wanting to compare.

    I'm not clear what you are attempting to do.

    Currently your code gets the hour of the current updated hour bar.

    BarsArray[1].GetTime(CurrentBars[1]).Hour is equal to Times[1][0].

    CurrentBars[1] would be the bar number of the hour bar currently updated. This would be getting the time of the currently updated bar and then the hour of that time.

    Is there is a specific data point you want from a previous bar?
    If so, which bar and what data point?

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello ChrisR,

      The hour series is BarsArray[1] correct?

      The hour of the currently updated hour bar would be Times[1][0].Hour if this what you are wanting to compare.

      I'm not clear what you are attempting to do.

      Currently your code gets the hour of the current updated hour bar.

      BarsArray[1].GetTime(CurrentBars[1]).Hour is equal to Times[1][0].

      CurrentBars[1] would be the bar number of the hour bar currently updated. This would be getting the time of the currently updated bar and then the hour of that time.

      Is there is a specific data point you want from a previous bar?
      If so, which bar and what data point?
      For example, what if the current hour is 11am, as in the time is say 11:20am. I want to create a list of all the historical 11am hour high prices over the lookback. But than when the hour changes to 12pm, I want to now create a list of the 12pm hour high prices over the lookback. The way I am doing it right now requires me to specify the hour. I just want the hour to be automatic/dynamic to whatever the current hour is.

      Comment


        #4
        Hello ChrisR,

        Times[1][0].Hour would be the hour of the currently updated 60 minute bar.

        It sounds like you want to loop over all minute bars that have the same hour as the 60 minute bar hour, is this correct?

        You could use BarsArray[0].GetBar(Times[1][0])) to get the bar number of the first minute bar in this hour.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello ChrisR,

          Times[1][0].Hour would be the hour of the currently updated 60 minute bar.

          It sounds like you want to loop over all minute bars that have the same hour as the 60 minute bar hour, is this correct?

          You could use BarsArray[0].GetBar(Times[1][0])) to get the bar number of the first minute bar in this hour.
          Perhaps Im not explaining this correctly, so will try once more.

          I want to be on a 1 minute chart while accessing a list of hourly bar highs from the past X sessions in the lookback that have the same hour as that current hour on the 1 minute chart. So if the current hour is the 10am hour, say time is 10:01, I want to be able to create a list of all the 10am hour highs in the lookback. I am currently doing this using;

          Code:
          if (BarsArray[1].GetTime(CurrentBars[1]).Hour == 10)
          {
          Make list
          }
          however as mentioned before, this is specific to the hour set by the integer. I would like to have this be dynamic and update to each new hour. So when the hour changes from the 10am hour to the 11am hour, I want the list to automatically provide the new list of all past 11am hour highs.
          Last edited by ChrisR; 02-25-2025, 08:18 PM.

          Comment


            #6
            Are you just doing this once, every time the 1 hour bar forms?
            If so, wouldn't this code work?
            if (BarsInProgress == 1)
            {
            if (IsFirstTickOfBar)
            {
            // Insert your code here
            }
            }

            Otherwise, you mentioned "that have the same hour as that current hour on the 1 minute chart"...

            if (BarsArray[0].GetTime(CurrentBars[0]).Hour == BarsArray[1].GetTime(CurrentBars[1]).Hour)
            {
            }
            This would be true for every minute from 10:00 to 10:59, using 10:00 as the example.
            Last edited by rockmanx00; 02-25-2025, 10:20 PM.

            Comment


              #7
              Hello ChrisR,

              BarsArray[1] is the 60 minute bar series.

              BarsArray[1].GetTime(CurrentBars[1]) is the date and time of the most recently updated bar of the 60 minute series.

              You are comparing the hour of the 60 minute bars to 10.

              Are you not wanting to compare the 1 minute bars hour to be the same as the 60 minute bars?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello ChrisR,

                BarsArray[1] is the 60 minute bar series.

                BarsArray[1].GetTime(CurrentBars[1]) is the date and time of the most recently updated bar of the 60 minute series.

                You are comparing the hour of the 60 minute bars to 10.

                Are you not wanting to compare the 1 minute bars hour to be the same as the 60 minute bars?
                I just want to put all the hourly candle highs in the lookback that match the current hour, into a list. I want that list to update when the hour changes to the next hour. How can I do that? And I would like that to work on any minute based chart, so using a 60m added data series.
                Last edited by ChrisR; 02-26-2025, 07:50 AM.

                Comment


                  #9
                  Hello ChrisR,

                  int lookBackPeriod = 20;

                  for (int index = 1; index < Math.Min(lookBackPeriod, CurrentBars[1]); index++)
                  {
                  if (Times[1][index].Hour == Times[0][0].Hour)
                  {
                  // this bar has the same hour as the current hour bar's hour
                  }
                  }
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Can this be accomplished with a List though? As I would like to get the average of those highs. Using list.average() is far more efficient than having to count and sum everything via a loop.

                    Comment


                      #11
                      Hello ChrisR,

                      You can add add a price to a list when the bar matches if you would like.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hello ChrisR,

                        You can add add a price to a list when the bar matches if you would like.
                        I have created the following, but there is a discrepancy with what is being printed.

                        When I take a normal 60 minute chart with 90 days loaded and use the following code:
                        Code:
                        if (Time[0].Hour == 12)
                        {
                           Print(High[0]);
                        }
                        This returns 61 prints with an average high price of ​​21678.76


                        However, utilizing​ the below code with the same 90 days loaded, but changed to a 1 minute chart, noting that the current hour is the same 11am-12pm hour:

                        Code:
                        if (CurrentBars[0] > 0 && CurrentBars[1] > 0)
                        {
                        
                           if (Times[0][0].Minute == 00) HiValue.Clear(); //Resets list at new hour
                        
                           int lookBackPeriod = 5000;
                        
                           for (int index = 1; index < Math.Min(lookBackPeriod, CurrentBars[1]); index++)
                           {
                              if (Times[1][index].Hour == Times[0][0].Hour)
                              {
                                 HiValue.Add(Highs[1][0]);
                        
                                 if (HiValue.Count > 0) Print(Math.Abs(HiValue.Average()));
                              }
                           }
                        }
                        ^^The intent for the above is to grab all the highs of the past hours over the last 90 days (arbitrary days loaded) where the hour matched the current hour, and place those highs into a list. Than get the average of that list. Reset the list at the start of each new hour.

                        This print is returning 21373 compared to the 21678.76 average that was pulled just using a normal 1 hour chart.


                        Last edited by ChrisR; 02-26-2025, 11:58 AM.

                        Comment


                          #13
                          Hello ChrisR,

                          Are you clearing the list before running the loop, or are you leaving all the elements from previous loops in the list?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello ChrisR,

                            Are you clearing the list before running the loop, or are you leaving all the elements from previous loops in the list?
                            I would like for the list to be cleared once a new hour starts, this way only historical hour highs that match the current live hour are added to the list. The goal is to just have a average hour high of historical hours that match current hour, utilizing list.average()

                            In the previous mentioned code, I am attempting to clear the list at the start of the new hour.
                            Last edited by ChrisR; 02-26-2025, 12:21 PM.

                            Comment


                              #15
                              Hello ChrisR,

                              Print out the elements in the list. Is this what you expect?

                              I think you should be clearing the list before you loop again and add all new elements.

                              However, the way you design your custom logic is up to you.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              627 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              359 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              562 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              568 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X