mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-20 15:58:07 +00:00
refactor(indicators):
This commit is contained in:
@@ -1,75 +1,112 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CCI_Oscillator_Calculator.mqh|
|
//| CCI_Oscillator_Calculator.mqh|
|
||||||
//| Wrapper for the CCI_Engine to produce Oscillator output. |
|
//| Wrapper for the CCI Calculator to produce Oscillator output. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
|
|
||||||
#include <MyIncludes\CCI_Engine.mqh>
|
#include <MyIncludes\CCI_Calculator.mqh>
|
||||||
|
|
||||||
//--- Base class for polymorphism
|
//+==================================================================+
|
||||||
|
//| CLASS: CCCI_OscillatorCalculator |
|
||||||
|
//+==================================================================+
|
||||||
class CCCI_OscillatorCalculator
|
class CCCI_OscillatorCalculator
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
virtual bool Init(int cci_p, int ma_p, ENUM_MA_METHOD ma_m)=0;
|
|
||||||
virtual void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &osc_buffer[])=0;
|
|
||||||
};
|
|
||||||
|
|
||||||
//--- Standard version
|
|
||||||
class CCCI_OscillatorCalculator_Std : public CCCI_OscillatorCalculator
|
|
||||||
{
|
|
||||||
protected:
|
protected:
|
||||||
CCCI_Engine *m_engine;
|
//--- Composition: Use the main CCI Calculator
|
||||||
|
CCCI_Calculator *m_cci_engine;
|
||||||
|
|
||||||
|
//--- Persistent Buffers for Incremental Calculation
|
||||||
|
double m_cci_buffer[];
|
||||||
|
double m_signal_buffer[];
|
||||||
|
double m_upper_dummy[]; // Not used for oscillator
|
||||||
|
double m_lower_dummy[]; // Not used for oscillator
|
||||||
|
|
||||||
|
int m_cci_period;
|
||||||
|
int m_ma_period;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CCCI_OscillatorCalculator_Std(void) { m_engine = new CCCI_Engine(); }
|
CCCI_OscillatorCalculator(void);
|
||||||
~CCCI_OscillatorCalculator_Std(void) { if(CheckPointer(m_engine)!=POINTER_INVALID) delete m_engine; }
|
virtual ~CCCI_OscillatorCalculator(void);
|
||||||
|
|
||||||
virtual bool Init(int cci_p, int ma_p, ENUM_MA_METHOD ma_m) override { return m_engine.Init(cci_p, ma_p, ma_m); }
|
//--- Init now takes ENUM_MA_TYPE and HA flag
|
||||||
virtual void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &osc_buffer[]) override
|
bool Init(int cci_p, int ma_p, ENUM_MA_TYPE ma_m, bool use_ha);
|
||||||
{
|
|
||||||
if(CheckPointer(m_engine)==POINTER_INVALID)
|
|
||||||
return;
|
|
||||||
|
|
||||||
double cci_values[], signal_values[];
|
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||||
ArrayResize(cci_values, rates_total);
|
double &osc_buffer[]);
|
||||||
ArrayResize(signal_values, rates_total);
|
|
||||||
|
|
||||||
m_engine.Calculate(rates_total, open, high, low, close, price_type, cci_values, signal_values);
|
|
||||||
|
|
||||||
int start_pos = m_engine.GetPeriodCCI() + m_engine.GetPeriodMA() - 2;
|
|
||||||
for(int i = start_pos; i < rates_total; i++)
|
|
||||||
{
|
|
||||||
osc_buffer[i] = cci_values[i] - signal_values[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//--- HA version
|
//+------------------------------------------------------------------+
|
||||||
class CCCI_OscillatorCalculator_HA : public CCCI_OscillatorCalculator
|
//| Constructor |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
CCCI_OscillatorCalculator::CCCI_OscillatorCalculator(void) : m_cci_engine(NULL)
|
||||||
{
|
{
|
||||||
protected:
|
}
|
||||||
CCCI_Engine *m_engine;
|
|
||||||
public:
|
//+------------------------------------------------------------------+
|
||||||
CCCI_OscillatorCalculator_HA(void) { m_engine = new CCCI_Engine_HA(); }
|
//| Destructor |
|
||||||
~CCCI_OscillatorCalculator_HA(void) { if(CheckPointer(m_engine)!=POINTER_INVALID) delete m_engine; }
|
//+------------------------------------------------------------------+
|
||||||
|
CCCI_OscillatorCalculator::~CCCI_OscillatorCalculator(void)
|
||||||
virtual bool Init(int cci_p, int ma_p, ENUM_MA_METHOD ma_m) override { return m_engine.Init(cci_p, ma_p, ma_m); }
|
{
|
||||||
virtual void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &osc_buffer[]) override
|
if(CheckPointer(m_cci_engine) != POINTER_INVALID)
|
||||||
|
delete m_cci_engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Init |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool CCCI_OscillatorCalculator::Init(int cci_p, int ma_p, ENUM_MA_TYPE ma_m, bool use_ha)
|
||||||
|
{
|
||||||
|
m_cci_period = cci_p;
|
||||||
|
m_ma_period = ma_p;
|
||||||
|
|
||||||
|
// Instantiate correct engine
|
||||||
|
if(use_ha)
|
||||||
|
m_cci_engine = new CCCI_Calculator_HA();
|
||||||
|
else
|
||||||
|
m_cci_engine = new CCCI_Calculator();
|
||||||
|
|
||||||
|
// Initialize engine (Bands params are dummy as we don't use them)
|
||||||
|
return m_cci_engine.Init(cci_p, ma_p, ma_m, 14, 2.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Main Calculation |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void CCCI_OscillatorCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
||||||
|
double &osc_buffer[])
|
||||||
|
{
|
||||||
|
if(CheckPointer(m_cci_engine) == POINTER_INVALID)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Resize internal buffers
|
||||||
|
if(ArraySize(m_cci_buffer) != rates_total)
|
||||||
{
|
{
|
||||||
if(CheckPointer(m_engine)==POINTER_INVALID)
|
ArrayResize(m_cci_buffer, rates_total);
|
||||||
return;
|
ArrayResize(m_signal_buffer, rates_total);
|
||||||
|
ArrayResize(m_upper_dummy, rates_total);
|
||||||
|
ArrayResize(m_lower_dummy, rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
double cci_values[], signal_values[];
|
// Calculate CCI and Signal (Incremental)
|
||||||
ArrayResize(cci_values, rates_total);
|
// The CCI engine handles its own incremental logic
|
||||||
ArrayResize(signal_values, rates_total);
|
m_cci_engine.Calculate(rates_total, prev_calculated, price_type, open, high, low, close,
|
||||||
|
m_cci_buffer, m_signal_buffer, m_upper_dummy, m_lower_dummy);
|
||||||
|
|
||||||
m_engine.Calculate(rates_total, open, high, low, close, price_type, cci_values, signal_values);
|
// Calculate Oscillator (CCI - Signal)
|
||||||
|
// Valid from: CCI Period + MA Period - 2
|
||||||
|
int start_pos = m_cci_period + m_ma_period - 2;
|
||||||
|
|
||||||
int start_pos = m_engine.GetPeriodCCI() + m_engine.GetPeriodMA() - 2;
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
for(int i = start_pos; i < rates_total; i++)
|
int loop_start = MathMax(start_pos, start_index);
|
||||||
{
|
|
||||||
osc_buffer[i] = cci_values[i] - signal_values[i];
|
if(prev_calculated == 0)
|
||||||
}
|
ArrayInitialize(osc_buffer, 0.0);
|
||||||
|
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
osc_buffer[i] = m_cci_buffer[i] - m_signal_buffer[i];
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user