2026-09-12 19:41:52 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| 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 |
|
| | | //+------------------------------------------------------------------+
|
2026-10-24 22:43:15 -05:00 | | | #include "Atr\\All.mqh"
|
2026-09-12 19:41:52 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-10-24 22:43:15 -05:00 | | | namespace TSN
|
2026-09-12 19:41:52 -05:00 | | | {
|
| | | //+------------------------------------------------------------------+
|
| | | //| Clase CDiff - Defines |
|
| | | //+------------------------------------------------------------------+
|
| | | enum ENUM_MODE_DIFF
|
| | | {
|
| | | MODE_DIFF_BY_FIXED_POITNS = 0, // By Points
|
2026-10-24 22:43:15 -05:00 | | | 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,
|
2026-09-12 19:41:52 -05:00 | | | };
|
| | |
|
| | | //--- Flags
|
| | | #define MQLARTICLES_DIFFCALC_FLAG_SUPPORT_HISTORIAL (1)
|
2026-10-24 22:43:15 -05:00 | | | #define MQLARTICLES_DIFFCALC_ALL_FLAG (MQLARTICLES_DIFFCALC_FLAG_SUPPORT_HISTORIAL)
|
2026-09-12 19:41:52 -05:00 | | |
|
2026-10-24 22:43:15 -05:00 | | | //--- Notas
|
2026-11-01 20:54:30 -05:00 | | | // 1. No se verifica mul si es valido o no
|
2026-10-24 22:43:15 -05:00 | | | // 2. Tampoco se valida symbol
|
2026-09-12 19:41:52 -05:00 | | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| class CDiff |
|
| | | //+------------------------------------------------------------------+
|
2026-10-24 22:43:15 -05:00 | | | class CDiff
|
2026-09-12 19:41:52 -05:00 | | | {
|
| | | protected:
|
| | | //---
|
| | | const ENUM_MODE_DIFF m_type; // Differene type
|
2026-10-24 22:43:15 -05:00 | | | const uint8_t m_flags; // Flags
|
2026-09-12 19:41:52 -05:00 | | | 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:
|
2026-11-01 20:54:30 -05:00 | | | int m_points;
|
2026-09-12 19:41:52 -05:00 | | |
|
| | | public:
|
2026-11-01 20:54:30 -05:00 | | | CDiffPoint(void) : CDiff(MODE_DIFF_BY_FIXED_POITNS, 0u), m_points(1) {}
|
2026-09-12 19:41:52 -05:00 | | | ~CDiffPoint(void) {}
|
| | |
|
| | | //---
|
2026-10-24 22:43:15 -05:00 | | | void Init(const string& symbol_, const int min_diff_points_);
|
2026-09-12 19:41:52 -05:00 | | |
|
| | | //---
|
2026-11-01 20:54:30 -05:00 | | | 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); }
|
2026-09-12 19:41:52 -05:00 | | | };
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Configures calculation by points |
|
| | | //+------------------------------------------------------------------+
|
2026-10-24 22:43:15 -05:00 | | | void CDiffPoint::Init(const string& symbol_, const int min_diff_points_)
|
2026-09-12 19:41:52 -05:00 | | | {
|
2026-11-01 20:54:30 -05:00 | | | m_points = (int)SymbolInfoDouble(symbol_, SYMBOL_POINT);
|
| | | m_diff = m_points * min_diff_points_;
|
2026-09-12 19:41:52 -05:00 | | | }
|
| | |
|
| | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| class CDiffAtr |
|
| | | //+------------------------------------------------------------------+
|
| | | class CDiffAtr : public CDiff
|
| | | {
|
| | | private:
|
2026-10-24 22:43:15 -05:00 | | | CAtr* m_atr; // Pointer to ATR object
|
| | | double m_mul; // Multiplier for ATR
|
2026-11-01 20:54:30 -05:00 | | | int m_idx; // Index for ATR
|
| | |
|
2026-09-12 19:41:52 -05:00 | | | public:
|
| | | CDiffAtr(void) : CDiff(MODE_DIFF_BY_ATR, MQLARTICLES_DIFFCALC_ALL_FLAG),
|
2026-10-24 22:43:15 -05:00 | | | m_idx(0), m_atr(NULL), m_mul(0.00) {}
|
2026-09-12 19:41:52 -05:00 | | | ~CDiffAtr(void) {}
|
| | |
|
| | | //--- General setterr
|
2026-10-24 22:43:15 -05:00 | | | ENUM_TSNMQLA_DIFF_INIT_RES Init(CAtr * atr_, int idx_, double mul_);
|
2026-09-12 19:41:52 -05:00 | | |
|
| | | //---
|
| | | void OnNewBar() override final;
|
| | |
|
2026-10-24 22:43:15 -05:00 | | | //--- Nuevo m_idx
|
2026-09-12 19:41:52 -05:00 | | | void SetNewIdx(const int bar_idx) override final;
|
| | |
|
| | | //--- Atr
|
2026-10-24 22:43:15 -05:00 | | | inline CAtr* GetAtrPointer() const { return m_atr; }
|
2026-09-12 19:41:52 -05:00 | | |
|
2026-10-24 22:43:15 -05:00 | | | //--- Atr m_mul
|
| | | inline double AtrMul() const { return m_mul; }
|
2026-09-12 19:41:52 -05:00 | | | void AtrMul(const double new_value);
|
| | |
|
| | | //---
|
2026-10-24 22:43:15 -05:00 | | | void OnHistorialCopyData(const int start, const int count) override final { m_atr.CopyData(start, count); }
|
2026-09-12 19:41:52 -05:00 | | | };
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | void CDiffAtr::OnNewBar(void)
|
| | | {
|
2026-10-24 22:43:15 -05:00 | | | m_diff = m_mul * m_atr[m_idx];
|
2026-09-12 19:41:52 -05:00 | | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Updates difference value based on ATR |
|
| | | //+------------------------------------------------------------------+
|
| | | void CDiffAtr::SetNewIdx(const int bar_idx)
|
| | | {
|
2026-10-24 22:43:15 -05:00 | | | m_diff = m_atr[bar_idx] * m_mul;
|
2026-09-12 19:41:52 -05:00 | | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Changes the ATR multiplier |
|
| | | //+------------------------------------------------------------------+
|
| | | void CDiffAtr::AtrMul(const double new_value)
|
| | | {
|
2026-10-24 22:43:15 -05:00 | | | m_mul = new_value;
|
2026-09-12 19:41:52 -05:00 | | | }
|
2026-10-24 22:43:15 -05:00 | | |
|
2026-09-12 19:41:52 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| Configures calculation by ATR |
|
| | | //+------------------------------------------------------------------+
|
2026-10-24 22:43:15 -05:00 | | | ENUM_TSNMQLA_DIFF_INIT_RES CDiffAtr::Init(CAtr* atr_, int idx_, double mul_)
|
2026-09-12 19:41:52 -05:00 | | | {
|
| | | //---
|
2026-10-24 22:43:15 -05:00 | | | if(!CheckPointer(atr_) || atr_.Handle() == INVALID_HANDLE)
|
| | | return TSNMQLA_DIFF_RES_INVALID_ATR_PTR;
|
2026-09-12 19:41:52 -05:00 | | |
|
| | | //---
|
| | | if(idx_ < 0)
|
2026-10-24 22:43:15 -05:00 | | | return TSNMQLA_DIFF_RES_INVALID_ATR_INVALID_IDX;
|
2026-09-12 19:41:52 -05:00 | | |
|
| | | //---
|
2026-10-24 22:43:15 -05:00 | | | m_atr = atr_;
|
| | | m_idx = idx_;
|
| | | m_mul = mul_;
|
| | | return TSNMQLA_DIFF_RES_OK;
|
2026-09-12 19:41:52 -05:00 | | | }
|
| | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| 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;
|
2026-10-24 22:43:15 -05:00 | | | diff_point.Init(symbol_, int(mul_or_points));
|
2026-09-12 19:41:52 -05:00 | | | break;
|
| | | }
|
| | | case MODE_DIFF_BY_ATR:
|
| | | {
|
| | | obj = new CDiffAtr();
|
| | | CDiffAtr* diff_atr = (CDiffAtr*)obj;
|
2026-11-01 20:54:30 -05:00 | | | const ENUM_TSNMQLA_DIFF_INIT_RES res = diff_atr.Init(atr_, idx_, mul_or_points);
|
2026-10-24 22:43:15 -05:00 | | | if(res)
|
| | | {
|
| | | FastLog(FUNCION_ACTUAL, CRITICAL_ERROR_TEXT, StringFormat("Failed init atr, err = %s", EnumToString(res)));
|
| | | return NULL;
|
| | | }
|
2026-09-12 19:41:52 -05:00 | | | 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
|
| | | //+------------------------------------------------------------------+
|