2025-10-01 12:22:08 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| BarControler.mqh |
|
| | | //| Copyright 2025, Niquel Mendoza. |
|
| | | //| https://www.mql5.com/es/users/nique_372/news |
|
| | | //+------------------------------------------------------------------+
|
| | | #property copyright "Copyright 2025, Niquel Mendoza."
|
| | | #property link "https://www.mql5.com/es/users/nique_372/news"
|
| | | #property strict
|
| | |
|
2025-11-10 09:33:53 -05:00 | | | #ifndef MQLARTICLES_UTILS_FA_BARCONTROLER_MQH
|
| | | #define MQLARTICLES_UTILS_FA_BARCONTROLER_MQH
|
2025-10-01 12:22:08 -05:00 | | |
|
2025-12-28 18:28:41 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-09-06 20:20:13 -05:00 | | | #include <TSN\\Time\\Main.mqh>
|
| | | #include "Int\\Constant.mqh"
|
2026-09-12 19:41:52 -05:00 | | | #include "..\\Tfi\\Base.mqh"
|
2025-10-01 12:22:08 -05:00 | | |
|
2026-09-12 19:41:52 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | namespace TSN
|
| | | {
|
2025-12-28 18:28:41 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-02-14 17:00:45 -05:00 | | | datetime CBarControlerNextTimeM(const datetime curr_time)
|
2025-12-28 18:28:41 -05:00 | | | {
|
2026-08-04 22:57:09 -05:00 | | | //---
|
| | | int mon = CTimeUtils::Month(curr_time);
|
| | | int year = CTimeUtils::Year(curr_time);
|
2026-01-06 06:47:46 -05:00 | | |
|
| | | //---
|
2026-08-04 22:57:09 -05:00 | | | mon++;
|
| | | if(mon > 12)
|
2025-12-28 20:28:39 -05:00 | | | {
|
2026-08-04 22:57:09 -05:00 | | | mon = 1;
|
| | | year++;
|
2025-12-28 20:28:39 -05:00 | | | }
|
2025-12-28 18:28:41 -05:00 | | |
|
2026-01-06 06:47:46 -05:00 | | | //---
|
2026-08-04 22:57:09 -05:00 | | | return CTimeUtils::DateFrom(year, mon, 1);
|
2025-12-28 18:28:41 -05:00 | | | }
|
| | |
|
2026-01-06 06:47:46 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-02-14 17:00:45 -05:00 | | | datetime CBarControlerNextTimeW(const datetime curr_time)
|
2026-01-06 06:47:46 -05:00 | | | {
|
| | | static MqlDateTime dt;
|
| | | CTimeUtils::TimeToStructFast(curr_time, dt);
|
| | |
|
| | | //---
|
| | | const int days_from_monday = (dt.day_of_week == 0) ? 6 : dt.day_of_week - 1;
|
| | | return (curr_time - (days_from_monday * 86400) - (dt.hour * 3600) - (dt.min * 60) - dt.sec) + 604800;
|
| | | }
|
2025-12-28 18:28:41 -05:00 | | |
|
2025-10-01 12:22:08 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| Class to control the opening of a candle |
|
| | | //+------------------------------------------------------------------+
|
| | | class CBarControler
|
| | | {
|
| | | private:
|
| | | datetime next_time;
|
| | | datetime prev_time;
|
2025-11-07 12:13:47 -05:00 | | | const ENUM_TIMEFRAMES bar_timeframe;
|
| | | const int period_in_seconds;
|
2026-01-06 13:03:52 -05:00 | | | const uint8_t value;
|
2025-10-01 12:22:08 -05:00 | | |
|
| | | public:
|
2026-01-06 13:03:52 -05:00 | | | CBarControler(ENUM_TIMEFRAMES _timeframe);
|
2025-12-28 18:28:41 -05:00 | | |
|
| | | //---
|
| | | inline int PeriodsInSeconds() const { return period_in_seconds; }
|
| | | inline ENUM_TIMEFRAMES Timeframe() const { return bar_timeframe; }
|
| | | inline datetime GetNextPrevBarTime() const { return prev_time; }
|
| | |
|
| | | //---
|
2026-02-14 17:00:45 -05:00 | | | bool IsNewBar(const datetime curr_time);
|
2025-10-01 12:22:08 -05:00 | | | };
|
| | | //+------------------------------------------------------------------+
|
2026-01-06 13:03:52 -05:00 | | | CBarControler::CBarControler(ENUM_TIMEFRAMES _timeframe)
|
2026-09-12 19:41:52 -05:00 | | | : next_time(wrong_datetime), prev_time(wrong_datetime), period_in_seconds(CTimeframe::PeriodSecondsFast(_timeframe)),
|
| | | bar_timeframe(_timeframe),
|
2026-01-06 13:03:52 -05:00 | | | value((_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0)))
|
2025-10-01 12:22:08 -05:00 | | | {
|
| | | }
|
2026-01-06 13:03:52 -05:00 | | |
|
2025-10-01 12:22:08 -05:00 | | | //+------------------------------------------------------------------+
|
2026-02-14 17:00:45 -05:00 | | | bool CBarControler::IsNewBar(const datetime curr_time)
|
2025-10-01 12:22:08 -05:00 | | | {
|
| | | if(curr_time >= next_time)
|
| | | {
|
| | | prev_time = next_time;
|
2026-01-06 13:03:52 -05:00 | | | 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;
|
| | | }
|
2025-10-01 12:22:08 -05:00 | | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
2026-05-28 12:01:58 -05:00 | | | //| Clase general |
|
2025-10-01 12:22:08 -05:00 | | | //+------------------------------------------------------------------+
|
| | | class CBarControlerFast
|
| | | {
|
| | | private:
|
| | | datetime next_time;
|
2025-11-07 12:13:47 -05:00 | | | const int period_in_seconds;
|
2026-01-06 06:47:46 -05:00 | | | const uint8_t value;
|
2025-10-01 12:22:08 -05:00 | | |
|
| | | public:
|
2026-01-06 06:47:46 -05:00 | | | CBarControlerFast(ENUM_TIMEFRAMES _timeframe);
|
2025-12-28 18:28:41 -05:00 | | |
|
| | | //---
|
2026-01-06 06:47:46 -05:00 | | | __forceinline datetime NextTime() const { return next_time; }
|
2025-12-28 18:28:41 -05:00 | | |
|
| | | //---
|
2026-02-14 17:00:45 -05:00 | | | bool IsNewBar(const datetime curr_time);
|
2025-10-01 12:22:08 -05:00 | | | };
|
| | | //+------------------------------------------------------------------+
|
2026-01-06 06:47:46 -05:00 | | | CBarControlerFast::CBarControlerFast(ENUM_TIMEFRAMES _timeframe)
|
2026-09-12 19:41:52 -05:00 | | | : next_time(wrong_datetime), period_in_seconds(CTimeframe::PeriodSecondsFast(_timeframe)),
|
| | | value((_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0)))
|
2025-10-01 12:22:08 -05:00 | | | {
|
| | | }
|
2025-12-28 18:28:41 -05:00 | | |
|
2025-10-01 12:22:08 -05:00 | | | //+------------------------------------------------------------------+
|
2026-02-14 17:00:45 -05:00 | | | bool CBarControlerFast::IsNewBar(const datetime curr_time)
|
2025-10-01 12:22:08 -05:00 | | | {
|
| | | if(curr_time >= next_time)
|
| | | {
|
2026-01-06 06:47:46 -05:00 | | | switch(value)
|
| | | {
|
2026-01-06 13:03:52 -05:00 | | | case 0:
|
2026-01-06 06:47:46 -05:00 | | | next_time = ((curr_time - (curr_time % period_in_seconds)) + period_in_seconds);
|
| | | break;
|
2026-01-06 13:03:52 -05:00 | | | case 1:
|
2026-01-06 06:47:46 -05:00 | | | next_time = CBarControlerNextTimeW(curr_time);
|
| | | break;
|
2026-01-06 13:03:52 -05:00 | | | case 2:
|
2026-01-06 06:47:46 -05:00 | | | next_time = CBarControlerNextTimeM(curr_time);
|
| | | break;
|
| | | }
|
2025-10-01 12:22:08 -05:00 | | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
2026-05-28 12:01:58 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | class CBarControlerFastW1
|
| | | {
|
| | | private:
|
| | | datetime next_time;
|
| | |
|
| | | public:
|
| | | CBarControlerFastW1();
|
| | | ~CBarControlerFastW1() {}
|
| | |
|
| | | //---
|
| | | __forceinline datetime NextTime() const { return next_time; }
|
| | |
|
| | | //---
|
| | | bool IsNewBar(const datetime curr_time);
|
| | | };
|
| | | //+------------------------------------------------------------------+
|
| | | CBarControlerFastW1::CBarControlerFastW1()
|
| | | : next_time(wrong_datetime)
|
| | | {
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | bool CBarControlerFastW1::IsNewBar(const datetime curr_time)
|
| | | {
|
| | | if(curr_time >= next_time)
|
| | | {
|
| | | next_time = CBarControlerNextTimeW(curr_time);
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | class CBarControlerFastMN1
|
| | | {
|
| | | private:
|
| | | datetime next_time;
|
| | |
|
| | | public:
|
| | | CBarControlerFastMN1();
|
| | | ~CBarControlerFastMN1() {}
|
| | |
|
| | | //---
|
| | | __forceinline datetime NextTime() const { return next_time; }
|
| | |
|
| | | //---
|
| | | bool IsNewBar(const datetime curr_time);
|
| | | };
|
| | | //+------------------------------------------------------------------+
|
| | | CBarControlerFastMN1::CBarControlerFastMN1()
|
| | | : next_time(wrong_datetime)
|
| | | {
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | bool CBarControlerFastMN1::IsNewBar(const datetime curr_time)
|
| | | {
|
| | | if(curr_time >= next_time)
|
| | | {
|
| | | next_time = CBarControlerNextTimeM(curr_time);
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | class CBarControlerFastMinors
|
| | | {
|
| | | private:
|
| | | datetime next_time;
|
| | | const int period_in_seconds;
|
| | |
|
| | | public:
|
| | | CBarControlerFastMinors(ENUM_TIMEFRAMES _timeframe);
|
| | | ~CBarControlerFastMinors() {}
|
| | |
|
| | | //---
|
| | | __forceinline datetime NextTime() const { return next_time; }
|
| | |
|
| | | //---
|
| | | bool IsNewBar(const datetime curr_time);
|
| | | };
|
| | | //+------------------------------------------------------------------+
|
| | | CBarControlerFastMinors::CBarControlerFastMinors(ENUM_TIMEFRAMES _timeframe)
|
2026-09-12 19:41:52 -05:00 | | | : next_time(wrong_datetime), period_in_seconds(CTimeframe::PeriodSecondsFast(_timeframe))
|
2026-05-28 12:01:58 -05:00 | | | {
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | bool CBarControlerFastMinors::IsNewBar(const datetime curr_time)
|
| | | {
|
| | | if(curr_time >= next_time)
|
| | | {
|
| | | next_time = ((curr_time - (curr_time % period_in_seconds)) + period_in_seconds);
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
2026-09-12 19:41:52 -05:00 | | | }
|
2026-05-28 12:01:58 -05:00 | | |
|
2025-11-10 09:33:53 -05:00 | | | #endif // MQLARTICLES_UTILS_FA_BARCONTROLER_MQH
|
2025-11-07 12:13:47 -05:00 | | | //+------------------------------------------------------------------+
|