//+------------------------------------------------------------------+ //| 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 input ENUM_STAT_CONFIDENCE Statistical_Confidence=CONFIDENCE_95;//---Confidence level for p-value //+------------------------------------------------------------------+ //| 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())); LeverageCorrelationResult lcr = LeverageCorrelationTest(returns,1,Statistical_Confidence); 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 ****"); VolatilityRunsResult vrr = VolatilityRunsAsymmetryTest(returns,Statistical_Confidence); 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(); //--- EngleNgResult enr = EngleNgSignBiasTest(returns,Statistical_Confidence); 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