Article-22258-Volatility-Mo.../Scripts/VolatilityModels/TARCh_Demo.mq5

65 lines
2.2 KiB
MQL5
Raw Permalink Normal View History

2026-06-03 20:03:04 +02:00
//+------------------------------------------------------------------+
//| TARCH_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<VolatilityModels\Arch\Univariate\mean.mqh>
//--- input parameters
input string Symbol_="AUDUSD";
input ENUM_TIMEFRAMES TimeFrame=PERIOD_D1;
input datetime StartDate=D'2025.01.01';
input ulong HistoryLen = 504;
input double ScaleFactor=100.;
input bool MeanConstant = true;
input ulong _P_ = 1;
input ulong _O_ = 1;
input ulong _Q_ = 1;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
vector prices;
if(!prices.CopyRates(Symbol_,TimeFrame,COPY_RATES_CLOSE,StartDate,HistoryLen))
{
Print(" failed to get close prices for ", Symbol_,". Error ", GetLastError());
return;
}
//---
prices = log(prices);
//---
vector returns = np::diff(prices);
//-- specify the model parameters
ArchParameters tarch_spec;
tarch_spec.observations=ScaleFactor*returns;
tarch_spec.include_constant = MeanConstant;
tarch_spec.vol_model_type = VOL_TARCH;
tarch_spec.garch_p = _P_;
tarch_spec.garch_q = _Q_;
tarch_spec.garch_o = _O_;
//-- initialize the mean model
ConstantMean tarch_model;
//---
if(!tarch_model.initialize(tarch_spec))
return;
//---get the model parameters
vector empty;
ArchModelResult tarch_params = tarch_model.fit(ScaleFactor);
//--check the model is fitted
if(!tarch_params.params.Size())
{
Print("Convergence failed ", GetLastError());
return;
}
//---
Print("TARCH model parameters ", tarch_params.params);
vector pv = tarch_params.pvalues();
Print("TARCH model pvalues: ");
for(ulong i = 0; i<pv.Size(); ++i)
Print(" pvalue for param at ", i , " :- ", pv[i]);
//---
}