| //+------------------------------------------------------------------+
|
| //| Main.mqh |
|
| //| Copyright 2025, Niquel Mendoza. |
|
| //| https://www.mql5.com/es/users/nique_372 |
|
| //+------------------------------------------------------------------+
|
| #property copyright "Copyright 2025, Niquel Mendoza."
|
| #property link "https://www.mql5.com/es/users/nique_372"
|
| #property strict
|
|
|
| //+------------------------------------------------------------------+
|
| //| Include |
|
| //+------------------------------------------------------------------+
|
| #include "Functions.mqh"
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| class CEstrategia : public TSN::CStrategyBase
|
| {
|
| private:
|
| //--- Variables miembro
|
| bool m_has_trade;
|
|
|
| //--- Funciones
|
| void Clean();
|
| void OpenOrder(ENUM_POSITION_TYPE type);
|
|
|
|
|
| public:
|
| CEstrategia(ulong magic_number_, string symbol_, ENUM_TIMEFRAMES timeframe_ = PERIOD_CURRENT, long chart_id_ = 0,
|
| int subwindow_ = 0, ulong max_deviation_ = NO_MAX_DEVIATION_DEFINED);
|
| ~CEstrategia(void);
|
|
|
| //--- Funciones de ejecucion
|
| void OnNewBar(const datetime curr_time) override final;
|
| void OnTick(const datetime curr_time) override final;
|
| void OnNewWeek(const datetime curr_time) override final;
|
| void OnInterupcion() override final;
|
| };
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| CEstrategia::CEstrategia(ulong magic_number_, string symbol_, ENUM_TIMEFRAMES timeframe_ = PERIOD_CURRENT, long chart_id_ = 0,
|
| int subwindow_ = 0, ulong max_deviation_ = NO_MAX_DEVIATION_DEFINED)
|
| : CStrategyBase(magic_number_, symbol_, timeframe_, chart_id_, subwindow_, max_deviation_, "Estrategia simple")
|
| {
|
| // Debido a que usaremos el evento OnNewWeel que heredamos de CAllBasicEvents
|
| // Entonces necesitamos que la clase se REGISTRE para recibir dicho evento.
|
| // A continuacion haremos eso:
|
| CBasicEvents::RegisterEvent(&this, BASICEVENT_REG_FLAG_ON_NEW_WEEK);
|
| }
|
|
|
|
|
| //+------------------------------------------------------------------+
|
| CEstrategia::~CEstrategia(void)
|
| {
|
| // En el destructor de la clase nos "de-suscribiremos" de el evento SOLO si aun el manager SIGUE activo
|
| if(CBasicEvents::IsActive()) // CONSULTAMOS si aun esta activo
|
| {
|
| // Si esta activo entonces nos desuscribirremos
|
| CBasicEvents::UnregisterEvent(&this, BASICEVENT_REG_FLAG_ON_NEW_WEEK);
|
| }
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| void CEstrategia::Clean(void)
|
| {
|
| m_has_trade = false; // Ponemos en false m_has_trade, el bot ya no operarara
|
| g_ob.FreeArrayMitigated(); // Limpiamos los arrays internos (donde se alamcenan los ob mitigados
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| void CEstrategia::OnTick(const datetime curr_time)
|
| {
|
| if(!m_has_trade)
|
| return;
|
|
|
| if(g_ob.SizeAlcistasMitigados() > 0)
|
| {
|
| LogInfo("Mitigacion de order block alcista AHORA", FUNCION_ACTUAL);
|
| OpenOrder(POSITION_TYPE_BUY);
|
| }
|
| else
|
| if(g_ob.SizeBajistasMitigados() > 0)
|
| {
|
| LogInfo("Mitigacion de order block bajista AHORA", FUNCION_ACTUAL);
|
| OpenOrder(POSITION_TYPE_SELL);
|
| }
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| void CEstrategia::OnNewBar(const datetime curr_time)
|
| {
|
| m_has_trade = g_risk.GetPositionsTotal() == 0;
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| void CEstrategia::OnNewWeek(const datetime curr_time)
|
| {
|
| g_ob.DeleteAll(); // Limpiamos todos los ob, esto ayuda a reducir la carga de memoria, etc.
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| void CEstrategia::OnInterupcion(void)
|
| {
|
| // Dado que el concpeto OB se ejeucta con la estrateiga.. habra "un paro" cuando se supere el MDL etc..
|
| // Por lo que en caso pase eso tenemos que AVISARLE a la clase que SE DENTRA su llamada (OnNewBar/OnTick)
|
| // Esto lo hacemos llamando a la funcion SetParo
|
| g_ob.SetParo();
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| Open Order Default |
|
| //+------------------------------------------------------------------+
|
| void CEstrategia::OpenOrder(ENUM_POSITION_TYPE type)
|
| {
|
| //---
|
| ::SymbolInfoTick(m_symbol, m_tick);
|
|
|
| //---
|
| if(type == POSITION_TYPE_BUY)
|
| {
|
| double entry = m_tick.ask;
|
| double sl = GetSL(entry, type, m_tick.time);
|
| double tp = GetTP(entry, type, m_tick.time);
|
|
|
| g_risk.SetStopLoss(m_symbol, entry - sl);
|
| double l = m_LOT_SIZE > 0.00 ? m_LOT_SIZE : g_risk.GetLote(ORDER_TYPE_BUY, entry, m_max_deviation, 0);
|
| m_trade.Buy(l, m_symbol, entry, sl, tp, "Ea buy");
|
| }
|
| else
|
| if(type == POSITION_TYPE_SELL)
|
| {
|
| double entry = m_tick.bid;
|
| double sl = GetSL(entry, type, m_tick.time);
|
| double tp = GetTP(entry, type, m_tick.time);
|
|
|
| g_risk.SetStopLoss(m_symbol, sl - entry);
|
| double l = m_LOT_SIZE > 0.00 ? m_LOT_SIZE : g_risk.GetLote(ORDER_TYPE_SELL, entry, m_max_deviation, 0);
|
| m_trade.Sell(l, m_symbol, entry, sl, tp, "Ea sell");
|
| }
|
|
|
| //---
|
| Clean();
|
| }
|
| //+------------------------------------------------------------------+
|