refactor: Standardized state safety and dynamic array index guards

This commit is contained in:
Toh4iem9
2026-06-29 11:59:59 +02:00
parent 2dc4d572ea
commit ba8fe2374f
@@ -1,11 +1,10 @@
//+------------------------------------------------------------------+
//| MovingAverage_Anchored_Engine.mqh|
//| Perry Kaufman & Welles Wilder Dynamic Anchored MA Engine. |
//| VERSION 1.10: Fixed missing standard Calculate body |
//| Copyright 2026, xxxxxxxx |
//| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, xxxxxxxx"
#property version "1.10" // Fully implemented both standard and volume-based Calculate bodies
#property version "1.27" // Standardized state safety and dynamic array index guards
#property description "Perry Kaufman & Welles Wilder Dynamic Anchored MA Engine."
#ifndef MOVING_AVERAGE_ANCHORED_ENGINE_MQH
#define MOVING_AVERAGE_ANCHORED_ENGINE_MQH
@@ -64,19 +63,32 @@ public:
bool Init(int period, ENUM_MA_TYPE ma_type, ENUM_ANCHOR_PERIOD anchor, string custom_start="09:00", string custom_end="18:00");
//--- Standard Calculate without Volume
//--- Standard Calculate with Gapped Segments (Odd & Even)
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
double &ma_odd[], double &ma_even[]);
//--- Overloaded Calculate with Volume (for VWMA support)
//--- Overloaded Calculate with Volume and Gapped Segments (for VWMA support)
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &volume[],
double &ma_odd[], double &ma_even[]);
//--- Standard Calculate with Continuous output (No Gaps - needed for Z-Score mean baseline)
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
double &ma_buffer[]);
//--- Overloaded Calculate with Volume and Continuous output (for VWMA baseline support)
void Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &volume[],
double &ma_buffer[]);
//--- Getter for anchor start index (O(1) complexity)
int GetAnchorStart(int index) const { return m_anchor_start[index]; }
int GetPeriod(void) const { return m_period; }
@@ -140,7 +152,7 @@ bool CMovingAverageAnchoredCalculator::IsTimeInSession(datetime time_val)
double CMovingAverageAnchoredCalculator::CalculateDynamicEMA(int idx, int active_p, double val, double &ema_array[])
{
double pr = 2.0 / (double)(active_p + 1.0);
if(idx == m_anchor_start[idx] || ema_array[idx-1] == EMPTY_VALUE || ema_array[idx-1] == 0)
if(idx == m_anchor_start[idx] || ema_array[idx-1] == EMPTY_VALUE)
ema_array[idx] = val;
else
ema_array[idx] = val * pr + ema_array[idx-1] * (1.0 - pr);
@@ -148,7 +160,7 @@ double CMovingAverageAnchoredCalculator::CalculateDynamicEMA(int idx, int active
}
//+------------------------------------------------------------------+
//| Calculate (Standard - No Volume) (FIXED: Added continuous body) |
//| Calculate (Segmented - No Volume) |
//+------------------------------------------------------------------+
void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
@@ -162,7 +174,7 @@ void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calcu
}
//+------------------------------------------------------------------+
//| Calculate (Overloaded - With Volume for VWMA) |
//| Calculate (Segmented - Overloaded - With Volume for VWMA) |
//+------------------------------------------------------------------+
void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
@@ -186,6 +198,15 @@ void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calcu
ArrayResize(m_temp_ema1, rates_total);
ArrayResize(m_temp_ema2, rates_total);
ArrayResize(m_temp_ema3, rates_total);
ArraySetAsSeries(m_price, false);
ArraySetAsSeries(m_volume, false);
ArraySetAsSeries(m_ma_internal, false);
ArraySetAsSeries(m_anchor_start, false);
ArraySetAsSeries(m_period_idx, false);
ArraySetAsSeries(m_temp_ema1, false);
ArraySetAsSeries(m_temp_ema2, false);
ArraySetAsSeries(m_temp_ema3, false);
}
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
@@ -390,6 +411,246 @@ void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calcu
}
}
//+------------------------------------------------------------------+
//| NEW OVERLOAD: Calculate (Continuous - Standard - No Volume) |
//+------------------------------------------------------------------+
void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
double &ma_buffer[])
{
long dummy_vol[];
ArrayResize(dummy_vol, rates_total);
ArrayInitialize(dummy_vol, 1);
Calculate(rates_total, prev_calculated, price_type, time, open, high, low, close, dummy_vol, ma_buffer);
}
//+------------------------------------------------------------------+
//| NEW OVERLOAD: Calculate (Continuous - With Volume) |
//+------------------------------------------------------------------+
void CMovingAverageAnchoredCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &volume[],
double &ma_buffer[])
{
if(rates_total < m_period)
return;
int start_index = (prev_calculated == 0) ? 0 : prev_calculated - 1;
// Resize Buffers
if(ArraySize(m_price) != rates_total)
{
ArrayResize(m_price, rates_total);
ArrayResize(m_volume, rates_total);
ArrayResize(m_ma_internal, rates_total);
ArrayResize(m_anchor_start, rates_total);
ArrayResize(m_period_idx, rates_total);
ArrayResize(m_temp_ema1, rates_total);
ArrayResize(m_temp_ema2, rates_total);
ArrayResize(m_temp_ema3, rates_total);
ArraySetAsSeries(m_price, false);
ArraySetAsSeries(m_volume, false);
ArraySetAsSeries(m_ma_internal, false);
ArraySetAsSeries(m_anchor_start, false);
ArraySetAsSeries(m_period_idx, false);
ArraySetAsSeries(m_temp_ema1, false);
ArraySetAsSeries(m_temp_ema2, false);
ArraySetAsSeries(m_temp_ema3, false);
}
if(!PreparePriceSeries(rates_total, start_index, price_type, open, high, low, close))
return;
for(int i = start_index; i < rates_total; i++)
m_volume[i] = (double)volume[i];
if(start_index == 0)
{
m_anchor_start[0] = 0;
m_period_idx[0] = 1;
m_ma_internal[0] = m_price[0];
m_temp_ema1[0] = m_price[0];
m_temp_ema2[0] = m_price[0];
m_temp_ema3[0] = m_price[0];
ma_buffer[0] = m_price[0];
start_index = 1;
}
for(int i = start_index; i < rates_total; i++)
{
bool new_period = false;
switch(m_anchor)
{
case ANCHOR_SESSION:
{
MqlDateTime dt_curr, dt_prev;
TimeToStruct(time[i], dt_curr);
TimeToStruct(time[i-1], dt_prev);
if(dt_curr.day_of_year != dt_prev.day_of_year || dt_curr.year != dt_prev.year)
new_period = true;
break;
}
case ANCHOR_WEEK:
{
MqlDateTime dt_curr, dt_prev;
TimeToStruct(time[i], dt_curr);
TimeToStruct(time[i-1], dt_prev);
if(dt_curr.day_of_week < dt_prev.day_of_week)
new_period = true;
break;
}
case ANCHOR_MONTH:
{
MqlDateTime dt_curr, dt_prev;
TimeToStruct(time[i], dt_curr);
TimeToStruct(time[i-1], dt_prev);
if(dt_curr.mon != dt_prev.mon || dt_curr.year != dt_prev.year)
new_period = true;
break;
}
case ANCHOR_CUSTOM_SESSION:
{
MqlDateTime dt_curr, dt_prev;
TimeToStruct(time[i], dt_curr);
TimeToStruct(time[i-1], dt_prev);
int min_curr = dt_curr.hour * 60 + dt_curr.min;
int min_prev = dt_prev.hour * 60 + dt_prev.min;
int start_min = m_start_hour * 60 + m_start_min;
bool day_changed = (dt_curr.day_of_year != dt_prev.day_of_year || dt_curr.year != dt_prev.year);
if(day_changed)
{
if(min_curr >= start_min)
new_period = true;
}
else
{
if(min_prev < start_min && min_curr >= start_min)
new_period = true;
}
break;
}
default:
break;
}
if(new_period)
{
m_anchor_start[i] = i;
m_period_idx[i] = m_period_idx[i-1] + 1;
}
else
{
m_anchor_start[i] = m_anchor_start[i-1];
m_period_idx[i] = m_period_idx[i-1];
}
int current_anchor_idx = m_anchor_start[i];
if(i == current_anchor_idx)
{
m_ma_internal[i] = m_price[i];
m_temp_ema1[i] = m_price[i];
m_temp_ema2[i] = m_price[i];
m_temp_ema3[i] = m_price[i];
}
else
{
int elapsed_bars = i - current_anchor_idx + 1;
int active_p = MathMin(m_period, elapsed_bars);
switch(m_ma_type)
{
case EMA:
{
m_ma_internal[i] = CalculateDynamicEMA(i, active_p, m_price[i], m_temp_ema1);
break;
}
case SMMA:
{
double pr = 1.0 / (double)active_p;
m_ma_internal[i] = m_price[i] * pr + m_ma_internal[i-1] * (1.0 - pr);
break;
}
case LWMA:
{
double sum = 0, w_sum = 0;
for(int k = 0; k < active_p; k++)
{
int w = active_p - k;
sum += m_price[i-k] * w;
w_sum += w;
}
m_ma_internal[i] = (w_sum > 0) ? (sum / w_sum) : m_price[i];
break;
}
case TMA:
{
int period1 = (int)ceil((active_p + 1.0) / 2.0);
double sum_tp = 0;
int count_tp = 0;
for(int j = 0; j < period1; j++)
{
sum_tp += m_price[i-j];
count_tp++;
}
m_temp_ema1[i] = (count_tp > 0) ? (sum_tp / count_tp) : m_price[i];
int period2 = active_p - period1 + 1;
double sum_f = 0;
int count_f = 0;
for(int j = 0; j < period2; j++)
{
sum_f += m_temp_ema1[i-j];
count_f++;
}
m_ma_internal[i] = (count_f > 0) ? (sum_f / count_f) : m_temp_ema1[i];
break;
}
case DEMA:
{
double ema1 = CalculateDynamicEMA(i, active_p, m_price[i], m_temp_ema1);
double ema2 = CalculateDynamicEMA(i, active_p, ema1, m_temp_ema2);
m_ma_internal[i] = 2.0 * ema1 - ema2;
break;
}
case TEMA:
{
double ema1 = CalculateDynamicEMA(i, active_p, m_price[i], m_temp_ema1);
double ema2 = CalculateDynamicEMA(i, active_p, ema1, m_temp_ema2);
double ema3 = CalculateDynamicEMA(i, active_p, ema2, m_temp_ema3);
m_ma_internal[i] = 3.0 * ema1 - 3.0 * ema2 + ema3;
break;
}
case VWMA:
{
double sum_pv = 0, sum_v = 0;
for(int k = 0; k < active_p; k++)
{
sum_pv += m_price[i-k] * m_volume[i-k];
sum_v += m_volume[i-k];
}
m_ma_internal[i] = (sum_v > 0) ? (sum_pv / sum_v) : m_price[i];
break;
}
default: // SMA
{
double sum = 0;
for(int k = 0; k < active_p; k++)
sum += m_price[i-k];
m_ma_internal[i] = sum / active_p;
break;
}
}
}
ma_buffer[i] = m_ma_internal[i];
}
}
//+------------------------------------------------------------------+
//| Prepare Price (Standard - Optimized) |
//+------------------------------------------------------------------+
@@ -452,6 +713,11 @@ bool CMovingAverageAnchoredCalculator_HA::PreparePriceSeries(int rates_total, in
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);
}
m_ha_calculator.Calculate(rates_total, start_index, open, high, low, close,