LocalTimeLine
Download scriptThis script is designed for the MetaTrader trading platform to provide a visual chronological roadmap on your charts. Its primary purpose is to automatically draw vertical lines and text labels that mark specific time milestones, helping traders keep track of significant dates or hours without having to manually calculate them.
Core Functionality
The tool identifies where you are in time based on the specific timeframe of your chart (e.g., minutes, hours, days, weeks, or months). Once it detects your current timeframe, it automatically adjusts its behavior to draw the appropriate markers:
- Short-Term Timeframes (Minutes/Hours): It places markers at regular hourly intervals.
- Daily Timeframes: It marks the beginning of new months.
- Weekly and Monthly Timeframes: It marks the beginning of new years.
Key Features
- Dynamic Scaling: The script is "aware" of your chart's zoom level. If you switch from a 5-minute chart to a Daily chart, the tool automatically switches from showing hourly lines to monthly lines so that the information remains relevant and clean.
- Automated Placement: It calculates exactly where the previous time markers occurred relative to the current price data. It then draws a vertical dotted line at those exact moments.
- Visual Labels: Alongside the vertical lines, it places text labels (such as dates or times) on the chart. These can be customized by color and size to ensure they are easy to read against your trading background.
- Clean Maintenance: The script includes logic to manage these objects efficiently. When you remove the tool or change settings, it is designed to clear out the old lines and labels so they do not clutter your workspace.
User Experience
For a trader, this means you can quickly glance at a chart and instantly see "where you are" in the week, month, or year. It removes the mental effort of tracking historical dates or specific hourly boundaries, providing a constant, organized timeline across all types of financial analysis.
//+------------------------------------------------------------------+
//| LocalTimeLine.mq4 |
//| |
//| By KumaKuma |
//+------------------------------------------------------------------+
// V.1.0.0 2010.05.16
// First release
//
// V.1.0.1 2010.05.17
// Bug Fix
// Fixed not to be drawn lines on weekend and holiday.
//
// V.1.0.2 2010.05.17
// Bug Fix
// Fixed not to be drawn wrong lines on MN-frame.
//
#property copyright ""
#property link "http://gp7g73hp.seesaa.net/"
#property indicator_chart_window
//extern color LabelColor = DimGray;
//extern int LabelFontSize = 12;
extern int LineNumber = 10;
extern color LineColor = DimGray;
extern int TextAngle = 0;
extern double TextLocationByRatio = 0.05;
extern color TextColor = DimGray;
extern int TextFontSize = 8;
bool flg = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
if( LineNumber > 50 ){
LineNumber = 50;
}
switch(Period())
{
case PERIOD_M1 :
DrawLineByHour( 1, TimeLocal(), TimeCurrent() );
break;
case PERIOD_M5 :
DrawLineByHour( 6, TimeLocal(), TimeCurrent() );
break;
case PERIOD_M15 :
DrawLineByHour( 12, TimeLocal(), TimeCurrent() );
break;
case PERIOD_M30 :
DrawLineByHour( 24, TimeLocal(), TimeCurrent() );
break;
case PERIOD_H1 :
DrawLineByHour( 24 * 7, TimeLocal(), TimeCurrent() );
break;
case PERIOD_H4 :
DrawLineByHour( 24 * 14, TimeLocal(), TimeCurrent() );
break;
case PERIOD_D1 :
DrawLineByMonth( 3, TimeLocal(), TimeCurrent() );
break;
case PERIOD_W1 :
DrawLineByYear( 1, TimeLocal(), TimeCurrent() );
break;
case PERIOD_MN1 :
DrawLineByYear( 3, TimeLocal(), TimeCurrent() );
break;
}
flg = true;
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
int i;
// ObjectDelete("LocalTimeLineLabel");
for( i = 0; i < LineNumber; i++ )
{
ObjectDelete("LocalTimeLineText" + i);
}
for( i = 0; i < LineNumber; i++ )
{
ObjectDelete("LocalTimeLineLine" + i);
}
flg = false;
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
switch(Period())
{
case PERIOD_M1 :
DrawLineByHour( 1, TimeLocal(), TimeCurrent() );
break;
case PERIOD_M5 :
DrawLineByHour( 6, TimeLocal(), TimeCurrent() );
break;
case PERIOD_M15 :
DrawLineByHour( 12, TimeLocal(), TimeCurrent() );
break;
case PERIOD_M30 :
DrawLineByHour( 24, TimeLocal(), TimeCurrent() );
break;
case PERIOD_H1 :
DrawLineByHour( 24 * 7, TimeLocal(), TimeCurrent() );
break;
case PERIOD_H4 :
DrawLineByHour( 24 * 14, TimeLocal(), TimeCurrent() );
break;
case PERIOD_D1 :
DrawLineByMonth( 3, TimeLocal(), TimeCurrent() );
break;
case PERIOD_W1 :
DrawLineByYear( 1, TimeLocal(), TimeCurrent() );
break;
case PERIOD_MN1 :
DrawLineByYear( 3, TimeLocal(), TimeCurrent() );
break;
}
flg = true;
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| DrawLineByHour |
//+------------------------------------------------------------------+
void DrawLineByHour(int n, int t_local, int t_current)
{
int temp_second;
int HourDifference;
int i;
double TextPos;
TextPos = WindowPriceMin() + (WindowPriceMax() - WindowPriceMin()) * TextLocationByRatio;
HourDifference = NormalizeDouble( ( t_local - t_current ) / 60.0 / 60.0, 0 );
temp_second = t_local;
temp_second = ( temp_second / 60 / 60 / n ) * 60 * 60 * n; //This is the second for the first line
/*
if( flg == false ){
ObjectCreate("LocalTimeLineLabel", OBJ_LABEL, 0, 0, 0);
ObjectSet("LocalTimeLineLabel",OBJPROP_XDISTANCE, PosX);
ObjectSet("LocalTimeLineLabel",OBJPROP_YDISTANCE, PosY);
ObjectSet("LocalTimeLineLabel", OBJPROP_BACK, false);
ObjectSetText("LocalTimeLineLabel", " JST:" + TimeToStr( temp_second ), LabelFontSize, "Arial", LabelColor);
ObjectSet("LocalTimeLineLabel", OBJPROP_CORNER, 1);
}
*/
// for( i = 0 ; i < LineNumber ; i++ ){
i = 0;
while( i < LineNumber ){
if( iBarShift(NULL, 0, temp_second - 60 * 60 * HourDifference, true) != -1 ){
if( flg == false ){
//Creating text objects and setting properties.
ObjectCreate("LocalTimeLineText" + i, OBJ_TEXT, 0, temp_second - 60 * 60 * HourDifference, TextPos);
ObjectSetText("LocalTimeLineText" + i, TimeToStr( temp_second ) , TextFontSize, "Arial", TextColor);
//Creating line objects and setting the properties
ObjectCreate("LocalTimeLineLine" + i, OBJ_VLINE, 0, temp_second - 60 * 60 * HourDifference , 0, 0, 0);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_STYLE,STYLE_DOT);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_COLOR,LineColor);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_BACK, true);
ObjectSetText("LocalTimeLineLine" + i, TimeToStr( temp_second ), 0, "Arial", 0);
}else{
//Moving text objects and setting properties.
ObjectSet("LocalTimeLineText" + i, OBJPROP_PRICE1, TextPos);
ObjectSet("LocalTimeLineText" + i, OBJPROP_TIME1, temp_second - 60 * 60 * HourDifference);
ObjectSet("LocalTimeLineText" + i, OBJPROP_ANGLE, TextAngle);
ObjectSetText("LocalTimeLineText" + i, TimeToStr( temp_second ), 0, 0, 0);
//Moving line objects and setting the properties
ObjectSet("LocalTimeLineLine" + i, OBJPROP_TIME1, temp_second - 60 * 60 * HourDifference);
ObjectSetText("LocalTimeLineLine" + i, TimeToStr( temp_second ), 0, 0, 0);
}
i++;
}
temp_second = temp_second - 60 * 60 * n;
}
return;
}
//+------------------------------------------------------------------+
//| DrawLineByMonth |
//+------------------------------------------------------------------+
void DrawLineByMonth(int n, int t_local, int t_current){
int temp_year;
int temp_month;
int HourDifference;
int i;
string t;
double TextPos;
TextPos = WindowPriceMin() + (WindowPriceMax() - WindowPriceMin()) * TextLocationByRatio;
HourDifference = NormalizeDouble( ( t_local - t_current ) / 60.0 / 60.0, 0 );
temp_year = TimeYear(t_local);
temp_month = TimeMonth(t_local) / n * n + 1; //This is the second for the first line
/*
if( flg == false ){
ObjectCreate("LocalTimeLineLabel", OBJ_LABEL, 0, 0, 0);
ObjectSet("LocalTimeLineLabel",OBJPROP_XDISTANCE, PosX);
ObjectSet("LocalTimeLineLabel",OBJPROP_YDISTANCE, PosY);
ObjectSet("LocalTimeLineLabel", OBJPROP_BACK, false);
ObjectSetText("LocalTimeLineLabel", " JST:" + temp_year + "." + temp_month + ".1", LabelFontSize, "Arial", LabelColor);
ObjectSet("LocalTimeLineLabel", OBJPROP_CORNER, 1);
}
*/
i = 0;
// for( i = 0 ; i < LineNumber ; i++ ){
while( i < LineNumber ){
t = temp_year + "." + PadLeft( temp_month, "0", 2 ) + ".01";
if( iBarShift(NULL, 0, StrToTime(t) - 60 * 60 * HourDifference, true) != -1 ){
if( flg == false ){
//Creating text objects and setting properties.
ObjectCreate("LocalTimeLineText" + i, OBJ_TEXT, 0, StrToTime(t) - 60 * 60 * HourDifference, TextPos);
ObjectSetText("LocalTimeLineText" + i, t, TextFontSize, "Arial", TextColor);
//Creating line objects and setting the properties
ObjectCreate("LocalTimeLineLine" + i, OBJ_VLINE, 0, StrToTime(t) - 60 * 60 * HourDifference , 0, 0, 0);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_STYLE,STYLE_DOT);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_COLOR,LineColor);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_BACK, true);
ObjectSetText("LocalTimeLineLine" + i, t, 0, "Arial", 0);
}else{
//Moving text objects and setting properties.
ObjectSet("LocalTimeLineText" + i, OBJPROP_PRICE1, TextPos);
ObjectSet("LocalTimeLineText" + i, OBJPROP_TIME1, StrToTime(t) - 60 * 60 * HourDifference);
ObjectSet("LocalTimeLineText" + i, OBJPROP_ANGLE, TextAngle);
ObjectSetText("LocalTimeLineText" + i, t, 0, 0, 0);
//Moving line objects and setting the properties
ObjectSet("LocalTimeLineLine" + i, OBJPROP_TIME1, StrToTime(t) - 60 * 60 * HourDifference);
ObjectSetText("LocalTimeLineLine" + i, t, 0, 0, 0);
}
i++;
}
temp_month = temp_month - n;
if( temp_month <= 0 ){
temp_year = temp_year - 1;
temp_month = temp_month + 12;
}
}
return;
}
//+------------------------------------------------------------------+
//| DrawLineByMonth |
//+------------------------------------------------------------------+
void DrawLineByYear(int n, int t_local, int t_current){
int temp_year;
int temp_month;
int HourDifference;
int i;
string t;
double TextPos;
TextPos = WindowPriceMin() + (WindowPriceMax() - WindowPriceMin()) * TextLocationByRatio;
HourDifference = NormalizeDouble( ( t_local - t_current ) / 60.0 / 60.0, 0 );
temp_year = TimeYear(t_local); //This is the second for the first line
/*
if( flg == false ){
ObjectCreate("LocalTimeLineLabel", OBJ_LABEL, 0, 0, 0);
ObjectSet("LocalTimeLineLabel",OBJPROP_XDISTANCE, PosX);
ObjectSet("LocalTimeLineLabel",OBJPROP_YDISTANCE, PosY);
ObjectSet("LocalTimeLineLabel", OBJPROP_BACK, false);
ObjectSetText("LocalTimeLineLabel", " JST:" + temp_year + "." + temp_month + ".1", LabelFontSize, "Arial", LabelColor);
ObjectSet("LocalTimeLineLabel", OBJPROP_CORNER, 1);
}
*/
i = 0;
//for( i = 0 ; i < LineNumber ; i++ ){
while( i < LineNumber ){
t = temp_year + ".01.01";
if( StrToTime(t) - 60 * 60 * HourDifference < iTime(NULL, 0, WindowFirstVisibleBar())){
Comment(WindowFirstVisibleBar());
break;
}
if( iBarShift(NULL, 0, StrToTime(t) - 60 * 60 * HourDifference, true) != -1 ){
if( flg == false ){
//Creating text objects and setting properties.
ObjectCreate("LocalTimeLineText" + i, OBJ_TEXT, 0, StrToTime(t) - 60 * 60 * HourDifference, TextPos);
ObjectSetText("LocalTimeLineText" + i, t, TextFontSize, "Arial", TextColor);
//Creating line objects and setting the properties
ObjectCreate("LocalTimeLineLine" + i, OBJ_VLINE, 0, StrToTime(t) - 60 * 60 * HourDifference , 0, 0, 0);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_STYLE,STYLE_DOT);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_COLOR,LineColor);
ObjectSet("LocalTimeLineLine" + i, OBJPROP_BACK, true);
ObjectSetText("LocalTimeLineLine" + i, t, 0, "Arial", 0);
}else{
//Moving text objects and setting properties.
ObjectSet("LocalTimeLineText" + i, OBJPROP_PRICE1, TextPos);
ObjectSet("LocalTimeLineText" + i, OBJPROP_TIME1, StrToTime(t) - 60 * 60 * HourDifference);
ObjectSet("LocalTimeLineText" + i, OBJPROP_ANGLE, TextAngle);
ObjectSetText("LocalTimeLineText" + i, t, 0, 0, 0);
//Moving line objects and setting the properties
ObjectSet("LocalTimeLineLine" + i, OBJPROP_TIME1, StrToTime(t) - 60 * 60 * HourDifference);
ObjectSetText("LocalTimeLineLine" + i, t, 0, 0, 0);
}
i++;
}
temp_year = temp_year - n;
}
return;
}
string PadLeft(string p1, string p2, int p3 ){
string temp;
int i;
int len;
temp = p1;
len = StringLen( p1 );
for( i = 0 ; i < p3 - len; i++ ){
temp = StringConcatenate( p2, temp );
}
return(temp);
}
Comments