cctimex
Download scriptThis script is designed for MetaTrader 4 users to provide a clear, real-time visual reference of the current server time and upcoming candle expiration times across multiple timeframes simultaneously. Instead of requiring the user to manually calculate when the next hourly, daily, or minute candle will switch, the tool automates this tracking and displays it directly on the trading chart.
Core Functionality
1. Dynamic Timeframe Tracking The script allows users to define a specific list of timeframes (such as 1-minute, 15-minute, 1-hour, or Daily) via an input string. It automatically parses this list and identifies the correct intervals for each. If no custom list is provided, it defaults to the timeframe currently active on your chart.
2. Server Time Synchronization One of the most critical features is the calculation of the "Server Time Offset." Because a trader's local computer time often differs from the broker's server time, the script automatically calculates the difference between the two. It uses this offset to ensure that the times displayed on your screen perfectly match the market data provided by your broker.
3. Live Countdown and Labeling The script populates the chart with a series of organized labels:
- Current Server Time: Displays the exact current time of the trading server in a prominent format.
- Timeframe Labels: Identifies each timeframe from your custom list (e.g., converting "60" to "H1" or "1440" to "D1").
- Next Candle Expiration: For every timeframe selected, the script calculates the exact second the next candle will begin. It displays this countdown value in real-time, allowing traders to see exactly how much time remains before a new period starts.
4. Automated Layout Management To keep the trading workspace clean, the script handles all the visual positioning automatically. It aligns the labels vertically and horizontally so they do not overlap with price action. It also includes a "cleanup" routine, ensuring that when you remove the script from your chart, all generated labels are automatically deleted to leave your workspace tidy.
How it benefits the user
By consolidating all relevant "time remaining" data into one visual area, this tool eliminates the need to constantly check the market watcher or manually subtract minutes from the clock. It provides a comprehensive overview of the trading environment, making it easier to manage entries and exits near candle closes.
//+------------------------------------------------------------------+
//| CCTimeX.mq4 |
//| Copyright © 2015, Barmaley |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
//---
#property indicator_chart_window
//---
int ts=12;
int PP;
int X0;
int Y0;
//---
string PPS;
int TFCount=0;
int TFSX[50];
//---
int ServerTimeOffset=0;// Ñìåùåíèå âðåìåíè ñåðâåðà îòíîñèòåëüíî ëîêàëüíîãî âðåìåíè êîìïüþòåðà
extern string TFS=" 1 5 15 20 30 60 240 480 1440 ";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
string s="ServerTime";
//---
ObjectCreate(s,OBJ_LABEL,0,0,0,0,0,0,0);
ObjectSetText(s,"");
X0=ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)/2;
Y0=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS)/9;
ObjectSet(s,OBJPROP_XDISTANCE,X0);
ObjectSet(s,OBJPROP_YDISTANCE,Y0);
//---
s="";
int i=0;
int n=StringLen(TFS);
//--- Óäàëåíèå ëèøíèõ ñèìâîëîâ èç ñïèñêà òàéìôðåéìîâ, îñòàþòñÿ òîëüêî öèôðû è ïðîáåëû
for(i=0;i<=n-1;i++)
{
int k=StringGetCharacter(TFS,i);
string ss=StringSubstr(TFS,i,1);
if(ss==" " || (k>=48 && k<=57)) s=s+ss;
}
//---
TFS=s+" "; s="";
//--- Îáðàáîòêà ñïèñêà òàéìôðåéìîâ
while(true)
{
i=StringFind(TFS," ");
if(i<0) break;
//---
n=StringLen(TFS);
if(i==0) TFS=StringSubstr(TFS,1,n-1);
else
{
ss=StringSubstr(TFS,0,i+1);
k=(int)ss;
if(k<=1440)
if((k<=60 && MathMod(60,k)==0) || (k>60 && MathMod(1440,k)==0))// Ïðîâåðêà ÒÔ íà êîððåêòíîñòü
{
TFSX[TFCount]=(int)ss;
TFCount++;
}
//---
TFS=StringSubstr(TFS,i,1000);
}
}
//---
if(TFCount==0) {TFCount++; TFSX[0]=_Period;} // Ïóñòîé ñïèñîê = òåêóùèé òàéìôðåéì
//---
for(i=0;i<=TFCount-1;i++)
{
s="ServerTime"+(string)i;
ObjectCreate(s,OBJ_LABEL,0,0,0,0,0,0,0);
ObjectSetText(s,"");
s="ServerTime"+(string)i+(string)i;
ObjectCreate(s,OBJ_LABEL,0,0,0,0,0,0,0);
ObjectSetText(s,"");
}
//---
EventSetTimer(1);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
string s="ServerTime";
//---
if( reason==REASON_CHARTCHANGE || reason==REASON_PARAMETERS) return;
//---
ObjectDelete(s);
//---
for(int i=0;i<=TFCount-1;i++)
{
s="ServerTime"+(string)i+(string)i;
ObjectDelete(s);
//---
s="ServerTime"+(string)i;
ObjectDelete(s);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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[])
{
//--- Ñìåùåíèå âðåìåíè ñåðâåðà îòíîñèòåëüíî ëîêàëüíîãî âðåìåíè êîìïüþòåðà
if(MarketInfo(_Symbol,MODE_TRADEALLOWED)>0) ServerTimeOffset=TimeCurrent()-TimeLocal();
//---
return(rates_total);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- Ïîëîæåíèå ñèíèõ ÷àñîâ
int x=ObjectGet("ServerTime",OBJPROP_XDISTANCE);
int y=ObjectGet("ServerTime",OBJPROP_YDISTANCE);
ts=ObjectGet("ServerTime",OBJPROP_FONTSIZE);
//---
datetime tt=TimeLocal()+ServerTimeOffset;
//--- Ñåðâåðíîå âðåìÿ
ObjectSetText("ServerTime",TimeToStr(tt,TIME_SECONDS),ts,"Courier New",clrRoyalBlue);
//--- Òàéìåðû áàðîâ
for(int i=0;i<=TFCount-1;i++)
{
PP=TFSX[i];
//---
PPS="M"+(string)PP;
if(PP==1440) PPS="D1";
else if(PP>=60) PPS="H"+(string)(PP/60);
//---
int n=tt/PP/60;
datetime it=n*PP*60;
datetime t=PP*60-tt+it;
//---
string s="ServerTime"+(string)i+(string)i;
ObjectSetText(s,TimeToStr(t,TIME_SECONDS),ts,"Courier New",clrRed);
ObjectSet(s,OBJPROP_XDISTANCE,x);
ObjectSet(s,OBJPROP_YDISTANCE,y+(ts+5)*(i+1));
//---
s="ServerTime"+(string)i;
ObjectSetText(s,PPS,ts,"Courier New",clrRed);
ObjectSet(s,OBJPROP_XDISTANCE,x-ts*5);
ObjectSet(s,OBJPROP_YDISTANCE,y+(ts+5)*(i+1));
}
}
//+------------------------------------------------------------------+
Comments