diff --git a/Include/MyIncludes/DSMA_Calculator.mqh b/Include/MyIncludes/DSMA_Calculator.mqh index 85d9bfb..a1db53b 100644 --- a/Include/MyIncludes/DSMA_Calculator.mqh +++ b/Include/MyIncludes/DSMA_Calculator.mqh @@ -1,6 +1,7 @@ //+------------------------------------------------------------------+ //| DSMA_Calculator.mqh | //| Calculation engine for the John Ehlers' DSMA. | +//| VERSION 2.00: Optimized for incremental calculation. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" @@ -8,26 +9,33 @@ #include //+==================================================================+ -//| | //| CLASS 1: CDSMACalculator (Base Class) | -//| | //+==================================================================+ class CDSMACalculator { protected: int m_period; - double m_price[]; - 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_zeros[]; // Zeros oscillator + double m_filt[]; // Smoothed Zeros + + //--- 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: CDSMACalculator(void) {}; virtual ~CDSMACalculator(void) {}; bool Init(int period); - void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &dsma_buffer[]); + + //--- 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 &dsma_buffer[]); }; +//+------------------------------------------------------------------+ +//| Init | //+------------------------------------------------------------------+ bool CDSMACalculator::Init(int period) { @@ -36,160 +44,198 @@ bool CDSMACalculator::Init(int period) } //+------------------------------------------------------------------+ -void CDSMACalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &dsma_buffer[]) +//| Main Calculation (Optimized) | +//+------------------------------------------------------------------+ +void CDSMACalculator::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 &dsma_buffer[]) { if(rates_total < m_period + 2) return; - if(!PreparePriceSeries(rates_total, price_type, open, high, low, close)) - return; -// --- Intermediate buffers --- - double zeros_buffer[], filt_buffer[]; - ArrayResize(zeros_buffer, rates_total); - ArrayResize(filt_buffer, rates_total); +//--- 1. Determine Start Index + int start_index; + if(prev_calculated == 0) + start_index = 0; + else + start_index = prev_calculated - 1; -// --- Step 1: Calculate "Zeros" oscillator --- - for(int i = 2; i < rates_total; i++) +//--- 2. Resize Buffers + if(ArraySize(m_price) != rates_total) { - zeros_buffer[i] = m_price[i] - m_price[i-2]; + ArrayResize(m_price, rates_total); + ArrayResize(m_zeros, rates_total); + ArrayResize(m_filt, rates_total); } -// --- Step 2: Smooth "Zeros" with a SuperSmoother --- -// Coefficients for SuperSmoother with Period/2 +//--- 3. Prepare Price (Optimized) + if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close)) + return; + +//--- 4. Calculate "Zeros" oscillator (Incremental) + int loop_start_zeros = MathMax(2, start_index); + + for(int i = loop_start_zeros; i < rates_total; i++) + { + m_zeros[i] = m_price[i] - m_price[i-2]; + } + +//--- 5. Smooth "Zeros" with a SuperSmoother (Incremental) int ss_period = m_period / 2; - double arg = 1.414 * M_PI / ss_period; + double arg = M_SQRT2 * M_PI / ss_period; double a1 = exp(-arg); double b1 = 2.0 * a1 * cos(arg); double c2 = b1; double c3 = -a1 * a1; double c1 = 1.0 - c2 - c3; - double filt1=0, filt2=0; // Previous values for SuperSmoother - for(int i = 2; i < rates_total; i++) + int loop_start_filt = MathMax(2, start_index); + + if(loop_start_filt == 2) { - filt_buffer[i] = c1 * (zeros_buffer[i] + zeros_buffer[i-1]) / 2.0 + c2 * filt1 + c3 * filt2; - filt2 = filt1; - filt1 = filt_buffer[i]; + m_filt[0] = 0; + m_filt[1] = 0; } -// --- Steps 3-6: Calculate RMS, Alpha, and final DSMA --- - double dsma_prev = 0; - for(int i = m_period + 1; i < rates_total; i++) + for(int i = loop_start_filt; i < rates_total; i++) + { + // Recursive calculation using persistent buffer [i-1], [i-2] + m_filt[i] = c1 * (m_zeros[i] + m_zeros[i-1]) / 2.0 + c2 * m_filt[i-1] + c3 * m_filt[i-2]; + } + +//--- 6. Calculate DSMA (Incremental) + int loop_start_dsma = MathMax(m_period + 1, start_index); + + if(prev_calculated == 0) + { + // Initialize first value + dsma_buffer[m_period] = m_price[m_period]; + } + + for(int i = loop_start_dsma; i < rates_total; i++) { // Step 3: Compute RMS (Standard Deviation) - double rms = 0; + // Optimization: For large periods, a sliding window sum of squares would be faster. + // But for standard periods (40), a loop is acceptable. + double sum_sq = 0; for(int j = 0; j < m_period; j++) { - rms += filt_buffer[i-j] * filt_buffer[i-j]; + sum_sq += m_filt[i-j] * m_filt[i-j]; } - rms = sqrt(rms / m_period); + double rms = sqrt(sum_sq / m_period); - // Step 4: Rescale Filt in terms of Standard Deviations + // Step 4: Rescale Filt double scaled_filt = 0; if(rms != 0) - scaled_filt = filt_buffer[i] / rms; + scaled_filt = m_filt[i] / rms; // Step 5: Calculate adaptive alpha double alpha1 = fabs(scaled_filt) * 5.0 / m_period; - // Clamp alpha to prevent instability + + // Clamp alpha if(alpha1 > 1.0) alpha1 = 1.0; - if(alpha1 < 2.0 / (m_period + 1.0)) - alpha1 = 2.0 / (m_period + 1.0); // Prevent it from being too slow + // Prevent it from being too slow (optional, but recommended by Ehlers) + // if(alpha1 < 2.0 / (m_period + 1.0)) alpha1 = 2.0 / (m_period + 1.0); - // Step 6: Calculate final DSMA value - if(i == m_period + 1) - dsma_prev = m_price[i]; // Initialize first value - dsma_buffer[i] = alpha1 * m_price[i] + (1.0 - alpha1) * dsma_prev; - dsma_prev = dsma_buffer[i]; + // Step 6: Calculate final DSMA value (Recursive EMA) + // Use dsma_buffer[i-1] which is persistent + dsma_buffer[i] = alpha1 * m_price[i] + (1.0 - alpha1) * dsma_buffer[i-1]; } } //+------------------------------------------------------------------+ -bool CDSMACalculator::PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) +//| Prepare Price (Standard - Optimized) | +//+------------------------------------------------------------------+ +bool CDSMACalculator::PreparePriceSeries(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) { - ArrayResize(m_price, rates_total); - 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