refactor: Refactored to strict 10-param calculation

This commit is contained in:
Toh4iem9
2025-11-28 12:06:43 +01:00
parent efd27b4b80
commit 9117719f32
+23 -31
View File
@@ -4,19 +4,10 @@
//| Copyright 2025, xxxxxxxx | //| Copyright 2025, xxxxxxxx |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link ""
//+==================================================================+ //--- Unified Price Enum
//| |
//| GLOBAL ENUM for Standard & Heikin Ashi Prices |
//| |
//+==================================================================+
// This enum provides a unified list for input parameters, allowing users
// to select from both standard and Heikin Ashi price sources.
enum ENUM_APPLIED_PRICE_HA_ALL enum ENUM_APPLIED_PRICE_HA_ALL
{ {
//--- Heikin Ashi Prices (negative values for easy identification)
PRICE_HA_CLOSE = -1, PRICE_HA_CLOSE = -1,
PRICE_HA_OPEN = -2, PRICE_HA_OPEN = -2,
PRICE_HA_HIGH = -3, PRICE_HA_HIGH = -3,
@@ -24,7 +15,6 @@ enum ENUM_APPLIED_PRICE_HA_ALL
PRICE_HA_MEDIAN = -5, PRICE_HA_MEDIAN = -5,
PRICE_HA_TYPICAL = -6, PRICE_HA_TYPICAL = -6,
PRICE_HA_WEIGHTED = -7, PRICE_HA_WEIGHTED = -7,
//--- Standard Prices (using built-in ENUM_APPLIED_PRICE values)
PRICE_CLOSE_STD = PRICE_CLOSE, PRICE_CLOSE_STD = PRICE_CLOSE,
PRICE_OPEN_STD = PRICE_OPEN, PRICE_OPEN_STD = PRICE_OPEN,
PRICE_HIGH_STD = PRICE_HIGH, PRICE_HIGH_STD = PRICE_HIGH,
@@ -34,24 +24,16 @@ enum ENUM_APPLIED_PRICE_HA_ALL
PRICE_WEIGHTED_STD= PRICE_WEIGHTED PRICE_WEIGHTED_STD= PRICE_WEIGHTED
}; };
//+==================================================================+
//| |
//| CLASS 1: CHeikinAshi_Calculator |
//| |
//+==================================================================+
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Class CHeikinAshi_Calculator. | //| Class CHeikinAshi_Calculator |
//| Purpose: Encapsulates the logic for calculating Heikin Ashi | //| Purpose: Stateless calculation engine for Heikin Ashi candles. |
//| candle values from standard OHLC data. This class is |
//| stateless and operates on external buffers. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
class CHeikinAshi_Calculator class CHeikinAshi_Calculator
{ {
public: public:
//--- Public Interface //--- STRICT INTERFACE: Always requires start_index for optimization.
void Calculate(const int rates_total, void Calculate(const int rates_total,
const int start_index,
const double &open[], const double &open[],
const double &high[], const double &high[],
const double &low[], const double &low[],
@@ -63,9 +45,10 @@ public:
}; };
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Calculates Heikin Ashi values for the entire history. | //| Implementation |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CHeikinAshi_Calculator::Calculate(const int rates_total, void CHeikinAshi_Calculator::Calculate(const int rates_total,
const int start_index,
const double &open[], const double &open[],
const double &high[], const double &high[],
const double &low[], const double &low[],
@@ -75,16 +58,26 @@ void CHeikinAshi_Calculator::Calculate(const int rates_total,
double &ha_low[], double &ha_low[],
double &ha_close[]) double &ha_close[])
{ {
if(rates_total < 1) if(rates_total < 2)
return; return;
ha_open[0] = (open[0] + close[0]) / 2.0; int i = start_index;
ha_close[0] = (open[0] + high[0] + low[0] + close[0]) / 4.0;
ha_high[0] = high[0];
ha_low[0] = low[0];
for(int i = 1; i < rates_total; i++) //--- Initialization logic (only if starting from the very beginning)
if(i == 0)
{ {
ha_open[0] = (open[0] + close[0]) / 2.0;
ha_close[0] = (open[0] + high[0] + low[0] + close[0]) / 4.0;
ha_high[0] = high[0];
ha_low[0] = low[0];
i = 1;
}
//--- Main Optimization Loop
//--- Calculates only from start_index to the end
for(; i < rates_total; i++)
{
// HA Open relies on the PREVIOUS calculated HA candle (i-1)
ha_open[i] = (ha_open[i - 1] + ha_close[i - 1]) / 2.0; ha_open[i] = (ha_open[i - 1] + ha_close[i - 1]) / 2.0;
ha_close[i] = (open[i] + high[i] + low[i] + close[i]) / 4.0; ha_close[i] = (open[i] + high[i] + low[i] + close[i]) / 4.0;
ha_high[i] = MathMax(high[i], MathMax(ha_open[i], ha_close[i])); ha_high[i] = MathMax(high[i], MathMax(ha_open[i], ha_close[i]));
@@ -92,4 +85,3 @@ void CHeikinAshi_Calculator::Calculate(const int rates_total,
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//+------------------------------------------------------------------+