namepace
private TimeSpan sessionStartTime;
private bool sessionResetDone;
onstate change:
highestHigh = double.MinValue; // Initialize to a very low value
lowestLow = double.MaxValue; // Initialize to a very high value
sessionStartTime = new TimeSpan(9, 25, 0);
sessionResetDone = false;
......
Onbarupdate
// Reset at the start of each new session (9:25 AM) and ensure it only happens once per day
DateTime sessionStartDate = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, sessionStartTime.Hours, sessionStartTime.Minutes, 0);
if (Time[0].TimeOfDay >= sessionStartTime && !sessionResetDone && Time[0].Date != sessionStartDate.Date)
{
Print("New session started at: " + Time[0].ToString("yyyy-MM-dd HH:mm:ss"));
highestHigh = double.MinValue; // Reset to a very low value
lowestLow = double.MaxValue; // Reset to a very high value
barsSinceNewTradingDay = 0; // Set bar count to 0 for the first bar of the session
sessionResetDone = true; // Mark the session as reset for the day
Print("First bar of session at " + Time[0].ToString("yyyy-MM-dd HH:mm:ss") + ". Resetting highestHigh to " + highestHigh + " and lowestLow to " + lowestLow);
}
else if (Time[0].TimeOfDay > sessionStartTime) // After the session has started
{
// Increment bar count after the first bar of the session has been processed
barsSinceNewTradingDay++;
Print("barsSinceNewTradingDay: " + barsSinceNewTradingDay + " at Bar time: " + Time[0].ToString("yyyy-MM-dd HH:mm:ss"));
// Calculate highestHigh and lowestLow for the first 3 bars of the session
if (barsSinceNewTradingDay <= 4)
{
highestHigh = Math.Max(highestHigh, High[0]);
lowestLow = Math.Min(lowestLow, Low[0]);
Print("Bar time: " + Time[0].ToString("yyyy-MM-dd HH:mm:ss") + ", Updated highestHigh: " + highestHigh + ", Updated lowestLow: " + lowestLow);
}
}
// Reset the flag at the end of the day or when the session ends
if (Time[0].Date != sessionStartDate.Date)
{
sessionResetDone = false; // Allow reset for the next day
Print("Session reset flag cleared at " + Time[0].ToString("yyyy-MM-dd HH:mm:ss"));
}
-----
The bold section above never gets acknowledged in the debug print attached . You will also notice that the bar count and highest high do not get updated at the start of a new day probably because the new day never gets recognized. How do i solve this please? I've tried it with Bars.IsFirstBarOfSession method also and i stll can't get it to work. I just need the openinig rage to update every day so that the breakout is calculated accordingly. please advise.
Thank you.
Tedla

Comment