mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-26 02:38:05 +00:00
refactor: Optimized for incremental calculation
This commit is contained in:
@@ -1,19 +1,28 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| TSI_Oscillator_Calculator.mqh|
|
//| TSI_Oscillator_Calculator.mqh|
|
||||||
//| Wrapper for the TSI_Engine to produce Oscillator output. |
|
//| Wrapper for the TSI Calculator to produce Oscillator output. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
|
|
||||||
#include <MyIncludes\TSI_Engine.mqh>
|
#include <MyIncludes\TSI_Calculator.mqh>
|
||||||
|
|
||||||
//--- Base class for polymorphism
|
//--- Base class for polymorphism
|
||||||
class CTSICalculatorOscillator
|
class CTSICalculatorOscillator
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
|
//--- Persistent Buffers for Incremental Calculation
|
||||||
|
double m_tsi_buffer[];
|
||||||
|
double m_signal_buffer[];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual bool Init(int slow_p, int fast_p, int signal_p, ENUM_MA_METHOD signal_ma)=0;
|
virtual bool Init(int slow_p, int fast_p, int signal_p, ENUM_MA_METHOD signal_ma)=0;
|
||||||
virtual void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
|
||||||
|
//--- Updated: Accepts prev_calculated
|
||||||
|
virtual 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[],
|
||||||
double &osc_buffer[])=0;
|
double &osc_buffer[])=0;
|
||||||
|
|
||||||
|
virtual ~CTSICalculatorOscillator() {};
|
||||||
};
|
};
|
||||||
|
|
||||||
//--- Standard version
|
//--- Standard version
|
||||||
@@ -25,23 +34,34 @@ public:
|
|||||||
CTSICalculatorOscillator_Std(void) { m_engine = new CTSICalculator(); }
|
CTSICalculatorOscillator_Std(void) { m_engine = new CTSICalculator(); }
|
||||||
~CTSICalculatorOscillator_Std(void) { if(CheckPointer(m_engine)!=POINTER_INVALID) delete m_engine; }
|
~CTSICalculatorOscillator_Std(void) { if(CheckPointer(m_engine)!=POINTER_INVALID) delete m_engine; }
|
||||||
|
|
||||||
virtual bool Init(int slow_p, int fast_p, int signal_p, ENUM_MA_METHOD signal_ma) override { return m_engine.Init(slow_p, fast_p, signal_p, signal_ma); }
|
virtual bool Init(int slow_p, int fast_p, int signal_p, ENUM_MA_METHOD signal_ma) override
|
||||||
virtual void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
{
|
||||||
|
return m_engine.Init(slow_p, fast_p, signal_p, signal_ma);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual 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[],
|
||||||
double &osc_buffer[]) override
|
double &osc_buffer[]) override
|
||||||
{
|
{
|
||||||
if(CheckPointer(m_engine)==POINTER_INVALID)
|
if(CheckPointer(m_engine)==POINTER_INVALID)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double tsi_values[], signal_values[];
|
// Resize internal buffers
|
||||||
ArrayResize(tsi_values, rates_total);
|
if(ArraySize(m_tsi_buffer) != rates_total)
|
||||||
ArrayResize(signal_values, rates_total);
|
ArrayResize(m_tsi_buffer, rates_total);
|
||||||
|
if(ArraySize(m_signal_buffer) != rates_total)
|
||||||
|
ArrayResize(m_signal_buffer, rates_total);
|
||||||
|
|
||||||
m_engine.Calculate(rates_total, price_type, open, high, low, close, tsi_values, signal_values);
|
// Calculate TSI and Signal (Incremental)
|
||||||
|
m_engine.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, m_tsi_buffer, m_signal_buffer);
|
||||||
|
|
||||||
|
// Calculate Oscillator (Incremental Loop)
|
||||||
int start_pos = m_engine.GetPeriodSlow() + m_engine.GetPeriodFast() + m_engine.GetPeriodSignal() - 1;
|
int start_pos = m_engine.GetPeriodSlow() + m_engine.GetPeriodFast() + m_engine.GetPeriodSignal() - 1;
|
||||||
for(int i = start_pos; i < rates_total; i++)
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
int loop_start = MathMax(start_pos, start_index);
|
||||||
|
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
osc_buffer[i] = tsi_values[i] - signal_values[i];
|
osc_buffer[i] = m_tsi_buffer[i] - m_signal_buffer[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -50,28 +70,39 @@ public:
|
|||||||
class CTSICalculatorOscillator_HA : public CTSICalculatorOscillator
|
class CTSICalculatorOscillator_HA : public CTSICalculatorOscillator
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
CTSICalculator *m_engine;
|
CTSICalculator_HA *m_engine;
|
||||||
public:
|
public:
|
||||||
CTSICalculatorOscillator_HA(void) { m_engine = new CTSICalculator_HA(); }
|
CTSICalculatorOscillator_HA(void) { m_engine = new CTSICalculator_HA(); }
|
||||||
~CTSICalculatorOscillator_HA(void) { if(CheckPointer(m_engine)!=POINTER_INVALID) delete m_engine; }
|
~CTSICalculatorOscillator_HA(void) { if(CheckPointer(m_engine)!=POINTER_INVALID) delete m_engine; }
|
||||||
|
|
||||||
virtual bool Init(int slow_p, int fast_p, int signal_p, ENUM_MA_METHOD signal_ma) override { return m_engine.Init(slow_p, fast_p, signal_p, signal_ma); }
|
virtual bool Init(int slow_p, int fast_p, int signal_p, ENUM_MA_METHOD signal_ma) override
|
||||||
virtual void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[],
|
{
|
||||||
|
return m_engine.Init(slow_p, fast_p, signal_p, signal_ma);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual 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[],
|
||||||
double &osc_buffer[]) override
|
double &osc_buffer[]) override
|
||||||
{
|
{
|
||||||
if(CheckPointer(m_engine)==POINTER_INVALID)
|
if(CheckPointer(m_engine)==POINTER_INVALID)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double tsi_values[], signal_values[];
|
// Resize internal buffers
|
||||||
ArrayResize(tsi_values, rates_total);
|
if(ArraySize(m_tsi_buffer) != rates_total)
|
||||||
ArrayResize(signal_values, rates_total);
|
ArrayResize(m_tsi_buffer, rates_total);
|
||||||
|
if(ArraySize(m_signal_buffer) != rates_total)
|
||||||
|
ArrayResize(m_signal_buffer, rates_total);
|
||||||
|
|
||||||
m_engine.Calculate(rates_total, price_type, open, high, low, close, tsi_values, signal_values);
|
// Calculate TSI and Signal (Incremental)
|
||||||
|
m_engine.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, m_tsi_buffer, m_signal_buffer);
|
||||||
|
|
||||||
|
// Calculate Oscillator (Incremental Loop)
|
||||||
int start_pos = m_engine.GetPeriodSlow() + m_engine.GetPeriodFast() + m_engine.GetPeriodSignal() - 1;
|
int start_pos = m_engine.GetPeriodSlow() + m_engine.GetPeriodFast() + m_engine.GetPeriodSignal() - 1;
|
||||||
for(int i = start_pos; i < rates_total; i++)
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
int loop_start = MathMax(start_pos, start_index);
|
||||||
|
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
osc_buffer[i] = tsi_values[i] - signal_values[i];
|
osc_buffer[i] = m_tsi_buffer[i] - m_signal_buffer[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user