81 lines
3.8 KiB
MQL5
81 lines
3.8 KiB
MQL5
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| EGARCH_Demo.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
|
||
|
|
#define __SLSQP__
|
||
|
|
#include "Arch\univariate\mean.mqh"
|
||
|
|
#resource "SPY2017.csv" as string equity_data
|
||
|
|
//--- Input parameters for script customization
|
||
|
|
input double ScaleFactor = 100.; //--- Rescaling multiplier to prevent optimizer underflow
|
||
|
|
input ulong _P_ = 1; //--- Short-term ARCH lag order
|
||
|
|
input ulong _O_ = 0; //--- Asymmetric term
|
||
|
|
input ulong _Q_ = 1; //--- Long-term variance persistence GARCH lag order
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Script program start function |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
void OnStart()
|
||
|
|
{
|
||
|
|
//---
|
||
|
|
vector prices;
|
||
|
|
//--- --- Step 1: Historical Data Fetch ---
|
||
|
|
matrix data = np::readcsv_from_string(equity_data,false,",",true,0);
|
||
|
|
prices = data.Col(1);
|
||
|
|
|
||
|
|
//--- --- 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);
|
||
|
|
|
||
|
|
//--- --- Step 3: Base Model Configuration Setup ---
|
||
|
|
//--- Initialize core specification fields mapping onto the EGARCH container
|
||
|
|
ArchParameters egarch_spec;
|
||
|
|
//--- Apply the scaling factor (multiplying by 100 scales returns to percentage form)
|
||
|
|
egarch_spec.observations = ScaleFactor * returns;
|
||
|
|
egarch_spec.vol_model_type = VOL_EGARCH;
|
||
|
|
egarch_spec.garch_p = _P_;
|
||
|
|
egarch_spec.garch_o = _O_;
|
||
|
|
egarch_spec.garch_q = _Q_;
|
||
|
|
//--- --- Step 5: Model Initialization ---
|
||
|
|
//--- Instantiate the continuous tracking constant mean container wrapper
|
||
|
|
ZeroMean egarch_model;
|
||
|
|
//--- Pass structural parameters down into the optimization initialization routine
|
||
|
|
if(!egarch_model.initialize(egarch_spec))
|
||
|
|
return;
|
||
|
|
//--- --- Step 6: Parameter Optimization (Fitting) ---
|
||
|
|
//--- Trigger the non-linear execution optimizer loop (SLSQP engine solver)
|
||
|
|
ArchModelResult egarch_params = egarch_model.fit();
|
||
|
|
//--- --- Step 7: Optimization Convergence Guard ---
|
||
|
|
//--- Verify that the resulting parameters array size matches the model criteria configurations
|
||
|
|
if(!egarch_params.params.Size())
|
||
|
|
{
|
||
|
|
Print("Convergence failed ", GetLastError());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
//--- Prepare output of model parameters
|
||
|
|
string pnames = egarch_model.volatility().parameterNames();
|
||
|
|
string vol_parameter_labels[];
|
||
|
|
//--- Organize parameter names into array for display
|
||
|
|
int labels = StringSplit(pnames,StringGetCharacter(",",0),vol_parameter_labels);
|
||
|
|
//--- Check number of labels is equal to number of model parameters
|
||
|
|
if(labels != int(egarch_params.params.Size()))
|
||
|
|
return;
|
||
|
|
//--- --- Step 8: Results Output Extraction ---
|
||
|
|
//--- Print optimal target parameter solutions to the MT5 journal
|
||
|
|
Print("EGARCH model parameters");
|
||
|
|
PrintFormat("%10s %10s %10s","Name","Value","Pvalue");
|
||
|
|
//--- Extract statistical asymptotic standard deviation errors mapped out to individual p-values
|
||
|
|
vector pv = egarch_params.pvalues();
|
||
|
|
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], egarch_params.params[i],pv[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|