forked from nique_372/MQLArticles
//+------------------------------------------------------------------+
| |
//| EmbAtrBase.mqh |
| |
//| Copyright 2026, Niquel Mendoza. |
| |
//| https://www.mql5.com |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2026, Niquel Mendoza."
| |
#property link "https://www.mql5.com"
| |
#property strict
| |
| |
| |
#ifndef MQLARTICLES_UTILS_FA_ATR_EMB_BASE_MQH
| |
#define MQLARTICLES_UTILS_FA_ATR_EMB_BASE_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "..\\Def.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| Base class to implement the atr |
| |
//+------------------------------------------------------------------+
| |
class CAtrOptimized
| |
{
| |
protected:
| |
ENUM_TIMEFRAMES m_timeframe;
| |
string m_symbol;
| |
int m_index;
| |
int m_period;
| |
double m_atr_value; // Valor ATR específico para m_index
| |
double m_wilder_factor;
| |
double m_inv_period;
| |
| |
//---
| |
double m_high[];
| |
double m_low[];
| |
double m_close[];
| |
double m_atr[];
| |
| |
//---
| |
CBarControler m_controler_curr;
| |
__forceinline double CalculeTrueRangeForIndexCts(int index);
| |
bool CopyData();
| |
| |
public:
| |
CAtrOptimized(ENUM_TIMEFRAMES tf, const string& symbol, int index, int period);
| |
~CAtrOptimized(void);
| |
| |
//--- Main
| |
virtual inline double GetAtrValue(const datetime curr_time) = 0;
| |
| |
//--- Getters
| |
__forceinline int Index() const { return m_index; }
| |
__forceinline ENUM_TIMEFRAMES Timeframe() const { return m_timeframe; }
| |
__forceinline int AtrPeriod() const { return m_period; }
| |
__forceinline string Simbolo() const { return m_symbol; }
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| Constructor and destructor |
| |
//+------------------------------------------------------------------+
| |
CAtrOptimized::CAtrOptimized(ENUM_TIMEFRAMES tf, const string& symbol, int index, int period)
| |
:
| |
m_controler_curr(tf), m_timeframe(tf), m_symbol(symbol), m_index(index), m_period(period),
| |
m_atr_value(0.0), m_wilder_factor(double(period - 1) / period), m_inv_period(1.0 / period)
| |
{
| |
ArraySetAsSeries(m_high, true);
| |
ArraySetAsSeries(m_low, true);
| |
ArraySetAsSeries(m_close, true);
| |
ArrayResize(m_atr, period + 2); // solo atr
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CAtrOptimized::~CAtrOptimized(void)
| |
{
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#define CopyAtrExtraValue 3
| |
#define CopyAtr(p) (p<<1)+CopyAtrExtraValue
| |
| |
//---
| |
bool CAtrOptimized::CopyData()
| |
{
| |
//---
| |
const int to_copy = CopyAtr(m_period);
| |
| |
//---
| |
if(CopyHigh(this.m_symbol, this.m_timeframe, m_index, to_copy, m_high) < to_copy)
| |
{
| |
#ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS
| |
FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, StringFormat("Al copiar data de los highs, start = %d - count = %d", m_index, to_copy));
| |
#endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS
| |
return false;
| |
}
| |
if(CopyClose(this.m_symbol, this.m_timeframe, m_index, to_copy, m_close) < to_copy)
| |
{
| |
#ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS
| |
FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, StringFormat("Al copiar data de los closes start = %d - count = %d", m_index, to_copy));
| |
#endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS
| |
return false;
| |
}
| |
if(CopyLow(this.m_symbol, this.m_timeframe, m_index, to_copy, m_low) < to_copy)
| |
{
| |
#ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS
| |
FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, StringFormat("Al copiar data de los lows, start = %d - count = %d", m_index, to_copy));
| |
#endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS
| |
return false;
| |
}
| |
return true;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
#define MQLARTICLES_UTILS_ATR_CALC_TR(IDX) (MathMax(m_high[IDX], m_close[(IDX) + 1]) - MathMin(m_low[IDX], m_close[(IDX) + 1]))
| |
| |
//+------------------------------------------------------------------+
| |
__forceinline double CAtrOptimized::CalculeTrueRangeForIndexCts(int index)
| |
{
| |
const double prev_close = iClose(m_symbol, m_timeframe, index + 1);
| |
return MathMax(iHigh(m_symbol, m_timeframe, index), prev_close) - MathMin(iLow(m_symbol, m_timeframe, index), prev_close);
| |
}
| |
//+------------------------------------------------------------------+
| |
| |
//---
| |
}
| |
| |
| |
#endif // MQLARTICLES_UTILS_FA_ATR_EMB_BASE_MQH
| |
//+------------------------------------------------------------------+
|