mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-11-02 11:16:15 +00:00
refactor: Zero-Lag MTF Fast-Path & Atomic Multi-Stream Pipeline
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
//| TSI_Combo_Pro.mq5 |
|
||||
//| Copyright 2026, xxxxxxxx|
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2026, xxxxxxxx"
|
||||
#property version "3.10" // Unified Native & MTF Release with Full VWMA Volume Routing
|
||||
#property copyright "Copyright 2026, xxxxxxxx"
|
||||
#property version "3.20" // Enterprise Refactor: Zero-Lag MTF Fast-Path & Atomic Multi-Stream Pipeline
|
||||
#property description "William Blau's True Strength Index (TSI) - Combined Tri-Plot Suite with VWMA Support."
|
||||
#property description "Displays Main TSI Line, Signal Line, and Oscillator Difference with Native & MTF support."
|
||||
|
||||
@@ -90,12 +90,12 @@ datetime h_time[];
|
||||
//--- Global Objects & State Management
|
||||
CTSICalculator *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 |
|
||||
@@ -184,8 +184,8 @@ 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 short_name = StringFormat("TSI Combo%s%s(%d,%d,%d)",
|
||||
ha_tag, tf_str,
|
||||
InpSlowPeriod, InpFastPeriod, InpSignalPeriod);
|
||||
@@ -228,7 +228,7 @@ int OnCalculate(const int rates_total,
|
||||
const int &spread[])
|
||||
{
|
||||
int warmup = InpSlowPeriod + InpFastPeriod + InpSignalPeriod;
|
||||
if(rates_total < warmup || CheckPointer(g_calculator) == POINTER_INVALID)
|
||||
if(rates_total < warmup || !g_calculator)
|
||||
return 0;
|
||||
|
||||
// Force chronological indexing
|
||||
@@ -257,7 +257,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))
|
||||
@@ -271,6 +271,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;
|
||||
@@ -305,122 +306,109 @@ int OnCalculate(const int rates_total,
|
||||
ArraySetAsSeries(h_res_signal, false);
|
||||
ArraySetAsSeries(h_res_osc, 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)
|
||||
// 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)
|
||||
|
||||
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 TSI Values with Volume Routing
|
||||
// Compute HTF TSI Values across history
|
||||
g_calculator.Calculate(g_htf_count, 0, h_open, h_high, h_low, h_close, h_vol,
|
||||
h_res_tsi, h_res_signal, h_res_osc);
|
||||
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)
|
||||
{
|
||||
BufferOsc[i] = h_res_osc[idx_htf];
|
||||
BufferTSI[i] = h_res_tsi[idx_htf];
|
||||
BufferSignal[i] = h_res_signal[idx_htf];
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferOsc[i] = 0.0;
|
||||
BufferTSI[i] = EMPTY_VALUE;
|
||||
BufferSignal[i] = EMPTY_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferOsc[i] = 0.0;
|
||||
BufferTSI[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];
|
||||
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];
|
||||
// Single atomic API call instead of 6 separate copies!
|
||||
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 with volume
|
||||
// Mock update strictly on forming candle
|
||||
g_calculator.Calculate(g_htf_count, g_htf_count, h_open, h_high, h_low, h_close, h_vol,
|
||||
h_res_tsi, h_res_signal, h_res_osc);
|
||||
}
|
||||
}
|
||||
|
||||
// 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 (3 Buffers)
|
||||
// Direct Vectorized Assignment (Zero API calls, Nanosecond Execution across 3 buffers)
|
||||
double osc_val = h_res_osc[live_idx];
|
||||
double tsi_val = h_res_tsi[live_idx];
|
||||
double sig_val = h_res_signal[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)
|
||||
{
|
||||
BufferOsc[i] = h_res_osc[idx_htf];
|
||||
BufferTSI[i] = h_res_tsi[idx_htf];
|
||||
BufferSignal[i] = h_res_signal[idx_htf];
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferOsc[i] = 0.0;
|
||||
BufferTSI[i] = EMPTY_VALUE;
|
||||
BufferSignal[i] = EMPTY_VALUE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferOsc[i] = 0.0;
|
||||
BufferTSI[i] = EMPTY_VALUE;
|
||||
BufferSignal[i] = EMPTY_VALUE;
|
||||
}
|
||||
BufferOsc[i] = osc_val;
|
||||
BufferTSI[i] = tsi_val;
|
||||
BufferSignal[i] = sig_val;
|
||||
}
|
||||
|
||||
return rates_total;
|
||||
|
||||
Reference in New Issue
Block a user