refactor(indicators): Refactored to use RSI_Engine

This commit is contained in:
Toh4iem9
2026-01-12 12:55:01 +01:00
parent 19c511a50a
commit bfebd48890
+31 -36
View File
@@ -1,11 +1,11 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| StochRSI_Slow_Calculator.mqh| //| StochRSI_Slow_Calculator.mqh|
//| VERSION 2.00: Uses MovingAverage_Engine for smoothing. | //| VERSION 3.00: Refactored to use RSI_Engine. |
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#include <MyIncludes\RSI_Pro_Calculator.mqh> #include <MyIncludes\RSI_Engine.mqh>
#include <MyIncludes\MovingAverage_Engine.mqh> #include <MyIncludes\MovingAverage_Engine.mqh>
//+==================================================================+ //+==================================================================+
@@ -17,7 +17,7 @@ protected:
int m_rsi_period, m_k_period; int m_rsi_period, m_k_period;
//--- Composition: RSI Engine + 2 MA Engines //--- Composition: RSI Engine + 2 MA Engines
CRSIProCalculator *m_rsi_calculator; CRSIEngine *m_rsi_engine;
CMovingAverageCalculator m_slowing_engine; // For Slow %K CMovingAverageCalculator m_slowing_engine; // For Slow %K
CMovingAverageCalculator m_signal_engine; // For %D CMovingAverageCalculator m_signal_engine; // For %D
@@ -25,6 +25,9 @@ protected:
double m_rsi_buffer[]; double m_rsi_buffer[];
double m_raw_k[]; double m_raw_k[];
//--- Factory Method for RSI Engine
virtual void CreateRSIEngine(void);
double Highest(const double &array[], int period, int current_pos); double Highest(const double &array[], int period, int current_pos);
double Lowest(const double &array[], int period, int current_pos); double Lowest(const double &array[], int period, int current_pos);
@@ -32,7 +35,6 @@ public:
CStochRSI_Slow_Calculator(void); CStochRSI_Slow_Calculator(void);
virtual ~CStochRSI_Slow_Calculator(void); virtual ~CStochRSI_Slow_Calculator(void);
//--- Init now takes ENUM_MA_TYPE for both smoothings
bool Init(int rsi_p, int k_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma); bool Init(int rsi_p, int k_p, int slow_p, ENUM_MA_TYPE slow_ma, int d_p, ENUM_MA_TYPE d_ma);
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
@@ -44,7 +46,7 @@ public:
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
CStochRSI_Slow_Calculator::CStochRSI_Slow_Calculator(void) CStochRSI_Slow_Calculator::CStochRSI_Slow_Calculator(void)
{ {
m_rsi_calculator = new CRSIProCalculator(); m_rsi_engine = NULL;
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -52,8 +54,16 @@ CStochRSI_Slow_Calculator::CStochRSI_Slow_Calculator(void)
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
CStochRSI_Slow_Calculator::~CStochRSI_Slow_Calculator(void) CStochRSI_Slow_Calculator::~CStochRSI_Slow_Calculator(void)
{ {
if(CheckPointer(m_rsi_calculator) != POINTER_INVALID) if(CheckPointer(m_rsi_engine) != POINTER_INVALID)
delete m_rsi_calculator; delete m_rsi_engine;
}
//+------------------------------------------------------------------+
//| Factory Method |
//+------------------------------------------------------------------+
void CStochRSI_Slow_Calculator::CreateRSIEngine(void)
{
m_rsi_engine = new CRSIEngine();
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -64,14 +74,14 @@ bool CStochRSI_Slow_Calculator::Init(int rsi_p, int k_p, int slow_p, ENUM_MA_TYP
m_rsi_period = (rsi_p < 1) ? 1 : rsi_p; m_rsi_period = (rsi_p < 1) ? 1 : rsi_p;
m_k_period = (k_p < 1) ? 1 : k_p; m_k_period = (k_p < 1) ? 1 : k_p;
if(CheckPointer(m_rsi_calculator) == POINTER_INVALID) CreateRSIEngine();
if(CheckPointer(m_rsi_engine) == POINTER_INVALID)
return false; return false;
// Init RSI calculator if(!m_rsi_engine.Init(m_rsi_period))
if(!m_rsi_calculator.Init(m_rsi_period, 1, SMA, 2.0))
return false; return false;
// Init MA Engines
if(!m_slowing_engine.Init(slow_p, slow_ma)) if(!m_slowing_engine.Init(slow_p, slow_ma))
return false; return false;
if(!m_signal_engine.Init(d_p, d_ma)) if(!m_signal_engine.Init(d_p, d_ma))
@@ -86,14 +96,10 @@ bool CStochRSI_Slow_Calculator::Init(int rsi_p, int k_p, int slow_p, ENUM_MA_TYP
void CStochRSI_Slow_Calculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, void CStochRSI_Slow_Calculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type,
double &k_buffer[], double &d_buffer[]) double &k_buffer[], double &d_buffer[])
{ {
// Minimum bars check
int min_bars = m_rsi_period + m_k_period + m_slowing_engine.GetPeriod() + m_signal_engine.GetPeriod(); int min_bars = m_rsi_period + m_k_period + m_slowing_engine.GetPeriod() + m_signal_engine.GetPeriod();
if(rates_total <= min_bars) if(rates_total <= min_bars)
return; return;
if(CheckPointer(m_rsi_calculator) == POINTER_INVALID)
return;
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1; int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
if(ArraySize(m_rsi_buffer) != rates_total) if(ArraySize(m_rsi_buffer) != rates_total)
@@ -102,14 +108,10 @@ void CStochRSI_Slow_Calculator::Calculate(int rates_total, int prev_calculated,
ArrayResize(m_raw_k, rates_total); ArrayResize(m_raw_k, rates_total);
} }
//--- 1. Calculate RSI (Incremental) //--- 1. Calculate RSI (Using Engine)
double dummy1[], dummy2[], dummy3[]; m_rsi_engine.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, m_rsi_buffer);
m_rsi_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close,
m_rsi_buffer, dummy1, dummy2, dummy3);
//--- 2. Calculate Raw %K (Fast %K) //--- 2. Calculate Raw %K (Fast %K)
// RSI valid from: m_rsi_period
// Raw %K valid from: m_rsi_period + m_k_period - 1
int raw_k_offset = m_rsi_period + m_k_period - 1; int raw_k_offset = m_rsi_period + m_k_period - 1;
int loop_start_k = MathMax(raw_k_offset, start_index); int loop_start_k = MathMax(raw_k_offset, start_index);
@@ -125,17 +127,11 @@ void CStochRSI_Slow_Calculator::Calculate(int rates_total, int prev_calculated,
m_raw_k[i] = (i > 0) ? m_raw_k[i-1] : 50.0; m_raw_k[i] = (i > 0) ? m_raw_k[i-1] : 50.0;
} }
//--- 3. Calculate Slow %K (Main Line) using Slowing Engine //--- 3. Calculate Slow %K (Main Line)
// Input: m_raw_k
// Output: k_buffer
m_slowing_engine.CalculateOnArray(rates_total, prev_calculated, m_raw_k, k_buffer, raw_k_offset); m_slowing_engine.CalculateOnArray(rates_total, prev_calculated, m_raw_k, k_buffer, raw_k_offset);
//--- 4. Calculate %D (Signal Line) using Signal Engine //--- 4. Calculate %D (Signal Line)
// Slow %K valid from: raw_k_offset + slowing_period - 1
int slow_k_offset = raw_k_offset + m_slowing_engine.GetPeriod() - 1; int slow_k_offset = raw_k_offset + m_slowing_engine.GetPeriod() - 1;
// Input: k_buffer
// Output: d_buffer
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, k_buffer, d_buffer, slow_k_offset); m_signal_engine.CalculateOnArray(rates_total, prev_calculated, k_buffer, d_buffer, slow_k_offset);
} }
@@ -178,17 +174,16 @@ double CStochRSI_Slow_Calculator::Lowest(const double &array[], int period, int
//+==================================================================+ //+==================================================================+
class CStochRSI_Slow_Calculator_HA : public CStochRSI_Slow_Calculator class CStochRSI_Slow_Calculator_HA : public CStochRSI_Slow_Calculator
{ {
public: protected:
CStochRSI_Slow_Calculator_HA(void); virtual void CreateRSIEngine(void) override;
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| | //| Factory Method (Heikin Ashi) |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
CStochRSI_Slow_Calculator_HA::CStochRSI_Slow_Calculator_HA(void) void CStochRSI_Slow_Calculator_HA::CreateRSIEngine(void)
{ {
if(CheckPointer(m_rsi_calculator) != POINTER_INVALID) m_rsi_engine = new CRSIEngine_HA();
delete m_rsi_calculator;
m_rsi_calculator = new CRSIProCalculator_HA();
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+