This tool is designed to monitor the integrity of your trading data by identifying "gaps" in the price history provided by your broker. In professional trading, it is vital that the timeline of candles is continuous; if there are missing periods where data should exist but doesn't appear on your chart, it can lead to inaccurate analysis or errors in other automated systems.
How the Logic Works
The script scans through your historical price data, comparing the timestamp of each candle to the one immediately preceding it. It calculates the expected time difference based on your current chart timeframe (e.g., 1-minute, 5-minute, or hourly charts).
Key Functional Components:
- Gap Detection: The script subtracts the previous candle's time from the current candle's time. If the difference is larger than what a single standard candle should represent, the tool flags this as "Missing Data."
- Smart Weekend Handling: To prevent false alarms, the logic includes a specific rule for weekends. It recognizes that markets are closed on Saturdays and Sundays; therefore, it automatically ignores gaps of approximately 49 hours, ensuring you aren't alerted for standard weekend closures.
- Visual Feedback: When the script finds a gap, it generates a value on your chart. This allows you to see exactly where data is missing by looking at the indicator's output window.
- Automated Alerts: You can customize the sensitivity of the tool using two primary settings:
- Alert Threshold: This defines how many missing periods must occur before the script takes action.
- Alert Limit: This controls how many total notifications the script will send to prevent your workspace from being overwhelmed by repeated messages for the same data gap.
Summary of Purpose
Instead of you having to manually scroll back through weeks or months of charts to ensure your broker is providing a complete data feed, this script automates that audit. It monitors the "heartbeat" of your price feed and notifies you only when there is a significant break in the chronological flow of information.
//+---------------------------------+
//| BREW_MissingData_Sep.mq4 |
//+---------------------------------+
#property copyright "Copyright © 2010, Brewmanz"
#property link "http://www.metaquotes.net/"
//#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 3
//---- input parameters
extern int AlertIfMissingPeriodsOver=10;
extern int AlertLimit=10;
//---- display buffers
double V1Buff[];
int AlertsToGo;
//+-------------------------------------------+
//| Custom indicator initialization function |
//+-------------------------------------------+
int init()
{
AlertsToGo = AlertLimit;
//Print("Init() starting");
string short_name;
IndicatorBuffers(1);
//---- indicator line
//SetIndexStyle( 0,DRAW_LINE);
SetIndexStyle( 0,DRAW_HISTOGRAM);
SetIndexBuffer(0,V1Buff);
//---- name for DataWindow and indicator subwindow label
short_name="BREW MissingData(" + AlertIfMissingPeriodsOver + ")";
IndicatorShortName(short_name);
SetIndexLabel(0,"100+MissingIntervals");
//----
SetIndexDrawBegin(0,1);
//----
//Print("Init() ending");
return(0);
}
//+--------------------------------------+
int start()
{
//Print("Start() top");
//----
//if(Bars<=AtrPeriod) return(0);
int ix, ixMax, ixP, counted_bars=IndicatorCounted();
//Print("Bars=",Bars,",IndCnt=",IndicatorCounted());
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
if(counted_bars>0) counted_bars--;
//---- count from oldest non-counted(treating most recent counted as to-be-recounted) to 0 (most recent)
ixMax=Bars-counted_bars;
for(ix = ixMax; ix >=0; ix--)
{
//if(ix > ixMax - TrailingBarsLength)//check if enough history to work
// continue;
datetime dThis = Time[ix];
datetime dPrev = Time[ix+1];
int periodStep = (dThis - dPrev)/(Period()*60);
// wait! if over weekend, deduct 'markets closed' time (49 hours)
if(TimeDayOfWeek(dThis) < TimeDayOfWeek(dPrev))
periodStep -= (49 * 60) / Period();
int periodMissed = periodStep - 1;
// is all okay?
if(periodMissed <= 0)
{
V1Buff[ix] = 0;//EMPTY_VALUE;
continue;
}
// oops. data is missing. could be weekend ...
if(TimeDayOfWeek(dThis) < TimeDayOfWeek(dPrev))
{
if(periodMissed * Period() == 49 * 60)
{
V1Buff[ix] = 0;//EMPTY_VALUE;
continue;
}
}
Print(TimeToStr(dPrev), "-", TimeToStr(dThis), " Missing ", periodMissed, " Periods of ", Period(), " mins");
if(periodMissed > AlertIfMissingPeriodsOver)
{
if(AlertsToGo > 0)
{
AlertsToGo--;
Alert(TimeToStr(dPrev), "-", TimeToStr(dThis), " Missing ", periodMissed, " Periods of ", Period(), " mins");
}
}
V1Buff[ix] = 100 + periodMissed;
}
//---- plot any calced values
//----
//Print("Start() ret");
return(0);
}
//+------------------------------------+
Comments