diff --git a/Include/MyIncludes/Fisher_Transform_Calculator.mqh b/Include/MyIncludes/Fisher_Transform_Calculator.mqh index 2ea0a88..1da4af8 100644 --- a/Include/MyIncludes/Fisher_Transform_Calculator.mqh +++ b/Include/MyIncludes/Fisher_Transform_Calculator.mqh @@ -1,6 +1,7 @@ //+------------------------------------------------------------------+ //| Fisher_Transform_Calculator.mqh | //| Calculation engine for the John Ehlers' Fisher Transform. | +//| VERSION 2.00: Optimized for incremental calculation. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" @@ -8,30 +9,35 @@ #include //+==================================================================+ -//| | //| CLASS 1: CFisherTransformCalculator (Base Class) | -//| | //+==================================================================+ class CFisherTransformCalculator { protected: int m_period; double m_alpha; - double m_price[]; // Will hold Median Price (Standard or HA) - // CORRECTED: Added close[] for the derived class - virtual bool PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]); + //--- Persistent Buffers for Incremental Calculation + double m_price[]; + double m_value1[]; // Smoothed normalized price + double m_fish[]; // Fisher Transform value + + //--- Updated: Accepts start_index + virtual bool PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]); public: CFisherTransformCalculator(void) {}; virtual ~CFisherTransformCalculator(void) {}; bool Init(int period, double alpha); - // CORRECTED: Added open[] and close[] for the derived class - void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], + + //--- Updated: Accepts prev_calculated + void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &fisher_buffer[], double &signal_buffer[]); }; +//+------------------------------------------------------------------+ +//| Init | //+------------------------------------------------------------------+ bool CFisherTransformCalculator::Init(int period, double alpha) { @@ -41,22 +47,40 @@ bool CFisherTransformCalculator::Init(int period, double alpha) } //+------------------------------------------------------------------+ -void CFisherTransformCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], +//| Main Calculation (Optimized) | +//+------------------------------------------------------------------+ +void CFisherTransformCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &fisher_buffer[], double &signal_buffer[]) { if(rates_total < m_period) return; - if(!PreparePriceSeries(rates_total, open, high, low, close)) + +//--- 1. Determine Start Index + int start_index; + if(prev_calculated == 0) + start_index = 0; + else + start_index = prev_calculated - 1; + +//--- 2. Resize Buffers + if(ArraySize(m_price) != rates_total) + { + ArrayResize(m_price, rates_total); + ArrayResize(m_value1, rates_total); + ArrayResize(m_fish, rates_total); + } + +//--- 3. Prepare Price (Optimized) + if(!PreparePriceSeries(rates_total, start_index, open, high, low, close)) return; - double value1 = 0, value1_prev = 0; - double fish = 0, fish_prev = 0; +//--- 4. Calculate Fisher Transform (Incremental Loop) + int loop_start = MathMax(m_period - 1, start_index); - for(int i = 0; i < rates_total; i++) + for(int i = loop_start; i < rates_total; i++) { - if(i < m_period -1) - continue; - + // Find Highest High and Lowest Low over period + // Optimization: For small periods (10), loop is fast. int high_idx = ArrayMaximum(m_price, i - m_period + 1, m_period); int low_idx = ArrayMinimum(m_price, i - m_period + 1, m_period); double maxH = m_price[high_idx]; @@ -66,55 +90,74 @@ void CFisherTransformCalculator::Calculate(int rates_total, const double &open[] if(maxH - minL != 0) norm_price = 2.0 * ((m_price[i] - minL) / (maxH - minL) - 0.5); - value1 = m_alpha * norm_price + (1.0 - m_alpha) * value1_prev; + // Recursive smoothing + // Use persistent buffer [i-1] + double value1_prev = (i > 0) ? m_value1[i-1] : 0; + m_value1[i] = m_alpha * norm_price + (1.0 - m_alpha) * value1_prev; - value1 = fmin(0.999, fmax(-0.999, value1)); + // Clamp value to avoid log error + if(m_value1[i] > 0.999) + m_value1[i] = 0.999; + if(m_value1[i] < -0.999) + m_value1[i] = -0.999; - fish = 0.5 * log((1.0 + value1) / (1.0 - value1)) + 0.5 * fish_prev; + // Fisher calculation + double fish_prev = (i > 0) ? m_fish[i-1] : 0; + m_fish[i] = 0.5 * log((1.0 + m_value1[i]) / (1.0 - m_value1[i])) + 0.5 * fish_prev; - fisher_buffer[i] = fish; - signal_buffer[i] = fish_prev; - - value1_prev = value1; - fish_prev = fish; + fisher_buffer[i] = m_fish[i]; + signal_buffer[i] = fish_prev; // Signal is 1-bar delayed Fisher } } //+------------------------------------------------------------------+ -bool CFisherTransformCalculator::PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[]) +//| Prepare Price (Standard - Optimized) | +//+------------------------------------------------------------------+ +bool CFisherTransformCalculator::PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) { - ArrayResize(m_price, rates_total); - for(int i=0; i