2026-07-24 23:03:06 +02:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| TestsForAsymmetry.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"
|
|
|
|
|
#define __SLSQP__
|
|
|
|
|
#property script_show_inputs
|
|
|
|
|
#include "Arch\Univariate\tests.mqh"
|
|
|
|
|
#include "Arch\Univariate\mean.mqh"
|
|
|
|
|
//--- input parameters
|
|
|
|
|
input ENUM_TIMEFRAMES TimeFrame = PERIOD_D1; //--- Data horizon interval
|
|
|
|
|
input datetime StartDate = D'2026.01.01'; //--- Historical capture anchor start date
|
|
|
|
|
input ulong HistoryLen = 2000; //--- Total historical data bars to request
|
2026-07-28 10:40:17 +02:00
|
|
|
input ENUM_STAT_CONFIDENCE Statistical_Confidence=CONFIDENCE_95;//---Confidence level for p-value
|
2026-07-24 23:03:06 +02:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Script program start function |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void OnStart()
|
|
|
|
|
{
|
|
|
|
|
//---
|
|
|
|
|
vector prices;
|
|
|
|
|
//--- --- Step 1: Historical Data Fetch ---
|
|
|
|
|
//--- Pull raw close prices into a high-performance vector array
|
|
|
|
|
if(!prices.CopyRates(NULL, TimeFrame, COPY_RATES_CLOSE, StartDate, HistoryLen) || HistoryLen > prices.Size())
|
|
|
|
|
{
|
|
|
|
|
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(_Symbol, " : ",EnumToString(TimeFrame)," : ", TimeToString(StartDate), " : ", string(prices.Size()));
|
2026-07-28 10:40:17 +02:00
|
|
|
LeverageCorrelationResult lcr = LeverageCorrelationTest(returns,1,Statistical_Confidence);
|
2026-07-24 23:03:06 +02:00
|
|
|
Print("**** Leverage Correlation Test ****");
|
|
|
|
|
PrintFormat("Corr(r_t-1, r_t^2): %.4f \n p-value: %.4f",lcr.correlation,lcr.p_value);
|
|
|
|
|
Print("Leverage correlation test's assertion of asymmetric volatility is ", lcr.significant_leverage_effect);
|
|
|
|
|
//---
|
|
|
|
|
Print("**** Volatility Runs Test ****");
|
2026-07-28 10:40:17 +02:00
|
|
|
VolatilityRunsResult vrr = VolatilityRunsAsymmetryTest(returns,Statistical_Confidence);
|
2026-07-24 23:03:06 +02:00
|
|
|
PrintFormat("Probability of high volatility following a negative return %.8f",vrr.p_high_vol_prev_down);
|
|
|
|
|
PrintFormat("Probability of high volatility following a positive return %.8f",vrr.p_high_vol_prev_up);
|
|
|
|
|
PrintFormat("T_stat %.8f", vrr.chi2_stat);
|
|
|
|
|
PrintFormat("Pvalue %.8f",vrr.p_value);
|
|
|
|
|
Print("Volatility Runs test's assertion of asymmetric volatility is ", vrr.significant_asymmetry);
|
|
|
|
|
//---
|
|
|
|
|
ArchParameters arch_params;
|
|
|
|
|
arch_params.mean_model_type = MEAN_CONSTANT;
|
|
|
|
|
arch_params.vol_model_type = VOL_GARCH;
|
|
|
|
|
arch_params.observations = returns*100.;
|
|
|
|
|
//---
|
|
|
|
|
ConstantMean ar_model;
|
|
|
|
|
//---
|
|
|
|
|
if(!ar_model.initialize(arch_params))
|
|
|
|
|
{
|
|
|
|
|
Print("Failed to initialize the model");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//---
|
|
|
|
|
ArchModelResult model_result = ar_model.fit();
|
|
|
|
|
//---
|
|
|
|
|
if(!model_result.params.Size())
|
|
|
|
|
return;
|
|
|
|
|
//---
|
|
|
|
|
returns = model_result.std_resid();
|
|
|
|
|
//---
|
2026-07-28 10:40:17 +02:00
|
|
|
EngleNgResult enr = EngleNgSignBiasTest(returns,Statistical_Confidence);
|
2026-07-24 23:03:06 +02:00
|
|
|
Print("**** Engle's NG sign bias test results ****");
|
|
|
|
|
string effects[4] = {"const", "sign-bias", "neg_size_bias", "pos_size_bias"};
|
|
|
|
|
PrintFormat("%-15s %-15s %-15s %-15s","Effect","Coeffs", "T_Stats","Pvalues");
|
|
|
|
|
//---
|
|
|
|
|
for(uint i = 0; i<enr.coefficients.Size(); ++i)
|
|
|
|
|
PrintFormat("%-15s %-15.8f %-15.8f %-15.8f", effects[i],enr.coefficients[i],enr.t_stats[i],enr.p_values[i]);
|
|
|
|
|
//---
|
|
|
|
|
PrintFormat("a2_minus_a3 %.8f \na2_vs_a3 t_stat %.8f \na2_vs_a3 pvalue %.8f", enr.a2_minus_a3,enr.a2_vs_a3_t_stat,enr.a2_vs_a3_p_value);
|
|
|
|
|
Print("Engle NG sign bias test's assertion of asymmetric volatility is ", enr.significant_asymmetry);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//+------------------------------------------------------------------+
|