TimeOut
Download scriptThis script is designed as a diagnostic monitoring tool for the MetaTrader platform. Its primary purpose is to measure and display "latency"—the delay or time gap between the actual market action and the moment that data reaches your trading terminal.
Here is a breakdown of how the logic functions in non-technical terms:
Core Functionality
The script acts as a stopwatch that constantly measures the delay of incoming price updates (ticks). It monitors how many seconds pass between the server's time and your local terminal's reception of that data, providing you with real-time feedback on the quality of your connection.
Key Features and Calculations
- Real-Time Delay Tracking: The tool calculates the "luft" (delay) for every single price update received. It identifies exactly how many seconds of lag are occurring at any given moment.
- Maximum Peak Detection: The script keeps track of the highest delay recorded during your current session. It logs both the maximum number of seconds and the specific time at which that peak occurred.
- Average Performance: It maintains a running tally of every update received to calculate a cumulative average. This helps you understand your "typical" connection speed over a long period, rather than just looking at momentary spikes.
- Visual Histograms: On the chart, it generates two visual elements:
- A Histogram (bar graph) showing the individual delay of each recent update.
- A Line Graph showing the moving average of those delays.
Visual Alerts and Information Display
To make this data easy to read at a glance, the script creates a dedicated dashboard on your screen with four specific text labels:
- Last Tick: The exact timestamp of the most recent update received.
- Last TimeOut: The current delay expressed in seconds. This value changes color dynamically based on severity:
- Standard/Safe: Small delays are shown in a neutral color.
- Attention: Moderate delays trigger a yellow warning.
- Warning: Significant delays trigger a red alert.
- Max TimeOut: Displays the worst connection spike recorded, including the time it happened.
- Ave TimeOut: Displays your average connection speed and the total number of updates processed by the script since it was opened.
Summary for the User
Essentially, this tool tells you how "fast" your feed is. If you notice the values turning red or the bars on the histogram jumping significantly, it indicates that your internet connection or the broker's server is experiencing a lag spike, which could potentially affect the execution of your trades.
//+------------------------------------------------------------------+
//| TimeOut.mq4 |
//| Äîáðîæåëàòåëü, thanks to: komposter |
//| mailto:komposterius@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Äîáðîæåëàòåëü, thanks to: komposter"
#property link "mailto:komposterius@mail.ru"
#property indicator_separate_window
#property indicator_minimum -1
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Lime
string name = "TimeOut";
bool first = true;
int Max_TimeOut;
datetime Max_TimeOut_Time;
double buf0[];
double buf1[];
double SummTimeOut, Average_TimeOut;
int Counted;
// ×àñîâîé ïîÿñ ( äëÿ Ìîñêâû GMT_shift = 3 )
extern int GMT_shift = 3;
// Êîë-âî îòðèñîâûâàåìûõ áàðîâ èíäèêàòîðà
extern int DrawBars = 333;
extern color OKColor = Lime;
extern color StandartColor = White;
extern color AttentionColor = Yellow;
extern color WarningColor = Red;
int init()
{
IndicatorShortName( name );
IndicatorDigits ( 3 );
SetIndexBuffer ( 0 , buf0 );
SetIndexStyle ( 0 , DRAW_HISTOGRAM );
SetIndexLabel ( 0 , "TimeOut" );
SetIndexBuffer ( 1 , buf1 );
SetIndexStyle ( 1 , DRAW_LINE );
SetIndexLabel ( 1 , "Average TimeOut" );
SetIndexEmptyValue ( 1, 0 );
for ( int i = MathMax( Bars, DrawBars ); i >= 0; i -- )
{ buf0[i] = 0; buf1[i] = 0; }
first = true;
return(0);
}
int start()
{
if ( DrawBars > Bars && Bars > 0 ) { DrawBars = Bars - 1; }
// èíèöèàëèçàöèÿ ïåðåìåííûõ
// öâåò âûâîäèìîé èíôîðìàöèè ïðè çíà÷åíèè èíäèêàòîðà <= 2
color _color = OKColor;
// ðàçìåð øðèôòà âûâîäèìîé èíôîðìàöèè ïðè çíà÷åíèè èíäèêàòîðà <= 2
double size = 13;
//ïðè ïåðâîì çàïóñêå â îêíå èíäèêàòîðà ñîçäà¸ì "òåêñòîâûå ìåòêè", â êîòîðûå áóäóò âûâîäèòüñÿ ðåçóëüòàòû.
if ( first == true )
{
//ïîèñê îêíà èíäèêàòîðà ïî èìåíè
int window = WindowFind(name);
//åñëè îêíî íå íàéäåíî - "òåêñòîâûå ìåòêè" áóäóò ñîçäàíû â îêíå ãðàôèêà
if( window < 0) { window = 0; }
ObjectCreate( name + "_1", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_1", OBJPROP_CORNER, 0);
ObjectSet( name + "_1", OBJPROP_XDISTANCE, 0);
ObjectSet( name + "_1", OBJPROP_YDISTANCE, 20);
ObjectSetText( name + "_1", "Last tick: ", 13, "Arial", StandartColor);
ObjectCreate( name + "_2", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_2", OBJPROP_XDISTANCE, 0);
ObjectSet( name + "_2", OBJPROP_YDISTANCE, 40);
ObjectSetText( name + "_2", "Last TimeOut: ", 13, "Arial", StandartColor);
ObjectCreate( name + "_3", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_3", OBJPROP_XDISTANCE, 0);
ObjectSet( name + "_3", OBJPROP_YDISTANCE, 60);
ObjectSetText( name + "_3", "Max TimeOut: ", 13, "Arial", StandartColor);
ObjectCreate( name + "_4", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_4", OBJPROP_XDISTANCE, 0);
ObjectSet( name + "_4", OBJPROP_YDISTANCE, 80);
ObjectSetText( name + "_4", "Ave TimeOut: ", 13, "Arial", StandartColor);
ObjectCreate( name + "_1_1", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_1_1", OBJPROP_XDISTANCE, 100);
ObjectSet( name + "_1_1", OBJPROP_YDISTANCE, 20);
ObjectSetText( name + "_1_1", "", 13, "Arial", WarningColor);
ObjectCreate( name + "_2_1", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_2_1", OBJPROP_XDISTANCE, 100);
ObjectSet( name + "_2_1", OBJPROP_YDISTANCE, 40);
ObjectSetText( name + "_2_1", "", size, "Arial", _color);
ObjectCreate( name + "_3_1", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_3_1", OBJPROP_XDISTANCE, 100);
ObjectSet( name + "_3_1", OBJPROP_YDISTANCE, 60);
ObjectSetText( name + "_3_1", "", 13, "Arial", WarningColor);
ObjectCreate( name + "_4_1", OBJ_LABEL, window,0,0,0,0,0,0);
ObjectSet( name + "_4_1", OBJPROP_XDISTANCE, 100);
ObjectSet( name + "_4_1", OBJPROP_YDISTANCE, 80);
ObjectSetText( name + "_4_1", "", 13, "Arial", WarningColor);
for ( int u = Bars-1; u >= 0; u -- )
{ buf0[u] = 0; buf1[u] = 0; }
first = false;
return(0);
}
//ïðîèçâîäèì ïîäñ÷¸òû
// - çàäåðæêà êîòèðîâêè
int luft = ( LocalTime() - GMT_shift*3600 ) - ( CurTime() - 3600 );
// - âðåìÿ è çíà÷åíèå ìàêñèìàëüíîé çàäåðæêè
if ( Max_TimeOut <= luft || Max_TimeOut == 0 ) { Max_TimeOut = luft; Max_TimeOut_Time = CurTime(); }
// - îáùàÿ çàäåðæêà
SummTimeOut += luft;
// - êîë-âî òèêîâ
Counted ++;
// - ñðåäíÿÿ çàäåðæêà
Average_TimeOut = SummTimeOut / Counted;
if ( luft > 2 )
{
if ( luft < 5 )
// öâåò è ðàçìåð øðèôòà âûâîäèìîé èíôîðìàöèè ïðè çíà÷åíèè èíäèêàòîðà îò 2 äî 5
{ _color = AttentionColor; /*size = 15;*/ }
else
// öâåò è ðàçìåð øðèôòà âûâîäèìîé èíôîðìàöèè ïðè çíà÷åíèè èíäèêàòîðà >= 5
{ _color = WarningColor; /*size = 20;*/ }
}
// èçîáðàæåíèå íà ãðàôèêå - îäíà ñâå÷à ñîîòâåòñòâóåò îäíîìó òèêó
// õðàíÿòñÿ ïîñëåäíèå DrawBars òèêîâ
for ( int i = DrawBars; i >= 0; i -- )
{ buf0[i+1] = buf0[i]; buf1[i+1] = buf1[i]; }
buf0[0] = luft;
buf1[0] = Average_TimeOut;
// âûâîä èíôîðìàöèè â "òåêñòîâûå ìåòêè"
ObjectSetText( name + "_1_1", TimeToStr( CurTime(), TIME_SECONDS ), 13, "Arial", OKColor);
ObjectSetText( name + "_2_1", luft + " ñåê", size, "Arial", _color);
ObjectSetText( name + "_3_1", Max_TimeOut + " ñåê" + " ( " + TimeToStr( Max_TimeOut_Time, TIME_SECONDS ) + " )", 13, "Arial", OKColor);
ObjectSetText( name + "_4_1", DoubleToStr( Average_TimeOut, 3 ) + " ñåê" + " ( çà " + Counted + " òèê(-à, -îâ) )", 13, "Arial", OKColor);
return(0);
}
int deinit()
{
// ïðè äåèíèöèàëèçàöèè - óäàëÿåì ñîçäàííûå "òåêñòîâûå ìåòêè"
ObjectDelete ( name );
ObjectDelete ( name + "_1" );
ObjectDelete ( name + "_2" );
ObjectDelete ( name + "_3" );
ObjectDelete ( name + "_4" );
ObjectDelete ( name + "_1_1" );
ObjectDelete ( name + "_2_1" );
ObjectDelete ( name + "_3_1" );
ObjectDelete ( name + "_4_1" );
return(0);
}
Comments