From befe2313edd95d3e9a687977601439cc7d3d1b06 Mon Sep 17 00:00:00 2001 From: Toh4iem9 Date: Mon, 10 Nov 2025 16:14:54 +0100 Subject: [PATCH] new files added --- Include/MyIncludes/MovingAverage_Engine.mqh | 202 ++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 Include/MyIncludes/MovingAverage_Engine.mqh diff --git a/Include/MyIncludes/MovingAverage_Engine.mqh b/Include/MyIncludes/MovingAverage_Engine.mqh new file mode 100644 index 0000000..ef074f4 --- /dev/null +++ b/Include/MyIncludes/MovingAverage_Engine.mqh @@ -0,0 +1,202 @@ +//+------------------------------------------------------------------+ +//| MovingAverage_Engine.mqh | +//| Universal engine for standard moving averages (SMA, EMA...).| +//| 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[]); + }; + +//--- 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