MQLArticles/RM/RM_Hooks.mqh

553 lines
40 KiB
MQL5

2025-12-21 12:44:08 -05:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Modificators.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
2025-12-21 12:46:48 -05:00
#property copyright "Copyright 2025, Niquel Mendoza."
2025-12-21 12:44:08 -05:00
#property link "https://www.mql5.com/es/users/nique_372/news"
#property strict
#ifndef MQLARTICLES_RM_HOOKS_MQH
#define MQLARTICLES_RM_HOOKS_MQH
#include "LossProfit.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class IRiskManagementHook : public CLoggerBase
{
public:
//--- Virtual functions
// Function that will be executed each time a position is closed
virtual void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) { }
// Function that will be executed each time an operation is opened
virtual void OnOpenPosition(ModifierOnOpenCloseStruct &on_open_close_struct) { }
// Function that will be executed only once (at the moment of creating the m_modifier)
virtual void OnInitModifier(ModfierInitInfo &on_init) { }
// Function that will be executed each new day
virtual void OnNewDay() { }
//---
virtual inline string Name() const = 0;
};
//+--------------------------------------------------------------------+
//| Class to integrate external modifications to risk management |
//+--------------------------------------------------------------------+
class CExtraModifications : public IRiskManagementHook
{
protected:
const CLossProfit* m_modifier;
ENUM_TYPE_LOSS_PROFIT m_property_to_modify;
string m_modifier_name;
public:
CExtraModifications(ENUM_TYPE_LOSS_PROFIT _property_to_modify = WRONG_VALUE) { m_property_to_modify = _property_to_modify; }
//--- Non-modifiable functions
// Function that returns the type of "maximum loss or profit" that this class is modifying
inline ENUM_TYPE_LOSS_PROFIT MaximumProfitOrLossAModify() const { return m_property_to_modify; };
// Function that will be used in CRiskManagement to assign the "maximum loss or profit" based on the type of "maximum loss or profit" chosen
// in the constructor.
void SetPointer(const CLossProfit* const _modifier);
// Name of the custom modifier (to differentiate from others)
inline string Name() const override final { return m_modifier_name; }
};
//+------------------------------------------------------------------+
//| Set pointer |
//+------------------------------------------------------------------+
void CExtraModifications::SetPointer(const CLossProfit* const _modifier)
{
//---
if(CheckPointer(_modifier) == POINTER_INVALID)
{
LogFatalError(StringFormat("The pointer to CLossProfit* for m_modifier %s is invalid", Name()), FUNCION_ACTUAL);
Remover();
return;
}
//---
if(_modifier.GetType() != MaximumProfitOrLossAModify())
{
LogFatalError(StringFormat("The type of the maximum loss/gain = %s, is different from the property to be modified = %s",
_modifier.Name(), EnumToString(MaximumProfitOrLossAModify())), FUNCION_ACTUAL);
Remover();
return;
}
//---
m_modifier = _modifier;
}
//+------------------------------------------------------------------+
//| Clase para aumentar el riesgo |
//+------------------------------------------------------------------+
enum ENUM_MULTIPLIER_METHOD_DR
{
DR_MULTIPLIER = 0, // Multiplier
DR_EXPONECIAL = 1, // Exponential
DR_SUMATORIO = 2 // Additive (Summation)
};
//--- Clase
class CDynamicRisk : public CExtraModifications
{
private:
double paso;
double val;
double percentage_a_empezar_modfiicacioneS;
ENUM_MULTIPLIER_METHOD_DR metod;
void Aumentar();
bool is_set;
public:
CDynamicRisk(double step, double percentage_to_empezar, ENUM_TYPE_LOSS_PROFIT property_to_modify, ENUM_MULTIPLIER_METHOD_DR multiplier_);
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override final;
void OnInitModifier(ModfierInitInfo &on_init) override final;
};
//+------------------------------------------------------------------+
CDynamicRisk::CDynamicRisk(double step, double percentage_to_empezar, ENUM_TYPE_LOSS_PROFIT property_to_modify, ENUM_MULTIPLIER_METHOD_DR multiplier_)
: CExtraModifications(property_to_modify)
{
m_modifier_name = "Risk enhancer by default";
paso = step;
percentage_a_empezar_modfiicacioneS = percentage_to_empezar;
metod = multiplier_;
is_set = false;
if((int)metod > 2 || metod < 0)
{
LogFatalError(StringFormat("The method %s is invalid", EnumToString(metod)), FUNCION_ACTUAL);
Remover();
}
}
//+------------------------------------------------------------------+
void CDynamicRisk::OnInitModifier(ModfierInitInfo & on_init)
{
percentage_a_empezar_modfiicacioneS /= 100.0;
percentage_a_empezar_modfiicacioneS *= on_init.balance;
Print("Balance to increase risk: ", percentage_a_empezar_modfiicacioneS);
val = m_modifier.GetCalcValue();
//Print("Valor del porcentaje: ", val);
}
//+------------------------------------------------------------------+
void CDynamicRisk::OnClosePosition(ModifierOnOpenCloseStruct & on_open_close_struct)
{
if(on_open_close_struct.position.profit > 0 && on_open_close_struct.profit_total > percentage_a_empezar_modfiicacioneS)
{
LogInfo(StringFormat("By increasing the risk, a profit of %.2f has been obtained. The initial balance has been exceeded by %.2f.",
on_open_close_struct.position.profit, percentage_a_empezar_modfiicacioneS), FUNCION_ACTUAL);
Aumentar();
m_modifier.SetCalcValue(val);
}
else
{
val = m_modifier.SetInitialCalcValue();
}
}
//+------------------------------------------------------------------+
void CDynamicRisk::Aumentar(void)
{
switch(metod)
{
case DR_MULTIPLIER:
val *= paso;
break;
case DR_EXPONECIAL:
val *= MathExp(paso);
break;
case DR_SUMATORIO:
val += paso;
break;
default:
Remover();
break;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CModificatorOscar : public CExtraModifications
{
private:
double max_risk;
double riesgo_inciial;
double fat_mul;
double prev_risk;
int hora_ny_init;
int min_ny_inti;
datetime hora_pa;
datetime hora_pd;
public:
CModificatorOscar(double max_risk_percent, double risk_init_percent, double multiplicator_, int init_ny_h, int init_ny_min);
//---
void OnNewDay() override final;
void OnInitModifier(ModfierInitInfo &on_init) override final;
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override final;
};
//+------------------------------------------------------------------+
CModificatorOscar::CModificatorOscar(double max_risk_percent, double risk_init_percent, double multiplicator_, int init_ny_h, int init_ny_min)
: CExtraModifications(LP_GMLPO) //Modify the risk by operation
{
max_risk = max_risk_percent;
riesgo_inciial = risk_init_percent;
fat_mul = multiplicator_;
hora_ny_init = init_ny_h;
min_ny_inti = init_ny_min;
m_modifier_name = "Modifier by oscar";
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CModificatorOscar::OnNewDay(void)
{
datetime hora_ny = HoraYMinutoADatetime(hora_ny_init, min_ny_inti, TimeCurrent());
hora_pa = hora_ny - 60;
hora_pd = hora_ny + 60;
}
//+------------------------------------------------------------------+
void CModificatorOscar::OnInitModifier(ModfierInitInfo &on_init)
{
riesgo_inciial /= 100.0;
riesgo_inciial *= on_init.balance;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CModificatorOscar::OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct)
{
bool es_perdida = on_open_close_struct.position.profit < 0.00;
bool cerro_por_sl = on_open_close_struct.deal_reason == DEAL_REASON_SL;
bool cerro_por_tp = on_open_close_struct.deal_reason == DEAL_REASON_TP;
bool cerrado_en_ny = TimeCurrent() >= hora_pa && TimeCurrent() <= hora_pd;
//---
if(cerrado_en_ny)
{
LogInfo("Operation closed during NY hours - current risk remains", FUNCION_ACTUAL);
return;
}
//---
if(es_perdida && cerro_por_sl)
{
LogInfo(StringFormat("SL reached - increasing risk. Loss: %.2f", on_open_close_struct.position.profit), FUNCION_ACTUAL);
double new_risk = m_modifier.GetCalcValue() * fat_mul;
if(new_risk <= max_risk)
m_modifier.SetCalcValue(new_risk);
else
m_modifier.SetCalcValue(max_risk);
}
else
if(!es_perdida && cerro_por_tp)
{
LogInfo("TP reached - resetting to initial risk", FUNCION_ACTUAL);
m_modifier.SetInitialCalcValue();
}
}
//+--------------------------------------------------------------------------------------+
//| Clase para modificar el riesgo en base a la estrategia de 2.0 |
//+--------------------------------------------------------------------------------------+
2025-12-21 12:46:48 -05:00
class CModifierDynamicRisk : public CExtraModifications
2025-12-21 12:44:08 -05:00
{