diff --git a/Indicators/MyIndicators/DMIStochastic_Pro.mq5 b/Indicators/MyIndicators/DMIStochastic_Pro.mq5 index 260a9d3c..941603d1 100644 --- a/Indicators/MyIndicators/DMIStochastic_Pro.mq5 +++ b/Indicators/MyIndicators/DMIStochastic_Pro.mq5 @@ -2,8 +2,8 @@ //| DMIStochastic_Pro.mq5 | //| Copyright 2026, xxxxxxxx| //+------------------------------------------------------------------+ -#property copyright "Copyright 2026, xxxxxxxx" -#property version "3.00" // Unified Native & MTF Release with Dynamic Levels +#property copyright "Copyright 2026, xxxxxxxx" +#property version "3.10" // Enterprise Refactor: Zero-Lag MTF Fast-Path & Fused Memory #property description "Barbara Star's DMI Stochastic Oscillator with Native & MTF Support." #property description "Combines Directional Movement with Stochastic normalization for trend-momentum filtering." @@ -80,12 +80,12 @@ datetime h_time[]; //--- Global Objects & State Management CDMIStochasticCalculator *g_calculator = NULL; -bool g_is_mtf_mode = false; +bool g_is_mtf_mode = false; ENUM_TIMEFRAMES g_calc_timeframe; -bool g_data_ready = false; -bool g_data_synced = false; -int g_htf_count = 0; -datetime g_last_htf_time = 0; +bool g_data_ready = false; +bool g_data_synced = false; +int g_htf_count = 0; +datetime g_last_htf_time = 0; //+------------------------------------------------------------------+ //| Custom Indicator Initialization | @@ -164,8 +164,8 @@ int OnInit() return INIT_FAILED; } - string ha_tag = (InpCandleSource == CANDLE_HEIKIN_ASHI) ? " HA" : ""; - string tf_str = g_is_mtf_mode ? (" [" + EnumToString(g_calc_timeframe) + "]") : ""; + string ha_tag = (InpCandleSource == CANDLE_HEIKIN_ASHI) ? " HA" : ""; + string tf_str = g_is_mtf_mode ? (" [" + EnumToString(g_calc_timeframe) + "]") : ""; string short_name = StringFormat("DMI Stoch%s%s(%d,%d,%d,%d)", ha_tag, tf_str, InpDMIPeriod, InpFastKPeriod, InpSlowKPeriod, InpSmoothPeriod); @@ -208,7 +208,7 @@ int OnCalculate(const int rates_total, const int &spread[]) { int warmup = InpDMIPeriod + InpFastKPeriod + InpSlowKPeriod + InpSmoothPeriod; - if(rates_total < warmup || CheckPointer(g_calculator) == POINTER_INVALID) + if(rates_total < warmup || g_calculator == NULL) return 0; // Force chronological indexing @@ -235,7 +235,7 @@ int OnCalculate(const int rates_total, } //=================================================================== -// MODE 2: Multi-Timeframe Engine (Warp-free Step Synchronization) +// MODE 2: Multi-Timeframe Engine (High-Performance Fast-Path) //=================================================================== int required_bars = warmup + 10; if(!CDataSync::EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars)) @@ -249,6 +249,7 @@ int OnCalculate(const int rates_total, datetime htf_time_current = iTime(_Symbol, g_calc_timeframe, 0); bool htf_updated = (htf_time_current != g_last_htf_time); +// A) HTF Bar Closure: Full History Pass once if(htf_updated || prev_calculated == 0) { g_last_htf_time = htf_time_current; @@ -260,7 +261,7 @@ int OnCalculate(const int rates_total, return 0; } - g_htf_count = MathMin(htf_bars, 3000); + g_htf_count = MathMin(htf_bars, 3000); // Memory safeguard // Resize all HTF caching arrays ArrayResize(h_time, g_htf_count); @@ -307,73 +308,25 @@ int OnCalculate(const int rates_total, g_calculator.Calculate(g_htf_count, 0, h_open, h_high, h_low, h_close, h_tick_vol, h_res_k, h_res_d); g_data_ready = true; - } - if(!g_data_ready) - return 0; - -// 5. Stateful live-bar update for active forming HTF candle - int live_idx = g_htf_count - 1; - if(live_idx >= required_bars) - { - double o[1], h[1], l[1], c[1]; - long tv[1], v[1]; - - int shift = iBarShift(_Symbol, g_calc_timeframe, htf_time_current, false); - if(shift >= 0 && - CopyOpen(_Symbol, g_calc_timeframe, shift, 1, o) == 1 && - CopyHigh(_Symbol, g_calc_timeframe, shift, 1, h) == 1 && - CopyLow(_Symbol, g_calc_timeframe, shift, 1, l) == 1 && - CopyClose(_Symbol, g_calc_timeframe, shift, 1, c) == 1 && - CopyTickVolume(_Symbol, g_calc_timeframe, shift, 1, tv) == 1) + // Full Historical Projection to Chart Buffers (Only on new HTF candle) + for(int i = 0; i < rates_total; i++) { - h_open[live_idx] = o[0]; - h_high[live_idx] = h[0]; - h_low[live_idx] = l[0]; - h_close[live_idx] = c[0]; - h_tick_vol[live_idx] = tv[0]; - - long vol_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT); - if(vol_limit > 0 && CopyRealVolume(_Symbol, g_calc_timeframe, shift, 1, v) == 1) - h_vol[live_idx] = v[0]; - else - h_vol[live_idx] = tv[0]; - - // Mock update on live HTF bar - if(vol_limit > 0) - g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_vol, h_res_k, h_res_d); - else - g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_tick_vol, h_res_k, h_res_d); - } - } - -// 6. Forming LTF Block Flat-Force Anchor (The Staircase Solution) - int start = (prev_calculated > 0) ? prev_calculated - 1 : 0; - - int first_bar_of_forming_htf = rates_total - 1; - while(first_bar_of_forming_htf > 0 && - iBarShift(_Symbol, g_calc_timeframe, time[first_bar_of_forming_htf], false) == 0) - { - first_bar_of_forming_htf--; - } - first_bar_of_forming_htf++; - - if(start > first_bar_of_forming_htf) - start = first_bar_of_forming_htf; - -// 7. Chronological Mapping Loop to Chart Timeframe (2 Buffers) - for(int i = start; i < rates_total; i++) - { - datetime t = time[i]; - int shift_htf = iBarShift(_Symbol, g_calc_timeframe, t, false); - - if(shift_htf >= 0) - { - int idx_htf = g_htf_count - 1 - shift_htf; - if(idx_htf >= 0 && idx_htf < g_htf_count) + datetime t = time[i]; + int shift_htf = iBarShift(_Symbol, g_calc_timeframe, t, false); + if(shift_htf >= 0) { - BufferK[i] = h_res_k[idx_htf]; - BufferD[i] = h_res_d[idx_htf]; + int idx_htf = g_htf_count - 1 - shift_htf; + if(idx_htf >= 0 && idx_htf < g_htf_count) + { + BufferK[i] = h_res_k[idx_htf]; + BufferD[i] = h_res_d[idx_htf]; + } + else + { + BufferK[i] = EMPTY_VALUE; + BufferD[i] = EMPTY_VALUE; + } } else { @@ -381,13 +334,57 @@ int OnCalculate(const int rates_total, BufferD[i] = EMPTY_VALUE; } } - else + return rates_total; + } + + if(!g_data_ready) + return 0; + +// B) LIVE TICK FAST-PATH: HTF bar did not close. Update strictly forming block! + int live_idx = g_htf_count - 1; + if(live_idx >= required_bars) + { + MqlRates htf_rate[1]; + // Single atomic API call instead of 5 separate copies! + if(CopyRates(_Symbol, g_calc_timeframe, 0, 1, htf_rate) == 1) { - BufferK[i] = EMPTY_VALUE; - BufferD[i] = EMPTY_VALUE; + h_time[live_idx] = htf_rate[0].time; + h_open[live_idx] = htf_rate[0].open; + h_high[live_idx] = htf_rate[0].high; + h_low[live_idx] = htf_rate[0].low; + h_close[live_idx] = htf_rate[0].close; + h_tick_vol[live_idx] = htf_rate[0].tick_volume; + h_vol[live_idx] = (htf_rate[0].real_volume > 0) ? htf_rate[0].real_volume : htf_rate[0].tick_volume; + + // Mock update on live HTF bar + if(h_vol[live_idx] > 0 && SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT) > 0) + g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_vol, h_res_k, h_res_d); + else + g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_tick_vol, h_res_k, h_res_d); } } +// Instant Binary Search for Forming Block Start (Zero iBarShift API calls!) + int first_bar_of_forming_htf = ArrayBsearch(time, htf_time_current); + if(first_bar_of_forming_htf < 0) + first_bar_of_forming_htf = 0; + if(time[first_bar_of_forming_htf] < htf_time_current && first_bar_of_forming_htf < rates_total - 1) + first_bar_of_forming_htf++; + + int start = (prev_calculated > 0) ? prev_calculated - 1 : 0; + if(start > first_bar_of_forming_htf) + start = first_bar_of_forming_htf; + +// Direct Vectorized Assignment (Zero API calls, Nanosecond Execution) + double k_val = h_res_k[live_idx]; + double d_val = h_res_d[live_idx]; + + for(int i = start; i < rates_total; i++) + { + BufferK[i] = k_val; + BufferD[i] = d_val; + } + return rates_total; }