Article-22235-Multiple-Regr.../trend.mq5
alexeynikolaev2 12d467990c new files added
2026-07-09 07:54:24 +03:00

70 lines
2.7 KiB
MQL5

//+------------------------------------------------------------------+
//| trend.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
double close[];
int n_rates = CopyClose(symbol, tf, from_t, to_t, close);
Print("*******************************************************");
PrintFormat("Number of downloaded quotes for market symbol %s: %d", symbol, n_rates);
Print("*******************************************************");
if(n_rates<2)
return;
//--- data preparation for regression
ulong n=n_rates;
vector y(n);
matrix X(n,2);
for(ulong i=0;i<n;++i)
{
y[i]=MathLog(close[i]);
X[i][0]=1.0;
X[i][1]=i+1.0;
}
//--- regression with linear trend
vector b,e;
double c;
regression(y,X,b,e,c);
//--- R-squared
PrintFormat("R-squared for linear trend: %.3f",R2(y,e));
Print("*******************************************************");
//--- Ramsey RESET test
RESET_test(y,X);
Print("*******************************************************");
//--- adding squares and cubes of bar numbers to the regressors
X.Resize(n_rates,4);
for(ulong i=0;i<(ulong)n_rates;++i)
{
X[i][2]=X[i][1]*X[i][1];
X[i][3]=X[i][2]*X[i][1];
}
//--- regression with added squares and cubes
regression(y,X,b,e,c);
//--- R-squared
PrintFormat("R-squared for linear, squared and cubed trend: %.3f",R2(y,e));
Print("*******************************************************");
//--- residuals plot
t_residuals_plot(e);
Print("*******************************************************");
//--- correlogram
correlogram(e);
Print("*******************************************************");
//--- Ljung-Box test for autocorrelation in residuals
Ljung_Box_test(e);
Print("*******************************************************");
}
//+------------------------------------------------------------------+