mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
refactor: Fully optimized with chronological safeguards and type-cast efficiency
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| ATR_Calculator.mqh |
|
||||
//| VERSION 2.32: Added strict array bounds safety checks. |
|
||||
//| VERSION 3.00: Optimized state safety & zero-lag registers |
|
||||
//| Copyright 2026, xxxxxxxx |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2026, xxxxxxxx"
|
||||
#property version "3.00" // Fully optimized with chronological safeguards and type-cast efficiency
|
||||
#property description "Institutional-grade stateful ATR Calculator Engine."
|
||||
|
||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||
|
||||
@@ -28,23 +30,23 @@ enum ENUM_ATR_SOURCE
|
||||
class CATRCalculator
|
||||
{
|
||||
protected:
|
||||
int m_atr_period;
|
||||
int m_atr_period;
|
||||
ENUM_ATR_DISPLAY_MODE m_display_mode;
|
||||
|
||||
//--- Persistent Buffer for True Range and Raw ATR
|
||||
double m_tr[];
|
||||
double m_atr_raw[];
|
||||
//--- Persistent State Buffers
|
||||
double m_tr[];
|
||||
double m_atr_raw[];
|
||||
|
||||
virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||
|
||||
public:
|
||||
CATRCalculator(void) {};
|
||||
virtual ~CATRCalculator(void) {};
|
||||
virtual ~CATRCalculator(void) {};
|
||||
|
||||
bool Init(int period, ENUM_ATR_DISPLAY_MODE mode);
|
||||
int GetPeriod(void) const { return m_atr_period; }
|
||||
bool Init(int period, ENUM_ATR_DISPLAY_MODE mode);
|
||||
int GetPeriod(void) const { return m_atr_period; }
|
||||
|
||||
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[]);
|
||||
void Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[]);
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -58,66 +60,69 @@ bool CATRCalculator::Init(int period, ENUM_ATR_DISPLAY_MODE mode)
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Main Calculation (Strict Safety) |
|
||||
//| Main Calculation (Strict Chronological Safety & Performance) |
|
||||
//+------------------------------------------------------------------+
|
||||
void CATRCalculator::Calculate(int rates_total, int prev_calculated, const double &open[], const double &high[], const double &low[], const double &close[], double &atr_buffer[])
|
||||
{
|
||||
// Safety 1: Period Check
|
||||
//--- Safety 1: Period check
|
||||
if(rates_total <= m_atr_period)
|
||||
return;
|
||||
|
||||
// Safety 2: Array Bounds Check (Crucial Fix)
|
||||
// Ensure all input arrays are at least as large as the loop limit (rates_total)
|
||||
//--- Safety 2: Boundary check to prevent access violations
|
||||
if(ArraySize(open) < rates_total || ArraySize(high) < rates_total ||
|
||||
ArraySize(low) < rates_total || ArraySize(close) < rates_total)
|
||||
{
|
||||
// Log error (optional) and exit to prevent crash
|
||||
return;
|
||||
}
|
||||
|
||||
int start_index;
|
||||
if(prev_calculated == 0)
|
||||
start_index = 0;
|
||||
else
|
||||
start_index = prev_calculated - 1;
|
||||
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
|
||||
|
||||
// Resize internal buffers
|
||||
//--- Resize state buffers and enforce chronological safety
|
||||
if(ArraySize(m_tr) != rates_total)
|
||||
{
|
||||
ArrayResize(m_tr, rates_total);
|
||||
ArrayResize(m_tr, rates_total);
|
||||
ArrayResize(m_atr_raw, rates_total);
|
||||
|
||||
ArraySetAsSeries(m_tr, false);
|
||||
ArraySetAsSeries(m_atr_raw, false);
|
||||
}
|
||||
|
||||
// Resize output buffer if needed (usually handled by caller, but safety first)
|
||||
//--- Enforce chronological safety on caller output buffer if resized
|
||||
if(ArraySize(atr_buffer) != rates_total)
|
||||
{
|
||||
ArrayResize(atr_buffer, rates_total);
|
||||
ArraySetAsSeries(atr_buffer, false);
|
||||
}
|
||||
|
||||
//--- Prepare True Range
|
||||
if(!PrepareTrueRange(rates_total, start_index, open, high, low, close))
|
||||
return;
|
||||
|
||||
int loop_start = MathMax(m_atr_period, start_index);
|
||||
double period_double = (double)m_atr_period;
|
||||
|
||||
//--- Primary calculation loop (Wilder's RMA smoothing)
|
||||
for(int i = loop_start; i < rates_total; i++)
|
||||
{
|
||||
if(i == m_atr_period) // Initialization (SMA)
|
||||
if(i == m_atr_period) // Initial SMA setup
|
||||
{
|
||||
double sum_tr = 0;
|
||||
double sum_tr = 0.0;
|
||||
for(int j = 0; j < m_atr_period; j++)
|
||||
sum_tr += m_tr[i-j];
|
||||
m_atr_raw[i] = sum_tr / m_atr_period;
|
||||
sum_tr += m_tr[i - j];
|
||||
m_atr_raw[i] = sum_tr / period_double;
|
||||
}
|
||||
else // Dynamic state-safe recursive smoothing
|
||||
{
|
||||
m_atr_raw[i] = (m_atr_raw[i - 1] * (period_double - 1.0) + m_tr[i]) / period_double;
|
||||
}
|
||||
else // Smoothing (RMA/Wilder's)
|
||||
m_atr_raw[i] = (m_atr_raw[i-1] * (m_atr_period - 1) + m_tr[i]) / m_atr_period;
|
||||
}
|
||||
|
||||
//--- Map raw values to display output
|
||||
for(int i = loop_start; i < rates_total; i++)
|
||||
{
|
||||
if(m_display_mode == ATR_PERCENT)
|
||||
{
|
||||
if(close[i] > 0)
|
||||
atr_buffer[i] = (m_atr_raw[i] / close[i]) * 100.0;
|
||||
else
|
||||
atr_buffer[i] = 0;
|
||||
atr_buffer[i] = (close[i] > 0.0) ? (m_atr_raw[i] / close[i]) * 100.0 : 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -127,14 +132,12 @@ void CATRCalculator::Calculate(int rates_total, int prev_calculated, const doubl
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Prepare True Range (Standard) |
|
||||
//| Prepare True Range (Standard - Optimized) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CATRCalculator::PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
// Correct logic: Start from 1 to allow [i-1] access
|
||||
int i = (start_index < 1) ? 1 : start_index;
|
||||
|
||||
// Handle special case for index 0 (if full recalc)
|
||||
if(start_index == 0)
|
||||
{
|
||||
m_tr[0] = high[0] - low[0];
|
||||
@@ -143,9 +146,8 @@ bool CATRCalculator::PrepareTrueRange(int rates_total, int start_index, const do
|
||||
for(; i < rates_total; i++)
|
||||
{
|
||||
double range1 = high[i] - low[i];
|
||||
// Bound check implicitly handled by Calculate's Safety 2, but logic ensures i-1 >= 0
|
||||
double range2 = MathAbs(high[i] - close[i-1]);
|
||||
double range3 = MathAbs(low[i] - close[i-1]);
|
||||
double range2 = MathAbs(high[i] - close[i - 1]);
|
||||
double range3 = MathAbs(low[i] - close[i - 1]);
|
||||
m_tr[i] = MathMax(range1, MathMax(range2, range3));
|
||||
}
|
||||
return true;
|
||||
@@ -158,25 +160,32 @@ class CATRCalculator_HA : public CATRCalculator
|
||||
{
|
||||
private:
|
||||
CHeikinAshi_Calculator m_ha_calculator;
|
||||
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||
double m_ha_open[], m_ha_high[], m_ha_low[], m_ha_close[];
|
||||
|
||||
protected:
|
||||
virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||
virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) override;
|
||||
};
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Prepare True Range (Heikin Ashi) |
|
||||
//| Prepare True Range (Heikin Ashi - Chronologically Safe) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CATRCalculator_HA::PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||
{
|
||||
//--- Resize HA caches and enforce chronological alignment
|
||||
if(ArraySize(m_ha_open) != rates_total)
|
||||
{
|
||||
ArrayResize(m_ha_open, rates_total);
|
||||
ArrayResize(m_ha_high, rates_total);
|
||||
ArrayResize(m_ha_low, rates_total);
|
||||
ArrayResize(m_ha_open, rates_total);
|
||||
ArrayResize(m_ha_high, rates_total);
|
||||
ArrayResize(m_ha_low, rates_total);
|
||||
ArrayResize(m_ha_close, rates_total);
|
||||
|
||||
ArraySetAsSeries(m_ha_open, false);
|
||||
ArraySetAsSeries(m_ha_high, false);
|
||||
ArraySetAsSeries(m_ha_low, false);
|
||||
ArraySetAsSeries(m_ha_close, false);
|
||||
}
|
||||
|
||||
//--- Delegate calculation to Heikin Ashi core toolkit
|
||||
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,
|
||||
m_ha_open, m_ha_high, m_ha_low, m_ha_close);
|
||||
|
||||
@@ -190,8 +199,8 @@ bool CATRCalculator_HA::PrepareTrueRange(int rates_total, int start_index, const
|
||||
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]);
|
||||
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