347 라인
16 KiB
MQL5
//+------------------------------------------------------------------+
| |||
//| TradingSession.mqh |
| |||
//| Copyright 2025, Leo. |
| |||
//| https://www.mql5.com/es/users/nique_372/news |
| |||
//+------------------------------------------------------------------+
| |||
#property copyright "Copyright 2025, Leo."
| |||
#property link "https://www.mql5.com/es/users/nique_372/news"
| |||
#property strict
| |||
| |||
#ifndef ICTLIBRARY_CONCEPTS_SESION_TRADING_SESSION_MQH
| |||
#define ICTLIBRARY_CONCEPTS_SESION_TRADING_SESSION_MQH
| |||
#include "..\\Base.mqh"
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
/*
| |||
Ejemplo con sesion 10-14
| |||
start_hour = 10
| |||
start_minute = 0
| |||
end_hour = 14
| |||
end_minute = 0
| |||
| |||
══════════╪══════════════════════════════════════════╪═══════════════════════════════════════
| |||
TIEMPO │ EXTERNO (API Pública) │ INTERNO (Eventos Automáticos)
| |||
══════════╪══════════════════════════════════════════╪═══════════════════════════════════════
| |||
│ │
| |||
09:59 │ IsInitSession() → false │
| |||
│ IsEndSession() → false │ OnOutOfSession() 🔄
| |||
│ EnSesion() → false │
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
10:00 ⚡ │ IsInitSession() → true ⭐ │ OnInitSession() ✅
| |||
│ IsEndSession() → false │ └─ Inicialización de sesión
| |||
│ EnSesion() → true ✅ │ OnSession() ✅
| |||
│ │ └─ Primera actualización
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
10:01 │ IsInitSession() → false │ OnSession() ✅
| |||
│ IsEndSession() → false │ └─ Actualización continua
| |||
│ EnSesion() → true ✅ │
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
... │ IsInitSession() → false │ OnSession() 🔄
| |||
│ IsEndSession() → false │ └─ Actualización continua
| |||
│ EnSesion() → true ✅ │
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
13:58 │ IsInitSession() → false │ OnSession() ✅
| |||
│ IsEndSession() → false │ └─ Actualización continua
| |||
│ EnSesion() → true ✅ │
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
13:59 │ IsInitSession() → false │ OnSession() ✅
| |||
│ IsEndSession() → false │ └─ Última actualización directa
| |||
│ EnSesion() → true ✅ │
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
14:00 ⚡ │ IsInitSession() → false │ OnEndSession() ✅
| |||
│ IsEndSession() → true ⭐ │ ├─ Captura final de datos
| |||
│ EnSesion() → false ❌ │ ├─ Calcula inicio/fin próxima sesión
| |||
│ │ └─ OnSession() (llamada interna)
| |||
│ │ └─ Actualización final max/min
| |||
│ │
| |||
──────────┼──────────────────────────────────────────┼───────────────────────────────────────
| |||
│ │
| |||
14:01 │ IsInitSession() → false │ OnOutOfSession() 🔄
| |||
│ IsEndSession() → false │ └─ Fuera de sesión
| |||
│ EnSesion() → false │
| |||
│ │
| |||
══════════╧══════════════════════════════════════════╧═══════════════════════════════════════
| |||
| |||
LEYENDA:
| |||
⚡ = Momento crítico (inicio/fin de sesión)
| |||
⭐ = Flag temporal (true solo en ese minuto)
| |||
✅ = Ejecución/Estado activo
| |||
❌ = Estado inactivo
| |||
🔄 = Ejecución repetitiva
| |||
| |||
Notas:
| |||
• EnSesion() retorna true durante [10:00, 13:59] (NO incluye 14:00)
| |||
• IsEndSession() retorna true SOLO a las 14:00 (flag temporal)
| |||
• Para incluir el último minuto en lógica: usar EnSesion() || IsEndSession()
| |||
• A las 14:00, OnEndSession() llama internamente a OnSession() para captura final*
| |||
| |||
| |||
*Depende del tipo de sesion (CRangeSession)
| |||
*/
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
class CTradingSession : public IIctConcept
| |||
{
| |||
private:
| |||
int m_handle;
| |||
| |||
//---
| |||
void Destroy();
| |||
| |||
public:
| |||
CTradingSession();
| |||
~CTradingSession();
| |||
| |||
//---
| |||
bool Create(string symbol_, ENUM_TIMEFRAMES timeframe_, long chart_id_, int subwin_, bool show_concept_,
| |||
bool forzar_lunes_viernes, int start_hour, int start_minute, int end_hour, int end_minute, string session_name);
| |||
| |||
//--- Setter principales
| |||
inline void SetExtra(color clr_line_init, color clr_line_end, ENUM_LINE_STYLE style_lines_, int width_lines_, ENUM_TYPE_DRAW_RANGE_SESSION type_draw_);
| |||
inline void SetGrapichStyles(ENUM_LINE_STYLE rect_style_, color clr_rect_, int widht_rect_, bool fill_rect_);
| |||
| |||
//---
| |||
inline bool ShowAll() override final;
| |||
inline bool HideAll() override final;
| |||
inline bool DeleteAll() override final;
| |||
inline void SetFlagMostrarObjetos(bool mostrar) override final;
| |||
inline void ForzeToActulizeCurrentTick() override final;
| |||
inline bool IsNewBarConcept() override final { return ICTTradingSession_IsNewBarConcept(m_handle); }
| |||
| |||
//--- Getters tiempo
| |||
inline datetime GetInitTimeSessionCurrNext();
| |||
inline datetime GetEndTimeSessionCurrNext();
| |||
inline datetime GetInitTimeSessionPrev();
| |||
inline datetime GetEndTimeSessionPrev();
| |||
inline int GetDuracionSessionSegundos();
| |||
inline int GetDuracionSessionMinutos();
| |||
| |||
//--- Estados de sesión
| |||
inline bool IsInitSession();
| |||
inline bool IsEndSession();
| |||
inline bool EnSesion();
| |||
inline uint8_t CurrState() { return ICTTradingSession_CurrState(m_handle); }
| |||
| |||
| |||
| |||
//--- Eventos
| |||
inline void OnNewBarM1();
| |||
| |||
//--- Nombre de la sesion
| |||
inline string GetSessionName();
| |||
| |||
//--- Log
| |||
inline void AddLogFlags(const uint8_t flags) override final;
| |||
void RemoveLogFlags(const uint8_t flags) override final;
| |||
| |||
//--- Getters
| |||
inline int Handle() const override final { return m_handle; }
| |||
inline string Symbol() const override final { return ICTTradingSession_Simbolo(m_handle); }
| |||
inline ENUM_TIMEFRAMES Timeframe() const override final { return ICTTradingSession_Timeframe(m_handle); }
| |||
inline long ChartId() const override final { return ICTTradingSession_ChartId(m_handle); }
| |||
inline int Subwin() const override final { return ICTTradingSession_Subwin(m_handle); }
| |||
inline bool InitialStateObj() const override final { return ICTTradingSession_GetInittialStateObj(m_handle); }
| |||
| |||
inline uint8_t ModifyFlags() const override final { return ICTTradingSession_ModifyFlags(m_handle); }
| |||
inline bool GetStateObj() const override final { return ICTTradingSession_GetStateObj(m_handle); }
| |||
inline void Summary() const override final { ICTTradingSession_Summary(m_handle); }
| |||
inline void ChangeChart(long new_chart_id, int new_subwin) override final { ICTTradingSession_ChangeChart(m_handle, new_chart_id, new_subwin); }
| |||
| |||
};
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
CTradingSession::CTradingSession()
| |||
: m_handle(INVALID_HANDLE)
| |||
{
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
CTradingSession::~CTradingSession()
| |||
{
| |||
Destroy();
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
bool CTradingSession::Create(string symbol_, ENUM_TIMEFRAMES timeframe_, long chart_id_, int subwin_, bool show_concept_,
| |||
bool forzar_lunes_viernes, int start_hour, int start_minute, int end_hour, int end_minute, string session_name)
| |||
{
| |||
if(m_handle != INVALID_HANDLE)
| |||
Destroy();
| |||
| |||
//---
| |||
m_handle = ICTTradingSession_Create(symbol_, timeframe_, chart_id_, subwin_, show_concept_, forzar_lunes_viernes, start_hour, start_minute, end_hour, end_minute, session_name);
| |||
| |||
//---
| |||
if(m_handle >= 0)
| |||
{
| |||
return true;
| |||
}
| |||
else
| |||
{
| |||
LogError("No se pudo crear un m_handle para CTradingSession", FUNCION_ACTUAL);
| |||
Remover();
| |||
}
| |||
| |||
//---
| |||
return false;
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
inline void CTradingSession::SetExtra(color clr_line_init, color clr_line_end, ENUM_LINE_STYLE style_lines_, int width_lines_, ENUM_TYPE_DRAW_RANGE_SESSION type_draw_)
| |||
{
| |||
ICTTradingSession_SetExtra(m_handle, clr_line_init, clr_line_end, style_lines_, width_lines_, type_draw_);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
inline void CTradingSession::SetGrapichStyles(ENUM_LINE_STYLE rect_style_, color clr_rect_, int widht_rect_, bool fill_rect_)
| |||
{
| |||
ICTTradingSession_SetGrapichStyles(m_handle, rect_style_, clr_rect_, widht_rect_, fill_rect_);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline bool CTradingSession::ShowAll()
| |||
{
| |||
return ICTTradingSession_ShowAll(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline bool CTradingSession::HideAll()
| |||
{
| |||
return ICTTradingSession_HideAll(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline bool CTradingSession::DeleteAll()
| |||
{
| |||
return ICTTradingSession_DeleteAll(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline void CTradingSession::SetFlagMostrarObjetos(bool mostrar)
| |||
{
| |||
ICTTradingSession_SetFlagMostrarObjetos(m_handle, mostrar);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline void CTradingSession::ForzeToActulizeCurrentTick()
| |||
{
| |||
ICTTradingSession_ForzeToActulizeCurrentTick(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline datetime CTradingSession::GetInitTimeSessionCurrNext()
| |||
{
| |||
return ICTTradingSession_GetInitTimeSessionCurrNext(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline datetime CTradingSession::GetEndTimeSessionCurrNext()
| |||
{
| |||
return ICTTradingSession_GetEndTimeSessionCurrNext(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline datetime CTradingSession::GetInitTimeSessionPrev()
| |||
{
| |||
return ICTTradingSession_GetInitTimeSessionPrev(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline datetime CTradingSession::GetEndTimeSessionPrev()
| |||
{
| |||
return ICTTradingSession_GetEndTimeSessionPrev(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline int CTradingSession::GetDuracionSessionSegundos()
| |||
{
| |||
return ICTTradingSession_GetDuracionSessionSegundos(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline int CTradingSession::GetDuracionSessionMinutos()
| |||
{
| |||
return ICTTradingSession_GetDuracionSessionMinutos(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline bool CTradingSession::IsInitSession()
| |||
{
| |||
return ICTTradingSession_IsInitSession(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline bool CTradingSession::IsEndSession()
| |||
{
| |||
return ICTTradingSession_IsEndSession(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline bool CTradingSession::EnSesion()
| |||
{
| |||
return ICTTradingSession_EnSesion(m_handle);
| |||
}
| |||
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline void CTradingSession::OnNewBarM1()
| |||
{
| |||
ICTTradingSession_OnNewBarM1(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline string CTradingSession::GetSessionName()
| |||
{
| |||
return ICTTradingSession_GetSesionName(m_handle);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
inline void CTradingSession::AddLogFlags(const uint8_t flags)
| |||
{
| |||
CLoggerBase::AddLogFlags(flags);
| |||
ICTTradingSession_AddLogFlags(m_handle, flags);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
void CTradingSession::RemoveLogFlags(const uint8_t flags)
| |||
{
| |||
CLoggerBase::RemoveLogFlags(flags);
| |||
ICTTradingSession_RemoveLogFlags(m_handle, flags);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
void CTradingSession::Destroy()
| |||
{
| |||
if(m_handle != INVALID_HANDLE)
| |||
{
| |||
ICTTradingSession_Delete(m_handle);
| |||
m_handle = INVALID_HANDLE;
| |||
}
| |||
}
| |||
| |||
#endif // ICTLIBRARY_CONCEPTS_SESION_TRADING_SESSION_MQH
| |||
//+------------------------------------------------------------------+
|