Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Max/min from certain bar index

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

    Max/min from certain bar index

    Hi,
    is there a way to get a max of highs and lows from a certain bar index to today? I retrieved the bar index.
    In particular, i have to get the previous annual and current annual max/min of highs/lows.

    Thanks

    #2
    Hello Davide999,

    Thank you for your post.

    HighestBar() and LowestBar() can be used to calculate the highest and lowest bar in the most recent period.

    https://ninjatrader.com/support/help...highestbar.htm

    https://ninjatrader.com/support/help.../lowestbar.htm

    Alternatively, check out this forum post which includes a sample script that calculates the highest bar in a specified range. You can modify it to calculate the lowest bar.

    https://forum.ninjatrader.com/forum/...ue#post1113896

    Please let me know if I can assist further.

    Comment


      #3
      Hi,
      I tried this: https://forum.ninjatrader.com/forum/...ue#post1113896
      All works fine only for HIGHS (I retrieved high of current month, previous, month, current year, previous year), the values of lows are all incorrect.
      I already tried to uncomment the other line of the for but still not working. I want to retrieve low value of current month, previous month, current year, previous year but I didn't understand how to do.
      I attach my code:
      Code:
      if (State == State.Historical && CurrentBar == Count - 2)
                  {
                      SessionIterator iterator1 = new SessionIterator(Bars);
                      SessionIterator iterator2 = new SessionIterator(Bars);
                      SessionIterator iterator3 = new SessionIterator(Bars);
                      SessionIterator iterator4 = new SessionIterator(Bars);
                      SessionIterator iterator5 = new SessionIterator(Bars);
                      SessionIterator iterator6 = new SessionIterator(Bars);
      
                      currentYear = DateTime.Now.Year;
                      previousYear = currentYear - 1;
                      currentMonth = DateTime.Now.Month;
                      previousMonth = currentMonth - 1;
      
                      IndiceControlloPartenzaCurrMonth = Bars.GetBar(new DateTime(currentYear, currentMonth, 1, 0, 0, 0));
                      IndiceControlloPartenzaCurrYear = Bars.GetBar(new DateTime(currentYear, 1, 1, 0, 0, 0));
                      IndiceControlloPartenzaPrevMonth = Bars.GetBar(new DateTime(currentYear, previousMonth, 1, 0, 0, 0));
                      IndiceControlloArrivoPrevMonth = Bars.GetBar(new DateTime(currentYear, previousMonth, DateTime.DaysInMonth(currentYear, previousMonth), 0, 0, 0)); //prendo il giorno finale del mese
                      IndiceControlloPartenzaPrevYear = Bars.GetBar(new DateTime(previousYear, 1, 1, 0, 0, 0));
                      IndiceControlloArrivoPrevYear = Bars.GetBar(new DateTime(previousYear, 12, 31, 0, 0, 0));
      
                      iterator1.CalculateTradingDay(Time[IndiceControlloPartenzaCurrMonth], true);
                      EndTimeCurrMonth = iterator1.ActualSessionEnd;
                      StartTimeCurrMonth = iterator1.ActualSessionBegin;
      
                      iterator2.CalculateTradingDay(Time[IndiceControlloPartenzaCurrYear], true);
                      EndTimeCurrYear = iterator2.ActualSessionEnd;
                      StartTimeCurrYear = iterator2.ActualSessionBegin;
      
                      iterator3.CalculateTradingDay(Time[IndiceControlloPartenzaPrevMonth], true);
                      EndTimePrevMonth = iterator3.ActualSessionEnd;
                      StartTimePrevMonth = iterator3.ActualSessionBegin;
      
                      iterator4.CalculateTradingDay(Time[IndiceControlloPartenzaPrevYear], true);
                      EndTimePrevYear = iterator4.ActualSessionEnd;
                      StartTimePrevYear = iterator4.ActualSessionBegin;
      
                      iterator5.CalculateTradingDay(Time[IndiceControlloArrivoPrevMonth], true);
                      EndTimePrevMonthArrivo = iterator5.ActualSessionEnd;
                      StartTimePrevMonthArrivo = iterator5.ActualSessionBegin;
      
                      iterator6.CalculateTradingDay(Time[IndiceControlloArrivoPrevYear], true);
                      EndTimePrevYearArrivo = iterator6.ActualSessionEnd;
                      StartTimePrevYearArrivo = iterator6.ActualSessionBegin;
      
                      if (StartTimeCurrMonth.Hour == 15){
                          startingIndexCurrMonth = Bars.GetBar(new DateTime(currentYear, currentMonth, 1, 15, 30, 0));
                      }
                      else if (StartTimeCurrMonth.Hour == 14){
                          startingIndexCurrMonth = Bars.GetBar(new DateTime(currentYear, currentMonth, 1, 14, 30, 0));
                      }
      
                      if (StartTimeCurrYear.Hour == 15){
                          startingIndexCurrYear= Bars.GetBar(new DateTime(currentYear, 1, 1, 15, 30, 0));
                      }
                      else if (StartTimeCurrYear.Hour == 14){
                          startingIndexCurrYear = Bars.GetBar(new DateTime(currentYear, 1, 1, 14, 30, 0));
                      }
      
                      if (StartTimePrevMonth.Hour == 15){
                          startingIndexPrevMonth = Bars.GetBar(new DateTime(currentYear, previousMonth, 1, 15, 30, 0));
                      }
                      else if (StartTimePrevMonth.Hour == 14){
                          startingIndexPrevMonth = Bars.GetBar(new DateTime(currentYear, previousMonth, 1, 14, 30, 0));
                      }
      
                      if (StartTimePrevYear.Hour == 15){
                          startingIndexPrevYear= Bars.GetBar(new DateTime(previousYear, 1, 1, 15, 30, 0));
                      }
                      else if (StartTimePrevYear.Hour == 14){
                          startingIndexPrevYear = Bars.GetBar(new DateTime(previousYear, 1, 1, 14, 30, 0));
                      }
      
                      if (StartTimePrevMonthArrivo.Hour == 15){
                          finishingIndexPrevMonth = Bars.GetBar(new DateTime(currentYear, previousMonth, DateTime.DaysInMonth(currentYear, previousMonth), 15, 30, 0));
                      }
                      else if (StartTimePrevMonthArrivo.Hour == 14){
                          finishingIndexPrevMonth = Bars.GetBar(new DateTime(currentYear, previousMonth, DateTime.DaysInMonth(currentYear, previousMonth), 14, 30, 0));
                      }
      
                      if (StartTimePrevYearArrivo.Hour == 15){
                          finishingIndexPrevYear= Bars.GetBar(new DateTime(previousYear, 12, 31, 15, 30, 0));
                      }
                      else if (StartTimePrevYearArrivo.Hour == 14){
                          finishingIndexPrevYear = Bars.GetBar(new DateTime(previousYear, 12, 31, 14, 30, 0));
                      }
      
                      barsRangeCurrMonth = Count - startingIndexCurrMonth;
                      barsRangeCurrYear = Count - startingIndexCurrYear;
                      barsRangePrevMonth = finishingIndexPrevMonth - startingIndexPrevMonth;
                      barsRangePrevYear = finishingIndexPrevYear - startingIndexPrevYear;
      
                      //Mese Corrente
                      highestBarAgoCurrMonth = HighestBarAgoInRange(High, barsRangeCurrMonth, 0);
                      lowestBarAgoCurrMonth = LowestBarAgoInRange(Low, barsRangeCurrMonth, 0);
      
                      //Mese Precedente
                      highestBarAgoPrevMonth = HighestBarAgoInRange(High, barsRangePrevMonth, 0);
                      lowestBarAgoPrevMonth = LowestBarAgoInRange(Low, barsRangePrevMonth, 0);
      
                      //Anno Corrente
                      highestBarAgoCurrYear = HighestBarAgoInRange(High, barsRangeCurrYear, 0);
                      lowestBarAgoCurrYear = LowestBarAgoInRange(Low, barsRangeCurrYear, 0);
      
                      //Anno Precedente
                      highestBarAgoPrevYear = HighestBarAgoInRange(High, barsRangePrevYear, finishingIndexPrevYear);
                      lowestBarAgoPrevYear = LowestBarAgoInRange(Low, barsRangePrevYear, 0);            
      
                      Print(Time[highestBarAgoCurrMonth] + " " + High[highestBarAgoCurrMonth]);  // -->OK
                      Print(Time[lowestBarAgoCurrMonth] + " " + Low[lowestBarAgoCurrMonth]);
                      Print(Time[highestBarAgoCurrYear] + " " + High[highestBarAgoCurrYear]); // -->OK
                      Print(Time[lowestBarAgoCurrYear] + " " + Low[lowestBarAgoCurrYear]);
                      Print(Time[highestBarAgoPrevMonth] + " " + High[highestBarAgoPrevMonth]); // -->OK
                      Print(Time[lowestBarAgoPrevMonth] + " " + Low[lowestBarAgoPrevMonth]);
                      Print(Time[highestBarAgoPrevYear] + " " + High[highestBarAgoPrevYear]); // -->OK
                      Print(Time[lowestBarAgoPrevYear] + " " + Low[lowestBarAgoPrevYear]);
                  }
              }​
      
      // must be called from a data driven method or used with TriggerCustomEvent
              private int HighestBarAgoInRange(ISeries<double> inputSeries, int period, int barsAgo)
              {
                  // check that maximumbarslookback is infinite?
                  // if (period + barsAgo > 256)            
      
                  int startBarsAgo    = ((period + barsAgo - 1) < CurrentBar) ? (period + barsAgo - 1) : CurrentBar;
                  int returnVal        = startBarsAgo;
                  double currentHigh    = inputSeries[startBarsAgo];
      
                  //for (int index = CurrentBar - barsAgo; index < CurrentBar - period - barsAgo; ++index)
                  for (int index = startBarsAgo - 1; index >= barsAgo; --index) //calcolo del massimo
                      if (inputSeries[index] > inputSeries[returnVal])
                          returnVal = index;
      
                  return returnVal;
              }
      
              // must be called from a data driven method or used with TriggerCustomEvent
              private int LowestBarAgoInRange(ISeries<double> inputSeries, int period, int barsAgo)
              {
                  // check that maximumbarslookback is infinite?
                  // if (period + barsAgo > 256)            
      
                  int startBarsAgo    = ((period + barsAgo - 1) < CurrentBar) ? (period + barsAgo - 1) : CurrentBar;
                  int returnVal        = startBarsAgo;
                  double currentLow    = inputSeries[startBarsAgo];
      
                  for (int index = CurrentBar - barsAgo; index < CurrentBar - period - barsAgo; ++index) //calcolo del minimo
      //            for (int index = startBarsAgo - 1; index >= barsAgo; --index)
                      if (inputSeries[index] > inputSeries[returnVal])
                          returnVal = index;
      
                  return returnVal;
              }​
      Please, can you explain the sample of that function and help me?

      Thanks

      Comment


        #4
        Hello,

        If you are trying to look for the lowest low in the range, you need to change the following line in LowestBarAgoInRange:

        if (inputSeries[index] > inputSeries[returnVal])

        This is checking for the highest high. Change this to:

        if (inputSeries[index] < inputSeries[returnVal])

        Comment


          #5
          Hi,
          Already tried without any correct results...
          I did not understand how it works....should be the same for HIGHS but this did not work....

          Let me know

          Thanks

          Comment


            #6
            Hello,

            In the example script, it loops from the start bar (period + barsAgo + 1) to the end bar (barsAgo), and saves the bar index to the returnValue variable if the index bar value is greater than the returnValue bar index's value.

            Your loop is looping through something else:

            Code:
            for (int index = CurrentBar - barsAgo; index < CurrentBar - period - barsAgo;
            It should match the example script:

            Code:
            for (int index = startBarsAgo - 1; index >= barsAgo; --index)
            Try changing your loop to match the loop from the example script.​

            Comment


              #7
              Hi,
              Ok; it works.

              Thanks

              Comment

              Latest Posts

              Collapse

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