time_in_range

Download script
Author: Alexander Sinitsyn
time_in_range

This script provides a versatile tool for traders to determine whether the current market time falls within a specific window. Essentially, it acts as a "smart gatekeeper" that allows you to automate actions based on the time of day—such as ensuring a trade only opens during high-liquidity sessions or preventing automated activities during late-night hours.

The logic is designed to be flexible, offering three different ways to input your desired time windows depending on how you prefer to organize your data:

1. Precise Hour and Minute Input

This is the most straightforward method. You can enter a specific starting hour and minute (e.g., 23:00) and a specific ending hour and minute (e.g., 02:15). The script converts these into a standardized numerical format to check if the current time sits between those two points.

2. Fractional Hour Input

This method is built for speed and convenience. Instead of typing hours and minutes separately, you can enter them as decimals (e.g., 05.50). The script interprets these decimals to establish your boundaries, making it easier to quickly define broad timeframes without needing to separate the units.

3. Single Variable Coding

This is a compact way to input time using a four-digit format (e.g., 0200 for 2:00 AM). To ensure the script remains reliable and "error-proof," this version includes internal logic that automatically corrects common data entry mistakes. For instance, if you enter a minute value over 60, the script will intelligently roll that extra time over into the hour column. It also caps the maximum time at 23:59 to maintain consistency with standard clock cycles.

Smart Cross-Day Handling

One of the most important features of this logic is how it handles "overnight" ranges. Most basic tools struggle when a time range starts on one day and ends on the next (for example, starting at 10:00 PM and ending at 3:00 AM).

This script identifies if your start time is later than your end time. If it is, the logic understands that you are looking for a range that spans across midnight. It automatically adjusts its "check" to confirm if the current time is either after the late-night start or before the early-morning finish, ensuring your trading rules remain accurate regardless of when the clock resets.

3 Views
0 Downloads
0 Favorites
time_in_range
//+------------------------------------------------------------------+
//|                                                time_in_range.mq4 |
//|                                               Alexander Sinitsyn |
//|                              https://www.mql5.com/en/users/wongv |
//+------------------------------------------------------------------+
#property library
#property copyright "Alexander Sinitsyn"
#property link      "https://www.mql5.com/en/users/wongv"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| TimeInRange function with separated hours and minutes input      |
//|                                                                  |
//| usage: if(TimeInRange(TimeCurrent(),23,00,02,15) { ... }         |
//|   checks if current time in range 23:00-02:15                    |
//+------------------------------------------------------------------+
bool TimeInRange(datetime time,int hours_from,int minutes_from,int hours_to,int minutes_to)
  {
   int range_from=hours_from*60+minutes_from;
   int range_to=hours_to*60+minutes_to;
   int ttc=(int)((time/60)%1440);

   return range_from>range_to?(ttc>=range_from || ttc<=range_to):(ttc>=range_from && ttc<=range_to);
  }
//+------------------------------------------------------------------+
//| TimeInRange function with fractional hours input                 |
//|                                                                  |
//| usage: if(TimeInRange(TimeCurrent(),05.50,09.25) { ... }         |
//|   checks if current time in range 05:30-09:15                    |
//|   ! please note that fractional part represents whole hour!      |
//+------------------------------------------------------------------+
bool TimeInRange(datetime time,double hours_from,double hours_to)
  {
   int range_from=(int)(hours_from*60);
   int range_to=(int)(hours_to*60);
   int ttc=(int)((time/60)%1440);

   return range_from>range_to?(ttc>=range_from || ttc<=range_to):(ttc>=range_from && ttc<=range_to);
  }
//+------------------------------------------------------------------+
//| TimeInRange function with time coded in single variable          |
//|                                                                  |
//| usage: if(TimeInRange(TimeCurrent(),0200,0835) { ... }           |
//|   checks if current time in range 02:00-08:35                    |
//+------------------------------------------------------------------+
bool TimeInRange(datetime time,int from,int to)
  {
   from=from>2359?2359:from;
   int hours_from=from/100;
   int minutes_from=from%100;
   hours_from+=minutes_from>=60?1:0;
   minutes_from=minutes_from>=60?0:minutes_from;
   hours_from=hours_from>=24?23:hours_from;

   to=to>2359?2359:to;
   int hours_to=to/100;
   int minutes_to=to%100;
   hours_to+=minutes_to>=60?1:0;
   minutes_to=minutes_to>=60?0:minutes_to;
   hours_to=hours_to>=24?23:hours_to;

   int range_from=hours_from*60+minutes_from;
   int range_to=hours_to*60+minutes_to;
   int ttc=(int)((time/60)%1440);

   return range_from>range_to?(ttc>=range_from || ttc<=range_to):(ttc>=range_from && ttc<=range_to);

  }
//+------------------------------------------------------------------+

Comments