MQLArticles/Strategy/Utils/YearFilter.mqh
2025-12-05 16:33:47 -05:00

252 lines
No EOL
16 KiB
MQL5

//+------------------------------------------------------------------+
//| YearFilter.mqh |
//| Copyright 2025, Leo. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Leo."
#property link "https://www.mql5.com"
#property strict
#ifndef MQLARTILES_STRATEGY_UTILS_YEARFILTER_MQH
#define MQLARTILES_STRATEGY_UTILS_YEARFILTER_MQH
//+------------------------------------------------------------------+
//| Clase para filtrar por año |
//+------------------------------------------------------------------+
class CYearFilter
{
private:
// Arreglo para almacenar el estado de cada mes (true = habilitado)
bool m_months[12];
public:
CYearFilter(void);
~CYearFilter(void);
// Métodos para configurar todos los meses a la vez
void Set(bool enero, bool febrero, bool marzo, bool abril, bool mayo, bool junio,
bool julio, bool agosto, bool septiembre, bool octubre, bool noviembre, bool diciembre);
// Métodos inline para configurar meses individuales (para mayor velocidad)
inline void SetEnero(bool value)
{
m_months[0] = value;
}
inline void SetFebrero(bool value)
{
m_months[1] = value;
}
inline void SetMarzo(bool value)
{
m_months[2] = value;
}
inline void SetAbril(bool value)
{
m_months[3] = value;
}
inline void SetMayo(bool value)
{
m_months[4] = value;
}
inline void SetJunio(bool value)
{
m_months[5] = value;
}
inline void SetJulio(bool value)
{
m_months[6] = value;
}
inline void SetAgosto(bool value)
{
m_months[7] = value;
}
inline void SetSeptiembre(bool value)
{
m_months[8] = value;
}
inline void SetOctubre(bool value)
{
m_months[9] = value;
}
inline void SetNoviembre(bool value)
{
m_months[10] = value;
}
inline void SetDiciembre(bool value)
{
m_months[11] = value;
}
// Métodos inline para obtener el estado de un mes (para mayor velocidad)
inline bool GetEnero() const
{
return m_months[0];
}
inline bool GetFebrero() const
{
return m_months[1];
}
inline bool GetMarzo() const
{
return m_months[2];
}
inline bool GetAbril() const
{
return m_months[3];
}
inline bool GetMayo() const
{
return m_months[4];
}
inline bool GetJunio() const
{
return m_months[5];
}
inline bool GetJulio() const
{
return m_months[6];
}
inline bool GetAgosto() const
{
return m_months[7];
}
inline bool GetSeptiembre() const
{
return m_months[8];
}
inline bool GetOctubre() const
{
return m_months[9];
}
inline bool GetNoviembre() const
{
return m_months[10];
}
inline bool GetDiciembre() const
{
return m_months[11];
}
// Método para establecer un mes específico por su índice (1-12)
inline void SetMonth(int monthIndex, bool value)
{
if(monthIndex >= 1 && monthIndex <= 12)
m_months[monthIndex - 1] = value;
}
// Método para obtener el estado de un mes específico por su índice (1-12)
inline bool GetMonth(int monthIndex) const
{
return (monthIndex >= 1 && monthIndex <= 12) ? m_months[monthIndex - 1] : false;
}
// Verificar si el mes actual es un mes de operación (optimizado para rendimiento)
inline bool IsMonthTrade() const;
// Verificar si un mes específico es un mes de operación (sobrecarga para una fecha concreta)
inline bool IsMonthTrade(datetime time) const;
// Verificar si un mes específico es un mes de operación (por índice de mes 1-12)
inline bool IsMonthTradeByMonthIndex(int monthIndex) const;
// Habilitar todos los meses
inline void EnableAll();
// Deshabilitar todos los meses
inline void DisableAll();
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CYearFilter::CYearFilter(void)
{
// Por defecto, habilitar todos los meses
EnableAll();
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CYearFilter::~CYearFilter(void)
{
// No se requieren acciones especiales
}
//+------------------------------------------------------------------+
//| Habilitar todos los meses |
//+------------------------------------------------------------------+
inline void CYearFilter::EnableAll()
{
for(int i = 0; i < 12; i++)
m_months[i] = true;
}
//+------------------------------------------------------------------+
//| Deshabilitar todos los meses |
//+------------------------------------------------------------------+
inline void CYearFilter::DisableAll()
{
for(int i = 0; i < 12; i++)
m_months[i] = false;
}
//+------------------------------------------------------------------+
//| Establecer todos los meses de una vez |
//+------------------------------------------------------------------+
void CYearFilter::Set(bool enero, bool febrero, bool marzo, bool abril, bool mayo, bool junio,
bool julio, bool agosto, bool septiembre, bool octubre, bool noviembre, bool diciembre)
{
m_months[0] = enero;
m_months[1] = febrero;
m_months[2] = marzo;
m_months[3] = abril;
m_months[4] = mayo;
m_months[5] = junio;
m_months[6] = julio;
m_months[7] = agosto;
m_months[8] = septiembre;
m_months[9] = octubre;
m_months[10] = noviembre;
m_months[11] = diciembre;
}
//+------------------------------------------------------------------+
//| Verificar si el mes actual es un mes de operación |
//+------------------------------------------------------------------+
inline bool CYearFilter::IsMonthTrade() const
{
// Obtener el mes actual (1-12) y convertirlo a índice de array (0-11)
MqlDateTime dt;
TimeToStruct(TimeLocal(), dt);
// Verificación directa para máxima velocidad (evitamos llamadas a funciones adicionales)
return m_months[dt.mon - 1];
}
//+------------------------------------------------------------------+
//| Verificar si un mes específico es un mes de operación |
//+------------------------------------------------------------------+
inline bool CYearFilter::IsMonthTrade(datetime time) const
{
// Convertir la fecha dada a estructura de fecha
MqlDateTime dt;
TimeToStruct(time, dt);
// Verificación directa para máxima velocidad
return m_months[dt.mon - 1];
}
//+------------------------------------------------------------------+
//| Verificar si un mes específico es un mes de operación |
//+------------------------------------------------------------------+
inline bool CYearFilter::IsMonthTradeByMonthIndex(int monthIndex) const
{
// Validar el índice del mes (1-12)
if(monthIndex < 1 || monthIndex > 12)
return false;
return m_months[monthIndex - 1];
}
//+------------------------------------------------------------------+
#endif // MQLARTILES_STRATEGY_UTILS_YEARFILTER_MQH