//+------------------------------------------------------------------+ //| HMA_Calculator.mqh | //| Calculation engine for Standard and Heikin Ashi HMA. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #include //+==================================================================+ //| | //| CLASS 1: CHMACalculator (Base Class) | //| | //+==================================================================+ class CHMACalculator { protected: int m_hma_period; double m_price[]; //--- Helper function for manual WMA calculation double CalculateWMA(int period, int index, const double &source_array[]); //--- Virtual method for preparing the price series. virtual bool PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type); public: CHMACalculator(void) {}; virtual ~CHMACalculator(void) {}; //--- Public methods bool Init(int period); void Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &hma_buffer[]); }; //+------------------------------------------------------------------+ //| CHMACalculator: Initialization | //+------------------------------------------------------------------+ bool CHMACalculator::Init(int period) { m_hma_period = (period < 1) ? 1 : period; return true; } //+------------------------------------------------------------------+ //| CHMACalculator: Main Calculation Method (Shared Logic) | //+------------------------------------------------------------------+ void CHMACalculator::Calculate(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type, double &hma_buffer[]) { int period_sqrt = (int)MathMax(1, MathRound(MathSqrt(m_hma_period))); int start_pos = m_hma_period + period_sqrt - 2; if(rates_total <= start_pos) return; if(!PreparePriceSeries(rates_total, open, high, low, close, price_type)) return; double wma_half[], wma_full[], raw_hma[]; ArrayResize(wma_half, rates_total); ArrayResize(wma_full, rates_total); ArrayResize(raw_hma, rates_total); int period_half = (int)MathMax(1, MathRound(m_hma_period / 2.0)); for(int i = m_hma_period - 1; i < rates_total; i++) { wma_half[i] = CalculateWMA(period_half, i, m_price); wma_full[i] = CalculateWMA(m_hma_period, i, m_price); raw_hma[i] = 2 * wma_half[i] - wma_full[i]; } for(int i = start_pos; i < rates_total; i++) { hma_buffer[i] = CalculateWMA(period_sqrt, i, raw_hma); } } //+------------------------------------------------------------------+ //| CHMACalculator: Helper for manual WMA calculation | //+------------------------------------------------------------------+ double CHMACalculator::CalculateWMA(int period, int index, const double &source_array[]) { double lwma_sum = 0, weight_sum = 0; for(int j=0; j 0) ? lwma_sum / weight_sum : 0.0; } //+------------------------------------------------------------------+ //| CHMACalculator: Prepares the standard source price series. | //+------------------------------------------------------------------+ bool CHMACalculator::PreparePriceSeries(int rates_total, const double &open[], const double &high[], const double &low[], const double &close[], ENUM_APPLIED_PRICE price_type) { ArrayResize(m_price, rates_total); switch(price_type) { case PRICE_OPEN: ArrayCopy(m_price, open, 0, 0, rates_total); break; case PRICE_HIGH: ArrayCopy(m_price, high, 0, 0, rates_total); break; case PRICE_LOW: ArrayCopy(m_price, low, 0, 0, rates_total); break; case PRICE_MEDIAN: for(int i=0; i