refactor: Full incremental support with selectable ATR src

This commit is contained in:
Toh4iem9
2025-12-08 12:43:37 +01:00
parent f1634f14c2
commit 9992f791f7
@@ -1,16 +1,21 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Bollinger_ATR_Oscillator_Calculator.mqh| //| Bollinger_ATR_Oscillator_Calculator.mqh|
//| Calculation engine for Standard and Heikin Ashi BB ATR Osc. | //| VERSION 2.20: Full incremental support with selectable ATR src.|
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#include <MyIncludes\HeikinAshi_Tools.mqh> #include <MyIncludes\HeikinAshi_Tools.mqh>
//--- Define the Enum here locally
enum ENUM_ATR_SOURCE
{
ATR_SOURCE_STANDARD, // Calculate ATR from standard candles
ATR_SOURCE_HEIKIN_ASHI // Calculate ATR from Heikin Ashi candles
};
//+==================================================================+ //+==================================================================+
//| |
//| CLASS 1: CBollingerATROscillatorCalculator (Standard) | //| CLASS 1: CBollingerATROscillatorCalculator (Standard) |
//| |
//+==================================================================+ //+==================================================================+
class CBollingerATROscillatorCalculator class CBollingerATROscillatorCalculator
{ {
@@ -18,196 +23,277 @@ protected:
int m_atr_period; int m_atr_period;
int m_bb_period; int m_bb_period;
double m_bb_dev; double m_bb_dev;
ENUM_ATR_SOURCE m_atr_source;
//--- Persistent Buffers
double m_price[]; double m_price[];
double m_atr_buffer[]; double m_atr_buffer[];
double m_ma_buffer[]; double m_ma_buffer[];
double m_upper_band[]; double m_upper_band[];
double m_lower_band[]; double m_lower_band[];
double m_tr[];
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
//--- Core logic separated to allow passing different High/Low/Close arrays
void CalculateCore(int rates_total, int start_index, const double &high[], const double &low[], const double &close[], double &osc_out[]);
public: public:
CBollingerATROscillatorCalculator(void) {}; CBollingerATROscillatorCalculator(void) {};
virtual ~CBollingerATROscillatorCalculator(void) {}; virtual ~CBollingerATROscillatorCalculator(void) {};
bool Init(int atr_p, int bb_p, double bb_dev); bool Init(int atr_p, int bb_p, double bb_dev, ENUM_ATR_SOURCE atr_src);
void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
virtual void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
double &osc_out[]); double &osc_out[]);
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CBollingerATROscillatorCalculator: Initialization | //| Init |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CBollingerATROscillatorCalculator::Init(int atr_p, int bb_p, double bb_dev) bool CBollingerATROscillatorCalculator::Init(int atr_p, int bb_p, double bb_dev, ENUM_ATR_SOURCE atr_src)
{ {
m_atr_period = (atr_p < 1) ? 1 : atr_p; m_atr_period = (atr_p < 1) ? 1 : atr_p;
m_bb_period = (bb_p < 1) ? 1 : bb_p; m_bb_period = (bb_p < 1) ? 1 : bb_p;
m_bb_dev = bb_dev; m_bb_dev = bb_dev;
m_atr_source = atr_src;
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CBollingerATROscillatorCalculator: Main Calculation Method | //| Main Calculate (Standard) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CBollingerATROscillatorCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], void CBollingerATROscillatorCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
double &osc_out[]) double &osc_out[])
{ {
int start_pos = MathMax(m_atr_period, m_bb_period); int start_pos = MathMax(m_atr_period, m_bb_period);
if(rates_total <= start_pos) if(rates_total <= start_pos)
return; return;
ArrayResize(m_price, rates_total); int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
ArrayResize(m_atr_buffer, rates_total);
ArrayResize(m_ma_buffer, rates_total);
ArrayResize(m_upper_band, rates_total);
ArrayResize(m_lower_band, rates_total);
if(!PreparePriceSeries(rates_total, price_type, open, high, low, close)) // Resize Buffers
if(ArraySize(m_price) != rates_total)
{
ArrayResize(m_price, rates_total);
ArrayResize(m_atr_buffer, rates_total);
ArrayResize(m_ma_buffer, rates_total);
ArrayResize(m_upper_band, rates_total);
ArrayResize(m_lower_band, rates_total);
ArrayResize(m_tr, rates_total);
}
// Prepare Price (Standard) - Fills m_price for BB calculation
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return; return;
//--- Step 1: Calculate ATR (always on standard candles) // Call Core with Standard Arrays for ATR
double tr[]; CalculateCore(rates_total, start_index, high, low, close, osc_out);
ArrayResize(tr, rates_total); }
for(int i = 1; i < rates_total; i++)
tr[i] = MathMax(high[i], close[i-1]) - MathMin(low[i], close[i-1]);
for(int i = m_atr_period; i < rates_total; i++) //+------------------------------------------------------------------+
//| Core Calculation Logic (ATR + BB + Osc) |
//+------------------------------------------------------------------+
void CBollingerATROscillatorCalculator::CalculateCore(int rates_total, int start_index, const double &high[], const double &low[], const double &close[], double &osc_out[])
{
//--- 1. Calculate ATR (Incremental)
int loop_start_atr = MathMax(m_atr_period, start_index);
// TR Calculation
int tr_start = (start_index < 1) ? 1 : start_index;
for(int i = tr_start; i < rates_total; i++)
m_tr[i] = MathMax(high[i], close[i-1]) - MathMin(low[i], close[i-1]);
for(int i = loop_start_atr; i < rates_total; i++)
{ {
if(i == m_atr_period) if(i == m_atr_period)
{ {
double sum=0; double sum=0;
for(int j=1; j<=m_atr_period; j++) for(int j=1; j<=m_atr_period; j++)
sum+=tr[j]; sum+=m_tr[j];
m_atr_buffer[i]=sum/m_atr_period; m_atr_buffer[i]=sum/m_atr_period;
} }
else else
m_atr_buffer[i] = (m_atr_buffer[i-1] * (m_atr_period - 1) + tr[i]) / m_atr_period; m_atr_buffer[i] = (m_atr_buffer[i-1] * (m_atr_period - 1) + m_tr[i]) / m_atr_period;
} }
//--- Step 2: Calculate Bollinger Bands components (on prepared price) //--- 2. Calculate Bollinger Bands (Incremental)
for(int i = m_bb_period - 1; i < rates_total; i++) // Uses m_price which is already prepared by PreparePriceSeries
int loop_start_bb = MathMax(m_bb_period - 1, start_index);
for(int i = loop_start_bb; i < rates_total; i++)
{ {
// SMA
double sum = 0; double sum = 0;
for(int j = 0; j < m_bb_period; j++) for(int j = 0; j < m_bb_period; j++)
sum += m_price[i-j]; sum += m_price[i-j];
m_ma_buffer[i] = sum / m_bb_period; m_ma_buffer[i] = sum / m_bb_period;
}
for(int i = m_bb_period - 1; i < rates_total; i++) // StdDev
{ double sum_sq = 0;
double std_dev_val = 0, sum_sq = 0;
for(int j = 0; j < m_bb_period; j++) for(int j = 0; j < m_bb_period; j++)
sum_sq += pow(m_price[i-j] - m_ma_buffer[i], 2); sum_sq += pow(m_price[i-j] - m_ma_buffer[i], 2);
std_dev_val = sqrt(sum_sq / m_bb_period); double std_dev = sqrt(sum_sq / m_bb_period);
m_upper_band[i] = m_ma_buffer[i] + m_bb_dev * std_dev_val; m_upper_band[i] = m_ma_buffer[i] + m_bb_dev * std_dev;
m_lower_band[i] = m_ma_buffer[i] - m_bb_dev * std_dev_val; m_lower_band[i] = m_ma_buffer[i] - m_bb_dev * std_dev;
} }
//--- Step 3: Calculate the final Oscillator value //--- 3. Calculate Oscillator
for(int i = start_pos; i < rates_total; i++) int start_pos = MathMax(m_atr_period, m_bb_period);
int loop_start_osc = MathMax(start_pos, start_index);
for(int i = loop_start_osc; i < rates_total; i++)
{ {
double bb_diff = m_upper_band[i] - m_lower_band[i]; double bb_diff = m_upper_band[i] - m_lower_band[i];
if(bb_diff != 0) if(bb_diff != 0)
osc_out[i] = m_atr_buffer[i] / bb_diff; osc_out[i] = m_atr_buffer[i] / bb_diff;
else
osc_out[i] = 0;
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CBollingerATROscillatorCalculator: Prepares the source price. | //| Prepare Price (Standard) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CBollingerATROscillatorCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) bool CBollingerATROscillatorCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
//--- Corrected: Added all price types for(int i = start_index; i < rates_total; i++)
switch(price_type)
{ {
case PRICE_CLOSE: switch(price_type)
ArrayCopy(m_price, close, 0, 0, rates_total); {
break; case PRICE_CLOSE:
case PRICE_OPEN: m_price[i] = close[i];
ArrayCopy(m_price, open, 0, 0, rates_total); break;
break; case PRICE_OPEN:
case PRICE_HIGH: m_price[i] = open[i];
ArrayCopy(m_price, high, 0, 0, rates_total); break;
break; case PRICE_HIGH:
case PRICE_LOW: m_price[i] = high[i];
ArrayCopy(m_price, low, 0, 0, rates_total); break;
break; case PRICE_LOW:
case PRICE_MEDIAN: m_price[i] = low[i];
for(int i=0; i<rates_total; i++) break;
case PRICE_MEDIAN:
m_price[i] = (high[i]+low[i])/2.0; m_price[i] = (high[i]+low[i])/2.0;
break; break;
case PRICE_TYPICAL: case PRICE_TYPICAL:
for(int i=0; i<rates_total; i++)
m_price[i] = (high[i]+low[i]+close[i])/3.0; m_price[i] = (high[i]+low[i]+close[i])/3.0;
break; break;
case PRICE_WEIGHTED: case PRICE_WEIGHTED:
for(int i=0; i<rates_total; i++) m_price[i] = (high[i]+low[i]+2*close[i])/4.0;
m_price[i] = (high[i]+low[i]+close[i]+close[i])/4.0; break;
break; default:
default: m_price[i] = close[i];
return false; break;
}
} }
return true; return true;
} }
//+==================================================================+ //+==================================================================+
//| | //| CLASS 2: CBollingerATROscillatorCalculator_HA |
//| CLASS 2: CBollingerATROscillatorCalculator_HA (Heikin Ashi) |
//| |
//+==================================================================+ //+==================================================================+
class CBollingerATROscillatorCalculator_HA : public CBollingerATROscillatorCalculator class CBollingerATROscillatorCalculator_HA : public CBollingerATROscillatorCalculator
{ {
private: private:
CHeikinAshi_Calculator m_ha_calculator; CHeikinAshi_Calculator m_ha_calculator;
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
protected: protected:
virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); virtual bool PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override;
public:
virtual void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
double &osc_out[]) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| CBollingerATROscillatorCalculator_HA: Prepares the source price. | //| Prepare Price (Heikin Ashi) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CBollingerATROscillatorCalculator_HA::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) bool CBollingerATROscillatorCalculator_HA::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
{ {
double ha_open[], ha_high[], ha_low[], ha_close[]; if(ArraySize(m_ha_open) != rates_total)
ArrayResize(ha_open, rates_total);
ArrayResize(ha_high, rates_total);
ArrayResize(ha_low, rates_total);
ArrayResize(ha_close, rates_total);
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
//--- Corrected: The HA version now also uses the selected price type from the HA candles
switch(price_type)
{ {
case PRICE_CLOSE: ArrayResize(m_ha_open, rates_total);
ArrayCopy(m_price, ha_close, 0, 0, rates_total); ArrayResize(m_ha_high, rates_total);
break; ArrayResize(m_ha_low, rates_total);
case PRICE_OPEN: ArrayResize(m_ha_close, rates_total);
ArrayCopy(m_price, ha_open, 0, 0, rates_total); }
break;
case PRICE_HIGH: m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
ArrayCopy(m_price, ha_high, 0, 0, rates_total); m_ha_open, m_ha_high, m_ha_low, m_ha_close);
break;
case PRICE_LOW: for(int i = start_index; i < rates_total; i++)
ArrayCopy(m_price, ha_low, 0, 0, rates_total); {
break; switch(price_type)
case PRICE_MEDIAN: {
for(int i=0; i<rates_total; i++) case PRICE_CLOSE:
m_price[i] = (ha_high[i]+ha_low[i])/2.0; m_price[i] = m_ha_close[i];
break; break;
case PRICE_TYPICAL: case PRICE_OPEN:
for(int i=0; i<rates_total; i++) m_price[i] = m_ha_open[i];
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i])/3.0; break;
break; case PRICE_HIGH:
case PRICE_WEIGHTED: m_price[i] = m_ha_high[i];
for(int i=0; i<rates_total; i++) break;
m_price[i] = (ha_high[i]+ha_low[i]+ha_close[i]+ha_close[i])/4.0; case PRICE_LOW:
break; m_price[i] = m_ha_low[i];
default: break;
return false; case PRICE_MEDIAN:
m_price[i] = (m_ha_high[i]+m_ha_low[i])/2.0;
break;
case PRICE_TYPICAL:
m_price[i] = (m_ha_high[i]+m_ha_low[i]+m_ha_close[i])/3.0;
break;
case PRICE_WEIGHTED:
m_price[i] = (m_ha_high[i]+m_ha_low[i]+2*m_ha_close[i])/4.0;
break;
default:
m_price[i] = m_ha_close[i];
break;
}
} }
return true; return true;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Calculate (HA Override) |
//+------------------------------------------------------------------+
void CBollingerATROscillatorCalculator_HA::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
double &osc_out[])
{
int start_pos = MathMax(m_atr_period, m_bb_period);
if(rates_total <= start_pos)
return;
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
// Resize Buffers (Same as base)
if(ArraySize(m_price) != rates_total)
{
ArrayResize(m_price, rates_total);
ArrayResize(m_atr_buffer, rates_total);
ArrayResize(m_ma_buffer, rates_total);
ArrayResize(m_upper_band, rates_total);
ArrayResize(m_lower_band, rates_total);
ArrayResize(m_tr, rates_total);
}
// 1. Prepare HA Data (and m_price for BB)
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return;
// 2. Call Core with Selected Arrays for ATR
if(m_atr_source == ATR_SOURCE_HEIKIN_ASHI)
{
// Use HA arrays for ATR
CalculateCore(rates_total, start_index, m_ha_high, m_ha_low, m_ha_close, osc_out);
}
else
{
// Use Standard arrays for ATR (Hybrid mode)
CalculateCore(rates_total, start_index, high, low, close, osc_out);
}
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+