CalSurprise/Include/CalSurprise/CalSurpriseClock.mqh
ayantrader ayantrader f9a08674e1 Add the CalSurprise library, study script and indicators
Standardises economic calendar surprises by each indicator's own
dispersion, recovers the broker's daylight saving schedule from price so
each release is anchored to the bar that moved, and regresses the signed
move on the standardised surprise to measure the per-event response.

Includes the study script that reproduces the article's numbers and the
two indicators built on the same engine.
2026-09-20 15:05:53 +05:00
243 lines
8.3 KiB
MQL5
//+------------------------------------------------------------------+
//| CalSurpriseClock.mqh |
//| MMQ — Muhammad Minhas Qamar |
//| www.mql5.com/en/articles/24850 |
//+------------------------------------------------------------------+
#property copyright "MMQ — Muhammad Minhas Qamar"
#property link "https://www.mql5.com/en/articles/24850"
#property version "1.00"
#property strict
#include "CalSurpriseTypes.mqh"
#define CLOCK_MIN_VOTES 5 // votes before a month sets its own offset
#define CLOCK_MAX_SLOTS 64 // distinct offsets a month may see
//+------------------------------------------------------------------+
//| CCalendarClock - the broker's own clock, recovered from price. |
//| |
//| Historical stamps are normalised with today's server offset, |
//| so a release across a DST boundary misses its own bar. Every |
//| event is pooled into one twelve-month table: one event gives |
//| too few releases a month, and alignment needs no forecast. |
//+------------------------------------------------------------------+
class CCalendarClock
{
private:
int m_month[12]; // minutes to add, by month of release
int m_votes[12]; // releases that agreed with it
int m_seen[12]; // releases that voted at all
int m_global; // modal offset over the whole pool
double m_share; // fraction of all votes agreeing
bool m_ready;
string m_error;
int Vote(const string symbol,const datetime stamp,
const int scan_minutes,bool &counted) const;
public:
CCalendarClock(void);
bool Calibrate(const string symbol,const ulong &ids[],
const int scan_minutes,
const datetime from,const datetime to);
int Offset(const datetime when) const;
datetime Anchor(const datetime stamp) const;
bool Ready(void) const { return m_ready; }
int Global(void) const { return m_global; }
double Share(void) const { return m_share; }
string LastError(void) const { return m_error; }
int MonthOffset(const int m) const { return (m >= 0 && m < 12 ? m_month[m] : 0); }
int MonthVotes(const int m) const { return (m >= 0 && m < 12 ? m_votes[m] : 0); }
int MonthSeen(const int m) const { return (m >= 0 && m < 12 ? m_seen[m] : 0); }
};
//+------------------------------------------------------------------+
//| An uncalibrated clock is the identity: stamps taken on trust. |
//+------------------------------------------------------------------+
CCalendarClock::CCalendarClock(void)
{
ArrayInitialize(m_month, 0);
ArrayInitialize(m_votes, 0);
ArrayInitialize(m_seen, 0);
m_global = 0;
m_share = 0.0;
m_ready = false;
m_error = "";
}
//+------------------------------------------------------------------+
//| Minutes to add to a stamp landing in the month of when. |
//+------------------------------------------------------------------+
int CCalendarClock::Offset(const datetime when) const
{
MqlDateTime d;
TimeToStruct(when, d);
return(m_month[d.mon - 1]);
}
//+------------------------------------------------------------------+
//| The stamp moved onto the bar the market actually reacted in. |
//+------------------------------------------------------------------+
datetime CCalendarClock::Anchor(const datetime stamp) const
{
return(stamp + Offset(stamp) * 60);
}
//+------------------------------------------------------------------+
//| Offset of the largest absolute minute return around one stamp. |
//| counted stays false when no move stands out: a quiet print. |
//+------------------------------------------------------------------+
int CCalendarClock::Vote(const string symbol,const datetime stamp,
const int scan_minutes,bool &counted) const
{
counted = false;
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
if(point <= 0.0)
return(0);
datetime a = stamp - scan_minutes * 60;
datetime b = stamp + scan_minutes * 60;
double o[], c[];
datetime t[];
int n = CopyOpen(symbol, PERIOD_M1, a, b, o);
if(n <= 0 || CopyClose(symbol, PERIOD_M1, a, b, c) != n ||
CopyTime(symbol, PERIOD_M1, a, b, t) != n)
return(0);
double mv[];
ArrayResize(mv, n);
double peak = -1.0;
int arg = 0;
for(int k = 0; k < n; k++)
{
mv[k] = MathAbs(c[k] - o[k]) / point;
if(mv[k] > peak)
{
peak = mv[k];
arg = (int)((t[k] - stamp) / 60);
}
}
ArraySort(mv);
double median = mv[n / 2];
if(median <= 0.0 || peak / median < SURPRISE_DOMINANCE)
return(0);
counted = true;
return(arg);
}
//+------------------------------------------------------------------+
//| Pool every release into one twelve-month table. A month keeps |
//| its own offset only on enough DST-sized votes, else the mode. |
//+------------------------------------------------------------------+
bool CCalendarClock::Calibrate(const string symbol,const ulong &ids[],
const int scan_minutes,
const datetime from,const datetime to)
{
m_error = "";
m_ready = false;
ArrayInitialize(m_month, 0);
ArrayInitialize(m_votes, 0);
ArrayInitialize(m_seen, 0);
int offs[12][CLOCK_MAX_SLOTS], tally[12][CLOCK_MAX_SLOTS], nv[12];
ArrayInitialize(nv, 0);
ArrayInitialize(offs, 0);
ArrayInitialize(tally, 0);
for(int e = 0; e < ArraySize(ids); e++)
{
MqlCalendarValue v[];
int n = CalendarValueHistoryByEvent(ids[e], v, from, to);
for(int i = 0; i < n; i++)
{
//--- alignment needs a release that moved, not a forecast.
if(!v[i].HasActualValue())
continue;
bool counted = false;
int arg = Vote(symbol, v[i].time, scan_minutes, counted);
MqlDateTime d;
TimeToStruct(v[i].time, d);
int m = d.mon - 1;
m_seen[m]++;
if(!counted)
continue;
int slot = -1;
for(int k = 0; k < nv[m] && slot < 0; k++)
if(offs[m][k] == arg)
slot = k;
if(slot >= 0)
tally[m][slot]++;
else if(nv[m] < CLOCK_MAX_SLOTS)
{
offs[m][nv[m]] = arg;
tally[m][nv[m]] = 1;
nv[m]++;
}
}
}
//--- global mode over every month's votes.
int gOff[], gTally[], gn = 0, grand = 0;
ArrayResize(gOff, 12 * CLOCK_MAX_SLOTS);
ArrayResize(gTally, 12 * CLOCK_MAX_SLOTS);
for(int m = 0; m < 12; m++)
{
for(int k = 0; k < nv[m]; k++)
{
grand += tally[m][k];
int slot = -1;
for(int g = 0; g < gn && slot < 0; g++)
if(gOff[g] == offs[m][k])
slot = g;
if(slot >= 0)
gTally[slot] += tally[m][k];
else
{
gOff[gn] = offs[m][k];
gTally[gn] = tally[m][k];
gn++;
}
}
}
if(gn <= 0)
{
m_error = "no release dominated its window; clock not calibrated";
return(false);
}
int gBest = 0;
for(int g = 0; g < gn; g++)
if(gTally[g] > gTally[gBest])
gBest = g;
m_global = gOff[gBest];
m_share = (grand > 0 ? (double)gTally[gBest] / grand : 0.0);
for(int m = 0; m < 12; m++)
{
int best = -1;
for(int k = 0; k < nv[m]; k++)
if(best < 0 || tally[m][k] > tally[m][best])
best = k;
bool trust = (best >= 0 && tally[m][best] >= CLOCK_MIN_VOTES &&
MathAbs(offs[m][best] - m_global) <= SURPRISE_OFFSET_BAND);
m_month[m] = (trust ? offs[m][best] : m_global);
m_votes[m] = (best >= 0 ? tally[m][best] : 0);
}
m_ready = true;
return(true);
}
//+------------------------------------------------------------------+