Login Page - Create Account

Support Board


Date/Time: Sat, 19 Apr 2025 10:27:39 +0000



Post From: Issue with Session Reset Logic in Custom ACSIL Code

[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