mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-23 17:28:06 +00:00
refactor: Optimized for incremental calculation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| ATR_Calculator.mqh |
|
//| ATR_Calculator.mqh |
|
||||||
//| VERSION 2.10: Added Percent mode. |
|
//| VERSION 2.21: Fixed ATR Percent incremental bug. |
|
||||||
//| Copyright 2025, xxxxxxxx |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
@@ -14,9 +14,7 @@ enum ENUM_CANDLE_SOURCE { CANDLE_STANDARD, CANDLE_HEIKIN_ASHI };
|
|||||||
enum ENUM_ATR_DISPLAY_MODE { ATR_POINTS, ATR_PERCENT };
|
enum ENUM_ATR_DISPLAY_MODE { ATR_POINTS, ATR_PERCENT };
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
|
||||||
//| CLASS 1: CATRCalculator (Base Class) |
|
//| CLASS 1: CATRCalculator (Base Class) |
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CATRCalculator
|
class CATRCalculator
|
||||||
{
|
{
|
||||||
@@ -24,21 +22,26 @@ protected:
|
|||||||
int m_atr_period;
|
int m_atr_period;
|
||||||
ENUM_ATR_DISPLAY_MODE m_display_mode;
|
ENUM_ATR_DISPLAY_MODE m_display_mode;
|
||||||
|
|
||||||
//--- Virtual method for preparing the raw True Range values.
|
//--- Persistent Buffer for True Range and Raw ATR
|
||||||
virtual void PrepareTrueRange(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &tr_buffer[]);
|
double m_tr[];
|
||||||
|
double m_atr_raw[]; // Stores ATR in points for recursion
|
||||||
|
|
||||||
|
//--- Updated: Accepts start_index
|
||||||
|
virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CATRCalculator(void) {};
|
CATRCalculator(void) {};
|
||||||
virtual ~CATRCalculator(void) {};
|
virtual ~CATRCalculator(void) {};
|
||||||
|
|
||||||
//--- Public methods
|
|
||||||
bool Init(int period, ENUM_ATR_DISPLAY_MODE mode);
|
bool Init(int period, ENUM_ATR_DISPLAY_MODE mode);
|
||||||
int GetPeriod(void) const { return m_atr_period; }
|
int GetPeriod(void) const { return m_atr_period; }
|
||||||
void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[]);
|
|
||||||
|
//--- Updated: Accepts prev_calculated
|
||||||
|
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[]);
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CATRCalculator: Initialization |
|
//| Init |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CATRCalculator::Init(int period, ENUM_ATR_DISPLAY_MODE mode)
|
bool CATRCalculator::Init(int period, ENUM_ATR_DISPLAY_MODE mode)
|
||||||
{
|
{
|
||||||
@@ -48,100 +51,125 @@ bool CATRCalculator::Init(int period, ENUM_ATR_DISPLAY_MODE mode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CATRCalculator: Main Calculation Method (Shared Logic) |
|
//| Main Calculation (Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CATRCalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[])
|
void CATRCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[])
|
||||||
{
|
{
|
||||||
if(rates_total <= m_atr_period)
|
if(rates_total <= m_atr_period)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//--- STEP 1: Calculate True Range (delegated to virtual method)
|
//--- 1. Determine Start Index
|
||||||
double tr[];
|
int start_index;
|
||||||
PrepareTrueRange(rates_total, open, high, low, close, tr);
|
if(prev_calculated == 0)
|
||||||
|
start_index = 0;
|
||||||
|
else
|
||||||
|
start_index = prev_calculated - 1;
|
||||||
|
|
||||||
//--- STEP 2: Calculate ATR (Wilder's Smoothing)
|
//--- 2. Resize Buffers
|
||||||
for(int i = 1; i < rates_total; i++)
|
if(ArraySize(m_tr) != rates_total)
|
||||||
{
|
{
|
||||||
if(i == m_atr_period) // Initialization with a simple average of TR
|
ArrayResize(m_tr, rates_total);
|
||||||
|
ArrayResize(m_atr_raw, rates_total);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- 3. Prepare True Range (Optimized)
|
||||||
|
if(!PrepareTrueRange(rates_total, start_index, open, high, low, close))
|
||||||
|
return;
|
||||||
|
|
||||||
|
//--- 4. Calculate ATR (Wilder's Smoothing) using Internal Raw Buffer
|
||||||
|
int loop_start = MathMax(m_atr_period, start_index);
|
||||||
|
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
if(i == m_atr_period) // Initialization
|
||||||
{
|
{
|
||||||
double sum_tr = 0;
|
double sum_tr = 0;
|
||||||
for(int j = 1; j <= m_atr_period; j++)
|
for(int j = 1; j <= m_atr_period; j++)
|
||||||
sum_tr += tr[j];
|
sum_tr += m_tr[j];
|
||||||
atr_buffer[i] = sum_tr / m_atr_period;
|
m_atr_raw[i] = sum_tr / m_atr_period;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if(i > m_atr_period) // Recursive calculation
|
// Recursive calculation uses m_atr_raw[i-1] which is always in POINTS
|
||||||
{
|
m_atr_raw[i] = (m_atr_raw[i-1] * (m_atr_period - 1) + m_tr[i]) / m_atr_period;
|
||||||
atr_buffer[i] = (atr_buffer[i-1] * (m_atr_period - 1) + tr[i]) / m_atr_period;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Step 3: Convert to percentage if requested ---
|
//--- 5. Output to Buffer (Convert if needed)
|
||||||
if(m_display_mode == ATR_PERCENT)
|
// We must update the output buffer from loop_start
|
||||||
|
for(int i = loop_start; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
for(int i = m_atr_period; i < rates_total; i++)
|
if(m_display_mode == ATR_PERCENT)
|
||||||
{
|
{
|
||||||
if(close[i] > 0)
|
if(close[i] > 0)
|
||||||
atr_buffer[i] = (atr_buffer[i] / close[i]) * 100.0;
|
atr_buffer[i] = (m_atr_raw[i] / close[i]) * 100.0;
|
||||||
else
|
else
|
||||||
atr_buffer[i] = 0;
|
atr_buffer[i] = 0;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
atr_buffer[i] = m_atr_raw[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CATRCalculator: Prepares raw TR from standard prices. |
|
//| Prepare True Range (Standard - Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CATRCalculator::PrepareTrueRange(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &tr_buffer[])
|
bool CATRCalculator::PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
ArrayResize(tr_buffer, rates_total);
|
int i = (start_index < 1) ? 1 : start_index;
|
||||||
for(int i = 1; i < rates_total; i++)
|
|
||||||
|
for(; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double range1 = high[i] - low[i];
|
double range1 = high[i] - low[i];
|
||||||
double range2 = MathAbs(high[i] - close[i-1]);
|
double range2 = MathAbs(high[i] - close[i-1]);
|
||||||
double range3 = MathAbs(low[i] - close[i-1]);
|
double range3 = MathAbs(low[i] - close[i-1]);
|
||||||
tr_buffer[i] = MathMax(range1, MathMax(range2, range3));
|
m_tr[i] = MathMax(range1, MathMax(range2, range3));
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
//| |
|
|
||||||
//| CLASS 2: CATRCalculator_HA (Heikin Ashi) |
|
//| CLASS 2: CATRCalculator_HA (Heikin Ashi) |
|
||||||
//| |
|
|
||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CATRCalculator_HA : public CATRCalculator
|
class CATRCalculator_HA : public CATRCalculator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
CHeikinAshi_Calculator m_ha_calculator;
|
CHeikinAshi_Calculator m_ha_calculator;
|
||||||
|
// Internal HA buffers
|
||||||
|
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//--- Overridden method to prepare Heikin Ashi based TR
|
virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||||
virtual void PrepareTrueRange(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &tr_buffer[]) override;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| CATRCalculator_HA: Prepares raw TR from HA prices. |
|
//| Prepare True Range (Heikin Ashi - Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CATRCalculator_HA::PrepareTrueRange(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], double &tr_buffer[])
|
bool CATRCalculator_HA::PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
//--- Intermediate Heikin Ashi Buffers
|
// Resize internal HA buffers
|
||||||
double ha_open[], ha_high[], ha_low[], ha_close[];
|
if(ArraySize(m_ha_open) != rates_total)
|
||||||
ArrayResize(ha_open, rates_total);
|
|
||||||
ArrayResize(ha_high, rates_total);
|
|
||||||
ArrayResize(ha_low, rates_total);
|
|
||||||
ArrayResize(ha_close, rates_total);
|
|
||||||
|
|
||||||
//--- Calculate the HA candles first
|
|
||||||
m_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
|
|
||||||
|
|
||||||
//--- Now, calculate TR using the HA candles
|
|
||||||
ArrayResize(tr_buffer, rates_total);
|
|
||||||
for(int i = 1; i < rates_total; i++)
|
|
||||||
{
|
{
|
||||||
double range1 = ha_high[i] - ha_low[i];
|
ArrayResize(m_ha_open, rates_total);
|
||||||
double range2 = MathAbs(ha_high[i] - ha_close[i-1]);
|
ArrayResize(m_ha_high, rates_total);
|
||||||
double range3 = MathAbs(ha_low[i] - ha_close[i-1]);
|
ArrayResize(m_ha_low, rates_total);
|
||||||
tr_buffer[i] = MathMax(range1, MathMax(range2, range3));
|
ArrayResize(m_ha_close, rates_total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--- STRICT CALL: Use the optimized 10-param HA calculation
|
||||||
|
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
|
||||||
|
m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
||||||
|
|
||||||
|
//--- Calculate TR using HA candles (Optimized loop)
|
||||||
|
int i = (start_index < 1) ? 1 : start_index;
|
||||||
|
|
||||||
|
for(; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
double range1 = m_ha_high[i] - m_ha_low[i];
|
||||||
|
double range2 = MathAbs(m_ha_high[i] - m_ha_close[i-1]);
|
||||||
|
double range3 = MathAbs(m_ha_low[i] - m_ha_close[i-1]);
|
||||||
|
m_tr[i] = MathMax(range1, MathMax(range2, range3));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user