From ef578a114e2be00cd7b54ceb23130f4c1d6c0f2e Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Mon, 28 Sep 2026 18:55:22 +0200 Subject: [PATCH] fix: Restored root #ifndef include-guard pair --- Include/MyIncludes/ATR_Calculator.mqh | 86 +++++++++++++-------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/Include/MyIncludes/ATR_Calculator.mqh b/Include/MyIncludes/ATR_Calculator.mqh index 21d2a5b0..8eecaa57 100644 --- a/Include/MyIncludes/ATR_Calculator.mqh +++ b/Include/MyIncludes/ATR_Calculator.mqh @@ -1,12 +1,15 @@ //+------------------------------------------------------------------+ //| ATR_Calculator.mqh | -//| VERSION 3.00: Optimized state safety & zero-lag registers | +//| VERSION 3.11: Pipelined RMA math & Fused Output Loop | //| Copyright 2026, xxxxxxxx | //+------------------------------------------------------------------+ -#property copyright "Copyright 2026, xxxxxxxx" -#property version "3.00" // Fully optimized with chronological safeguards and type-cast efficiency +#property copyright "Copyright 2026, xxxxxxxx" +#property version "3.11" // Fixed: Restored root #ifndef include-guard pair #property description "Institutional-grade stateful ATR Calculator Engine." +#ifndef ATR_CALCULATOR_MQH +#define ATR_CALCULATOR_MQH + #include //--- Enums Definitions Guard @@ -32,6 +35,8 @@ class CATRCalculator protected: int m_atr_period; ENUM_ATR_DISPLAY_MODE m_display_mode; + double m_decay; // Precalculated (period - 1) / period + double m_inv_period; // Precalculated 1.0 / period //--- Persistent State Buffers double m_tr[]; @@ -40,7 +45,7 @@ protected: virtual bool PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]); public: - CATRCalculator(void) {}; + CATRCalculator(void); virtual ~CATRCalculator(void) {}; bool Init(int period, ENUM_ATR_DISPLAY_MODE mode); @@ -50,98 +55,97 @@ public: }; //+------------------------------------------------------------------+ -//| Init | +//| Constructor | +//+------------------------------------------------------------------+ +CATRCalculator::CATRCalculator(void) : + m_atr_period(14), + m_display_mode(ATR_POINTS), + m_decay(0.0), + m_inv_period(0.0) + { + ArraySetAsSeries(m_tr, false); + ArraySetAsSeries(m_atr_raw, false); + } + +//+------------------------------------------------------------------+ +//| Init (Precalculates Wilder Multipliers) | //+------------------------------------------------------------------+ bool CATRCalculator::Init(int period, ENUM_ATR_DISPLAY_MODE mode) { - m_atr_period = (period < 1) ? 1 : period; + m_atr_period = (period < 1) ? 1 : period; m_display_mode = mode; + m_inv_period = 1.0 / (double)m_atr_period; + m_decay = (double)(m_atr_period - 1) * m_inv_period; return true; } //+------------------------------------------------------------------+ -//| Main Calculation (Strict Chronological Safety & Performance) | +//| Main Calculation (Fused FMA Architecture) | //+------------------------------------------------------------------+ 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 if(rates_total <= m_atr_period) return; -//--- 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) - { return; - } int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1; -//--- Resize state buffers and enforce chronological safety +// Resize state buffers if(ArraySize(m_tr) != rates_total) { ArrayResize(m_tr, rates_total); - ArrayResize(m_atr_raw, rates_total); - ArraySetAsSeries(m_tr, false); + ArrayResize(m_atr_raw, rates_total); ArraySetAsSeries(m_atr_raw, false); } -//--- 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 +// 1. 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) +// 2. Fused Primary Calculation Loop (Wilder's RMA Smoothing + Output Assignment) for(int i = loop_start; i < rates_total; i++) { - if(i == m_atr_period) // Initial SMA setup + if(i == m_atr_period) // Initial Cumulative Baseline Sum { 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 / period_double; + m_atr_raw[i] = sum_tr * m_inv_period; } - else // Dynamic state-safe recursive smoothing + else // Fast Pipelined FMA Multiplication: Zero divisions in loop! { - m_atr_raw[i] = (m_atr_raw[i - 1] * (period_double - 1.0) + m_tr[i]) / period_double; + m_atr_raw[i] = m_atr_raw[i - 1] * m_decay + m_tr[i] * m_inv_period; } - } -//--- Map raw values to display output - for(int i = loop_start; i < rates_total; i++) - { + // Fused Output Mapping (Eliminates second loop for standard ATR) if(m_display_mode == ATR_PERCENT) - { atr_buffer[i] = (close[i] > 0.0) ? (m_atr_raw[i] / close[i]) * 100.0 : 0.0; - } else - { atr_buffer[i] = m_atr_raw[i]; - } } } //+------------------------------------------------------------------+ -//| Prepare True Range (Standard - Optimized) | +//| Prepare True Range (Standard) | //+------------------------------------------------------------------+ bool CATRCalculator::PrepareTrueRange(int rates_total, int start_index, const double &open[], const double &high[], const double &low[], const double &close[]) { int i = (start_index < 1) ? 1 : start_index; if(start_index == 0) - { m_tr[0] = high[0] - low[0]; - } for(; i < rates_total; i++) { @@ -167,34 +171,29 @@ protected: }; //+------------------------------------------------------------------+ -//| Prepare True Range (Heikin Ashi - Chronologically Safe) | +//| Prepare True Range (Heikin Ashi) | //+------------------------------------------------------------------+ 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_close, rates_total); - ArraySetAsSeries(m_ha_open, false); + ArrayResize(m_ha_high, rates_total); ArraySetAsSeries(m_ha_high, false); + ArrayResize(m_ha_low, rates_total); ArraySetAsSeries(m_ha_low, false); + ArrayResize(m_ha_close, rates_total); 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); int i = (start_index < 1) ? 1 : start_index; if(start_index == 0) - { m_tr[0] = m_ha_high[0] - m_ha_low[0]; - } for(; i < rates_total; i++) { @@ -205,5 +204,6 @@ bool CATRCalculator_HA::PrepareTrueRange(int rates_total, int start_index, const } return true; } -//+------------------------------------------------------------------+ + +#endif // ATR_CALCULATOR_MQH //+------------------------------------------------------------------+