132 líneas
5,5 KiB
MQL5
132 líneas
5,5 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| TestScalingLambdaMaxSymbol.mq5 |
|
|
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
|
//| http://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2000-2026, MetaQuotes Ltd."
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
#property script_show_inputs
|
|
//--- input parameters
|
|
input string WorkSymbol = "EURUSD"; // Symbol
|
|
input int YearStart = 2024;
|
|
input int YearEnd = 2025;
|
|
#include <Graphics\Graphic.mqh>
|
|
//+------------------------------------------------------------------+
|
|
//| GetHistoricalData |
|
|
//+------------------------------------------------------------------+
|
|
bool GetHistoricalData(double &data[], string symbol, ENUM_TIMEFRAMES tf, int year_start, int year_end)
|
|
{
|
|
datetime from = StringToTime(IntegerToString(year_start) + ".01.01 00:00");
|
|
datetime to = StringToTime(IntegerToString(year_end) + ".12.31 23:59");
|
|
int copied = CopyClose(symbol, tf, from, to, data);
|
|
if(copied <= 0)
|
|
{
|
|
Print("Error in CopyClose: ", GetLastError());
|
|
ArrayResize(data, 0);
|
|
return false;
|
|
}
|
|
//PrintFormat("Loaded bars: %d (%s %s)", ArraySize(data), symbol, EnumToString(tf));
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| LinearRegression |
|
|
//+------------------------------------------------------------------+
|
|
void LinearRegression(const double &x[], const double &y[], int n, double &a, double &b)
|
|
{
|
|
double sx = 0, sy = 0, sxx = 0, sxy = 0;
|
|
for(int i = 0; i < n; i++)
|
|
{
|
|
sx += x[i];
|
|
sy += y[i];
|
|
sxx += x[i] * x[i];
|
|
sxy += x[i] * y[i];
|
|
}
|
|
double denom = n * sxx - sx * sx;
|
|
a = (n * sxy - sx * sy) / denom;
|
|
b = (sy - a * sx) / n;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Scaling test for one timeframe |
|
|
//+------------------------------------------------------------------+
|
|
bool TestScalingLambaMaxTF(string symbol, ENUM_TIMEFRAMES tf, double &logT_out[], double &logLambda_out[], double &alpha_out)
|
|
{
|
|
MathSrand(42);
|
|
double prices[];
|
|
if(!GetHistoricalData(prices, symbol, tf, YearStart, YearEnd))
|
|
return false;
|
|
int Tvals[];
|
|
int nT=8;
|
|
int T0=64;
|
|
ArrayResize(Tvals, nT);
|
|
for(int i = 0; i < nT; i++)
|
|
Tvals[i] = T0 << i;
|
|
ArrayResize(logT_out, nT);
|
|
ArrayResize(logLambda_out, nT);
|
|
int data_size = ArraySize(prices);
|
|
vector<double> data_prices;
|
|
for(int i = 0; i < nT; i++)
|
|
{
|
|
int T = Tvals[i];
|
|
int MC = 1000;
|
|
double lambda_sum = 0.0;
|
|
for(int k = 0; k < MC; k++)
|
|
{
|
|
if(data_size < T)
|
|
break;
|
|
int start = MathRand() % (data_size - T);
|
|
data_prices.Resize(T);
|
|
for(int j=0; j<T; j++)
|
|
data_prices[j]=prices[start+j];
|
|
lambda_sum += data_prices.L1TrendFilterLambdaMax();
|
|
}
|
|
double lambda_avg = lambda_sum / MC;
|
|
logT_out[i] = MathLog((double)T);
|
|
logLambda_out[i] = MathLog(lambda_avg);
|
|
//PrintFormat("TF=%s T=%5d <lambda_max>=%.6f", EnumToString(tf), T, lambda_avg);
|
|
}
|
|
double c;
|
|
LinearRegression(logT_out, logLambda_out, nT, alpha_out, c);
|
|
PrintFormat("%s (%s) estimated scaling exponent = %.4f", symbol,EnumToString(tf), alpha_out);
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| TestScalingLambdaMaxSymbol |
|
|
//+------------------------------------------------------------------+
|
|
void TestScalingLambdaMaxSymbol(string symbol)
|
|
{
|
|
ENUM_TIMEFRAMES timeframes[] = {PERIOD_M1, PERIOD_M2, PERIOD_M3, PERIOD_M4, PERIOD_M5, PERIOD_M6,
|
|
PERIOD_M10, PERIOD_M12, PERIOD_M15, PERIOD_M20, PERIOD_M30, PERIOD_H1
|
|
};
|
|
uint colors[] = {clrRed,clrBlue,clrGreen,clrOrange,clrPurple,clrDarkGreen,clrCyan,
|
|
clrNavy,clrOrangeRed,clrDodgerBlue,clrCrimson,clrDarkRed
|
|
};
|
|
//---
|
|
CGraphic g;
|
|
g.Create(0, "ScalingLawTest", 0, 0, 0, 1000, 600);
|
|
g.BackgroundMain("Scaling law of lambda_max (" + symbol + ")");
|
|
g.BackgroundMainSize(16);
|
|
PrintFormat("%s scaling test for standard timeframes",symbol);
|
|
for(int i = 0; i < ArraySize(timeframes); i++)
|
|
{
|
|
double logT[], logLambda[], alpha;
|
|
// Print("processing timeframe: ", EnumToString(timeframes[i]), " -----");
|
|
if(TestScalingLambaMaxTF(symbol, timeframes[i], logT, logLambda, alpha))
|
|
{
|
|
g.CurveAdd(logT, logLambda, ColorToARGB(colors[i % ArraySize(colors)],255), CURVE_POINTS_AND_LINES, EnumToString(timeframes[i]));
|
|
}
|
|
}
|
|
g.CurvePlotAll();
|
|
g.Update();
|
|
//---
|
|
DebugBreak();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start function |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
//--- estimate lambda_max scale exponent for price data
|
|
TestScalingLambdaMaxSymbol(WorkSymbol);
|
|
}
|
|
//+------------------------------------------------------------------+
|