Article-22235-Multiple-Regr.../days.mq5

91 lines
3.6 KiB
MQL5
Raw Permalink Normal View History

2026-07-09 07:53:33 +03:00
//+------------------------------------------------------------------+
//| days.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 = "GOOGL"; // Market symbol for analysis
input datetime from_t = D'2024.04.01'; // Start of quotes analysis time interval
input datetime to_t = D'2026.04.01'; // End of quotes analysis time interval
input ENUM_TIMEFRAMES tf = PERIOD_D1; // Time frame
//---
#include "EconometricsM.mqh"
//---
void OnStart()
{
//--- quotes downloading
MqlRates rates[];
int n_rates = CopyRates(symbol, tf, from_t, to_t, rates);
PrintFormat("Number of downloaded quotes for market symbol %s: %d", symbol, n_rates);
Print("*******************************************************");
if(n_rates < 2)
return;
//--- y and X calculation
ulong n=n_rates;
vector y(n);
matrix X=matrix::Full(n,5,0.0);
MqlDateTime ts;
ulong dow;
for(ulong i = 0; i < n; ++i)
{
y[i]=MathLog(rates[i].close/rates[i].open);
X[i][0]=1.0;
TimeToStruct(rates[i].time,ts);
dow=ts.day_of_week;
if(dow>0&&dow<5)
X[i][dow]=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("*******************************************************");
//--- Cook distance
vector D;
double bound=4.0/n;
Cook_dist(y,X,D);
PrintFormat("Cook distance max: %.4f, threshold value: %.4f, their ratio: %.2f",D.Max(),bound,D.Max()/bound);
Print("*******************************************************");
//--- output of outliers in the data (rows with high Cook's distance)
Print("outliers in the data (time, return, regressors):");
double ratio_max=5.0;
for(ulong i=0;i<n;++i)
if(D[i]>ratio_max*bound)
Print(rates[i].time,", ",(MathExp(y[i])-1.0)*100.0,"%, ",X.Row(i));
Print("*******************************************************");
}
//+------------------------------------------------------------------+