refactor: Refactored with single comparison symbol and O(1) chart-native access

This commit is contained in:
Toh4iem9
2026-06-20 11:21:42 +02:00
parent 5ced198001
commit 60e277df50
@@ -3,7 +3,7 @@
//| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, xxxxxxxx"
#property version "1.30" // Refactored to use unified calculator engine and 5-channel layout
#property version "1.40" // Refactored with single comparison symbol and O(1) chart-native access
#property description "Wyckoff-style Cointegration Bands on Main Chart."
#property description "Projects dynamic equilibrium line (Z=0), warning (Z=+-1.5) and extreme (Z=+-2.0) bands."
#property indicator_chart_window
@@ -58,19 +58,18 @@ enum ENUM_ANCHOR_PERIOD
};
//--- Input Parameters
input string InpSymbolA = "UKOIL"; // Symbol A (Main Chart Equivalent, e.g. UKOIL or BRENT)
input string InpSymbolB = "USOIL"; // Symbol B (Benchmark, e.g. USOIL or WTI)
input string InpSecondSymbol = "USOIL"; // Comparison Symbol (Symbol B)
input ENUM_ANCHOR_PERIOD InpAnchor = ANCHOR_NONE; // Dynamic Anchored Reset Period
input int InpLookback = 120; // Rolling Window size (Used if Anchor = NONE)
input string InpCustomStart = "09:00"; // Custom Session Start (HH:MM, Broker Time)
input string InpCustomEnd = "18:00"; // Custom Session End (HH:MM, Broker Time)
input int InpLookback = 120; // Rolling Window size (Used if Anchor = NONE)
input string InpCustomStart = "09:00"; // Custom Session Start (HH:MM, Broker Time)
input string InpCustomEnd = "18:00"; // Custom Session End (HH:MM, Broker Time)
//--- Dynamic Channel Options
input bool InpDrawCenterLine = true; // Draw Center Equilibrium Line?
input bool InpDrawInnerBands = true; // Draw Inner (Warning) Bands?
input double InpInnerMultiplier = 1.5; // Inner Band Z-Score Multiplier
input bool InpDrawOuterBands = true; // Draw Outer (Extreme) Bands?
input double InpOuterMultiplier = 2.0; // Outer Band Z-Score Multiplier
input bool InpDrawCenterLine = true; // Draw Center Equilibrium Line?
input bool InpDrawInnerBands = true; // Draw Inner (Warning) Bands?
input double InpInnerMultiplier = 1.5; // Inner Band Z-Score Multiplier
input bool InpDrawOuterBands = true; // Draw Outer (Extreme) Bands?
input double InpOuterMultiplier = 2.0; // Outer Band Z-Score Multiplier
//--- Buffers
double BufMiddle[];
@@ -138,6 +137,16 @@ int OnInit()
g_data_synced = false;
g_anchor_start_idx = 0;
//--- Verify if the secondary comparison symbol exists in broker offerings
bool is_custom = false;
if(!SymbolExist(InpSecondSymbol, is_custom))
{
string err_msg = StringFormat("PairsBands Pro Error: Symbol '%s' does not exist in your broker's database!", InpSecondSymbol);
Alert(err_msg);
Print(err_msg);
return(INIT_FAILED);
}
SetIndexBuffer(0, BufMiddle, INDICATOR_DATA);
SetIndexBuffer(1, BufUpperOuter, INDICATOR_DATA);
SetIndexBuffer(2, BufLowerOuter, INDICATOR_DATA);
@@ -166,7 +175,7 @@ int OnInit()
// Configure shortname dynamically based on mode
string anchor_name = EnumToString(InpAnchor);
string short_name = StringFormat("PairsBands Pro(%s vs %s, %s)",
InpSymbolA, InpSymbolB,
_Symbol, InpSecondSymbol,
(InpAnchor == ANCHOR_NONE ? (string)InpLookback : StringSubstr(anchor_name, 7)));
IndicatorSetString(INDICATOR_SHORTNAME, short_name);
@@ -180,6 +189,9 @@ int OnInit()
return INIT_FAILED;
}
//--- Initialize 1-second timer for weekend/async chart refreshes
EventSetTimer(1);
return(INIT_SUCCEEDED);
}
@@ -188,7 +200,8 @@ int OnInit()
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(CheckPointer(g_calc) == POINTER_DYNAMIC)
EventKillTimer();
if(CheckPointer(g_calc) != POINTER_INVALID)
delete g_calc;
}
@@ -211,8 +224,8 @@ int OnCalculate(const int rates_total,
required_bars = 1000; // Need larger history depth for monthly/weekly/custom anchors
//--- Ensure both symbol histories are fully loaded in the terminal
if(!EnsureDataReady(InpSymbolA, _Period, required_bars) ||
!EnsureDataReady(InpSymbolB, _Period, required_bars))
if(!EnsureDataReady(_Symbol, _Period, required_bars) ||
!EnsureDataReady(InpSecondSymbol, _Period, required_bars))
{
g_data_synced = false;
return 0; // Wait for next tick to let history load
@@ -221,8 +234,7 @@ int OnCalculate(const int rates_total,
g_data_synced = true;
//--- Retrieve chart-independent default close values for index-0 fallbacks
double default_close_A = iClose(InpSymbolA, _Period, 0);
double default_close_B = iClose(InpSymbolB, _Period, 0);
double default_close_B = iClose(InpSecondSymbol, _Period, 0);
//--- 1. Advanced Bar-Time Synchronization & Alignment Loop (O(1) incremental)
ArrayResize(g_sync_close_A, rates_total);
@@ -234,17 +246,13 @@ int OnCalculate(const int rates_total,
for(int i = loop_start; i < rates_total; i++)
{
// Sync Symbol A Price
int shift_A = iBarShift(InpSymbolA, _Period, time[i], false);
if(shift_A >= 0)
g_sync_close_A[i] = iClose(InpSymbolA, _Period, shift_A);
else
g_sync_close_A[i] = (i > 0) ? g_sync_close_A[i-1] : default_close_A;
// Symbol A is the native chart symbol -> direct O(1) memory access
g_sync_close_A[i] = close[i];
// Sync Symbol B Price
int shift_B = iBarShift(InpSymbolB, _Period, time[i], false);
int shift_B = iBarShift(InpSecondSymbol, _Period, time[i], false);
if(shift_B >= 0)
g_sync_close_B[i] = iClose(InpSymbolB, _Period, shift_B);
g_sync_close_B[i] = iClose(InpSecondSymbol, _Period, shift_B);
else
g_sync_close_B[i] = (i > 0) ? g_sync_close_B[i-1] : default_close_B;
}
@@ -381,5 +389,22 @@ int OnCalculate(const int rates_total,
return(rates_total);
}
//+------------------------------------------------------------------+
//| OnTimer |
//| Handles loading checks and force-redraws |
//+------------------------------------------------------------------+
void OnTimer()
{
if(!g_data_synced)
{
int required_bars = InpLookback + 5;
if(EnsureDataReady(InpSecondSymbol, _Period, required_bars))
{
g_data_synced = true;
ChartRedraw(); // Force MT5 to invoke OnCalculate
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+