//+------------------------------------------------------------------+ //| Main.mqh | //| Copyright 2025, Niquel Mendoza. | //| https://www.mql5.com/es/users/nique_372 | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, Niquel Mendoza." #property link "https://www.mql5.com/es/users/nique_372" #property strict #ifndef MQLARTICLES_UTILS_TFMANAGER_MQH #define MQLARTICLES_UTILS_TFMANAGER_MQH //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #include "FA\\BarControler.mqh" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //#define NEW_BAR_MANAGER_DEBUG //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // Quiza una opt pero nota curr_time debera de ser positiov por que implcaitme se hace algo como (unsigned)seconds //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ struct pack(8) SBarControler { datetime next_time; uint period_in_seconds; uint8_t value; //--- SBarControler() : next_time(wrong_datetime) { } //--- inline void Create(const ENUM_TIMEFRAMES _timeframe) { period_in_seconds = CTimeframe::PeriodSecondsFastSeg(_timeframe); // Esta clase solo la usara CNewBarManager value = (_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0)); } //--- inline void NewBar(const datetime curr_time, bool& res) { if(curr_time >= next_time) { switch(value) { case 0: next_time = ((curr_time - (curr_time % period_in_seconds)) + period_in_seconds); break; case 1: next_time = CBarControlerNextTimeW(curr_time); break; case 2: next_time = CBarControlerNextTimeM(curr_time); break; default: break; } res = true; } else res = false; } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // Excluyendo PERIOD_CURRENT #define CNEWBARMANAGER_TOTAL_TF (21) #define CNEWBARMANAGER_IDX_M1 0 /* timeframes[1] = 1 | EnumToString: PERIOD_M1 | PeriodSeconds: 60 timeframes[2] = 2 | EnumToString: PERIOD_M2 | PeriodSeconds: 120 timeframes[3] = 3 | EnumToString: PERIOD_M3 | PeriodSeconds: 180 timeframes[4] = 4 | EnumToString: PERIOD_M4 | PeriodSeconds: 240 timeframes[5] = 5 | EnumToString: PERIOD_M5 | PeriodSeconds: 300 timeframes[6] = 6 | EnumToString: PERIOD_M6 | PeriodSeconds: 360 timeframes[7] = 10 | EnumToString: PERIOD_M10 | PeriodSeconds: 600 timeframes[8] = 12 | EnumToString: PERIOD_M12 | PeriodSeconds: 720 timeframes[9] = 15 | EnumToString: PERIOD_M15 | PeriodSeconds: 900 timeframes[10] = 20 | EnumToString: PERIOD_M20 | PeriodSeconds: 1200 timeframes[11] = 30 | EnumToString: PERIOD_M30 | PeriodSeconds: 1800 timeframes[12] = 16385 | EnumToString: PERIOD_H1 | PeriodSeconds: 3600 timeframes[13] = 16386 | EnumToString: PERIOD_H2 | PeriodSeconds: 7200 timeframes[14] = 16387 | EnumToString: PERIOD_H3 | PeriodSeconds: 10800 timeframes[15] = 16388 | EnumToString: PERIOD_H4 | PeriodSeconds: 14400 timeframes[16] = 16390 | EnumToString: PERIOD_H6 | PeriodSeconds: 21600 timeframes[17] = 16392 | EnumToString: PERIOD_H8 | PeriodSeconds: 28800 timeframes[18] = 16396 | EnumToString: PERIOD_H12 | PeriodSeconds: 43200 timeframes[19] = 16408 | EnumToString: PERIOD_D1 | PeriodSeconds: 86400 timeframes[20] = 32769 | EnumToString: PERIOD_W1 | PeriodSeconds: 604800 timeframes[21] = 49153 | EnumToString: PERIOD_MN1 | PeriodSeconds: 2592000 */ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CNewBarManager { private: //--- Correcta ejecucion int8_t m_timeframe_to_idx[MQLARTICLES_TFI_FINAL_TSIZE]; // Tf to idx in controler arr SBarControler m_controlers_arr[CNEWBARMANAGER_TOTAL_TF]; // Todos los controladores base datetime m_next_time_m1_bar; int8_t m_total_execute_functions; // No mayor a 128 no hay tantos TF en mt5 public: CNewBarManager(); ~CNewBarManager() {} //--- bool m_is[CNEWBARMANAGER_TOTAL_TF]; // Acceso con out_i/pos | 0 es para M1 //--- void GetPosExecute(ENUM_TIMEFRAMES tf, int8_t& out_i); // Obtiene un indice inline void Execute(const datetime curr_time); // Ejecucion general //--- __forceinline int8_t TotalRegisterTimframes() const { return m_total_execute_functions; } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #define CNewBarManager_IsNewBar(instance, pos) (instance.m_is[pos]) #define CNewBarManager_IsNewBarM1(instance) (instance.m_is[CNEWBARMANAGER_IDX_M1]) //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CNewBarManager::CNewBarManager(void) : m_total_execute_functions(0), m_next_time_m1_bar(0) { //--- ArrayInitialize(m_timeframe_to_idx, -1); ArrayInitialize(m_is, false); //--- m_timeframe_to_idx[CTimeframe::ToIdxWVer(PERIOD_M1)] = CNEWBARMANAGER_IDX_M1; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ inline void CNewBarManager::Execute(const datetime curr_time) { if((m_is[CNEWBARMANAGER_IDX_M1] = (curr_time >= m_next_time_m1_bar))) { m_next_time_m1_bar = ((curr_time - (curr_time % 60u)) + 60u); for(int8_t i = 0; i < m_total_execute_functions; i++) m_controlers_arr[i].NewBar(curr_time, m_is[i + 1]); } } //+------------------------------------------------------------------+ //| Controlador | //+------------------------------------------------------------------+ void CNewBarManager::GetPosExecute(ENUM_TIMEFRAMES tf, int8_t& out_i) { //--- const ENUM_TIMEFRAMES real_tf = (tf == PERIOD_CURRENT ? _Period : tf); const char idx = CTimeframe::ToIdxWVer(real_tf); //--- assing if((out_i = m_timeframe_to_idx[idx]) != -1) return; // salimos de golpe //--- out_i = m_total_execute_functions + 1; //--- ahora si m_timeframe_to_idx[idx] = out_i; m_controlers_arr[m_total_execute_functions].Create(real_tf); //--- nuevo.. m_total_execute_functions++; #ifdef NEW_BAR_MANAGER_DEBUG PrintFormat("[INFO] %s: IDX = %d | Timeframe = %s | New total = %d", __FUNCTION__, out_i, EnumToString(tf), m_total_execute_functions); #endif // NEW_BAR_MANAGER_DEBUG } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // Reglas de uso // 1. Normalemte la abse de todo es el "m1" si qiueres llamar a otro tienes que envolverlo en un M1 // daod que se acutlzia cada barra m1... por jeemplo no puedes callear a CNewBarManager_IsNewBar asi tal cual a lo menos que sabes que es M1 // aunque para eso mejro usa CNewBarManager_IsNewBarM1 directo... } //+------------------------------------------------------------------+ #endif // MQLARTICLES_UTILS_TFMANAGER_MQH