ICTLibraryExamples/Plantillas/Main.mqh
157 라인
6.3 KiB
MQL5
Nique_372
2026-01-06 06:46:42 -05:00
//+------------------------------------------------------------------+
//| 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"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-23 21:51:38 -05:00
class CEstrategia : public TSN::CStrategyBase
Nique_372
2026-01-06 06:46:42 -05:00
{
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);
Nique_372
2026-02-01 12:28:19 -05:00
~CEstrategia(void);
Nique_372
2026-01-06 06:46:42 -05:00
//--- Funciones de ejecucion
Nique_372
2026-02-14 17:50:28 -05:00
void OnNewBar(const datetime curr_time) override final;
void OnTick(const datetime curr_time) override final;
void OnNewWeek(const datetime curr_time) override final;
Nique_372
2026-01-06 06:46:42 -05:00
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")
{
Nique_372
2026-02-01 12:28:19 -05:00
// 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);
}
Nique_372
2026-01-06 06:46:42 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-02-14 17:50:28 -05:00
void CEstrategia::OnTick(const datetime curr_time)
Nique_372
2026-01-06 06:46:42 -05:00
{
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);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-02-14 17:50:28 -05:00
void CEstrategia::OnNewBar(const datetime curr_time)
Nique_372
2026-01-06 06:46:42 -05:00
{
m_has_trade = g_risk.GetPositionsTotal() == 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-02-14 17:50:28 -05:00
void CEstrategia::OnNewWeek(const datetime curr_time)
Nique_372
2026-01-06 06:46:42 -05:00
{
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);
Nique_372
2026-11-01 22:12:14 -05:00
g_risk.SetStopLoss(m_symbol, entry - sl);
Nique_372
2026-01-06 06:46:42 -05:00
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);
Nique_372
2026-11-01 22:12:14 -05:00
g_risk.SetStopLoss(m_symbol, sl - entry);
Nique_372
2026-01-06 06:46:42 -05:00
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();
}
//+------------------------------------------------------------------+