Article-23989-APARCH-Volati.../Taylor_Effect_Visualization.mq5

68 lines
2.8 KiB
MQL5
Raw Permalink Normal View History

2026-08-10 23:47:33 +02:00
//+------------------------------------------------------------------+
//| Taylor_Effect_Visualization.mq5 |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property script_show_inputs
#include"Arch\univariate\acf.mqh"
//--- input parameters
input datetime StartDate = D'2025.01.01'; //--- Historical capture anchor stop date
input ulong HistoryLen = 5000; //--- Total historical data bars to request
//---
ulong Max_Lags = 100;
double Powers_Start = 0.5;
double Powers_Stop = 2.0;
double Powers_Step = 0.5;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
vector prices;
//--- --- Historical Data Fetch ---
//--- Pull close prices directly into an array using native vector operations
if(!prices.CopyRates(NULL,PERIOD_CURRENT, COPY_RATES_CLOSE, StartDate, HistoryLen))
{
Print(" failed to get close prices for ", _Symbol, ". Error ", GetLastError());
return;
}
//--- --- Transform Prices to Returns ---
//--- Map closing prices to logarithmic space
prices = log(prices);
//--- Compute log returns: r_t = ln(P_t) - ln(P_{t-1})
vector returns = np::diff(prices) * 100.0;
//--- Demean the data and
vector demeaned_returns = returns - returns.Mean();
vector abs_returns = fabs(demeaned_returns);
vector powers = np::arange(Powers_Start,Powers_Stop+Powers_Step,Powers_Step);
//--- Compute the Autocorrelation at different powers of absolute returns
//--- This is the data plotted on the y-axis of the graph
vector t_series;
ACFResult acf_result;
vector acf_results[];
ArrayResize(acf_results,(int)powers.Size());
for(ulong i = 0; i<powers.Size(); ++i)
{
t_series = pow(abs_returns,powers[i]);
acf_result = acf(t_series,Max_Lags,0.05,true,true,false,true);
acf_results[i] = np::sliceVector(acf_result.acf,1);
}
//--- The data on x-axis of plot
vector lags = np::arange(Max_Lags,1.0,1.0);
//--- Plot the graph
//--- Prepare curve labels
string ylabels[];
ArrayResize(ylabels,(int)powers.Size());
for(uint i = 0; i<ylabels.Size(); ++i)
ylabels[i] = "Pow("+string(powers[i])+")";
//--- Show the graphic
np::plotxys(lags,acf_results,ylabels,"Autocorrelation Drop off","Lag","ACF",false,0,0,0,0,750,500,true,3,CURVE_LINES,30);
//---
return;
}
//+------------------------------------------------------------------+