Login Page - Create Account

Support Board


Date/Time: Sat, 19 Apr 2025 09:58:41 +0000



[Programming Help] - Issue with Session Reset Logic in Custom ACSIL Code

View Count: 102

[2025-03-28 15:06:28]
__Kai__ - Posts: 15
Dear Sierra Chart Support,

I am encountering an issue with my ACSIL code that is designed to detect session changes or the start of RTH at 9:30, and then calculate a 'lookback' variable representing the number of bars since the reset. However, the reset is unexpectedly triggered every 2-5 bars instead of only at the intended session start or RTH time.

Below is a snippet of my code:


int lookback = cumulative_SMA_lookback;
if (SessionBreakType == 1)
{
static int sessionStartIndex = 0;
static SCDateTime lastSessionDate; // Last date when the reset was executed

// Determine the start time of the current trading day
SCDateTime currentDayStart = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.Index]);
// Compare with the start time of the previous bar (if available)
SCDateTime previousDayStart = (sc.Index > 0)
? sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.Index - 1])
: currentDayStart;

// If it is the first bar or the trading day start changes
// and no reset has yet been executed for this trading day
if ((sc.Index == 0 || currentDayStart != previousDayStart) &&
currentDayStart != lastSessionDate)
{
sessionStartIndex = sc.Index;
lastSessionDate = currentDayStart;
}

lookback = sc.Index - sessionStartIndex + 1;
}
else if (SessionBreakType == 2)
{
static int rthStartIndex = 0;
static SCDateTime lastRTHDate; // Date when the reset was last executed

SCDateTime currentBarDate = sc.BaseDateTimeIn[sc.Index].GetDate();
SCDateTime rthStartTime = currentBarDate;
rthStartTime.SetTimeHMS(9, 30, 0);

// Reset only if no reset has been executed for this day
// AND the current bar is >= 9:30 and the previous bar was before 9:30
if (currentBarDate != lastRTHDate &&
sc.BaseDateTimeIn[sc.Index] >= rthStartTime &&
(sc.Index == 0 || sc.BaseDateTimeIn[sc.Index - 1] < rthStartTime))
{
rthStartIndex = sc.Index;
lastRTHDate = currentBarDate;
}

// Calculate the lookback from the last RTH start index
lookback = sc.Index - rthStartIndex + 1;
}

Additionally, I have observed that when I manually trigger a Recalculate event, the code appears to work correctly for a few additional candles, but then the issue reoccurs with the reset being triggered every few bars.

Could you please advise what might be causing these intermittent resets? Is there a potential issue with handling session boundaries or bar index management that I might be overlooking? Any guidance or known issues regarding this behavior in ACSIL would be greatly appreciated.

Thank you for your assistance.

Greetings Kai
[2025-03-28 16:41:22]
JohnR - User831573 - Posts: 330
I don't have a specific answer for you, but you may want to try to step through the code with the VS2022 debugger.

I have not used these functions, but the docs say SCDateTime currentDayStart = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.Index]);

Typically this will be the Date-Time of a bar. -- notice the word BAR in the function. You might want to change to this function.

sc.GetSessionTimesFromChart()

int GetSessionTimesFromChart(const int ChartNumber, n_ACSIL::s_ChartSessionTimes& r_ChartSessionTimes);

The sc.GetSessionTimesFromChart() function sets the chart session times in r_ChartSessionTimes for the chart defined by ChartNumber.


r_ChartSessionTimes: A s_ChartSessionTimes structure that contains the session times for the given chart. The s_ChartSessionTimes members are the following:
SCDateTime StartTime
SCDateTime EndTime
SCDateTime EveningStartTime
SCDateTime EveningEndTime
int UseEveningSessionTimes
int NewBarAtSessionStart
int LoadWeekendDataSetting


JohnR

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

Login

Login Page - Create Account