mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-21 16:28:06 +00:00
refactor: Fully corrected class implementation and dynamic initialization
This commit is contained in:
@@ -1,12 +1,22 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Fisher_Transform_Calculator.mqh |
|
//| Fisher_Transform_Calculator.mqh |
|
||||||
//| Calculation engine for the John Ehlers' Fisher Transform. |
|
//| Copyright 2026, xxxxxxxx|
|
||||||
//| VERSION 2.00: Optimized for incremental calculation. |
|
|
||||||
//| Copyright 2025, xxxxxxxx |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
|
#property version "2.12" // Fully corrected class implementation and dynamic initialization
|
||||||
|
|
||||||
|
#ifndef FISHER_TRANSFORM_CALCULATOR_MQH
|
||||||
|
#define FISHER_TRANSFORM_CALCULATOR_MQH
|
||||||
|
|
||||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||||
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
|
|
||||||
|
//--- Enum for Signal Line Type
|
||||||
|
enum ENUM_FISHER_SIGNAL_TYPE
|
||||||
|
{
|
||||||
|
SIGNAL_DELAY_1BAR, // Classic Ehlers (1-Bar Delay)
|
||||||
|
SIGNAL_MA // Custom Moving Average (Supports VWMA)
|
||||||
|
};
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| CLASS 1: CFisherTransformCalculator (Base Class) |
|
//| CLASS 1: CFisherTransformCalculator (Base Class) |
|
||||||
@@ -17,8 +27,17 @@ protected:
|
|||||||
int m_period;
|
int m_period;
|
||||||
double m_alpha;
|
double m_alpha;
|
||||||
|
|
||||||
|
//--- Signal Settings
|
||||||
|
ENUM_FISHER_SIGNAL_TYPE m_signal_type;
|
||||||
|
int m_signal_period;
|
||||||
|
ENUM_MA_TYPE m_signal_method;
|
||||||
|
|
||||||
|
//--- Composition
|
||||||
|
CMovingAverageCalculator *m_signal_engine;
|
||||||
|
|
||||||
//--- Persistent Buffers for Incremental Calculation
|
//--- Persistent Buffers for Incremental Calculation
|
||||||
double m_price[];
|
double m_price[];
|
||||||
|
double m_volume[]; // Local volume double buffer for VWMA support
|
||||||
double m_value1[]; // Smoothed normalized price
|
double m_value1[]; // Smoothed normalized price
|
||||||
double m_fish[]; // Fisher Transform value
|
double m_fish[]; // Fisher Transform value
|
||||||
|
|
||||||
@@ -26,61 +45,110 @@ protected:
|
|||||||
virtual bool PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
|
virtual bool PreparePriceSeries(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CFisherTransformCalculator(void) {};
|
CFisherTransformCalculator(void);
|
||||||
virtual ~CFisherTransformCalculator(void) {};
|
virtual ~CFisherTransformCalculator(void);
|
||||||
|
|
||||||
bool Init(int period, double alpha);
|
bool Init(int period, double alpha, ENUM_FISHER_SIGNAL_TYPE sig_type, int sig_period, ENUM_MA_TYPE sig_method);
|
||||||
|
|
||||||
//--- Updated: Accepts prev_calculated
|
//--- Standard Calculate (Without volume data) - Redirects to overloaded with dummy volume fallback
|
||||||
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
|
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[]);
|
double &fisher_buffer[], double &signal_buffer[]);
|
||||||
|
|
||||||
|
//--- Overloaded Calculate with Volume (Specifically for VWMA support)
|
||||||
|
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||||
|
const long &volume[],
|
||||||
|
double &fisher_buffer[], double &signal_buffer[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Constructor |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
CFisherTransformCalculator::CFisherTransformCalculator(void)
|
||||||
|
{
|
||||||
|
m_signal_engine = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Destructor |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
CFisherTransformCalculator::~CFisherTransformCalculator(void)
|
||||||
|
{
|
||||||
|
if(CheckPointer(m_signal_engine) != POINTER_INVALID)
|
||||||
|
delete m_signal_engine;
|
||||||
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Init |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CFisherTransformCalculator::Init(int period, double alpha)
|
bool CFisherTransformCalculator::Init(int period, double alpha, ENUM_FISHER_SIGNAL_TYPE sig_type, int sig_period, ENUM_MA_TYPE sig_method)
|
||||||
{
|
{
|
||||||
m_period = (period < 2) ? 2 : period;
|
m_period = (period < 2) ? 2 : period;
|
||||||
m_alpha = alpha;
|
m_alpha = alpha;
|
||||||
|
m_signal_type = sig_type;
|
||||||
|
m_signal_period = (sig_period < 1) ? 1 : sig_period;
|
||||||
|
m_signal_method = sig_method;
|
||||||
|
|
||||||
|
if(m_signal_type == SIGNAL_MA)
|
||||||
|
{
|
||||||
|
m_signal_engine = new CMovingAverageCalculator();
|
||||||
|
if(CheckPointer(m_signal_engine) == POINTER_INVALID || !m_signal_engine.Init(m_signal_period, m_signal_method))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Main Calculation (Optimized) |
|
//| Calculate (Standard OHLC) - Dummy Volume Fallback Pattern |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CFisherTransformCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
|
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[])
|
double &fisher_buffer[], double &signal_buffer[])
|
||||||
|
{
|
||||||
|
long dummy_vol[];
|
||||||
|
ArrayResize(dummy_vol, rates_total);
|
||||||
|
ArrayInitialize(dummy_vol, 1);
|
||||||
|
Calculate(rates_total, prev_calculated, open, high, low, close, dummy_vol, fisher_buffer, signal_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Overloaded Calculate (OHLC) with Volume |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void CFisherTransformCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||||
|
const long &volume[],
|
||||||
|
double &fisher_buffer[], double &signal_buffer[])
|
||||||
{
|
{
|
||||||
if(rates_total < m_period)
|
if(rates_total < m_period)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//--- 1. Determine Start Index
|
//--- 1. Determine Start Index
|
||||||
int start_index;
|
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||||
if(prev_calculated == 0)
|
|
||||||
start_index = 0;
|
|
||||||
else
|
|
||||||
start_index = prev_calculated - 1;
|
|
||||||
|
|
||||||
//--- 2. Resize Buffers
|
//--- 2. Resize Buffers & force strict chronological sorting
|
||||||
if(ArraySize(m_price) != rates_total)
|
if(ArraySize(m_price) != rates_total)
|
||||||
{
|
{
|
||||||
ArrayResize(m_price, rates_total);
|
ArrayResize(m_price, rates_total);
|
||||||
|
ArrayResize(m_volume, rates_total);
|
||||||
ArrayResize(m_value1, rates_total);
|
ArrayResize(m_value1, rates_total);
|
||||||
ArrayResize(m_fish, rates_total);
|
ArrayResize(m_fish, rates_total);
|
||||||
|
|
||||||
|
ArraySetAsSeries(m_price, false);
|
||||||
|
ArraySetAsSeries(m_volume, false);
|
||||||
|
ArraySetAsSeries(m_value1, false);
|
||||||
|
ArraySetAsSeries(m_fish, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- 3. Prepare Price (Optimized)
|
//--- 3. Prepare Price (Optimized)
|
||||||
if(!PreparePriceSeries(rates_total, start_index, open, high, low, close))
|
if(!PreparePriceSeries(rates_total, start_index, open, high, low, close))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
for(int i = start_index; i < rates_total; i++)
|
||||||
|
m_volume[i] = (double)volume[i];
|
||||||
|
|
||||||
//--- 4. Calculate Fisher Transform (Incremental Loop)
|
//--- 4. Calculate Fisher Transform (Incremental Loop)
|
||||||
int loop_start = MathMax(m_period - 1, start_index);
|
int loop_start = MathMax(m_period - 1, start_index);
|
||||||
|
|
||||||
for(int i = loop_start; i < rates_total; i++)
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
// Find Highest High and Lowest Low over period
|
// 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 high_idx = ArrayMaximum(m_price, i - m_period + 1, m_period);
|
||||||
int low_idx = ArrayMinimum(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];
|
double maxH = m_price[high_idx];
|
||||||
@@ -90,8 +158,7 @@ void CFisherTransformCalculator::Calculate(int rates_total, int prev_calculated,
|
|||||||
if(maxH - minL != 0)
|
if(maxH - minL != 0)
|
||||||
norm_price = 2.0 * ((m_price[i] - minL) / (maxH - minL) - 0.5);
|
norm_price = 2.0 * ((m_price[i] - minL) / (maxH - minL) - 0.5);
|
||||||
|
|
||||||
// Recursive smoothing
|
// Recursive smoothing using persistent buffer [i-1]
|
||||||
// Use persistent buffer [i-1]
|
|
||||||
double value1_prev = (i > 0) ? m_value1[i-1] : 0;
|
double value1_prev = (i > 0) ? m_value1[i-1] : 0;
|
||||||
m_value1[i] = m_alpha * norm_price + (1.0 - m_alpha) * value1_prev;
|
m_value1[i] = m_alpha * norm_price + (1.0 - m_alpha) * value1_prev;
|
||||||
|
|
||||||
@@ -106,7 +173,21 @@ void CFisherTransformCalculator::Calculate(int rates_total, int prev_calculated,
|
|||||||
m_fish[i] = 0.5 * log((1.0 + m_value1[i]) / (1.0 - m_value1[i])) + 0.5 * fish_prev;
|
m_fish[i] = 0.5 * log((1.0 + m_value1[i]) / (1.0 - m_value1[i])) + 0.5 * fish_prev;
|
||||||
|
|
||||||
fisher_buffer[i] = m_fish[i];
|
fisher_buffer[i] = m_fish[i];
|
||||||
signal_buffer[i] = fish_prev; // Signal is 1-bar delayed Fisher
|
}
|
||||||
|
|
||||||
|
//--- 5. Calculate Signal Line
|
||||||
|
if(m_signal_type == SIGNAL_DELAY_1BAR)
|
||||||
|
{
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
|
signal_buffer[i] = m_fish[i-1];
|
||||||
|
}
|
||||||
|
else // SIGNAL_MA (Smoothed Moving Average supporting Volume-Weighting / VWMA)
|
||||||
|
{
|
||||||
|
if(CheckPointer(m_signal_engine) != POINTER_INVALID)
|
||||||
|
{
|
||||||
|
// Map calculated m_fish buffer as close source, and m_volume double buffer as volume source
|
||||||
|
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, m_fish, m_volume, signal_buffer, m_period - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,6 +229,11 @@ bool CFisherTransformCalculator_HA::PreparePriceSeries(int rates_total, int star
|
|||||||
ArrayResize(m_ha_high, rates_total);
|
ArrayResize(m_ha_high, rates_total);
|
||||||
ArrayResize(m_ha_low, rates_total);
|
ArrayResize(m_ha_low, rates_total);
|
||||||
ArrayResize(m_ha_close, rates_total);
|
ArrayResize(m_ha_close, rates_total);
|
||||||
|
|
||||||
|
ArraySetAsSeries(m_ha_open, false);
|
||||||
|
ArraySetAsSeries(m_ha_high, false);
|
||||||
|
ArraySetAsSeries(m_ha_low, false);
|
||||||
|
ArraySetAsSeries(m_ha_close, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STRICT CALL: Use the optimized 10-param HA calculation
|
//--- STRICT CALL: Use the optimized 10-param HA calculation
|
||||||
@@ -160,4 +246,5 @@ bool CFisherTransformCalculator_HA::PreparePriceSeries(int rates_total, int star
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
#endif // FISHER_TRANSFORM_CALCULATOR_MQH
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user