Article-23989-APARCH-Volati.../APARCH_NestingTest.mq5
2026-08-10 23:47:33 +02:00

117 lines
5.4 KiB
MQL5

//+------------------------------------------------------------------+
//| APARCH_NestingTest.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\mean.mqh"
//--- input parameters
input datetime StartDate = D'2025.01.01'; //--- Historical capture anchor start date
input ulong HistoryLen = 5000; //--- Total historical data bars to request
input double ScaleFactor = 100.; //--- Rescaling multiplier to prevent optimizer underflow
input int num_digits = 4; //--- Number of digits for comparison of volatility series
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
vector prices;
//--- --- Step 1: 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;
}
//--- --- Step 2: 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);
Print("*********** GARCH(1,1) VS APARCH(1,0,1,2.0) MODEL LOGLIKELIHOOD COMPARISON ***********");
//--- Initialize core specification fields mapping onto the aparch container
ArchParameters spec;
//--- Apply the scaling factor (multiplying by 100 scales returns to percentage form)
spec.observations = ScaleFactor * returns;
spec.vol_model_type = VOL_APARCH;
spec.garch_p = 1;
spec.garch_o = 0;
spec.garch_q = 1;
spec.aparch_delta = 2.0;
//--- Instantiate the continuous tracking zero mean container wrapper
ZeroMean aparch_model;
//--- Pass structural parameters down into the optimization initialization routine
if(!aparch_model.initialize(spec))
return;
//--- Trigger the non-linear execution optimizer loop (SLSQP engine solver)
ArchModelResult aparch_params = aparch_model.fit();
//--- Verify that the resulting parameters array size matches the model criteria configurations
if(aparch_params.solver_return_code)
{
Print("Convergence failed ", GetLastError());
return;
}
//--- Prepare output of model parameters
string pnames = aparch_model.volatility().parameterNames();
string vol_parameter_labels[];
//--- Organize parameter names into array for display
int labels = StringSplit(pnames,StringGetCharacter(",",0),vol_parameter_labels);
//---
spec.vol_model_type = VOL_GARCH;
spec.garch_p = 1;
spec.garch_o = 0;
spec.garch_q = 1;
//---
ZeroMean garch_model;
//--- Pass structural parameters down into the optimization initialization routine
if(!garch_model.initialize(spec))
return;
//--- --- Step 6: Parameter Optimization (Fitting) ---
//--- Trigger the non-linear execution optimizer loop (SLSQP engine solver)
ArchModelResult garch_params = garch_model.fit();
//--- --- Step 7: Optimization Convergence Guard ---
//--- Verify that the resulting parameters array size matches the model criteria configurations
if(garch_params.solver_return_code)
{
Print("Convergence failed ", GetLastError());
return;
}
//-- compare loglikelihood result
PrintFormat("Garch loglikelihood %.6f\nAparch loglikelihood %.6f", garch_params.loglikelihood,aparch_params.loglikelihood);
//---
PrintFormat("Conditional volatility series comparison result. (The number of mismatched elements) : %d", garch_params.conditional_volatility.CompareByDigits(aparch_params.conditional_volatility,num_digits));
//---
vector pv = aparch_params.pvalues();
//--- --- Step 8: Results Output Extraction ---
//--- Print optimal target parameter solutions to the MT5 journal
Print("Aparch(1,0,1,2.0) model parameters");
PrintFormat("%10s %10s %10s","Name","Value","Pvalue");
//--- Extract statistical asymptotic standard deviation errors mapped out to individual p-values
for(ulong i = 0; i < pv.Size(); ++i)
{
//--- Log individual calculated p-values step-by-step to evaluate structural significance
PrintFormat("%10s %10.4f %10.4f",vol_parameter_labels[i], aparch_params.params[i],pv[i]);
}
//---
pv = garch_params.pvalues();
//--- --- Step 8: Results Output Extraction ---
//--- Print optimal target parameter solutions to the MT5 journal
Print("Garch(1,1) model parameters");
PrintFormat("%10s %10s %10s","Name","Value","Pvalue");
//--- Extract statistical asymptotic standard deviation errors mapped out to individual p-values
for(ulong i = 0; i < pv.Size(); ++i)
{
//--- Log individual calculated p-values step-by-step to evaluate structural significance
PrintFormat("%10s %10.4f %10.4f",vol_parameter_labels[i], garch_params.params[i],pv[i]);
}
}
//+------------------------------------------------------------------+