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

74 lines
2.5 KiB
MQL5
Raw Permalink Normal View History

2026-06-03 20:03:04 +02:00
//+------------------------------------------------------------------+
//| GJR_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()
{
if(_O_<1)
{
Alert(" invalid setting, GJR GARCH models need an asymmetry parameter of at least 1\n Otherwise it becomes a regular GARCH model");
return;
}
//---download data
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 gjr_spec;
gjr_spec.observations=ScaleFactor*returns;
gjr_spec.include_constant = MeanConstant;
gjr_spec.vol_model_type = VOL_GJR_GARCH;
gjr_spec.garch_p = _P_;
gjr_spec.garch_q = _Q_;
gjr_spec.garch_o = _O_;
//-- initialize the mean model
ConstantMean gjr_model;
//---
if(!gjr_model.initialize(gjr_spec))
return;
//---get the model parameters
vector empty;
ArchModelResult gjr_params = gjr_model.fit(ScaleFactor);
//--check the model is fitted
if(!gjr_params.params.Size())
{
Print("Convergence failed ", GetLastError());
return;
}
//---
Print("GJR-GARCH model parameters ", gjr_params.params);
//---
Print("GJR-GARCH model parameters ", gjr_params.params);
vector pv = gjr_params.pvalues();
Print("GJR-GARCH model pvalues: ");
for(ulong i = 0; i<pv.Size(); ++i)
Print(" pvalue for param at ", i , " :- ", pv[i]);
//---
}
//+------------------------------------------------------------------+