2026-01-06 16:18:06 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| 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
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-09-06 20:20:13 -05:00 | | | #include "FA\\BarControler.mqh"
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | //#define NEW_BAR_MANAGER_DEBUG
|
| | |
|
2026-09-12 19:41:52 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-10-23 20:55:45 -05:00 | | | namespace TSN
|
2026-09-12 19:41:52 -05:00 | | | {
|
2026-10-23 20:55:45 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | // Quiza una opt pero nota curr_time debera de ser positiov por que implcaitme se hace algo como (unsigned)seconds
|
| | |
|
2026-01-06 16:18:06 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | struct pack(8) SBarControler
|
| | | {
|
| | | datetime next_time;
|
2026-10-23 20:55:45 -05:00 | | | uint period_in_seconds;
|
| | | uint8_t value;
|
2026-01-06 16:18:06 -05:00 | | |
|
| | |
|
| | | //---
|
| | | SBarControler()
|
| | | : next_time(wrong_datetime)
|
| | | {
|
| | | }
|
| | |
|
| | | //---
|
2026-02-14 17:00:45 -05:00 | | | inline void Create(const ENUM_TIMEFRAMES _timeframe)
|
2026-01-06 16:18:06 -05:00 | | | {
|
2026-09-12 19:41:52 -05:00 | | | period_in_seconds = CTimeframe::PeriodSecondsFastSeg(_timeframe); // Esta clase solo la usara CNewBarManager
|
2026-01-06 16:18:06 -05:00 | | | value = (_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0));
|
| | | }
|
| | |
|
| | | //---
|
2026-05-25 11:02:25 -05:00 | | | inline void NewBar(const datetime curr_time, bool& res)
|
2026-01-06 16:18:06 -05:00 | | | {
|
| | | 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;
|
2026-10-23 20:55:45 -05:00 | | | default:
|
| | | break;
|
2026-01-06 16:18:06 -05:00 | | | }
|
| | | res = true;
|
| | | }
|
| | | else
|
| | | res = false;
|
| | | }
|
| | | };
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | // Excluyendo PERIOD_CURRENT
|
2026-10-23 20:55:45 -05:00 | | | #define CNEWBARMANAGER_TOTAL_TF (21)
|
2026-01-06 16:18:06 -05:00 | | | #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
|
2026-10-23 20:55:45 -05:00 | | | 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
|
2026-05-25 11:02:25 -05:00 | | | datetime m_next_time_m1_bar;
|
2026-10-23 20:55:45 -05:00 | | | int8_t m_total_execute_functions; // No mayor a 128 no hay tantos TF en mt5
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | public:
|
| | | CNewBarManager();
|
| | | ~CNewBarManager() {}
|
| | |
|
| | | //---
|
2026-10-23 20:55:45 -05:00 | | | bool m_is[CNEWBARMANAGER_TOTAL_TF]; // Acceso con out_i/pos | 0 es para M1
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | //---
|
| | | void GetPosExecute(ENUM_TIMEFRAMES tf, int8_t& out_i); // Obtiene un indice
|
2026-02-14 17:00:45 -05:00 | | | inline void Execute(const datetime curr_time); // Ejecucion general
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | //---
|
2026-10-23 20:55:45 -05:00 | | | __forceinline int8_t TotalRegisterTimframes() const { return m_total_execute_functions; }
|
2026-01-06 16:18:06 -05:00 | | | };
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-10-23 20:55:45 -05:00 | | | #define CNewBarManager_IsNewBar(instance, pos) (instance.m_is[pos])
|
| | | #define CNewBarManager_IsNewBarM1(instance) (instance.m_is[CNEWBARMANAGER_IDX_M1])
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | CNewBarManager::CNewBarManager(void)
|
2026-10-23 20:55:45 -05:00 | | | : m_total_execute_functions(0), m_next_time_m1_bar(0)
|
2026-01-06 16:18:06 -05:00 | | | {
|
2026-10-23 20:55:45 -05:00 | | | //---
|
| | | ArrayInitialize(m_timeframe_to_idx, -1);
|
| | | ArrayInitialize(m_is, false);
|
2026-01-06 16:18:06 -05:00 | | |
|
2026-10-23 20:55:45 -05:00 | | | //---
|
| | | m_timeframe_to_idx[CTimeframe::ToIdxWVer(PERIOD_M1)] = CNEWBARMANAGER_IDX_M1;
|
| | | }
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-02-14 17:00:45 -05:00 | | | inline void CNewBarManager::Execute(const datetime curr_time)
|
2026-01-06 16:18:06 -05:00 | | | {
|
2026-10-23 20:55:45 -05:00 | | | if((m_is[CNEWBARMANAGER_IDX_M1] = (curr_time >= m_next_time_m1_bar)))
|
2026-01-06 16:18:06 -05:00 | | | {
|
2026-10-29 17:44:49 -05:00 | | | m_next_time_m1_bar = ((curr_time - (curr_time % 60u)) + 60u);
|
2026-10-23 20:55:45 -05:00 | | | for(int8_t i = 0; i < m_total_execute_functions; i++)
|
| | | m_controlers_arr[i].NewBar(curr_time, m_is[i + 1]);
|
2026-01-06 16:18:06 -05:00 | | | }
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Controlador |
|
| | | //+------------------------------------------------------------------+
|
| | | void CNewBarManager::GetPosExecute(ENUM_TIMEFRAMES tf, int8_t& out_i)
|
| | | {
|
| | | //---
|
2026-10-23 20:55:45 -05:00 | | | const ENUM_TIMEFRAMES real_tf = (tf == PERIOD_CURRENT ? _Period : tf);
|
| | | const char idx = CTimeframe::ToIdxWVer(real_tf);
|
2026-01-06 16:18:06 -05:00 | | |
|
2026-10-23 20:55:45 -05:00 | | | //--- assing
|
| | | if((out_i = m_timeframe_to_idx[idx]) != -1)
|
| | | return; // salimos de golpe
|
2026-01-06 16:18:06 -05:00 | | |
|
| | | //---
|
2026-10-23 20:55:45 -05:00 | | | out_i = m_total_execute_functions + 1;
|
2026-01-06 16:18:06 -05:00 | | |
|
2026-10-23 20:55:45 -05:00 | | | //--- ahora si
|
| | | m_timeframe_to_idx[idx] = out_i;
|
| | | m_controlers_arr[m_total_execute_functions].Create(real_tf);
|
2026-01-06 16:18:06 -05:00 | | |
|
2026-10-23 20:55:45 -05:00 | | | //--- nuevo..
|
| | | m_total_execute_functions++;
|
2026-11-05 12:56:42 -05:00 | | |
|
| | | #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
|
2026-01-06 16:18:06 -05:00 | | | }
|
2026-11-05 12:56:42 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | // 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...
|
| | |
|
2026-09-12 19:41:52 -05:00 | | | }
|
2026-01-06 16:18:06 -05:00 | | | //+------------------------------------------------------------------+
|
| | | #endif // MQLARTICLES_UTILS_TFMANAGER_MQH
|