//+------------------------------------------------------------------+ //| 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 #ifndef MQLARTICLES_UTILS_FA_BARCONTROLER_MQH #define MQLARTICLES_UTILS_FA_BARCONTROLER_MQH //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #include #include #include //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //--- Notas // 1. Varios miembros son publicos en las clases de controler si ud las modifica sabe lo que hace.. por lo qeu peude dar // comportamiento indefinido si ud modifica de "mala" forma las varaibels.. //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ datetime CBarControlerNextTimeM(const datetime curr_time) { //--- int mon = CTimeUtils::Month(curr_time); int year = CTimeUtils::Year(curr_time); //--- mon++; if(mon > 12) { mon = 1; year++; } //--- return CTimeUtils::DateFrom(year, mon, 1); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ datetime CBarControlerNextTimeW(const datetime curr_time) { 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; } //+------------------------------------------------------------------+ //| Class to control the opening of a candle | //+------------------------------------------------------------------+ struct CBarControler { private: datetime next_time; datetime prev_time; public: // publicos (igual son const) ENUM_TIMEFRAMES bar_timeframe; uint period_in_seconds; uint8_t value; public: CBarControler(ENUM_TIMEFRAMES _timeframe); //--- __forceinline datetime GetNextPrevBarTime() const { return next_time; } //--- bool IsNewBar(const datetime curr_time); }; //+------------------------------------------------------------------+ CBarControler::CBarControler(ENUM_TIMEFRAMES _timeframe) : next_time(0), prev_time(0), period_in_seconds(CTimeframe::PeriodSecondsFast(_timeframe)), bar_timeframe(_timeframe), value((_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0))) { } //+------------------------------------------------------------------+ bool CBarControler::IsNewBar(const datetime curr_time) { if(curr_time >= next_time) { prev_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; } return true; } return false; } //+------------------------------------------------------------------+ //| Clase general | //+------------------------------------------------------------------+ struct CBarControlerFast { private: datetime next_time; public: uint period_in_seconds; uint8_t value; public: CBarControlerFast(ENUM_TIMEFRAMES _timeframe); //--- __forceinline datetime NextTime() const { return next_time; } //--- bool IsNewBar(const datetime curr_time); }; //+------------------------------------------------------------------+ CBarControlerFast::CBarControlerFast(ENUM_TIMEFRAMES _timeframe) : next_time(0), period_in_seconds(CTimeframe::PeriodSecondsFast(_timeframe)), value((_timeframe == PERIOD_W1 ? 1 : (_timeframe == PERIOD_MN1 ? 2 : 0))) { } //+------------------------------------------------------------------+ bool CBarControlerFast::IsNewBar(const datetime curr_time) { 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; } return true; } return false; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ struct CBarControlerFastW1 { private: datetime next_time; public: CBarControlerFastW1(); //--- __forceinline datetime NextTime() const { return next_time; } //--- bool IsNewBar(const datetime curr_time); }; //+------------------------------------------------------------------+ CBarControlerFastW1::CBarControlerFastW1() : next_time(0) { } //+------------------------------------------------------------------+ bool CBarControlerFastW1::IsNewBar(const datetime curr_time) { if(curr_time >= next_time) { next_time = CBarControlerNextTimeW(curr_time); return true; } return false; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ struct CBarControlerFastMN1 { private: datetime next_time; public: CBarControlerFastMN1(); //--- __forceinline datetime NextTime() const { return next_time; } //--- bool IsNewBar(const datetime curr_time); }; //+------------------------------------------------------------------+ CBarControlerFastMN1::CBarControlerFastMN1() : next_time(0) { } //+------------------------------------------------------------------+ bool CBarControlerFastMN1::IsNewBar(const datetime curr_time) { if(curr_time >= next_time) { next_time = CBarControlerNextTimeM(curr_time); return true; } return false; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #define TSNQMLART_FA_BARCONTROLER_DECMINOR(TF) \ struct CBarControlerFastMinors_##TF \ { \ private: \ datetime next_time; \ public: \ CBarControlerFastMinors_##TF() : next_time(0) {} \ __forceinline datetime NextTime() const { return next_time; } \ bool IsNewBar(const datetime curr_time) \ { \ if(curr_time >= next_time) \ { \ next_time = ((curr_time - (curr_time % TSNSTD_TFI_PSECONDS_##TF)) + TSNSTD_TFI_PSECONDS_##TF); \ return true; \ } \ return false; \ } \ }; //--- TSNQMLART_FA_BARCONTROLER_DECMINOR(D1) } #endif // MQLARTICLES_UTILS_FA_BARCONTROLER_MQH //+------------------------------------------------------------------+