| //+------------------------------------------------------------------+
|
| //| SurpriseLab.mq5 |
|
| //| 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
|
| #property script_show_inputs
|
|
|
| #include <CalSurprise/CalSurpriseCore.mqh>
|
|
|
| input ENUM_SURPRISE_STD InpMode = SURPRISE_STD_ROBUST; // standardiser
|
| input int InpScan = 240; // calibration scan, minutes
|
| input bool InpAnchor = true; // false leaves raw calendar stamps
|
| input string InpSymbols = "EURUSD,GBPUSD,USDJPY,XAUUSD";
|
| input string InpHorizons = "1,5,15,60"; // minutes
|
| input string InpReport = "surprise_lab.csv";
|
|
|
| //--- the US releases that carry both actual and forecast since 2017.
|
| ulong EventIds[] =
|
| {
|
| 840030016, // Nonfarm Payrolls
|
| 840030015, // Unemployment Rate
|
| 840030005, // CPI m/m
|
| 840030006, // Core CPI m/m
|
| 840010001, // Core PCE Price Index m/m
|
| 840020010, // Retail Sales m/m
|
| 840020011, // Core Retail Sales m/m
|
| 840040001, // ISM Manufacturing PMI
|
| 840040003, // ISM Non-Manufacturing PMI
|
| 840190001, // ADP Nonfarm Employment Change
|
| 840140001 // Initial Jobless Claims
|
| };
|
|
|
| //+------------------------------------------------------------------+
|
| //| Append one line to the open report handle. |
|
| //+------------------------------------------------------------------+
|
| void Emit(const int h,const string s)
|
| {
|
| FileWriteString(h, s + "\r\n");
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| Measure every event against every symbol and horizon. The clock |
|
| //| is calibrated once: the offset belongs to the calendar. |
|
| //+------------------------------------------------------------------+
|
| void OnStart()
|
| {
|
| int h = FileOpen(InpReport, FILE_WRITE | FILE_TXT | FILE_ANSI | FILE_COMMON);
|
| if(h == INVALID_HANDLE)
|
| {
|
| Print("cannot open report: ", GetLastError());
|
| return;
|
| }
|
|
|
| if(!CCalendarSurprise::WaitForCalendar(60000))
|
| {
|
| Emit(h, "FATAL,calendar did not settle within 60s");
|
| FileClose(h);
|
| return;
|
| }
|
|
|
| string syms[], hors[];
|
| int nsym = StringSplit(InpSymbols, ',', syms);
|
| int nhor = StringSplit(InpHorizons, ',', hors);
|
| for(int s = 0; s < nsym; s++)
|
| SymbolSelect(syms[s], true);
|
|
|
| string modes[3] = {"full", "expanding", "robust"};
|
| Emit(h, StringFormat("standardiser,%s,scan_minutes,%d", modes[(int)InpMode], InpScan));
|
| //--- one clock for the whole run: DST belongs to the server.
|
| CCalendarClock clock;
|
| if(!clock.Calibrate(syms[0], EventIds, InpScan, D'2017.01.01', TimeCurrent()))
|
| Emit(h, "CLOCK WARNING," + clock.LastError());
|
|
|
| Emit(h, "");
|
| Emit(h, "== SERVER CLOCK ==");
|
| Emit(h, StringFormat("global_offset_min,%d,agreement,%.2f", clock.Global(), clock.Share()));
|
| Emit(h, "month,offset_min,votes,releases_seen");
|
| for(int m = 0; m < 12; m++)
|
| Emit(h, StringFormat("%d,%d,%d,%d", m + 1, clock.MonthOffset(m),
|
| clock.MonthVotes(m), clock.MonthSeen(m)));
|
|
|
| Emit(h, "");
|
| Emit(h, "== EVENTS ==");
|
| Emit(h, "id,name,currency,importance,n_records,n_usable,n_corrupt,surprise_mean,surprise_sd");
|
|
|
| SurpriseResponse rows[];
|
| string rowev[];
|
| int nrow = 0;
|
| ArrayResize(rows, ArraySize(EventIds) * nsym * nhor);
|
| ArrayResize(rowev, ArraySize(EventIds) * nsym * nhor);
|
|
|
| for(int e = 0; e < ArraySize(EventIds); e++)
|
| {
|
| CCalendarSurprise cs;
|
| cs.SetStandardiser(InpMode);
|
|
|
| if(!cs.Load(EventIds[e], D'2017.01.01', TimeCurrent()))
|
| {
|
| Emit(h, StringFormat("%I64u,LOAD FAILED,%s", EventIds[e], cs.LastError()));
|
| continue;
|
| }
|
| if(InpAnchor)
|
| cs.Anchor(clock);
|
|
|
| SurpriseEvent ev;
|
| cs.Event(ev);
|
| string nm = ev.name;
|
| StringReplace(nm, ",", ";");
|
| Emit(h, StringFormat("%I64u,%s,%s,%d,%d,%d,%d,%.4f,%.4f",
|
| ev.id, nm, ev.currency, ev.importance,
|
| ev.n_records, ev.n_usable, ev.n_corrupt,
|
| ev.surprise_mean, ev.surprise_sd));
|
|
|
| for(int s = 0; s < nsym; s++)
|
| {
|
| for(int k = 0; k < nhor; k++)
|
| {
|
| SurpriseResponse r;
|
| if(!cs.Response(syms[s], (int)StringToInteger(hors[k]), r))
|
| continue;
|
| rows[nrow] = r;
|
| rowev[nrow] = nm;
|
| nrow++;
|
| }
|
| }
|
| }
|
|
|
| Emit(h, "");
|
| Emit(h, "== RESPONSE ==");
|
| Emit(h, "event,symbol,horizon_min,n,slope_pips_per_sigma,stderr,t_stat,r2,mean_abs_move,flat");
|
| for(int i = 0; i < nrow; i++)
|
| Emit(h, StringFormat("%s,%s,%d,%d,%.3f,%.3f,%.2f,%.4f,%.2f,%s",
|
| rowev[i], rows[i].symbol, rows[i].horizon_min, rows[i].n,
|
| rows[i].slope, rows[i].stderr, rows[i].t_stat,
|
| rows[i].r2, rows[i].mean_abs_move,
|
| (rows[i].band_spans_zero ? "FLAT" : "")));
|
|
|
| FileClose(h);
|
|
|
| Print("SurpriseLab finished: ", nrow, " response rows");
|
| }
|
| //+------------------------------------------------------------------+
|