Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

time of day not resetting properly to calculate OR

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    time of day not resetting properly to calculate OR

    I'm trying to calculate the high and low of 15 opening range of each session to include in my breakout strategy but I cannot get it to reset every day. I have included debug prints to show me the change of day which never fires:
    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
    Attached Files

    #2
    Hello snoinvest,

    I would recommend using Bars.IsFirstBarOfSession to check if this is the first bar of a new session.

    If you need the start time of a new session (and not a new day as many sessions span two days) use a sessionIterator.


    You may find the PreventEntryAfterExitOnCloseExample linked below helpful, it resets a value on the first bar of a new session and uses a sessionIterator.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    59 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    74 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X