Time Filter Roll over to next day Question

 
i wanted to check my code below with the community if i did it correctly so that if a user sets in the input variables 

\\ Global input section at the top of my code. 

input int TimeStartHour = 21;
input int TimeStartMin = 0;
input int TimeEndHour = 9;
input int TimeEndMin = 0;



that the time it actually will start will be 21:00 and then go through midnight and the time it will end will be the next day at. 9am .  (SERVER TIME )



void OnTick(){
    MqlDateTime structTime;
    TimeCurrent(structTime);

    // Set the start time
    structTime.hour = TimeStartHour;
    structTime.min = TimeStartMin;
    datetime timeStart = StructToTime(structTime);

    // Set the end time
    structTime.hour = TimeEndHour;
    structTime.min = TimeEndMin;
    datetime timeEnd = StructToTime(structTime);

    // Check if the current time is within the specified range
    bool isTime = TimeCurrent() >= timeStart && TimeCurrent() < timeEnd;

    // Check if it's past midnight
    if (structTime.hour >= TimeEndHour && structTime.min >= TimeEndMin) {
        // Increment the day by 1
        structTime.day++;

        // Set the start time to the next day
        structTime.hour = TimeStartHour;
        structTime.min = TimeStartMin;
        timeStart = StructToTime(structTime);

        // Set the end time to the next day
        structTime.hour = TimeEndHour;
        structTime.min = TimeEndMin;
        timeEnd = StructToTime(structTime);
    }

    // Check if the current time is within the specified range on the next day
    if (isTime == false && TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) {
        isTime = true;
    }




   // Execute the desired code if the current time is within the specified range
    if (isTime) {
        // Your code here.   Buy or Sell order using posTick or CTrade 
    }
}


and to check if its outputting correctly i do have a  Print comment statement here is the clip of it below. but when i set it to the example variables i am talking about above. as the start time being 21 and then end time being 9 

i notice. that on the print variable it is showing the same day and not rolling over to the next day.  

Comment ("\nServer Time: ",TimeCurrent(),
            "\nServer Filter Time Start:  ",timeStart,
            "\nServer Filter Time End:    ",timeEnd,
            "\nTrading Activate:  ",isTime);

should the updated code look like the following below using day_of_week from StructTime


void OnTick(){
    MqlDateTime structTime;
    TimeCurrent(structTime);

    // Set the start time
    structTime.hour = TimeStartHour;
    structTime.min = TimeStartMin;
    // set the start day to Monday
    structTime.day_of_week = 1;
    datetime timeStart = StructToTime(structTime);

    // Set the end time
    structTime.hour = TimeEndHour;
    structTime.min = TimeEndMin;
    // set the end day to Friday
    structTime.day_of_week = 5;
    datetime timeEnd = StructToTime(structTime);

    // Check if the current time is within the specified range
    bool isTime = TimeCurrent() >= timeStart && TimeCurrent() < timeEnd;

    // Check if it's past midnight
    if (structTime.hour >= TimeEndHour && structTime.min >= TimeEndMin) {
        // Increment the day by 1
        structTime.day++;

        // Set the start time to the next day
        structTime.hour = TimeStartHour;
        structTime.min = TimeStartMin;
        // set the next day's start day to Monday
        structTime.day_of_week = 1;
        timeStart = StructToTime(structTime);

        // Set the end time to the next day
        structTime.hour = TimeEndHour;
        structTime.min = TimeEndMin;
        // set the next day's end day to Friday
        structTime.day_of_week = 5;
        timeEnd = StructToTime(structTime);
    }

    // Check if the current time is within the specified range on the next day
    if (isTime == false && TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) {
        isTime = true;
    }
}
 
how to fix the issue 

void OnTick(){
    MqlDateTime structTime;
    TimeCurrent(structTime);

    // Set the start time
    structTime.hour = TimeStartHour;
    structTime.min = TimeStartMin;
    datetime timeStart = StructToTime(structTime);

    // Set the end time
    structTime.hour = TimeEndHour;
    structTime.min = TimeEndMin;
    datetime timeEnd = StructToTime(structTime);

    // Check if TimeEndHour is on the next day
    if (TimeEndHour < TimeStartHour) {
        structTime.day++;
        timeEnd = StructToTime(structTime);
    }

    // Check if the current time is within the specified range
    bool isTime = TimeCurrent() >= timeStart && TimeCurrent() < timeEnd;

    // Check if it's past midnight
    if (TimeCurrent() >= timeEnd && TimeCurrent() < timeStart) {
        // Increment the day by 1
        structTime.day++;

        // Set the start time to the next day
        structTime.hour = TimeStartHour;
        structTime.min = TimeStartMin;
        timeStart = StructToTime(structTime);

        // Set the end time to the next day
        structTime.hour = TimeEndHour;
        structTime.min = TimeEndMin;
        timeEnd = StructToTime(structTime);
    }

    // Check if the current time is within the specified range on the next day
    if (isTime == false && TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) {
        isTime = true;
    }

    // Execute the desired code if the current time is within the specified range
    if (isTime) {
        // Your code here
    }
}
Code in yellow is what was missing now  when it checks the time it knows to move it to the next day 
code in Green is what goes near your Order Information.    

i am posting this incase anyone else wants to add  a Time Filter  to their EA in MQL5
 
The issue I am having with now is the midnight function doesn’t work. When midnight happens it changes the start time even if I remove the section of code where it says to change the start time. 
 
Ricks creations #: The issue I am having with now is the midnight function doesn’t work. When midnight happens it changes the start time even if I remove the section of code where it says to change the start time. 

Your issue is that you are converting clock time to datetime for comparison with TimeCurrent(), thus you have to handle midnight. Just use clock time.
          Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)

Not tested, not compiled, just typed.

void OnTick(){
    int now   = time();
    int start = TimeStartHour * 3600 + TimeStartMin *60;
    int end   = TimeEndHour   * 3600 + TimeEndtMin  *60;
    bool isValidTime = start < end ? start <= now && now < end : start <= now || now < end;

Not tested, not compiled, just typed.

Reason: