From db69c944f63b55d2467e997a6dbec5b06042b1a2 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Wed, 24 Sep 2025 09:15:15 +0200 Subject: [PATCH] new files added --- .../Bollinger_ATR_Oscillator_Calculator.mqh | 213 ++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 Include/MyIncludes/Bollinger_ATR_Oscillator_Calculator.mqh diff --git a/Include/MyIncludes/Bollinger_ATR_Oscillator_Calculator.mqh b/Include/MyIncludes/Bollinger_ATR_Oscillator_Calculator.mqh new file mode 100644 index 0000000..2f859d3 --- /dev/null +++ b/Include/MyIncludes/Bollinger_ATR_Oscillator_Calculator.mqh @@ -0,0 +1,213 @@ +//+------------------------------------------------------------------+ +//| Bollinger_ATR_Oscillator_Calculator.mqh| +//| Calculation engine for Standard and Heikin Ashi BB ATR Osc. | +//| Copyright 2025, xxxxxxxx | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2025, xxxxxxxx" + +#include + +//+==================================================================+ +//| | +//| CLASS 1: CBollingerATROscillatorCalculator (Standard) | +//| | +//+==================================================================+ +class CBollingerATROscillatorCalculator + { +protected: + int m_atr_period; + int m_bb_period; + double m_bb_dev; + + double m_price[]; + double m_atr_buffer[]; + double m_ma_buffer[]; + double m_upper_band[]; + double m_lower_band[]; + + virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); + +public: + CBollingerATROscillatorCalculator(void) {}; + virtual ~CBollingerATROscillatorCalculator(void) {}; + + bool Init(int atr_p, int bb_p, double bb_dev); + void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], + double &osc_out[]); + }; + +//+------------------------------------------------------------------+ +//| CBollingerATROscillatorCalculator: Initialization | +//+------------------------------------------------------------------+ +bool CBollingerATROscillatorCalculator::Init(int atr_p, int bb_p, double bb_dev) + { + m_atr_period = (atr_p < 1) ? 1 : atr_p; + m_bb_period = (bb_p < 1) ? 1 : bb_p; + m_bb_dev = bb_dev; + return true; + } + +//+------------------------------------------------------------------+ +//| CBollingerATROscillatorCalculator: Main Calculation Method | +//+------------------------------------------------------------------+ +void CBollingerATROscillatorCalculator::Calculate(int rates_total, 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; + + 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); + + if(!PreparePriceSeries(rates_total, price_type, open, high, low, close)) + return; + +//--- Step 1: Calculate ATR (always on standard candles) + double tr[]; + 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++) + { + if(i == m_atr_period) + { + double sum=0; + for(int j=1; j<=m_atr_period; j++) + sum+=tr[j]; + m_atr_buffer[i]=sum/m_atr_period; + } + else + m_atr_buffer[i] = (m_atr_buffer[i-1] * (m_atr_period - 1) + tr[i]) / m_atr_period; + } + +//--- Step 2: Calculate Bollinger Bands components (on prepared price) + for(int i = m_bb_period - 1; i < rates_total; i++) + { + double sum = 0; + for(int j = 0; j < m_bb_period; j++) + sum += m_price[i-j]; + m_ma_buffer[i] = sum / m_bb_period; + } + for(int i = m_bb_period - 1; i < rates_total; i++) + { + double std_dev_val = 0, sum_sq = 0; + for(int j = 0; j < m_bb_period; j++) + sum_sq += pow(m_price[i-j] - m_ma_buffer[i], 2); + std_dev_val = sqrt(sum_sq / m_bb_period); + + m_upper_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_val; + } + +//--- Step 3: Calculate the final Oscillator value + for(int i = start_pos; i < rates_total; i++) + { + double bb_diff = m_upper_band[i] - m_lower_band[i]; + if(bb_diff != 0) + osc_out[i] = m_atr_buffer[i] / bb_diff; + } + } + +//+------------------------------------------------------------------+ +//| CBollingerATROscillatorCalculator: Prepares the source price. | +//+------------------------------------------------------------------+ +bool CBollingerATROscillatorCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) + { +//--- Corrected: Added all price types + switch(price_type) + { + case PRICE_CLOSE: + ArrayCopy(m_price, close, 0, 0, rates_total); + break; + case PRICE_OPEN: + ArrayCopy(m_price, open, 0, 0, rates_total); + break; + case PRICE_HIGH: + ArrayCopy(m_price, high, 0, 0, rates_total); + break; + case PRICE_LOW: + ArrayCopy(m_price, low, 0, 0, rates_total); + break; + case PRICE_MEDIAN: + for(int i=0; i