Article-22235-Multiple-Regr.../news.mq5
alexeynikolaev2 40b6c8da0f new files added
2026-07-09 07:54:13 +03:00

143 lines
6.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 string country1 = "US"; // first news source
input string country2 = "EU"; // second news source
input datetime from_t = D'2024.04.01'; // Start of analysis time interval
input datetime to_t = D'2026.04.01'; // End of analysis time interval
//---
#include "EconometricsM.mqh"
//---
const datetime SECONDS_IN_DAY=24*60*60;
//---
//+------------------------------------------------------------------+
//| to get full list events ids for country use events2file.mq5 |
//+------------------------------------------------------------------+
ulong events_ids1[]= {840010007,840020001,840030006,840030015,840050014};
ulong events_ids2[]= {999010007,999030010,999030016,999030018,999030020};
//--
void OnStart()
{
//--- quotes downloading
MqlRates rates[];
int n_rates = CopyRates(symbol, PERIOD_D1, from_t, to_t, rates);
PrintFormat("Number of downloaded quotes for market symbol %s: %d", symbol, n_rates);
if(n_rates < 2)
return;
//--- news days downloading
datetime days1[],days2[];
news_days(from_t,to_t,country1,events_ids1,days1);
news_days(from_t,to_t,country2,events_ids2,days2);
PrintFormat("Number of days with news for region %s: %d",country1,ArraySize(days1));
PrintFormat("Number of days with news for region %s: %d",country2,ArraySize(days2));
Print("*******************************************************");
//--- y and X calculation
ulong n=n_rates;
vector y(n);
datetime t;
int it;
matrix X=matrix::Full(n,3,0.0);
for(ulong i=0;i<n;++i)
{
y[i]=MathLog(rates[i].high/rates[i].low);
X[i][0]=1.0;
t=rates[i].time;
it=ArrayBsearch(days1,t);
if(t==days1[it])
X[i][1]=1.0;
it=ArrayBsearch(days2,t);
if(t==days2[it])
X[i][2]=1.0;
}
//--- regression
vector b,e;
double c;
matrix XX;
regression(y,X,b,e,c,XX);
//--- R-squared
PrintFormat("R-squared: %.3f",R2(y,e));
PrintFormat("R-squared adjusted: %.3f",R2_adj(y,e,X.Cols()));
Print("*******************************************************");
//--- residuals plot
t_residuals_plot(e);
Print("*******************************************************");
//--- correlogram
correlogram(e);
Print("*******************************************************");
//--- Ljung-Box test for autocorrelation in residuals
Ljung_Box_test(e);
Print("*******************************************************");
//--- EPDF of residuals vs. normal density
epdf_vs_normalpdf(e);
Print("*******************************************************");
//--- QQ-plot of residuals vs. normal distribution
qq_plot(e);
Print("*******************************************************");
//--- Jarque-Bera test for normality
Jarque_Bera_test(e);
Print("*******************************************************");
//--- full F-test for all coefficients together
F_test_all(y,X);
Print("*******************************************************");
//--- statistics on individual coefficients
CoefficientStats cs[];
parameter_stat(XX,b,c,n,cs);
ArrayPrint(cs,5);
Print("*******************************************************");
//--- prognoses
SPrognose prgns;
double conf_level=0.9;
//--- prognose for day with news just from US: X1=1, X2=0
vector xnew= {1,1,0};
prgns.xnew=xnew;
prognose(XX,b,c,n,prgns,conf_level);
PrintFormat("volatility prognose for just US news in percentages for confidence level: %.2f",conf_level);
PrintFormat("point prognose: %.2f%%",(MathExp(prgns.point_progn)-1.0)*100.0);
PrintFormat("confidence interval: from %.2f%% to %.2f%%",(MathExp(prgns.conf_low)-1.0)*100.0,(MathExp(prgns.conf_high)-1.0)*100.0);
PrintFormat("prognose interval: from %.2f%% to %.2f%%",(MathExp(prgns.progn_low)-1.0)*100.0,(MathExp(prgns.progn_high)-1.0)*100.0);
Print("*******************************************************");
//--- prognose for day with no news: X1=0, X2=0
prgns.xnew[1]=0.0;
prognose(XX,b,c,n,prgns,conf_level);
PrintFormat("volatility prognose for NO news in percentages for confidence level: %.2f",conf_level);
PrintFormat("point prognose: %.2f%%",(MathExp(prgns.point_progn)-1.0)*100.0);
PrintFormat("confidence interval: from %.2f%% to %.2f%%",(MathExp(prgns.conf_low)-1.0)*100.0,(MathExp(prgns.conf_high)-1.0)*100.0);
PrintFormat("prognose interval: from %.2f%% to %.2f%%",(MathExp(prgns.progn_low)-1.0)*100.0,(MathExp(prgns.progn_high)-1.0)*100.0);
Print("*******************************************************");
}
//+------------------------------------------------------------------+
//| function for getting days with news |
//+------------------------------------------------------------------+
void news_days(datetime from,datetime to,string country,ulong& event_ids[],datetime& days[])
{
ArrayResize(days,0);
MqlCalendarValue values[];
if(CalendarValueHistory(values,from,to,country)<=0)
{Print("CalendarValueHistory() error #",GetLastError()); return;}
ArraySort(event_ids);
ulong event_id;
datetime t0;
int ndays=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;
t0=values[i].time;
t0-=t0%SECONDS_IN_DAY;
++ndays;
ArrayResize(days,ndays,100);
days[ndays-1]=t0;
}
if(ndays>0)
ArraySort(days);
}
//+------------------------------------------------------------------+