MQLArticles/RM/Modificators.mqh

533 行
39 KiB
MQL5

2025-09-22 09:09:20 -05:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Modificators.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
2025-11-10 09:33:30 -05:00
#property copyright "Copyright 2025, Niquel Mendoza."
2025-09-22 09:09:20 -05:00
#property link "https://www.mql5.com/es/users/nique_372/news"
#property strict
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
2025-09-22 09:09:20 -05:00
#ifndef MQLARTICLES_RM_MODIFICATORS_MQH
2025-11-26 07:03:20 -05:00
#define MQLARTICLES_RM_MODIFICATORS_MQH
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
#include "LossProfit.mqh"
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
//+--------------------------------------------------------------------+
//| Class to integrate external modifications to risk management |
//+--------------------------------------------------------------------+
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
class CExtraModifications : public CLoggerBase
2025-09-24 14:00:58 -05:00
{
protected:
2025-09-22 09:09:20 -05:00
const CLossProfit* m_modifier;
2025-09-24 14:00:58 -05:00
ENUM_TYPE_LOSS_PROFIT m_property_to_modify;
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
string m_modifier_name;
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
public:
2025-09-22 09:09:20 -05:00
CExtraModifications(ENUM_TYPE_LOSS_PROFIT _property_to_modify = WRONG_VALUE) { m_property_to_modify = _property_to_modify; }
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
//--- Non-modifiable functions
2025-09-22 09:09:20 -05:00
2025-11-26 07:03:20 -05:00
// Function that returns the type of "maximum loss or profit" that this class is modifying
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
inline ENUM_TYPE_LOSS_PROFIT MaximumProfitOrLossAModify() const { return m_property_to_modify; };
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
// 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.
2025-09-24 14:00:58 -05:00
void SetPointer(const CLossProfit* const _modifier);
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
// Name of the custom modifier (to differentiate from others)
2025-09-22 09:09:20 -05:00
inline string Name() const { return m_modifier_name; }
2025-09-24 14:00:58 -05:00
//--- Virtual functions
2025-09-22 09:09:20 -05:00
// Function that will be executed each time a position is closed
2025-09-24 14:00:58 -05:00
virtual void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) { }
2025-09-22 09:09:20 -05:00
// 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)
2025-09-22 09:09:20 -05:00
virtual void OnInitModifier(ModfierInitInfo &on_init) { }
// Function that will be executed each new day
virtual void OnNewDay() { }
};
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
//+------------------------------------------------------------------+
2025-09-22 09:09:20 -05:00
//| Set pointer |
2025-09-24 14:00:58 -05:00
//+------------------------------------------------------------------+
void CExtraModifications::SetPointer(const CLossProfit* const _modifier)
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
//---
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
if(CheckPointer(_modifier) == POINTER_INVALID)
{
LogFatalError(StringFormat("The pointer to CLossProfit* for m_modifier %s is invalid", Name()), FUNCION_ACTUAL);
Remover();
return;
2025-09-24 14:00:58 -05:00
2025-11-26 07:03:20 -05:00
}
2025-09-22 09:09:20 -05:00
//---
if(_modifier.GetType() != MaximumProfitOrLossAModify())
2025-09-24 14:00:58 -05:00
{
2025-09-22 09:09:20 -05:00
LogFatalError(StringFormat("The type of the maximum loss/gain = %s, is different from the property to be modified = %s",
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
_modifier.Name(), EnumToString(MaximumProfitOrLossAModify())), FUNCION_ACTUAL);
Remover();
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
return;
}
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
//---
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)
2025-09-22 09:09:20 -05:00
};
//--- Clase
class CDynamicRisk : public CExtraModifications
{
private:
double paso;
double val;
double percentage_a_empezar_modfiicacioneS;
ENUM_MULTIPLIER_METHOD_DR metod;
void Aumentar();
2025-09-24 14:00:58 -05:00
bool is_set;
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
public:
CDynamicRisk(double step, double percentage_to_empezar, ENUM_TYPE_LOSS_PROFIT property_to_modify, ENUM_MULTIPLIER_METHOD_DR multiplier_);
2025-09-22 09:09:20 -05:00
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override;
void OnInitModifier(ModfierInitInfo &on_init) override;
};
//+------------------------------------------------------------------+
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
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);
2025-09-24 14:00:58 -05:00
Remover();
2025-09-22 09:09:20 -05:00
}
}
2025-09-24 14:00:58 -05:00
2025-11-26 07:03:20 -05:00
//+------------------------------------------------------------------+
2025-09-22 09:09:20 -05:00
void CDynamicRisk::OnInitModifier(ModfierInitInfo & on_init)
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
{
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
percentage_a_empezar_modfiicacioneS /= 100.0;
percentage_a_empezar_modfiicacioneS *= on_init.balance;
Print("Balance to increase risk: ", percentage_a_empezar_modfiicacioneS);
2025-09-24 14:00:58 -05:00
2025-11-26 07:03:20 -05:00
val = m_modifier.GetCalcValue();
2025-09-22 09:09:20 -05:00
//Print("Valor del porcentaje: ", val);
}
//+------------------------------------------------------------------+
2025-09-22 09:09:20 -05:00
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);
2025-09-22 09:09:20 -05:00
Aumentar();
2025-09-24 14:00:58 -05:00
m_modifier.SetCalcValue(val);
2025-09-22 09:09:20 -05:00
}
else
{
val = m_modifier.SetInitialCalcValue();
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
}
}
//+------------------------------------------------------------------+
void CDynamicRisk::Aumentar(void)
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
{
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
switch(metod)
{
case DR_MULTIPLIER:
val *= paso;
2025-09-22 09:09:20 -05:00
break;
case DR_EXPONECIAL:
2025-09-24 14:00:58 -05:00
val *= MathExp(paso);
2025-09-22 09:09:20 -05:00
break;
case DR_SUMATORIO:
val += paso;
2025-11-26 07:03:20 -05:00
break;
2025-09-22 09:09:20 -05:00
default:
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
Remover();
break;
}
}
//+------------------------------------------------------------------+
//| |
2025-09-22 09:09:20 -05:00
//+------------------------------------------------------------------+
class CModificatorOscar : public CExtraModifications
{
private:
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
double max_risk;
double riesgo_inciial;
double fat_mul;
double prev_risk;
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
int hora_ny_init;
int min_ny_inti;
datetime hora_pa;
datetime hora_pd;
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
public:
CModificatorOscar(double max_risk_percent, double risk_init_percent, double multiplicator_, int init_ny_h, int init_ny_min);
void OnNewDay() override;
2025-09-22 09:09:20 -05:00
void OnInitModifier(ModfierInitInfo &on_init) override;
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override;
};
//+------------------------------------------------------------------+
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
{
2025-09-22 09:09:20 -05:00
max_risk = max_risk_percent;
2025-09-24 14:00:58 -05:00
2025-09-22 09:09:20 -05:00
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;
}
//+------------------------------------------------------------------+
//| |
2025-09-24 14:00:58 -05:00
//+------------------------------------------------------------------+
2025-09-22 09:09:20 -05:00
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)
2025-11-26 07:03:20 -05:00
{
2025-09-22 09:09:20 -05:00
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)
2025-09-22 09:09:20 -05:00
m_modifier.SetCalcValue(new_risk);
else
2025-09-22 09:09:20 -05:00
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-09-24 14:00:58 -05:00
class CModifierDynamicRisk : public CExtraModifications
2025-09-22 09:09:20 -05:00
{
2025-11-10 09:33:30 -05:00
private:
2025-09-22 09:09:20 -05:00
double to_applied[];