refactor: Zero-Lag MTF Fast-Path & Zero-Copy Memory

This commit is contained in:
Toh4iem9
2026-10-07 15:30:42 +02:00
parent a06d5ad916
commit d5f8921385
@@ -2,8 +2,8 @@
//| Laguerre_RSI_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 & Zero-Copy Memory
#property description "John Ehlers' Laguerre Relative Strength Index with Native & MTF Support."
#property description "Features time-warped zero-lag momentum analysis with optional smoothed signal line."
@@ -26,7 +26,7 @@
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrLightCoral
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_width1 1
//--- Included Engines & Core Tools
#include <MyIncludes\Laguerre_RSI_Calculator.mqh>
@@ -80,21 +80,21 @@ input int InpWidthSignal = 1; //
double BufferLRSI[];
double BufferSignal[];
//--- Internal HTF Data Caches (Chronological Arrays)
//--- Internal HTF Data Caches
double h_open[], h_high[], h_low[], h_close[];
long h_tick_vol[], h_vol[];
long h_volume[];
double h_res_lrsi[], h_res_signal[];
datetime h_time[];
//--- Global Objects & State Management
CLaguerreRSICalculator *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 |
@@ -182,13 +182,11 @@ int OnInit()
return INIT_FAILED;
}
string ha_tag = (InpSourcePrice <= PRICE_HA_CLOSE) ? " HA" : "";
string tf_str = g_is_mtf_mode ? (" [" + EnumToString(g_calc_timeframe) + "]") : "";
string ha_tag = (InpSourcePrice <= PRICE_HA_CLOSE) ? " HA" : "";
string tf_str = g_is_mtf_mode ? (" [" + EnumToString(g_calc_timeframe) + "]") : "";
string sig_str = (InpDisplayMode == DISPLAY_LRSI_AND_SIGNAL) ? StringFormat(" | %s(%d)", EnumToString(InpSignalMAType), InpSignalPeriod) : "";
string short_name = StringFormat("Laguerre RSI%s%s(γ=%.3f)%s",
ha_tag, tf_str,
InpGamma, sig_str);
string short_name = StringFormat("Laguerre RSI%s%s(γ=%.3f)%s", ha_tag, tf_str, InpGamma, sig_str);
IndicatorSetString(INDICATOR_SHORTNAME, short_name);
PlotIndexSetString(0, PLOT_LABEL, "Laguerre RSI");
@@ -229,7 +227,7 @@ int OnCalculate(const int rates_total,
const int &spread[])
{
int required_bars = InpSignalPeriod + 10;
if(rates_total < required_bars || CheckPointer(g_calculator) == POINTER_INVALID)
if(rates_total < required_bars || !g_calculator)
return 0;
// Force chronological indexing
@@ -241,16 +239,25 @@ int OnCalculate(const int rates_total,
ArraySetAsSeries(tick_volume, false);
ArraySetAsSeries(volume, false);
bool use_vwma = (InpDisplayMode == DISPLAY_LRSI_AND_SIGNAL && InpSignalMAType == VWMA);
//===================================================================
// MODE 1: Direct Current Timeframe Calculation (Zero-Lag O(1))
//===================================================================
if(!g_is_mtf_mode)
{
long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
if(volume_limit > 0)
g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, volume, BufferLRSI, BufferSignal);
if(use_vwma)
{
long volume_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
if(volume_limit > 0)
g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, volume, BufferLRSI, BufferSignal);
else
g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, tick_volume, BufferLRSI, BufferSignal);
}
else
g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, tick_volume, BufferLRSI, BufferSignal);
{
g_calculator.Calculate(rates_total, prev_calculated, open, high, low, close, BufferLRSI, BufferSignal);
}
if(InpDisplayMode == DISPLAY_LRSI_ONLY)
{
@@ -263,7 +270,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)
//===================================================================
if(!CDataSync::EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars))
{
@@ -276,6 +283,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 / Startup: Perform full history sync once
if(htf_updated || prev_calculated == 0)
{
g_last_htf_time = htf_time_current;
@@ -287,7 +295,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);
@@ -295,8 +303,7 @@ int OnCalculate(const int rates_total,
ArrayResize(h_high, g_htf_count);
ArrayResize(h_low, g_htf_count);
ArrayResize(h_close, g_htf_count);
ArrayResize(h_tick_vol, g_htf_count);
ArrayResize(h_vol, g_htf_count);
ArrayResize(h_volume, g_htf_count);
ArrayResize(h_res_lrsi, g_htf_count);
ArrayResize(h_res_signal, g_htf_count);
@@ -305,114 +312,113 @@ int OnCalculate(const int rates_total,
ArraySetAsSeries(h_high, false);
ArraySetAsSeries(h_low, false);
ArraySetAsSeries(h_close, false);
ArraySetAsSeries(h_tick_vol, false);
ArraySetAsSeries(h_vol, false);
ArraySetAsSeries(h_volume, false);
ArraySetAsSeries(h_res_lrsi, false);
ArraySetAsSeries(h_res_signal, false);
if(CopyTime(_Symbol, g_calc_timeframe, 0, g_htf_count, h_time) != g_htf_count ||
CopyOpen(_Symbol, g_calc_timeframe, 0, g_htf_count, h_open) != g_htf_count ||
CopyHigh(_Symbol, g_calc_timeframe, 0, g_htf_count, h_high) != g_htf_count ||
CopyLow(_Symbol, g_calc_timeframe, 0, g_htf_count, h_low) != g_htf_count ||
CopyClose(_Symbol, g_calc_timeframe, 0, g_htf_count, h_close) != g_htf_count ||
CopyTickVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, h_tick_vol) != g_htf_count)
// Atomic bulk rates copy (Single API call replaces 6 calls!)
MqlRates htf_bulk_rates[];
if(CopyRates(_Symbol, g_calc_timeframe, 0, g_htf_count, htf_bulk_rates) != g_htf_count)
{
g_data_ready = false;
return 0;
}
long vol_limit = (long)SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_LIMIT);
if(vol_limit > 0)
CopyRealVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, h_vol);
else
ArrayCopy(h_vol, h_tick_vol, 0, 0, g_htf_count);
// Compute HTF Laguerre RSI Values
if(vol_limit > 0)
g_calculator.Calculate(g_htf_count, 0, h_open, h_high, h_low, h_close, h_vol, h_res_lrsi, h_res_signal);
for(int i = 0; i < g_htf_count; i++)
{
h_time[i] = htf_bulk_rates[i].time;
h_open[i] = htf_bulk_rates[i].open;
h_high[i] = htf_bulk_rates[i].high;
h_low[i] = htf_bulk_rates[i].low;
h_close[i] = htf_bulk_rates[i].close;
h_volume[i] = (vol_limit > 0 && htf_bulk_rates[i].real_volume > 0) ? (long)htf_bulk_rates[i].real_volume : (long)htf_bulk_rates[i].tick_volume;
}
// Compute HTF Laguerre RSI Values across history
if(use_vwma)
g_calculator.Calculate(g_htf_count, 0, h_open, h_high, h_low, h_close, h_volume, h_res_lrsi, h_res_signal);
else
g_calculator.Calculate(g_htf_count, 0, h_open, h_high, h_low, h_close, h_tick_vol, h_res_lrsi, h_res_signal);
g_calculator.Calculate(g_htf_count, 0, h_open, h_high, h_low, h_close, h_res_lrsi, h_res_signal);
g_data_ready = true;
// Full Historical Projection to Chart Buffers (Only on new HTF candle)
for(int i = 0; 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)
{
BufferLRSI[i] = h_res_lrsi[idx_htf];
BufferSignal[i] = (InpDisplayMode == DISPLAY_LRSI_AND_SIGNAL) ? h_res_signal[idx_htf] : EMPTY_VALUE;
}
else
{
BufferLRSI[i] = EMPTY_VALUE;
BufferSignal[i] = EMPTY_VALUE;
}
}
else
{
BufferLRSI[i] = EMPTY_VALUE;
BufferSignal[i] = EMPTY_VALUE;
}
}
return rates_total;
}
if(!g_data_ready)
return 0;
// 5. Stateful live-bar update for active forming HTF candle
// 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)
{
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)
MqlRates htf_rate[1];
// Single atomic API call instead of 5 separate copies!
if(CopyRates(_Symbol, g_calc_timeframe, 0, 1, htf_rate) == 1)
{
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];
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;
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];
h_volume[live_idx] = (vol_limit > 0 && htf_rate[0].real_volume > 0) ? (long)htf_rate[0].real_volume : (long)htf_rate[0].tick_volume;
// 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_lrsi, h_res_signal);
// Mock update strictly on forming candle
if(use_vwma)
g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_volume, h_res_lrsi, h_res_signal);
else
g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_tick_vol, h_res_lrsi, h_res_signal);
g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_res_lrsi, h_res_signal);
}
}
// 6. Forming LTF Block Flat-Force Anchor (The Staircase Solution)
// 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;
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)
// Direct Vectorized Assignment (Zero API calls, Nanosecond Execution across 2 buffers)
double lrsi_val = h_res_lrsi[live_idx];
double sig_val = (InpDisplayMode == DISPLAY_LRSI_AND_SIGNAL) ? h_res_signal[live_idx] : EMPTY_VALUE;
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)
{
BufferLRSI[i] = h_res_lrsi[idx_htf];
BufferSignal[i] = (InpDisplayMode == DISPLAY_LRSI_AND_SIGNAL) ? h_res_signal[idx_htf] : EMPTY_VALUE;
}
else
{
BufferLRSI[i] = EMPTY_VALUE;
BufferSignal[i] = EMPTY_VALUE;
}
}
else
{
BufferLRSI[i] = EMPTY_VALUE;
BufferSignal[i] = EMPTY_VALUE;
}
BufferLRSI[i] = lrsi_val;
BufferSignal[i] = sig_val;
}
return rates_total;