347 lines
28 KiB
MQL5
347 lines
28 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 ICTLIBRARYEASY_SRC_CONCEPTS_TRADINGSESSION_MQH
|
|
#define ICTLIBRARYEASY_SRC_CONCEPTS_TRADINGSESSION_MQH
|
|
#include "..\\Base\\Imports.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 CLoggerBase
|
|
{
|
|
private:
|
|
int handle;
|
|
string symbol;
|
|
ENUM_TIMEFRAMES timeframe;
|
|
long chart_id;
|
|
int subwin;
|
|
bool show_concept;
|
|
|
|
//---
|
|
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();
|
|
inline bool HideAll();
|
|
inline bool DeleteAll();
|
|
inline void SetFlagMostrarObjetos(bool mostrar);
|
|
inline void ForzeToActulizeCurrentTick();
|
|
|
|
//--- 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();
|
|
|
|
|
|
//--- Eventos
|
|
inline void OnTickEvent();
|
|
|
|
//--- Utilidades
|
|
inline string GetSessionName();
|
|
inline void AddLogFlags(const uint8_t flags) override;
|
|
void RemoveLogFlags(const uint8_t flags) override;
|
|
|
|
//---
|
|
inline int Handle() const { return handle; }
|
|
inline string Symbol() const { return symbol; }
|
|
inline ENUM_TIMEFRAMES Timeframe() const { return timeframe; }
|
|
inline long ChartId() const { return chart_id; }
|
|
inline int Subwin() const { return subwin; }
|
|
inline bool ShowConcept() const { return show_concept; }
|
|
};
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
CTradingSession::CTradingSession()
|
|
: handle(INVALID_HANDLE), symbol(NULL), timeframe(WRONG_VALUE), chart_id(0), subwin(0), show_concept(false)
|
|
{
|
|
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
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(handle != INVALID_HANDLE)
|
|
Destroy();
|
|
|
|
handle = ICTTradingSession_Create(symbol_, timeframe_, chart_id_, subwin_, show_concept_, forzar_lunes_viernes, start_hour, start_minute, end_hour, end_minute, session_name);
|
|
|
|
if(handle >= 0)
|
|
{
|
|
symbol = symbol_;
|
|
timeframe = timeframe_;
|
|
chart_id = chart_id_;
|
|
subwin = subwin_;
|
|
show_concept = show_concept_;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
FastLog(FUNCION_ACTUAL, FATAL_ERROR_TEXT, "No se pudo crear un handle para CTradingSession");
|
|
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(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(handle, rect_style_, clr_rect_, widht_rect_, fill_rect_);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline bool CTradingSession::ShowAll()
|
|
{
|
|
return ICTTradingSession_ShowAll(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline bool CTradingSession::HideAll()
|
|
{
|
|
return ICTTradingSession_HideAll(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline bool CTradingSession::DeleteAll()
|
|
{
|
|
return ICTTradingSession_DeleteAll(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline void CTradingSession::SetFlagMostrarObjetos(bool mostrar)
|
|
{
|
|
ICTTradingSession_SetFlagMostrarObjetos(handle, mostrar);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline void CTradingSession::ForzeToActulizeCurrentTick()
|
|
{
|
|
ICTTradingSession_ForzeToActulizeCurrentTick(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline datetime CTradingSession::GetInitTimeSessionCurrNext()
|
|
{
|
|
return ICTTradingSession_GetInitTimeSessionCurrNext(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline datetime CTradingSession::GetEndTimeSessionCurrNext()
|
|
{
|
|
return ICTTradingSession_GetEndTimeSessionCurrNext(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline datetime CTradingSession::GetInitTimeSessionPrev()
|
|
{
|
|
return ICTTradingSession_GetInitTimeSessionPrev(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline datetime CTradingSession::GetEndTimeSessionPrev()
|
|
{
|
|
return ICTTradingSession_GetEndTimeSessionPrev(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline int CTradingSession::GetDuracionSessionSegundos()
|
|
{
|
|
return ICTTradingSession_GetDuracionSessionSegundos(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline int CTradingSession::GetDuracionSessionMinutos()
|
|
{
|
|
return ICTTradingSession_GetDuracionSessionMinutos(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline bool CTradingSession::IsInitSession()
|
|
{
|
|
return ICTTradingSession_IsInitSession(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline bool CTradingSession::IsEndSession()
|
|
{
|
|
return ICTTradingSession_IsEndSession(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline bool CTradingSession::EnSesion()
|
|
{
|
|
return ICTTradingSession_EnSesion(handle);
|
|
}
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline void CTradingSession::OnTickEvent()
|
|
{
|
|
ICTTradingSession_OnTickEvent(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline string CTradingSession::GetSessionName()
|
|
{
|
|
return ICTTradingSession_GetSesionName(handle);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline void CTradingSession::AddLogFlags(uint8_t log_flags)
|
|
{
|
|
ICTTradingSession_AddLogFlags(handle, log_flags);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
inline void CTradingSession::RemoveLogFlags(uint8_t log_flags)
|
|
{
|
|
ICTTradingSession_RemoveLogFlags(handle, log_flags);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void CTradingSession::Destroy()
|
|
{
|
|
if(handle != INVALID_HANDLE)
|
|
{
|
|
ICTTradingSession_Delete(handle);
|
|
handle = INVALID_HANDLE;
|
|
symbol = NULL;
|
|
timeframe = WRONG_VALUE;
|
|
chart_id = 0;
|
|
subwin = 0;
|
|
show_concept = false;
|
|
}
|
|
}
|
|
|
|
#endif // ICTLIBRARYEASY_SRC_CONCEPTS_TRADINGSESSION_MQH
|
|
//+------------------------------------------------------------------+
|