//+------------------------------------------------------------------+ //| MovingAverage_Engine.mqh | //| VERSION 1.11: Added public GetPeriod() method. | //| Copyright 2025, xxxxxxxx | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, xxxxxxxx" #include //--- Enum to select the MA type for calculation --- enum ENUM_MA_TYPE { SMA, EMA, SMMA, LWMA }; //+==================================================================+ class CMovingAverageCalculator { protected: int m_period; ENUM_MA_TYPE m_ma_type; double m_price[]; virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]); public: CMovingAverageCalculator(void) {}; virtual ~CMovingAverageCalculator(void) {}; bool Init(int period, ENUM_MA_TYPE ma_type); void Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &ma_buffer[]); //--- NEW: Public getter for the period int GetPeriod(void) const { return m_period; } }; //--- Derived class for Heikin Ashi version --- class CMovingAverageCalculator_HA : public CMovingAverageCalculator { private: CHeikinAshi_Calculator m_ha_calculator; protected: virtual bool PreparePriceSeries(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[]) override; }; //+==================================================================+ //| METHOD IMPLEMENTATIONS | //+==================================================================+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool CMovingAverageCalculator::Init(int period, ENUM_MA_TYPE ma_type) { m_period = (period < 1) ? 1 : period; m_ma_type = ma_type; return true; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CMovingAverageCalculator::Calculate(int rates_total, ENUM_APPLIED_PRICE price_type, const double &open[], const double &high[], const double &low[], const double &close[], double &ma_buffer[]) { if(rates_total < m_period) return; if(!PreparePriceSeries(rates_total, price_type, open, high, low, close)) return; int start_pos = m_period - 1; for(int i = start_pos; i < rates_total; i++) { switch(m_ma_type) { case EMA: case SMMA: if(i == start_pos) { double sum=0; for(int j=0; j0) ma_buffer[i]=sum/w_sum; } break; default: // SMA { double sum=0; for(int j=0; j