Article-23435-OLS-Generaliz.../news.mq5
alexeynikolaev2 248dccf82c new files added
2026-08-12 10:37:03 +03:00

103 lines
4.1 KiB
MQL5

//+------------------------------------------------------------------+
//| news.mq5 |
//| Copyright 2000-2026, MetaQuotes Ltd. |
//| www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2026, MetaQuotes Ltd."
#property link "https://www.mql5.com"
//---
#property script_show_inputs
//---
input string symbol = "EURUSD"; // Market symbol for analysis
input datetime from_t = D'2025.10.15'; // Start of quotes analysis time interval
input datetime to_t = D'2026.04.15'; // End of quotes analysis time interval
input ENUM_TIMEFRAMES tf = PERIOD_M5; // Time frame
input string nsource = "US"; // News source
input double ntau = 3000.0; // Time normalization parameter
//---
#include "EconometricsM.mqh" // Header file with regression_Newey_West() function
//---
//+------------------------------------------------------------------+
//| to get full list events ids for country use events2file.mq5 |
//+------------------------------------------------------------------+
ulong events_ids[]= {840010007,840020001,840030006,840030015,840050014};
//---
void OnStart()
{
//--- quotes downloading
MqlRates rates[];
int n_rates = CopyRates(symbol, tf, from_t, to_t, rates);
PrintFormat("Number of downloaded quotes for symbol %s: %d", symbol, n_rates);
if(n_rates<3)
return;
//--- Data set creation
ulong n=(ulong)n_rates;
datetime ntime[];
news_time(from_t-24*60*60,to_t+24*60*60,nsource,events_ids,ntime);
if(ArraySize(ntime)<1)
{Print("No news"); return;}
Print(ArraySize(ntime)," news");
vector y(n);
matrix X=matrix::Ones(n,2);
for(ulong i=0;i<n;++i)
{
y[i]=MathLog(rates[i].high/rates[i].low);
X[i][1]=tnorm(rates[i].time-ntime[ArrayBsearch(ntime,rates[i].time)]);
}
//--- Target scaling by standaard deviation
y/=y.Std();
//--- Coefficient statistics and prognose for both methods
CoefficientStats stat[];
// Forecast for the time of news release
SPrognose prog;
prog.xnew.Resize(2);
prog.xnew[0]=1.0;
prog.xnew[1]=1.0;
//--- Newey-West method
regression_Newey_West(y,X,stat,prog);
ArrayPrint(stat);
Print("===================================");
prog.print();
Print("===================================");
//--- Standard OLS method
regression_Newey_West(y,X,stat,prog,false);
Print("===================================");
ArrayPrint(stat);
Print("===================================");
prog.print();
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| function for getting news times |
//+------------------------------------------------------------------+
void news_time(datetime from,datetime to,string country,ulong& event_ids[],datetime& ntime[])
{
ArrayResize(ntime,0);
MqlCalendarValue values[];
if(CalendarValueHistory(values,from,to,country)<=0)
{Print("CalendarValueHistory() error #",GetLastError()); return;}
ArraySort(event_ids);
ulong event_id;
int nt=0;
for(int i=0;i<ArraySize(values);++i)
{
event_id=event_ids[ArrayBsearch(event_ids,values[i].event_id)];
if(event_id!=values[i].event_id)
continue;
++nt;
ArrayResize(ntime,nt,100);
ntime[nt-1]=values[i].time;
}
if(nt>0)
ArraySort(ntime);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| function for time normalization |
//+------------------------------------------------------------------+
double tnorm(datetime t)
{
double dt=t/ntau;
return MathExp(-dt*dt);
}
//+------------------------------------------------------------------+