mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-24 09:48:05 +00:00
refactor: Refactored with strict chronological array safeguards
This commit is contained in:
@@ -5,6 +5,10 @@
|
|||||||
//| Copyright 2026, xxxxxxxx |
|
//| Copyright 2026, xxxxxxxx |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2026, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
|
#property version "1.10" // Refactored with strict chronological array safeguards
|
||||||
|
|
||||||
|
#ifndef EFFICIENCY_RATIO_CALCULATOR_MQH
|
||||||
|
#define EFFICIENCY_RATIO_CALCULATOR_MQH
|
||||||
|
|
||||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||||
|
|
||||||
@@ -13,15 +17,15 @@
|
|||||||
//+==================================================================+
|
//+==================================================================+
|
||||||
class CEfficiencyRatioCalculator
|
class CEfficiencyRatioCalculator
|
||||||
{
|
{
|
||||||
protected:
|
private:
|
||||||
int m_period;
|
int m_period;
|
||||||
double m_price[]; // Persistent price buffer
|
double m_price[]; // Persistent price buffer
|
||||||
|
|
||||||
virtual bool PreparePrice(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
|
bool PreparePrice(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CEfficiencyRatioCalculator() {};
|
CEfficiencyRatioCalculator() {};
|
||||||
virtual ~CEfficiencyRatioCalculator() {};
|
~CEfficiencyRatioCalculator() {};
|
||||||
|
|
||||||
bool Init(int period);
|
bool Init(int period);
|
||||||
|
|
||||||
@@ -41,7 +45,7 @@ bool CEfficiencyRatioCalculator::Init(int period)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Main Calculation |
|
//| Main Calculation (Optimized) |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void CEfficiencyRatioCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
|
void CEfficiencyRatioCalculator::Calculate(int rates_total, int prev_calculated, ENUM_APPLIED_PRICE price_type,
|
||||||
const double &open[], const double &high[],
|
const double &open[], const double &high[],
|
||||||
@@ -51,40 +55,42 @@ void CEfficiencyRatioCalculator::Calculate(int rates_total, int prev_calculated,
|
|||||||
if(rates_total <= m_period)
|
if(rates_total <= m_period)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 1. Resize Internal
|
//--- Enforce strict chronological safety on output array
|
||||||
if(ArraySize(m_price) != rates_total)
|
if(ArraySize(out_er) != rates_total)
|
||||||
ArrayResize(m_price, rates_total);
|
{
|
||||||
|
ArrayResize(out_er, rates_total);
|
||||||
|
ArraySetAsSeries(out_er, false);
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Prepare Data
|
//--- Prepare internal price data
|
||||||
int prepare_start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
int prepare_start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
if(!PreparePrice(rates_total, prepare_start, price_type, open, high, low, close))
|
if(!PreparePrice(rates_total, prepare_start, price_type, open, high, low, close))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 3. Calculate ER
|
//--- Incremental optimization loop
|
||||||
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : m_period;
|
int start_index = (prev_calculated > 0) ? prev_calculated - 1 : m_period;
|
||||||
if(start_index < m_period)
|
if(start_index < m_period)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < m_period; i++)
|
||||||
|
out_er[i] = 0.0;
|
||||||
start_index = m_period;
|
start_index = m_period;
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = start_index; i < rates_total; i++)
|
for(int i = start_index; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double net_change = MathAbs(m_price[i] - m_price[i - m_period]);
|
double net_change = MathAbs(m_price[i] - m_price[i - m_period]);
|
||||||
double sum_change = 0.0;
|
double sum_change = 0.0;
|
||||||
|
|
||||||
// Sum absolute bar-to-bar changes over period
|
// Sum absolute bar-to-bar changes over the period
|
||||||
for(int k = 0; k < m_period; k++)
|
for(int k = 0; k < m_period; k++)
|
||||||
{
|
{
|
||||||
sum_change += MathAbs(m_price[i - k] - m_price[i - k - 1]);
|
sum_change += MathAbs(m_price[i - k] - m_price[i - k - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sum_change > 1.0e-9) // Determine efficiency
|
if(sum_change > 1.0e-9)
|
||||||
out_er[i] = net_change / sum_change;
|
out_er[i] = net_change / sum_change;
|
||||||
else
|
else
|
||||||
out_er[i] = 1.0; // If no volatility, mathematically efficient (flat line) but usually handled as 0 or previous.
|
out_er[i] = 0.0; // Default to 0.0 (unaligned / noisy flat market)
|
||||||
// 1.0 is technically correct for straight line, but in trading sum_change=0 usually happens with gaps or bad data.
|
|
||||||
// Let's default to 0.0 for safety in trading context if flat.
|
|
||||||
|
|
||||||
if(sum_change == 0.0)
|
|
||||||
out_er[i] = 0.0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +99,12 @@ void CEfficiencyRatioCalculator::Calculate(int rates_total, int prev_calculated,
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
bool CEfficiencyRatioCalculator::PreparePrice(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
bool CEfficiencyRatioCalculator::PreparePrice(int rates_total, int start_index, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[])
|
||||||
{
|
{
|
||||||
|
if(ArraySize(m_price) != rates_total)
|
||||||
|
{
|
||||||
|
ArrayResize(m_price, rates_total);
|
||||||
|
ArraySetAsSeries(m_price, false);
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = start_index; i < rates_total; i++)
|
for(int i = start_index; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
switch(price_type)
|
switch(price_type)
|
||||||
@@ -110,13 +122,13 @@ bool CEfficiencyRatioCalculator::PreparePrice(int rates_total, int start_index,
|
|||||||
m_price[i] = low[i];
|
m_price[i] = low[i];
|
||||||
break;
|
break;
|
||||||
case PRICE_MEDIAN:
|
case PRICE_MEDIAN:
|
||||||
m_price[i] = (high[i]+low[i])*0.5;
|
m_price[i] = (high[i] + low[i]) * 0.5;
|
||||||
break;
|
break;
|
||||||
case PRICE_TYPICAL:
|
case PRICE_TYPICAL:
|
||||||
m_price[i] = (high[i]+low[i]+close[i])/3.0;
|
m_price[i] = (high[i] + low[i] + close[i]) / 3.0;
|
||||||
break;
|
break;
|
||||||
case PRICE_WEIGHTED:
|
case PRICE_WEIGHTED:
|
||||||
m_price[i] = (high[i]+low[i]+close[i]*2.0)*0.25;
|
m_price[i] = (high[i] + low[i] + close[i] * 2.0) * 0.25;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
m_price[i] = close[i];
|
m_price[i] = close[i];
|
||||||
@@ -125,4 +137,6 @@ bool CEfficiencyRatioCalculator::PreparePrice(int rates_total, int start_index,
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif // EFFICIENCY_RATIO_CALCULATOR_MQH
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user