vertical_time_lines

Download script
Author: Copyright 2014, MetaQuotes Software Corp.
vertical_time_lines

This script is designed for the MetaTrader 4 platform to automate the creation of vertical time lines on a trading chart. It serves as a visual navigation and planning tool, allowing traders to clearly identify specific times of the day across a long history of market data.

Core Functionality

Automated Historical Plotting The primary purpose of this script is to automatically draw up to six distinct vertical lines at specific clock times (e.g., 08:00, 09:00, etc.) for every trading day within a user-defined historical period. Instead of a trader manually placing hundreds of lines, the script scans back through history and populates the chart instantly based on your settings.

Customizable Visual Parameters The script provides a high level of personalization to ensure the chart remains clean and readable. Users can independently configure:

  • Visibility: Toggle each of the six lines on or off.
  • Styling: Choose unique colors for different time slots (e.g., blue for market opens, orange for mid-day shifts).
  • Appearance: Adjust the thickness (width) and style (solid, dashed, dotted, etc.) of each individual line.

How the Logic Works

Initialization and Cleanup When you first load the script onto a chart, it performs an "audit" of the workspace. It clears any existing lines created by previous sessions to ensure a clean slate. It then generates a batch of objects based on the number of "Historical Days" specified in the settings. When the script is removed or the chart is closed, it automatically cleans up these lines to prevent cluttering your trading platform.

Time Calculation Engine The script includes a specialized logic component that handles calendar complexities. To ensure lines appear correctly, the code identifies specific dates and skips over non-trading days (such as weekends). It intelligently calculates the previous trading day even when jumping across months or years, ensuring that the vertical lines align perfectly with the historical price action of every valid trading session.

Dynamic Data Positioning While the lines are primarily vertical markers for time, the script contains logic to calculate the high and low price points during specific intervals. This ensures that as the script iterates through your history, it correctly maps the visual markers to the corresponding candles on your current chart view.

Practical Use Case

This tool is ideal for traders who rely on "session-based" trading strategies. By marking consistent times (like the London Open, New York session starts, or specific news releases) across hundreds of days of history, a trader can quickly visualize how price behavior changes at those specific moments across different weeks and months.

11 Views
0 Downloads
0 Favorites
vertical_time_lines
//+------------------------------------------------------------------+
//|                                          Vertical Time Lines.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com 
//|
//| Author: File45
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window


enum LW
{
   One = 1,
   Two = 2,
   Three = 3,
   Four = 4,
   Five = 5,
};   

// DEFAULT INPUTS : START
//-----------------------------------------------------
input int Historical_Days = 400; // Historical days
  
input color  Line_1_Color = DodgerBlue; // Line 1  Color 
input bool   Line_1_Visible = true;     // Line 1  Visible
input string Line_1_Time = "08:00";     // Line 1  Time
input LW     Line_1_Width = 1;          // Line 1  Width
input ENUM_LINE_STYLE Line_1_Style = 2; // Line 1  Style

input color  Line_2_Color = DodgerBlue; // Line 2  Color
input bool   Line_2_Visible = true;     // Line 2  Visible
input string Line_2_Time = "09:00";     // Line 2  Time 
input LW     Line_2_Width = 1;          // Line 2  Width
input ENUM_LINE_STYLE Line_2_Style = 2; // Line 2  Style

input color  Line_3_Color = Magenta;    // Line 3  Color
input bool   Line_3_Visible = false;    // Line 3  Visible
input string Line_3_Time = "10:00";     // Line 3  Time
input LW     Line_3_Width = 1;          // Line 3  Width
input ENUM_LINE_STYLE Line_3_Style = 2; // Line 3  Style

input color  Line_4_Color = Magenta;    // Line 4  Color
input bool   Line_4_Visible = false;    // Line 4  Visible
input string Line_4_Time = "11:00";     // Line 4  Time
input LW     Line_4_Width = 1;          // Line 4  Width
input ENUM_LINE_STYLE Line_4_Style = 2; // Line 4  Style

input color  Line_5_Color = DarkOrange; // Line 5  Color
input bool   Line_5_Visible = false;    // Line 5  Visible
input string Line_5_Time = "12:00";     // Line 5  Time
input LW     Line_5_Width = 1;          // Line 5  Width
input ENUM_LINE_STYLE Line_5_Style = 2; // Line 5  Style

input color  Line_6_Color = DarkOrange; // Line 6  Color
input bool   Line_6_Visible = false;    // Line 6  Visible 
input string Line_6_Time = "13:00";     // Line 6  Time
input LW     Line_6_Width = 1;          // Line 6  Width
input ENUM_LINE_STYLE Line_6_Style = 2; // Line 6  Style  
//----------------------------------------------------- 
// DEFAULT INPUTS : END 

string T1_End = "Time_Line_1"; 
string T2_End = "Time_Line_2"; 
string T3_End = "Time_Line_3";  
string T4_End = "Time_Line_4"; 
string T5_End = "Time_Line_5";  
string T6_End = "Time_Line_6";    
 

