refactor: Two-Pointer MTF Fast-Path, Fixed HA Price Offsets & Atomic CopyRates

This commit is contained in:
Toh4iem9
2026-10-30 14:38:41 +01:00
parent bfe270bb5f
commit 9bbbfc6797
+103 -108
View File
@@ -2,10 +2,10 @@
//| MovingAverage_Pro.mq5 |
//| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, xxxxxxxx"
#property version "3.00" // Unified Native & MTF Universal Moving Average
#property copyright "Copyright 2026, xxxxxxxx"
#property version "3.10" // Enterprise Edition: Two-Pointer MTF Fast-Path, Fixed HA Price Offsets & Atomic CopyRates
#property description "Universal Moving Average (SMA, EMA, SMMA, LWMA, TMA, DEMA, TEMA, VWMA)."
#property description "Features unified Native & MTF pipelines, Heikin Ashi pricing, and full visual customization."
#property description "Features unified Native & MTF pipelines, Heikin Ashi pricing, and zero-API live tick rendering."
#property indicator_chart_window
#property indicator_buffers 1
@@ -40,20 +40,20 @@ input int InpWidthMA = 2; // Line Wid
double BufferMA[];
//--- Internal HTF Data Caches (Chronological Arrays)
double h_open[], h_high[], h_low[], h_close[];
double h_vol[];
double h_res_ma[];
double h_open[], h_high[], h_low[], h_close[];
double h_vol[];
double h_res_ma[];
datetime h_time[];
//--- Global Objects & State Management
CMovingAverageCalculator *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 |
@@ -89,7 +89,17 @@ int OnInit()
PlotIndexSetInteger(0, PLOT_LINE_WIDTH, InpWidthMA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
// Type-Accurate Draw Begin Setting
int draw_begin = InpPeriod - 1;
if(InpMAType == DEMA)
draw_begin = 2 * InpPeriod - 2;
else
if(InpMAType == TEMA)
draw_begin = 3 * InpPeriod - 3;
else
if(InpMAType == TMA)
draw_begin = InpPeriod - 1;
if(g_is_mtf_mode)
draw_begin = 0;
@@ -153,7 +163,8 @@ int OnCalculate(const int rates_total,
const long &volume[],
const int &spread[])
{
if(rates_total < InpPeriod || CheckPointer(g_calculator) == POINTER_INVALID)
int required_bars = InpPeriod + 5;
if(rates_total < required_bars || CheckPointer(g_calculator) == POINTER_INVALID)
return 0;
// Force chronological indexing
@@ -165,8 +176,9 @@ int OnCalculate(const int rates_total,
ArraySetAsSeries(tick_volume, false);
ArraySetAsSeries(volume, false);
// EXACT MATHEMATICAL APPLIED PRICE MAPPING (Eliminates Off-by-One Mismatch!)
ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ?
(ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) :
(ENUM_APPLIED_PRICE)(-((int)InpSourcePrice) - 1) :
(ENUM_APPLIED_PRICE)InpSourcePrice;
//===================================================================
@@ -184,10 +196,10 @@ 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 = InpPeriod + 10;
if(!CDataSync::EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars))
int required_bars_mtf = InpPeriod + 10;
if(!CDataSync::EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars_mtf))
{
g_data_synced = false;
return 0;
@@ -198,12 +210,13 @@ 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;
int htf_bars = iBars(_Symbol, g_calc_timeframe);
if(htf_bars < required_bars)
if(htf_bars < required_bars_mtf)
{
g_data_ready = false;
return 0;
@@ -212,130 +225,112 @@ int OnCalculate(const int rates_total,
g_htf_count = MathMin(htf_bars, 3000);
// Resize all HTF caching arrays
ArrayResize(h_time, g_htf_count);
ArrayResize(h_open, g_htf_count);
ArrayResize(h_high, g_htf_count);
ArrayResize(h_low, g_htf_count);
ArrayResize(h_close, g_htf_count);
ArrayResize(h_vol, g_htf_count);
ArrayResize(h_res_ma, g_htf_count);
if(ArraySize(h_time) != g_htf_count)
{
ArrayResize(h_time, g_htf_count);
ArrayResize(h_open, g_htf_count);
ArrayResize(h_high, g_htf_count);
ArrayResize(h_low, g_htf_count);
ArrayResize(h_close, g_htf_count);
ArrayResize(h_vol, g_htf_count);
ArrayResize(h_res_ma, g_htf_count);
ArraySetAsSeries(h_time, false);
ArraySetAsSeries(h_open, false);
ArraySetAsSeries(h_high, false);
ArraySetAsSeries(h_low, false);
ArraySetAsSeries(h_close, false);
ArraySetAsSeries(h_vol, false);
ArraySetAsSeries(h_res_ma, false);
ArraySetAsSeries(h_time, false);
ArraySetAsSeries(h_open, false);
ArraySetAsSeries(h_high, false);
ArraySetAsSeries(h_low, false);
ArraySetAsSeries(h_close, false);
ArraySetAsSeries(h_vol, false);
ArraySetAsSeries(h_res_ma, 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)
// Single Atomic Bulk Rates Copy
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)
for(int i = 0; i < g_htf_count; i++)
{
long temp_vol[];
if(CopyRealVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, temp_vol) == g_htf_count)
{
for(int i = 0; i < g_htf_count; i++)
h_vol[i] = (double)temp_vol[i];
}
}
else
{
long temp_vol[];
if(CopyTickVolume(_Symbol, g_calc_timeframe, 0, g_htf_count, temp_vol) == g_htf_count)
{
for(int i = 0; i < g_htf_count; i++)
h_vol[i] = (double)temp_vol[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_vol[i] = (vol_limit > 0 && htf_bulk_rates[i].real_volume > 0) ? (double)htf_bulk_rates[i].real_volume : (double)htf_bulk_rates[i].tick_volume;
}
// Compute HTF Moving Average
// Compute HTF Moving Average on Clean HTF Domain
g_calculator.Calculate(g_htf_count, 0, price_type, h_open, h_high, h_low, h_close, h_vol, h_res_ma);
g_data_ready = true;
// TWO-POINTER CHRONOLOGICAL SWEEP (Zero API calls, Nanosecond Execution!)
int htf_ptr = 0;
datetime oldest_htf = h_time[0];
for(int i = 0; i < rates_total; i++)
{
datetime t = time[i];
if(t < oldest_htf)
{
BufferMA[i] = EMPTY_VALUE;
continue;
}
while(htf_ptr + 1 < g_htf_count && h_time[htf_ptr + 1] <= t)
htf_ptr++;
BufferMA[i] = h_res_ma[htf_ptr];
}
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 candle in RAM!
int live_idx = g_htf_count - 1;
if(live_idx >= required_bars)
if(live_idx >= required_bars_mtf)
{
double o[1], h[1], l[1], c[1];
datetime t_bar[1];
long v[1];
int shift = iBarShift(_Symbol, g_calc_timeframe, htf_time_current, false);
if(shift >= 0 &&
CopyTime(_Symbol, g_calc_timeframe, shift, 1, t_bar) == 1 &&
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)
MqlRates htf_rate[1];
if(CopyRates(_Symbol, g_calc_timeframe, 0, 1, htf_rate) == 1)
{
h_time[live_idx] = t_bar[0];
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_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] = (double)v[0];
else
if(CopyTickVolume(_Symbol, g_calc_timeframe, shift, 1, v) == 1)
h_vol[live_idx] = (double)v[0];
h_vol[live_idx] = (vol_limit > 0 && htf_rate[0].real_volume > 0) ? (double)htf_rate[0].real_volume : (double)htf_rate[0].tick_volume;
// Mock update on live bar
// Mock incremental update on forming HTF bar
g_calculator.Calculate(g_htf_count, g_htf_count, price_type, h_open, h_high, h_low, h_close, h_vol, h_res_ma);
}
}
// 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
// Direct Vectorized Assignment (Nanosecond execution across forming chart block)
double ma_val = h_res_ma[live_idx];
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)
{
BufferMA[i] = h_res_ma[idx_htf];
}
else
{
BufferMA[i] = EMPTY_VALUE;
}
}
else
{
BufferMA[i] = EMPTY_VALUE;
}
BufferMA[i] = ma_val;
}
return rates_total;
@@ -346,7 +341,7 @@ int OnCalculate(const int rates_total,
//+------------------------------------------------------------------+
void OnTimer()
{
int required_bars = InpPeriod + 10;
int required_bars = InpPeriod + 15;
CDataSync::OnTimerUpdate(_Symbol, g_calc_timeframe, required_bars, g_data_synced);
}
//+------------------------------------------------------------------+