Files
mql5/Include/MyIncludes/WPR_Calculator.mqh
T

116 lines
4.7 KiB
Plaintext
Raw Normal View History

2025-10-01 19:17:32 +02:00
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
//| WPR_Calculator.mqh |
//| VERSION 3.00: Uses Stochastic & MA Engines. |
2025-10-01 19:17:32 +02:00
//| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
2025-12-19 17:51:23 +01:00
#include <MyIncludes\StochasticFast_Calculator.mqh>
#include <MyIncludes\MovingAverage_Engine.mqh>
2025-10-01 19:17:32 +02:00
//+==================================================================+
2025-12-19 17:51:23 +01:00
//| CLASS: CWPRCalculator |
2025-10-01 19:17:32 +02:00
//+==================================================================+
class CWPRCalculator
{
protected:
2025-12-19 17:51:23 +01:00
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[];
2025-10-01 19:17:32 +02:00
public:
CWPRCalculator(void);
virtual ~CWPRCalculator(void);
2025-12-19 17:51:23 +01:00
//--- Init now takes ENUM_MA_TYPE for Signal
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,
2025-10-01 19:17:32 +02:00
double &wpr_buffer[], double &signal_buffer[]);
};
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
//| Constructor |
2025-10-01 19:17:32 +02:00
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
CWPRCalculator::CWPRCalculator(void) : m_stoch_calc(NULL)
2025-10-01 19:17:32 +02:00
{
}
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
//| Destructor |
2025-10-01 19:17:32 +02:00
//+------------------------------------------------------------------+
CWPRCalculator::~CWPRCalculator(void)
{
2025-12-19 17:51:23 +01:00
if(CheckPointer(m_stoch_calc) != POINTER_INVALID)
delete m_stoch_calc;
2025-10-01 19:17:32 +02:00
}
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
//| Init |
2025-10-01 19:17:32 +02:00
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
bool CWPRCalculator::Init(int wpr_p, int signal_p, ENUM_MA_TYPE signal_ma, bool use_ha)
2025-10-01 19:17:32 +02:00
{
2025-12-19 17:51:23 +01:00
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))
2025-10-01 19:17:32 +02:00
return false;
2025-12-19 17:51:23 +01:00
// Init Signal Engine
return m_signal_engine.Init(signal_p, signal_ma);
2025-10-01 19:17:32 +02:00
}
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
//| Main Calculation |
2025-10-01 19:17:32 +02:00
//+------------------------------------------------------------------+
2025-12-19 17:51:23 +01:00
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,
2025-10-01 19:17:32 +02:00
double &wpr_buffer[], double &signal_buffer[])
{
2025-12-19 17:51:23 +01:00
if(rates_total <= m_wpr_period + m_signal_engine.GetPeriod())
return;
if(CheckPointer(m_stoch_calc) == POINTER_INVALID)
2025-10-01 19:17:32 +02:00
return;
2025-12-19 17:51:23 +01:00
// Resize internal buffers
if(ArraySize(m_k_buffer) != rates_total)
2025-10-01 19:17:32 +02:00
{
2025-12-19 17:51:23 +01:00
ArrayResize(m_k_buffer, rates_total);
ArrayResize(m_dummy_d, rates_total);
2025-10-01 19:17:32 +02:00
}
2025-12-19 17:51:23 +01:00
//--- 1. Calculate %K using StochFast Engine
// This gives us values in 0..100 range
// Note: StochFast handles incremental logic internally
m_stoch_calc.Calculate(rates_total, prev_calculated, open, high, low, close, m_k_buffer, m_dummy_d);
2025-10-01 19:17:32 +02:00
2025-12-19 17:51:23 +01:00
//--- 2. Convert %K to WPR (%R = %K - 100)
// WPR range is -100..0
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
int loop_start = MathMax(m_wpr_period - 1, start_index);
for(int i = loop_start; i < rates_total; i++)
{
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);
2025-10-01 19:17:32 +02:00
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+