int OnInit()
{
   DeleteObjects();
  for (int i=0; i<Historical_Days; i++) 
  {
     CreateObjects("T1"+ IntegerToString(i), Line_1_Color);
     ObjectSet("T1"+IntegerToString(i), OBJPROP_WIDTH, Line_1_Width);
     ObjectSet("T1"+IntegerToString(i), OBJPROP_STYLE, Line_1_Style);
     
     CreateObjects("T2"+IntegerToString(i), Line_2_Color);
     ObjectSet("T2"+IntegerToString(i), OBJPROP_WIDTH, Line_2_Width);
     ObjectSet("T2"+IntegerToString(i), OBJPROP_STYLE, Line_2_Style);
     
     CreateObjects("T3"+IntegerToString(i), Line_3_Color);
     ObjectSet("T3"+IntegerToString(i), OBJPROP_WIDTH, Line_3_Width);
     ObjectSet("T3"+IntegerToString(i), OBJPROP_STYLE, Line_3_Style);
     
     CreateObjects("T4"+IntegerToString(i), Line_4_Color);
     ObjectSet("T4"+IntegerToString(i), OBJPROP_WIDTH, Line_4_Width);
     ObjectSet("T4"+IntegerToString(i), OBJPROP_STYLE, Line_4_Style);
     
     CreateObjects("T5"+IntegerToString(i), Line_5_Color);
     ObjectSet("T5"+IntegerToString(i), OBJPROP_WIDTH, Line_5_Width);
     ObjectSet("T5"+IntegerToString(i), OBJPROP_STYLE, Line_5_Style);
     
     CreateObjects("T6"+IntegerToString(i), Line_6_Color);
     ObjectSet("T6"+IntegerToString(i), OBJPROP_WIDTH, Line_6_Width);
     ObjectSet("T6"+IntegerToString(i), OBJPROP_STYLE, Line_6_Style);
  }

   return(INIT_SUCCEEDED);
}

void deinit() 
{
  DeleteObjects();
}

void CreateObjects(string no, color cl)
{
  ObjectCreate(no, OBJ_VLINE, 0, 0,0, 0,0);
 // ObjectSet(no, OBJPROP_WIDTH, Line_Width_1_2_3_4_or_5);
 // ObjectSet(no, OBJPROP_STYLE, Line_Style);
  ObjectSet(no, OBJPROP_COLOR, cl);
  ObjectSet(no, OBJPROP_BACK, false);
}

void DeleteObjects() 
{
  for (int i=0; i<Historical_Days; i++) 
  {
    ObjectDelete("T1"+IntegerToString(i));
    ObjectDelete("T2"+IntegerToString(i));
    ObjectDelete("T3"+IntegerToString(i));
    ObjectDelete("T4"+IntegerToString(i));
    ObjectDelete("T5"+IntegerToString(i));
    ObjectDelete("T6"+IntegerToString(i));
  }
}

void DrawObjects(datetime dt, string no, string tb, string te) 
{
  datetime t1, t2;
  double   p1, p2;
  int      b1, b2;

  t1=StrToTime(TimeToStr(dt, TIME_DATE)+" "+tb);
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+te);
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
  ObjectSet(no, OBJPROP_TIME1 , t1);
  ObjectSet(no, OBJPROP_PRICE1, p1);
  ObjectSet(no, OBJPROP_TIME2 , t2);
  ObjectSet(no, OBJPROP_PRICE2, p2);
}

datetime decDateTradeDay (datetime dt) 
{
  int ty=TimeYear(dt);
  int tm=TimeMonth(dt);
  int td=TimeDay(dt);
  int th=TimeHour(dt);
  int ti=TimeMinute(dt);

  td--;
  if (td==0) 
  {
    tm--;
    if (tm==0) 
    {
      ty--;
      tm=12;
    }
    if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(IntegerToString(ty)+"."+IntegerToString(tm)+"."+IntegerToString(td)+" "+IntegerToString(th)+":"+IntegerToString(ti)));
}




int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
 {

   datetime dt=CurTime();

   for (int i=0; i<Historical_Days; i++) 
   {
      if(Line_1_Visible == true)
      {
         DrawObjects(dt, "T1"+IntegerToString(i), Line_1_Time, T1_End);
      }
      if(Line_2_Visible == true)
      {
         DrawObjects(dt, "T2"+IntegerToString(i), Line_2_Time, T2_End);
      }
      if(Line_3_Visible == true)
      {
         DrawObjects(dt, "T3"+IntegerToString(i), Line_3_Time, T3_End);
      }
      if(Line_4_Visible == true)
      {
         DrawObjects(dt, "T4"+IntegerToString(i), Line_4_Time, T4_End);
      }
      if(Line_5_Visible == true)
      {
         DrawObjects(dt, "T5"+IntegerToString(i), Line_5_Time, T5_End);
      }
      if(Line_6_Visible == true)
      {
         DrawObjects(dt, "T6"+IntegerToString(i), Line_6_Time, T6_End);
      }
        
      dt=decDateTradeDay(dt);
      while (TimeDayOfWeek(dt)>5) dt=decDateTradeDay(dt);
      
   }

   return(rates_total);
}

Comments