forked from nique_372/MQLArticles
157 lines
5 KiB
MQL5
157 lines
5 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| 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 "FuncionesBases.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
datetime CBarControlerNextTimeM(const datetime curr_time)
| |
{
| |
static MqlDateTime tm;
| |
CTimeUtils::TimeToStructFast(curr_time, tm);
| |
| |
//---
| |
tm.mon++;
| |
if(tm.mon > 12)
| |
{
| |
tm.mon = 1;
| |
tm.year++;
| |
}
| |
| |
//---
| |
tm.day = 1;
| |
tm.hour = 0;
| |
tm.sec = 0;
| |
tm.min = 0;
| |
return CTimeUtils::CreateDateTimeByStruct(tm);
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
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 |
| |
//+------------------------------------------------------------------+
| |
class CBarControler
| |
{
| |
private:
| |
datetime next_time;
| |
datetime prev_time;
| |
const ENUM_TIMEFRAMES bar_timeframe;
| |
const int period_in_seconds;
| |
const uint8_t value;
| |
| |
public:
| |
CBarControler(ENUM_TIMEFRAMES _timeframe);
| |
| |
//---
| |
inline int PeriodsInSeconds() const { return period_in_seconds; }
| |
inline ENUM_TIMEFRAMES Timeframe() const { return bar_timeframe; }
| |
inline datetime GetNextPrevBarTime() const { return prev_time; }
| |
| |
//---
| |
bool IsNewBar(const datetime curr_time);
| |
};
| |
//+------------------------------------------------------------------+
| |
CBarControler::CBarControler(ENUM_TIMEFRAMES _timeframe)
| |
: next_time(wrong_datetime), prev_time(wrong_datetime), period_in_seconds(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;
| |
}
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| Class to control the opening of a candle ("fast") |
| |
//+------------------------------------------------------------------+
| |
class CBarControlerFast
| |
{
| |
private:
| |
datetime next_time;
| |
const int period_in_seconds;
| |
const 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(wrong_datetime), period_in_seconds(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;
| |
}
| |
| |
#endif // MQLARTICLES_UTILS_FA_BARCONTROLER_MQH
| |
//+------------------------------------------------------------------+
|