diff --git a/Include/MyIncludes/DPO_Calculator.mqh b/Include/MyIncludes/DPO_Calculator.mqh index 4b91593..8539be0 100644 --- a/Include/MyIncludes/DPO_Calculator.mqh +++ b/Include/MyIncludes/DPO_Calculator.mqh @@ -1,64 +1,59 @@ //+------------------------------------------------------------------+ //| DPO_Calculator.mqh | //| Engine for calculating the Detrended Price Oscillator. | +//| VERSION 2.00: Optimized for incremental calculation. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #include +//+==================================================================+ +//| CLASS 1: CDPOCalculator (Base Class) | //+==================================================================+ class CDPOCalculator { protected: int m_period; CMovingAverageCalculator *m_ma_calc; - double m_price[]; // Internal price buffer - virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); + //--- Persistent Buffers for Incremental Calculation + double m_price[]; + double m_ma_buffer[]; + + //--- Updated: Accepts start_index + 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[]); public: CDPOCalculator(void); virtual ~CDPOCalculator(void); bool Init(int period, ENUM_MA_TYPE ma_type); - void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], + + //--- Updated: Accepts prev_calculated + 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 &dpo_buffer[]); }; //+------------------------------------------------------------------+ -//| | +//| Constructor | //+------------------------------------------------------------------+ -class CDPOCalculator_HA : public CDPOCalculator +CDPOCalculator::CDPOCalculator(void) { -public: - CDPOCalculator_HA(void); - }; - -//+==================================================================+ -//| METHOD IMPLEMENTATIONS | -//+==================================================================+ - -//+------------------------------------------------------------------+ -//| | -//+------------------------------------------------------------------+ -CDPOCalculator::CDPOCalculator(void) { m_ma_calc = new CMovingAverageCalculator(); } -//+------------------------------------------------------------------+ -//| | -//+------------------------------------------------------------------+ -CDPOCalculator::~CDPOCalculator(void) { if(CheckPointer(m_ma_calc) != POINTER_INVALID) delete m_ma_calc; } -//+------------------------------------------------------------------+ -//| | -//+------------------------------------------------------------------+ -CDPOCalculator_HA::CDPOCalculator_HA(void) - { - if(CheckPointer(m_ma_calc) != POINTER_INVALID) - delete m_ma_calc; - m_ma_calc = new CMovingAverageCalculator_HA(); + m_ma_calc = new CMovingAverageCalculator(); } //+------------------------------------------------------------------+ -//| | +//| Destructor | +//+------------------------------------------------------------------+ +CDPOCalculator::~CDPOCalculator(void) + { + if(CheckPointer(m_ma_calc) != POINTER_INVALID) + delete m_ma_calc; + } + +//+------------------------------------------------------------------+ +//| Init | //+------------------------------------------------------------------+ bool CDPOCalculator::Init(int period, ENUM_MA_TYPE ma_type) { @@ -69,75 +64,159 @@ bool CDPOCalculator::Init(int period, ENUM_MA_TYPE ma_type) } //+------------------------------------------------------------------+ -//| | +//| Main Calculation (Optimized) | //+------------------------------------------------------------------+ -void CDPOCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], +void CDPOCalculator::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 &dpo_buffer[]) { if(rates_total < m_period) return; if(CheckPointer(m_ma_calc) == POINTER_INVALID) return; - if(!PreparePriceSeries(rates_total, price_type, open, high, low, close)) + + int start_index; + if(prev_calculated == 0) + start_index = 0; + else + start_index = prev_calculated - 1; + +// Resize Buffers + if(ArraySize(m_price) != rates_total) + { + ArrayResize(m_price, rates_total); + ArrayResize(m_ma_buffer, rates_total); + } + + if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close)) return; -//--- Step 1: Calculate the standard, lagging MA into an internal buffer --- - double ma_buffer[]; - ArrayResize(ma_buffer, rates_total); - m_ma_calc.Calculate(rates_total, price_type, open, high, low, close, ma_buffer); +//--- Step 1: Calculate the standard, lagging MA into an internal buffer (Incremental) + m_ma_calc.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, m_ma_buffer); -//--- Step 2: Calculate DPO by subtracting the shifted MA from the price --- +//--- Step 2: Calculate DPO (Incremental Loop) int shift = (m_period / 2) + 1; + int loop_start = MathMax(shift, start_index); - for(int i = 0; i < rates_total; i++) + for(int i = loop_start; i < rates_total; i++) { int source_index = i - shift; - if(source_index >= 0 && ma_buffer[source_index] != EMPTY_VALUE) - dpo_buffer[i] = m_price[i] - ma_buffer[source_index]; + // Ensure we don't access out of bounds or empty values + if(source_index >= 0 && m_ma_buffer[source_index] != EMPTY_VALUE && m_ma_buffer[source_index] != 0.0) + dpo_buffer[i] = m_price[i] - m_ma_buffer[source_index]; else dpo_buffer[i] = EMPTY_VALUE; } } //+------------------------------------------------------------------+ -//| | +//| Prepare Price (Standard - Optimized) | //+------------------------------------------------------------------+ -bool CDPOCalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) +bool CDPOCalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) { - if(ArraySize(m_price) != rates_total) - if(ArrayResize(m_price, rates_total) != rates_total) - return false; - - switch(price_type) + for(int i = start_index; i < rates_total; i++) { - 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