//+------------------------------------------------------------------+ //| Atr.mqh | //| Copyright 2026, Niquel Mendoza. | //| https://www.mql5.com/es/users/nique_372/news | //+------------------------------------------------------------------+ #property copyright "Copyright 2026 , Niquel Mendoza." #property link "https://www.mql5.com/es/users/nique_372/news" #property strict #ifndef MQLARTICLES_UTILS_FA_ATR_IND_MQH #define MQLARTICLES_UTILS_FA_ATR_IND_MQH //+------------------------------------------------------------------+ //| Includes | //+------------------------------------------------------------------+ #include "Def.mqh" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| Class to handle the atr | //+------------------------------------------------------------------+ class CAtr { private: double arr[]; ENUM_TIMEFRAMES timeframe; int handle; public: ~CAtr(); CAtr(); //--- inline bool IsIdxValid(const int idx, const string& function_name) const; //--- void SetAsSeries(bool flag); bool CopyData(int start, int count); bool CopyData(int count); //--- bool Create(ENUM_TIMEFRAMES tf, const string& symb, int period, bool execute_on_bar, bool hide_indicator = true); bool Create(const int handle_); bool Sincronize(const int min_bars_calc, const int ms_sleep, int attemps = 100); //--- inline ENUM_TIMEFRAMES Timeframe() const { return timeframe; } inline int Handle() const { return handle; } void Free() { ArrayFree(arr); } //--- // Para evitar erroers use el check.. double operator[](int _index) const { return arr[_index]; } }; //+------------------------------------------------------------------+ //| Contructor and Destructor | //+------------------------------------------------------------------+ CAtr::CAtr(void) : handle(INVALID_HANDLE), timeframe(WRONG_VALUE) { ArrayResize(arr, 0); } //+------------------------------------------------------------------+ CAtr::~CAtr() { if(handle != INVALID_HANDLE) IndicatorRelease(handle); //Free(); } //+------------------------------------------------------------------+ //| Check index | //+------------------------------------------------------------------+ inline bool CAtr::IsIdxValid(const int idx, const string& function_name) const { if(int(arr.Size()) > idx && idx >= 0) return true; else { #ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS FastLog(function_name, FATAL_ERROR_TEXT, StringFormat("Acceso al array de m_atr denegado, indice %d fuera de rango, tamaƱo del array = %u", idx, arr.Size())); #endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS return false; } } //+------------------------------------------------------------------+ bool CAtr::CopyData(int start, int count) { ResetLastError(); if(CopyBuffer(handle, 0, start, count, arr) != count) { #ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, StringFormat("No se pudo copiar data del indicador de m_atr [start %d -> count: %d], ultimo error = %d", start, count, GetLastError())); #endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS return false; } // FastLog(FUNCION_ACTUAL, INFO_TEXT, StringFormat("El handle del indicador es %s", (handle == INVALID_HANDLE ? "invalido" : "valido"))); return true; } //+------------------------------------------------------------------+ bool CAtr::CopyData(int count) { ResetLastError(); if(CopyBuffer(handle, 0, 0, count, arr) != count) { #ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, StringFormat("No se pudo copiar data del indicador de m_atr [0 -> count: %d], ultimo error = %d", count, GetLastError())); #endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS return false; } return true; } //+------------------------------------------------------------------+ bool CAtr::Create(ENUM_TIMEFRAMES tf, const string& symb, int period, bool execute_on_bar, bool hide_indicator = true) { ResetLastError(); //--- if(hide_indicator) TesterHideIndicators(true); //--- const int handle_ = iCustom(symb, (timeframe = tf), "::AtrCts.ex5", period, execute_on_bar); //--- if(hide_indicator) TesterHideIndicators(false); //--- if(handle_ == INVALID_HANDLE) return false; //--- this.handle = handle_; return true; } //+------------------------------------------------------------------+ bool CAtr::Create(const int handle_) { if(handle_ == INVALID_HANDLE) return false; //--- this.handle = handle_; return true; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool CAtr::Sincronize(const int min_bars_calc, const int ms_sleep, int attemps = 100) { //--- while(attemps-- > 0) { if(BarsCalculated(this.handle) >= min_bars_calc) { return true; } //--- Sleep(ms_sleep); } //--- #ifndef MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, StringFormat("Fallo al obtener data del indicador Atr | # Intenos = %d | Min Bars = %d | SleepMs = %d", attemps, min_bars_calc, ms_sleep)); #endif // MQLARTICLES_UTILS_ATR_DISABLE_ERRLOGS //--- return false; } //+------------------------------------------------------------------+ void CAtr::SetAsSeries(bool flag) { ArraySetAsSeries(this.arr, flag); } //+------------------------------------------------------------------+ bool DataIsValid(const double &arr[]) { for(int i = 0; i < (int)arr.Size(); i++) { if(arr[i] == 0.00 || arr[i] == EMPTY_VALUE) return false; } return true; } } #endif // MQLARTICLES_UTILS_FA_ATR_IND_MQH //+------------------------------------------------------------------+