Login Page - Create Account

Support Board


Date/Time: Sat, 01 Mar 2025 13:25:03 +0000



[Programming Help] - How to count bars between 8.30 and 9.30 for last 3 days

View Count: 505

[2021-09-28 13:16:07]
tomas262 - Posts: 145
I use range bars so number of bars differ based on volatility. I use this code to count bars between 8.30 and 9.30 for previous 3 days but this method seems kinda slow.. would you please suggest a more effective way in respect of how SC operates?

// last X days session bar counter
  if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED)
  {
    int total_sum = 0;
    int this_day_sum = 0;
    int daysCounter = 0;
    // Counting bars backwards
    for (int i = sc.Index; i >= 0; i--)
    {
      // This bar time
      int BarTime = sc.BaseDateTimeIn[i].GetTime();
      int BarDate = sc.BaseDateTimeIn[i].GetDate();
      // Prev bar time
      int BarTimePrevious = sc.BaseDateTimeIn[i - 1].GetTime();

      // Counting bars in session
      if (BarDate < sc.BaseDateTimeIn[sc.Index].GetDate() && BarTime >= HMS_TIME(SessionStartHour.GetInt(), SessionStartMinute.GetInt(), 0) && BarTime < HMS_TIME(SessionEndHour.GetInt(), SessionEndMinute.GetInt(), 0))
        this_day_sum++;

      // Session START met - reset daily counter
      if (BarDate < sc.BaseDateTimeIn[sc.Index].GetDate() && BarTimePrevious < HMS_TIME(SessionStartHour.GetInt(), SessionStartMinute.GetInt(), 0) && BarTime >= HMS_TIME(SessionStartHour.GetInt(), SessionStartMinute.GetInt(), 0))
      {
        total_sum += this_day_sum;
        this_day_sum = 0;
        daysCounter++;
      }

      if (daysCounter >= DaysToCountBars.GetInt()) break;
    }

    int barCounterAvg = round(total_sum / DaysToCountBars.GetInt());

    SCString barCounterBars;
    barCounterBars.Format("BarsAvg(%i): %i", DaysToCountBars.GetInt(), barCounterAvg);
    if (DisplayBarCounter.GetYesNo() == 1) sc.AddAndManageSingleTextUserDrawnDrawingForStudy(sc, 0, 30, 50, BarCounter, 0, barCounterBars, 1, 0, 0);
  }

Date Time Of Last Edit: 2021-09-28 13:19:16
[2021-09-30 15:03:21]
User99735 - Posts: 234
With sc.AutoLoop set to 1, the above code will loop set through all the bars between 0, and current bar sc.Index, for each bar. So the loop for (int i = sc.Index; i >= 0; i--) can be removed along with suitable changes to the // Counting bars in session if check.

With sc.AutoLook set to 0, code looks fine.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account