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