Login Page - Create Account

Support Board


Date/Time: Sat, 16 Aug 2025 13:53:01 +0000



[Programming Help] - ACSIL Calculate Bar Duration in Seconds

View Count: 1048

[2022-12-30 07:11:46]
TriStar Trading - Posts: 165
Can anyone help with ACSIL code to calculate the duration of the current price bar in seconds? I'm using range bars. Thanks in advance, Mike
[2022-12-30 12:33:14]
User431178 - Posts: 761
Take a look at sc.BaseDataEndDateTime and SCDateTimeSpan (in scdatetime.h).
ACSIL Interface Members - Variables and Arrays: sc.BaseDataEndDateTime[]

There is also the SCDateTime member function GetTimeInSeconds, so you could compare the start time in seconds vs. end time in seconds.
Working with the SCDateTime Variables and Values: GetTimeInSeconds()
[2022-12-31 18:57:41]
TriStar Trading - Posts: 165
Here is how I did it after stealing some code here and there. :) Thanks.

//Set the configuration variables and defaults.
SCSubgraphRef BarSeconds = sc.Subgraph[0];

if (sc.SetDefaults)
{
//Set the configuration and defaults
sc.GraphName = "Bar Duration In Seconds;
sc.GraphRegion = 1; //Not Main graph region
sc.AutoLoop = 1;
sc.ValueFormat = 0;

//Define the Subgraphs
BarSeconds.Name = "Bar Seconds";
BarSeconds.DrawStyle = DRAWSTYLE_BAR;
BarSeconds.LineWidth = 2;
BarSeconds.PrimaryColor = RGB(128,128,128);

return;
}

SCDateTimeMS BarEndDateTime = sc.GetEndingDateTimeForBarIndex(sc.Index);
SCDateTimeMS ElapsedTime = static_cast<float>((BarEndDateTime - sc.BaseDateTimeIn[sc.Index] + SCDateTime::MICROSECONDS(1)).GetAsDouble());

BarSeconds[sc.Index] = ElapsedTime.GetTimeInSeconds();
Date Time Of Last Edit: 2022-12-31 18:58:30
[2023-01-01 01:14:34]
User183724 - Posts: 194
thx for posting this
[2025-08-15 12:02:13]
aknsyu71@gmail - Posts: 60
To use in your own study


      // Calculate the duration of the last closed bar in seconds
      SCDateTimeMS BarEndDateTime = sc.GetEndingDateTimeForBarIndex(sc.Index - 1);
      SCDateTimeMS BarStartDateTime = sc.BaseDateTimeIn[sc.Index - 1];
      SCDateTimeMS BarDuration = BarEndDateTime - BarStartDateTime + SCDateTime::MICROSECONDS(1);
      float LastClosedBarSeconds = static_cast<float>(BarDuration.GetTimeInSeconds());  
      //int64_t LastClosedBarMicroseconds =static_cast<int64_t>(BarDuration.GetAsDouble() * 86400000000.0); // Convert days to microseconds

      if (LastClosedBarSeconds < 1) {FormulaResult=0;}  //if bar is less then a second then ignore

[2025-08-15 16:44:23]
User719512 - Posts: 318
Sierra chart provides examples using most all of its API.

C:\SierraChart\ACS_Source\Studies8.cpp

SCSFExport scsf_BarTimeDuration(SCStudyInterfaceRef sc)

or ask Grok to fill in details and/or tailor to your specific requirements.

You have to decide what you consider bar duration, especially in slow markets


BAR BAR BAR
|_| // start to end, ignore time between bars
|____| // start to start, includes time between bars
|____| // end to end, includes time between bars

Date Time Of Last Edit: 2025-08-15 16:57:11

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

Login

Login Page - Create Account