주시
1
0
포크
이미 MQLArticles을(를) 포크했습니다
0
원본 프로젝트 nique_372/MQLArticles
MQLArticles/Utils/TFManager.mqh
211 라인
8.5 KiB
MQL5
Nique_372
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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-09-06 20:20:13 -05:00
#include "FA\\BarControler.mqh"
Nique_372
2026-01-06 16:18:06 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//#define NEW_BAR_MANAGER_DEBUG
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-23 20:55:45 -05:00
namespace TSN
{
Nique_372
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
Nique_372
2026-01-06 16:18:06 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
struct pack(8) SBarControler
{
datetime next_time;
Nique_372
2026-10-23 20:55:45 -05:00
uint period_in_seconds;
uint8_t value;
Nique_372
2026-01-06 16:18:06 -05:00
//---
SBarControler()
: next_time(wrong_datetime)
{
}
//---
Nique_372
2026-02-14 17:00:45 -05:00
inline void Create(const ENUM_TIMEFRAMES _timeframe)
Nique_372
2026-01-06 16:18:06 -05:00
{
period_in_seconds = CTimeframe::PeriodSecondsFastSeg(_timeframe); // Esta clase solo la usara CNewBarManager
Nique_372
2026-01-06 16:18:06 -05:00
value = (_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0));
}
//---
Nique_372
2026-05-25 11:02:25 -05:00
inline void NewBar(const datetime curr_time, bool& res)
Nique_372
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;
Nique_372
2026-10-23 20:55:45 -05:00
default:
break;
Nique_372
2026-01-06 16:18:06 -05:00
}
res = true;
}
else
res = false;
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Excluyendo PERIOD_CURRENT
Nique_372
2026-10-23 20:55:45 -05:00
#define CNEWBARMANAGER_TOTAL_TF (21)
Nique_372
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
Nique_372
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
Nique_372
2026-05-25 11:02:25 -05:00
datetime m_next_time_m1_bar;
Nique_372
2026-10-23 20:55:45 -05:00
int8_t m_total_execute_functions; // No mayor a 128 no hay tantos TF en mt5
Nique_372
2026-01-06 16:18:06 -05:00
public:
CNewBarManager();
~CNewBarManager() {}
//---
Nique_372
2026-10-23 20:55:45 -05:00
bool m_is[CNEWBARMANAGER_TOTAL_TF]; // Acceso con out_i/pos | 0 es para M1
Nique_372
2026-01-06 16:18:06 -05:00
//---
void GetPosExecute(ENUM_TIMEFRAMES tf, int8_t& out_i); // Obtiene un indice
Nique_372
2026-02-14 17:00:45 -05:00
inline void Execute(const datetime curr_time); // Ejecucion general
Nique_372
2026-01-06 16:18:06 -05:00
//---
Nique_372
2026-10-23 20:55:45 -05:00
__forceinline int8_t TotalRegisterTimframes() const { return m_total_execute_functions; }
Nique_372
2026-01-06 16:18:06 -05:00
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
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])
Nique_372
2026-01-06 16:18:06 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CNewBarManager::CNewBarManager(void)
Nique_372
2026-10-23 20:55:45 -05:00
: m_total_execute_functions(0), m_next_time_m1_bar(0)
Nique_372
2026-01-06 16:18:06 -05:00
{
Nique_372
2026-10-23 20:55:45 -05:00
//---
ArrayInitialize(m_timeframe_to_idx, -1);
ArrayInitialize(m_is, false);
Nique_372
2026-01-06 16:18:06 -05:00
Nique_372
2026-10-23 20:55:45 -05:00
//---
m_timeframe_to_idx[CTimeframe::ToIdxWVer(PERIOD_M1)] = CNEWBARMANAGER_IDX_M1;
}
Nique_372
2026-01-06 16:18:06 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-02-14 17:00:45 -05:00
inline void CNewBarManager::Execute(const datetime curr_time)
Nique_372
2026-01-06 16:18:06 -05:00
{
Nique_372
2026-10-23 20:55:45 -05:00
if((m_is[CNEWBARMANAGER_IDX_M1] = (curr_time >= m_next_time_m1_bar)))
Nique_372
2026-01-06 16:18:06 -05:00
{
Nique_372
2026-10-29 17:44:49 -05:00
m_next_time_m1_bar = ((curr_time - (curr_time % 60u)) + 60u);
Nique_372
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]);
Nique_372
2026-01-06 16:18:06 -05:00
}
}
//+------------------------------------------------------------------+
//| Controlador |
//+------------------------------------------------------------------+
void CNewBarManager::GetPosExecute(ENUM_TIMEFRAMES tf, int8_t& out_i)
{
//---
Nique_372
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);
Nique_372
2026-01-06 16:18:06 -05:00
Nique_372
2026-10-23 20:55:45 -05:00
//--- assing
if((out_i = m_timeframe_to_idx[idx]) != -1)
return; // salimos de golpe
Nique_372
2026-01-06 16:18:06 -05:00
//---
Nique_372
2026-10-23 20:55:45 -05:00
out_i = m_total_execute_functions + 1;
Nique_372
2026-01-06 16:18:06 -05:00
Nique_372
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);
Nique_372
2026-01-06 16:18:06 -05:00
Nique_372
2026-10-23 20:55:45 -05:00
//--- nuevo..
m_total_execute_functions++;
Nique_372
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
Nique_372
2026-01-06 16:18:06 -05:00
}
Nique_372
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...
}
Nique_372
2026-01-06 16:18:06 -05:00
//+------------------------------------------------------------------+
#endif // MQLARTICLES_UTILS_TFMANAGER_MQH