From 62c44987bde386ab8d2b3b099f8ed68fdbb153a8 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Sun, 7 Jun 2026 20:11:12 +0200 Subject: [PATCH] refactor: Optimized with incremental benchmark alignment --- .../MyIndicators/Quant/AlphaBeta_Pro.mq5 | 104 ++++++++++-------- 1 file changed, 61 insertions(+), 43 deletions(-) diff --git a/Indicators/MyIndicators/Quant/AlphaBeta_Pro.mq5 b/Indicators/MyIndicators/Quant/AlphaBeta_Pro.mq5 index 44de168..9ff394f 100644 --- a/Indicators/MyIndicators/Quant/AlphaBeta_Pro.mq5 +++ b/Indicators/MyIndicators/Quant/AlphaBeta_Pro.mq5 @@ -4,8 +4,8 @@ //| Copyright 2026, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, xxxxxxxx" -#property version "1.20" // Fixed Display Logic using Unified Buffer -#property description "Rolling Alpha (Excess Return) or Beta (Volatility)." +#property version "1.30" // Optimized with incremental benchmark alignment +#property description "Rolling Alpha (Excess Return) or Beta (Volatility) aligned incrementally." #property indicator_separate_window #property indicator_buffers 2 @@ -32,6 +32,9 @@ input string InpForexBench = "DX"; // Forex Bench double BufDisplay[]; // The Visible Output double BufColors[]; // The Color Index +//--- Aligned benchmark close prices array +double g_bench_close[]; + CMathStatisticsCalculator *g_stats; string g_bench_symbol; @@ -53,7 +56,6 @@ int OnInit() PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_HISTOGRAM); PlotIndexSetString(0, PLOT_LABEL, "Alpha"); - // Levels IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, 0.0); } else // BETA @@ -63,7 +65,6 @@ int OnInit() PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_LINE); PlotIndexSetString(0, PLOT_LABEL, "Beta"); - // Levels IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, 1.0); } @@ -76,68 +77,86 @@ int OnInit() return(INIT_SUCCEEDED); } -void OnDeinit(const int r) { if(CheckPointer(g_stats)==POINTER_DYNAMIC) delete g_stats; } +//+------------------------------------------------------------------+ +//| Deinit | +//+------------------------------------------------------------------+ +void OnDeinit(const int r) + { + if(CheckPointer(g_stats) == POINTER_DYNAMIC) + delete g_stats; + } //+------------------------------------------------------------------+ //| Calculate | //+------------------------------------------------------------------+ -int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) { if(rates_total < InpLookback + 5) return 0; +//--- 1. Incremental Benchmark Price Alignment (O(1) complexity per tick) + ArrayResize(g_bench_close, rates_total); + int loop_start_sync = (prev_calculated == 0) ? 0 : prev_calculated - 1; + if(loop_start_sync < 0) + loop_start_sync = 0; + + for(int i = loop_start_sync; i < rates_total; i++) + { + int shift = iBarShift(g_bench_symbol, _Period, time[i], false); + if(shift >= 0) + { + g_bench_close[i] = iClose(g_bench_symbol, _Period, shift); + } + else + { + g_bench_close[i] = (i > 0) ? g_bench_close[i-1] : close[i]; + } + } + +//--- 2. Main Stats Calculation int start = (prev_calculated > InpLookback) ? prev_calculated - 1 : InpLookback; for(int i = start; i < rates_total; i++) { - // 1. Fetch Local Data + // Extract Local Data (Optimized via lightning-fast ArrayCopy) double asset_sub[]; ArrayResize(asset_sub, InpLookback); - for(int k=0; k 0) - BufColors[i] = 1.0; // Lime + BufColors[i] = 1.0; // Lime else if(alpha < 0) BufColors[i] = 2.0; // Red else - BufColors[i] = 0.0; + BufColors[i] = 0.0; // Gray } } @@ -161,11 +180,10 @@ int OnCalculate(const int rates_total, const int prev_calculated, const datetime } //+------------------------------------------------------------------+ -//| | +//| IsForexPair | //+------------------------------------------------------------------+ bool IsForexPair(string sym) { -// Safety: If symbol IS one of the benchmarks, we don't classify it as generic forex pair here if(sym == InpBenchmark || sym == InpForexBench) return false;