//+------------------------------------------------------------------+ //| DiffCalc.mqh | //| Copyright 2026, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, MetaQuotes Ltd." #property link "https://www.mql5.com" #property strict #ifndef MQLARTICLES_UTILS_FA_DIFFCALC_MQH #define MQLARTICLES_UTILS_FA_DIFFCALC_MQH //+------------------------------------------------------------------+ //| Include | //+------------------------------------------------------------------+ #include "Atr\\All.mqh" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| Clase CDiff - Defines | //+------------------------------------------------------------------+ enum ENUM_MODE_DIFF { MODE_DIFF_BY_FIXED_POITNS = 0, // By Points MODE_DIFF_BY_ATR // By Atr }; //--- enum ENUM_TSNMQLA_DIFF_INIT_RES { TSNMQLA_DIFF_RES_OK, TSNMQLA_DIFF_RES_INVALID_ATR_PTR, TSNMQLA_DIFF_RES_INVALID_ATR_INVALID_IDX, }; //--- Flags #define MQLARTICLES_DIFFCALC_FLAG_SUPPORT_HISTORIAL (1) #define MQLARTICLES_DIFFCALC_ALL_FLAG (MQLARTICLES_DIFFCALC_FLAG_SUPPORT_HISTORIAL) //--- Notas // 1. No se verifica mul si es valido o no // 2. Tampoco se valida symbol //+------------------------------------------------------------------+ //| class CDiff | //+------------------------------------------------------------------+ class CDiff { protected: //--- const ENUM_MODE_DIFF m_type; // Differene type const uint8_t m_flags; // Flags double m_diff; // Calculated difference value public: CDiff(const ENUM_MODE_DIFF type, const uint8_t flags) : m_type(type), m_diff(0.00), m_flags(flags) {} ~CDiff() {} //--- Method to update on new candle virtual void OnNewBar() {} //--- __forceinline ENUM_MODE_DIFF GetTypeDiff() const { return m_type; } __forceinline double Diff() const { return m_diff; } //--- Flags __forceinline bool HasFlags(const int flags) const { return (m_flags & flags) != 0; } __forceinline bool HasAllFlags(const int flags) const { return (m_flags & flags) == flags; } __forceinline uint8_t GetFlags() const { return m_flags; } //--- Modo historial virtual void SetNewIdx(const int bar_idx) {} virtual void OnHistorialCopyData(const int start, const int count) {} }; //+------------------------------------------------------------------+ //| class CDiffPoint | //+------------------------------------------------------------------+ class CDiffPoint : public CDiff { private: int m_points; public: CDiffPoint(void) : CDiff(MODE_DIFF_BY_FIXED_POITNS, 0u), m_points(1) {} ~CDiffPoint(void) {} //--- void Init(const string& symbol_, const int min_diff_points_); //--- inline void DiffPoints(const int new_diff_points) { m_diff = new_diff_points * m_points; } inline int DiffPoints() const { return (int)(m_diff / m_points); } }; //+------------------------------------------------------------------+ //| Configures calculation by points | //+------------------------------------------------------------------+ void CDiffPoint::Init(const string& symbol_, const int min_diff_points_) { m_points = (int)SymbolInfoDouble(symbol_, SYMBOL_POINT); m_diff = m_points * min_diff_points_; } //+------------------------------------------------------------------+ //| class CDiffAtr | //+------------------------------------------------------------------+ class CDiffAtr : public CDiff { private: CAtr* m_atr; // Pointer to ATR object double m_mul; // Multiplier for ATR int m_idx; // Index for ATR public: CDiffAtr(void) : CDiff(MODE_DIFF_BY_ATR, MQLARTICLES_DIFFCALC_ALL_FLAG), m_idx(0), m_atr(NULL), m_mul(0.00) {} ~CDiffAtr(void) {} //--- General setterr ENUM_TSNMQLA_DIFF_INIT_RES Init(CAtr * atr_, int idx_, double mul_); //--- void OnNewBar() override final; //--- Nuevo m_idx void SetNewIdx(const int bar_idx) override final; //--- Atr inline CAtr* GetAtrPointer() const { return m_atr; } //--- Atr m_mul inline double AtrMul() const { return m_mul; } void AtrMul(const double new_value); //--- void OnHistorialCopyData(const int start, const int count) override final { m_atr.CopyData(start, count); } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CDiffAtr::OnNewBar(void) { m_diff = m_mul * m_atr[m_idx]; } //+------------------------------------------------------------------+ //| Updates difference value based on ATR | //+------------------------------------------------------------------+ void CDiffAtr::SetNewIdx(const int bar_idx) { m_diff = m_atr[bar_idx] * m_mul; } //+------------------------------------------------------------------+ //| Changes the ATR multiplier | //+------------------------------------------------------------------+ void CDiffAtr::AtrMul(const double new_value) { m_mul = new_value; } //+------------------------------------------------------------------+ //| Configures calculation by ATR | //+------------------------------------------------------------------+ ENUM_TSNMQLA_DIFF_INIT_RES CDiffAtr::Init(CAtr* atr_, int idx_, double mul_) { //--- if(!CheckPointer(atr_) || atr_.Handle() == INVALID_HANDLE) return TSNMQLA_DIFF_RES_INVALID_ATR_PTR; //--- if(idx_ < 0) return TSNMQLA_DIFF_RES_INVALID_ATR_INVALID_IDX; //--- m_atr = atr_; m_idx = idx_; m_mul = mul_; return TSNMQLA_DIFF_RES_OK; } //+------------------------------------------------------------------+ //| Creates a CDiff object on the heap and returns its pointer | //+------------------------------------------------------------------+ CDiff* CreateDiffptr(const ENUM_MODE_DIFF diff_mod, const string& symbol_, CAtr* atr_, const int idx_, const double mul_or_points) { //--- CDiff* obj = NULL; //--- switch(diff_mod) { case MODE_DIFF_BY_FIXED_POITNS: { obj = new CDiffPoint(); CDiffPoint* diff_point = (CDiffPoint*)obj; diff_point.Init(symbol_, int(mul_or_points)); break; } case MODE_DIFF_BY_ATR: { obj = new CDiffAtr(); CDiffAtr* diff_atr = (CDiffAtr*)obj; const ENUM_TSNMQLA_DIFF_INIT_RES res = diff_atr.Init(atr_, idx_, mul_or_points); if(res) { FastLog(FUNCION_ACTUAL, CRITICAL_ERROR_TEXT, StringFormat("Failed init atr, err = %s", EnumToString(res))); return NULL; } break; } default: FastLog(FUNCION_ACTUAL, CRITICAL_ERROR_TEXT, StringFormat("Invalid type of Diff = %s", EnumToString(diff_mod))); return NULL; } //-- return obj; } } #endif // MQLARTICLES_UTILS_FA_DIFFCALC_MQH //+------------------------------------------------------------------+