mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-23 17:28:06 +00:00
refactor: Uses Stochastic & MA Engines
This commit is contained in:
@@ -1,107 +1,115 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| WPR_Calculator.mqh |
|
//| WPR_Calculator.mqh |
|
||||||
//| Adapter for the StochasticFast_Calculator to produce WPR. |
|
//| VERSION 3.00: Uses Stochastic & MA Engines. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
|
|
||||||
#include <MyIncludes\StochasticFast_Calculator.mqh> // Re-use the Fast Stoch engine
|
#include <MyIncludes\StochasticFast_Calculator.mqh>
|
||||||
|
#include <MyIncludes\MovingAverage_Engine.mqh>
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
//| CLASS: CWPRCalculator |
|
||||||
//| CLASS 1: CWPRCalculator (Base Class) |
|
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CWPRCalculator
|
class CWPRCalculator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
CStochasticFastCalculator *m_stoch_calculator;
|
int m_wpr_period;
|
||||||
|
|
||||||
|
//--- Composition: Use StochFast for core logic + MA Engine for Signal
|
||||||
|
CStochasticFastCalculator *m_stoch_calc;
|
||||||
|
CMovingAverageCalculator m_signal_engine;
|
||||||
|
|
||||||
|
//--- Intermediate Buffer for %K (0..100 range)
|
||||||
|
double m_k_buffer[];
|
||||||
|
//--- Dummy buffer for StochFast %D (we don't use it, but API requires it)
|
||||||
|
double m_dummy_d[];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CWPRCalculator(void);
|
CWPRCalculator(void);
|
||||||
virtual ~CWPRCalculator(void);
|
virtual ~CWPRCalculator(void);
|
||||||
|
|
||||||
bool Init(int wpr_p, int signal_p, ENUM_MA_METHOD signal_ma);
|
//--- Init now takes ENUM_MA_TYPE for Signal
|
||||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
|
bool Init(int wpr_p, int signal_p, ENUM_MA_TYPE signal_ma, bool use_ha);
|
||||||
|
|
||||||
|
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,
|
||||||
double &wpr_buffer[], double &signal_buffer[]);
|
double &wpr_buffer[], double &signal_buffer[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CWPRCalculator: Constructor |
|
//| Constructor |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CWPRCalculator::CWPRCalculator(void)
|
CWPRCalculator::CWPRCalculator(void) : m_stoch_calc(NULL)
|
||||||
{
|
{
|
||||||
m_stoch_calculator = new CStochasticFastCalculator();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CWPRCalculator: Destructor |
|
//| Destructor |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
CWPRCalculator::~CWPRCalculator(void)
|
CWPRCalculator::~CWPRCalculator(void)
|
||||||
{
|
{
|
||||||
if(CheckPointer(m_stoch_calculator) != POINTER_INVALID)
|
if(CheckPointer(m_stoch_calc) != POINTER_INVALID)
|
||||||
delete m_stoch_calculator;
|
delete m_stoch_calc;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CWPRCalculator: Initialization |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CWPRCalculator::Init(int wpr_p, int signal_p, ENUM_MA_METHOD signal_ma)
|
bool CWPRCalculator::Init(int wpr_p, int signal_p, ENUM_MA_TYPE signal_ma, bool use_ha)
|
||||||
{
|
{
|
||||||
if(CheckPointer(m_stoch_calculator) == POINTER_INVALID)
|
m_wpr_period = (wpr_p < 1) ? 1 : wpr_p;
|
||||||
|
|
||||||
|
// Instantiate correct Stoch calculator
|
||||||
|
if(use_ha)
|
||||||
|
m_stoch_calc = new CStochasticFastCalculator_HA();
|
||||||
|
else
|
||||||
|
m_stoch_calc = new CStochasticFastCalculator();
|
||||||
|
|
||||||
|
// Init StochFast. We only care about %K (period = wpr_p).
|
||||||
|
// %D params for StochFast are dummy (1, SMA) as we ignore its %D output.
|
||||||
|
if(!m_stoch_calc.Init(m_wpr_period, 1, SMA))
|
||||||
return false;
|
return false;
|
||||||
// WPR Period is Fast Stoch %K Period, Signal Period is Fast Stoch %D Period
|
|
||||||
return m_stoch_calculator.Init(wpr_p, signal_p, signal_ma);
|
// Init Signal Engine
|
||||||
|
return m_signal_engine.Init(signal_p, signal_ma);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CWPRCalculator: Main Calculation Method |
|
//| Main Calculation |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CWPRCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[],
|
void CWPRCalculator::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 &wpr_buffer[], double &signal_buffer[])
|
double &wpr_buffer[], double &signal_buffer[])
|
||||||
{
|
{
|
||||||
if(CheckPointer(m_stoch_calculator) == POINTER_INVALID)
|
if(rates_total <= m_wpr_period + m_signal_engine.GetPeriod())
|
||||||
|
return;
|
||||||
|
if(CheckPointer(m_stoch_calc) == POINTER_INVALID)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double k_buffer[], d_buffer[];
|
// Resize internal buffers
|
||||||
ArrayResize(k_buffer, rates_total);
|
if(ArraySize(m_k_buffer) != rates_total)
|
||||||
ArrayResize(d_buffer, rates_total);
|
|
||||||
|
|
||||||
m_stoch_calculator.Calculate(rates_total, open, high, low, close, k_buffer, d_buffer);
|
|
||||||
|
|
||||||
for(int i = 0; i < rates_total; i++)
|
|
||||||
{
|
{
|
||||||
if(k_buffer[i] != EMPTY_VALUE)
|
ArrayResize(m_k_buffer, rates_total);
|
||||||
wpr_buffer[i] = k_buffer[i] - 100.0;
|
ArrayResize(m_dummy_d, rates_total);
|
||||||
else
|
|
||||||
wpr_buffer[i] = EMPTY_VALUE;
|
|
||||||
|
|
||||||
if(d_buffer[i] != EMPTY_VALUE)
|
|
||||||
signal_buffer[i] = d_buffer[i] - 100.0;
|
|
||||||
else
|
|
||||||
signal_buffer[i] = EMPTY_VALUE;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//+==================================================================+
|
//--- 1. Calculate %K using StochFast Engine
|
||||||
//| |
|
// This gives us values in 0..100 range
|
||||||
//| CLASS 2: CWPRCalculator_HA (Heikin Ashi) |
|
// Note: StochFast handles incremental logic internally
|
||||||
//| |
|
m_stoch_calc.Calculate(rates_total, prev_calculated, open, high, low, close, m_k_buffer, m_dummy_d);
|
||||||
//+==================================================================+
|
|
||||||
class CWPRCalculator_HA : public CWPRCalculator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CWPRCalculator_HA(void);
|
|
||||||
};
|
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//--- 2. Convert %K to WPR (%R = %K - 100)
|
||||||
//| CWPRCalculator_HA: Constructor |
|
// WPR range is -100..0
|
||||||
//+------------------------------------------------------------------+
|
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||||
CWPRCalculator_HA::CWPRCalculator_HA(void)
|
int loop_start = MathMax(m_wpr_period - 1, start_index);
|
||||||
{
|
|
||||||
if(CheckPointer(m_stoch_calculator) != POINTER_INVALID)
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
delete m_stoch_calculator;
|
{
|
||||||
m_stoch_calculator = new CStochasticFastCalculator_HA();
|
wpr_buffer[i] = m_k_buffer[i] - 100.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- 3. Calculate Signal Line using MA Engine
|
||||||
|
// Offset: m_wpr_period - 1 (same as Stoch %K)
|
||||||
|
m_signal_engine.CalculateOnArray(rates_total, prev_calculated, wpr_buffer, signal_buffer, m_wpr_period - 1);
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user