88 lines
4.2 KiB
MQL5
88 lines
4.2 KiB
MQL5
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Aparch_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
|
||
|
|
#include"Arch\univariate\mean.mqh"
|
||
|
|
//--- Input parameters for script customization
|
||
|
|
input datetime StartDate = D'2025.01.01'; //--- Historical capture anchor start date
|
||
|
|
input ulong HistoryLen = 1000; //--- Total historical data bars to request
|
||
|
|
input double ScaleFactor = 100.; //--- Rescaling multiplier to prevent optimizer underflow
|
||
|
|
input ulong _P_ = 1; //--- Short-term ARCH lag order
|
||
|
|
input ulong _O_ = 1; //--- Asymmetric term
|
||
|
|
input ulong _Q_ = 1; //--- Long-term variance persistence GARCH lag order
|
||
|
|
input double Delta = EMPTY_VALUE; //--- Delta value
|
||
|
|
input bool CommonAsymmetry=false; //--- Enable common asymmetric coefficents
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| 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);
|
||
|
|
|
||
|
|
//--- --- Step 3: Base Model Configuration Setup ---
|
||
|
|
//--- Initialize core specification fields mapping onto the aparch container
|
||
|
|
ArchParameters aparch_spec;
|
||
|
|
//--- Apply the scaling factor (multiplying by 100 scales returns to percentage form)
|
||
|
|
aparch_spec.observations = ScaleFactor * returns;
|
||
|
|
aparch_spec.vol_model_type = VOL_APARCH;
|
||
|
|
aparch_spec.garch_p = _P_;
|
||
|
|
aparch_spec.garch_o = _O_;
|
||
|
|
aparch_spec.garch_q = _Q_;
|
||
|
|
aparch_spec.aparch_common_asym = CommonAsymmetry;
|
||
|
|
aparch_spec.aparch_delta = Delta;
|
||
|
|
//--- --- Step 5: Model Initialization ---
|
||
|
|
//--- Instantiate the continuous tracking zero mean container wrapper
|
||
|
|
ZeroMean aparch_model;
|
||
|
|
//--- Pass structural parameters down into the optimization initialization routine
|
||
|
|
if(!aparch_model.initialize(aparch_spec))
|
||
|
|
return;
|
||
|
|
//--- --- Step 6: Parameter Optimization (Fitting) ---
|
||
|
|
//--- Trigger the non-linear execution optimizer loop (SLSQP engine solver)
|
||
|
|
ArchModelResult aparch_params = aparch_model.fit();
|
||
|
|
//--- --- Step 7: Optimization Convergence Guard ---
|
||
|
|
//--- 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);
|
||
|
|
//---
|
||
|
|
vector pv = aparch_params.pvalues();
|
||
|
|
//--- --- Step 8: Results Output Extraction ---
|
||
|
|
//--- Print optimal target parameter solutions to the MT5 journal
|
||
|
|
Print("Aparch 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]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|