MQLArticles/RM/Modificators.mqh

534 lines
39 KiB
MQL5
Raw Permalink Normal View History

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 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372/news"
#property strict
#ifndef RISK_MODIFICATORS_MQH
#define RISK_MODIFICATORS_MQH
#include "LossProfit.mqh"
//+--------------------------------------------------------------------+
2025-09-24 14:00:58 -05:00
//| Class to integrate external modifications to risk management |
2025-09-22 09:09:20 -05:00
//+--------------------------------------------------------------------+
class CExtraModifications : public CLoggerBase
2025-09-22 09:09:20 -05:00
{
protected:
2025-09-24 14:00:58 -05:00
CLossProfit* m_modifier;
ENUM_TYPE_LOSS_PROFIT m_property_to_modify;
string m_modifier_name;
2025-09-22 09:09:20 -05:00
public:
2025-09-24 14:00:58 -05:00
CExtraModifications(ENUM_TYPE_LOSS_PROFIT _property_to_modify = WRONG_VALUE) { m_property_to_modify = _property_to_modify; }
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
//--- 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.
2025-09-22 09:09:20 -05:00
void SetPointer(CLossProfit* _modifier);
2025-09-24 14:00:58 -05:00
// Name of the custom modifier (to differentiate from others)
inline string Name() const { return m_modifier_name; }
//--- Virtual functions
// Function that will be executed each time a position is closed
2025-09-22 09:09:20 -05:00
virtual void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) { }
2025-09-24 14:00:58 -05:00
// Function that will be executed each time an operation is opened
2025-09-22 09:09:20 -05:00
virtual void OnOpenPosition(ModifierOnOpenCloseStruct &on_open_close_struct) { }
2025-09-24 14:00:58 -05:00
// 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) { }
2025-09-24 14:00:58 -05:00
// Function that will be executed each new day
virtual void OnNewDay() { }
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
//| Set pointer |
2025-09-22 09:09:20 -05:00
//+------------------------------------------------------------------+
void CExtraModifications::SetPointer(CLossProfit * _modifier)
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
//---
if(CheckPointer(_modifier) == POINTER_INVALID)
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
LogFatalError(StringFormat("The pointer to CLossProfit* for m_modifier %s is invalid", Name()), FUNCION_ACTUAL);
2025-09-22 09:09:20 -05:00
Remover();
return;
}
2025-09-24 14:00:58 -05:00
//---
if(_modifier.GetType() != MaximumProfitOrLossAModify())
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
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);
2025-09-22 09:09:20 -05:00
Remover();
return;
}
2025-09-24 14:00:58 -05:00
//---
m_modifier = _modifier;
2025-09-22 09:09:20 -05:00
}
//+------------------------------------------------------------------+
2025-09-24 14:00:58 -05:00
//| Clase para aumentar el riesgo |
2025-09-22 09:09:20 -05:00
//+------------------------------------------------------------------+
enum ENUM_MULTIPLIER_METHOD_DR
{
DR_MULTIPLIER = 0, // Multiplier
DR_EXPONECIAL = 1, // Exponential
DR_SUMATORIO = 2 // Additive (Summation)
};
//--- Clase
class CDynamicRisk : public CExtraModifications
2025-09-22 09:09:20 -05:00
{
private:
double paso;
double val;
double percentage_a_empezar_modfiicacioneS;
ENUM_MULTIPLIER_METHOD_DR metod;
void Aumentar();
bool is_set;
public:
2025-09-24 14:00:58 -05:00
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
CDynamicRisk::CDynamicRisk(double step, double percentage_to_empezar, ENUM_TYPE_LOSS_PROFIT property_to_modify, ENUM_MULTIPLIER_METHOD_DR multiplier_)
: CExtraModifications(property_to_modify)
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
m_modifier_name = "Risk enhancer by default";
paso = step;
percentage_a_empezar_modfiicacioneS = percentage_to_empezar;
metod = multiplier_;
is_set = false;
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
if((int)metod > 2 || metod < 0)
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
LogFatalError(StringFormat("The method %s is invalid", EnumToString(metod)), FUNCION_ACTUAL);
2025-09-22 09:09:20 -05:00
Remover();
}
}
//+------------------------------------------------------------------+
void CDynamicRisk::OnInitModifier(ModfierInitInfo & on_init)
{
percentage_a_empezar_modfiicacioneS /= 100.0;
percentage_a_empezar_modfiicacioneS *= on_init.balance;
2025-09-24 14:00:58 -05:00
Print("Balance to increase risk: ", percentage_a_empezar_modfiicacioneS);
val = m_modifier.GetPercentage();
2025-09-22 09:09:20 -05:00
//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)
{
2025-09-24 14:00:58 -05:00
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.SetPercentage(val);
2025-09-22 09:09:20 -05:00
}
else
{
2025-09-24 14:00:58 -05:00
m_modifier.SetInitialPercentageOrMoney();
val = m_modifier.GetPercentage();
2025-09-22 09:09:20 -05:00
}
}
//+------------------------------------------------------------------+
void CDynamicRisk::Aumentar(void)
{
2025-09-24 14:00:58 -05:00
switch(metod)
2025-09-22 09:09:20 -05:00
{
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
2025-09-22 09:09:20 -05:00
{
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;
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)
2025-09-24 14:00:58 -05:00
: CExtraModifications(LP_GMLPO) //Modify the risk by operation
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
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";
2025-09-22 09:09:20 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CModificatorOscar::OnNewDay(void)
{
2025-09-24 14:00:58 -05:00
datetime hora_ny = HoraYMinutoADatetime(hora_ny_init, min_ny_inti, TimeCurrent());
2025-09-22 09:09:20 -05:00
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)
{
2025-09-24 14:00:58 -05:00
LogInfo("Operation closed during NY hours - current risk remains", FUNCION_ACTUAL);
2025-09-22 09:09:20 -05:00
return;
}
//---
if(es_perdida && cerro_por_sl)
{
2025-09-24 14:00:58 -05:00
LogInfo(StringFormat("SL reached - increasing risk. Loss: %.2f", on_open_close_struct.position.profit), FUNCION_ACTUAL);
double new_risk = m_modifier.GetPercentage() * fat_mul;
2025-09-22 09:09:20 -05:00
if(new_risk <= max_risk)
2025-09-24 14:00:58 -05:00
m_modifier.SetPercentage(new_risk);
2025-09-22 09:09:20 -05:00
else
2025-09-24 14:00:58 -05:00
m_modifier.SetPercentage(max_risk);
2025-09-22 09:09:20 -05:00
}
else
if(!es_perdida && cerro_por_tp)
{
2025-09-24 14:00:58 -05:00
LogInfo("TP reached - resetting to initial risk", FUNCION_ACTUAL);
m_modifier.SetInitialPercentageOrMoney();
2025-09-22 09:09:20 -05:00
}
}
//+--------------------------------------------------------------------------------------+
//| Clase para modificar el riesgo en base a la estrategia de 2.0 |
//+--------------------------------------------------------------------------------------+
class CModifierDynamicRisk : public CExtraModifications
2025-09-22 09:09:20 -05:00
{
private:
double to_applied[];
int index_gmlpo;
public:
CModifierDynamicRisk(string percentages_to_applied);
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override;
};
//+------------------------------------------------------------------+
CModifierDynamicRisk::CModifierDynamicRisk(string percentages_to_applied)
: CExtraModifications(LP_GMLPO)
2025-09-22 09:09:20 -05:00
{
StrTo::CstArray(to_applied, percentages_to_applied, ',');
2025-09-24 14:00:58 -05:00
index_gmlpo = 0;
m_modifier_name = "Dynamic Risk 2.0";
2025-09-22 09:09:20 -05:00
}
//+------------------------------------------------------------------+
void CModifierDynamicRisk::OnClosePosition(ModifierOnOpenCloseStruct & on_open_close_struct)
{
if(on_open_close_struct.position.profit < 0.00) //La posicion salio SL
{
// Print(__FUNCTION__, "Se a obtenido un sl modificando el riesgo...");
2025-09-24 14:00:58 -05:00
m_modifier.SetPercentage(to_applied[index_gmlpo]);
2025-09-22 09:09:20 -05:00
if(index_gmlpo < (int)to_applied.Size() - 1)
index_gmlpo++;
}
else //La posicion salio TP
{
//Print(__FUNCTION__, "Se a obtenido un tp modificando el riesgo...");
2025-09-24 14:00:58 -05:00
index_gmlpo = 0;
2025-09-22 09:09:20 -05:00
2025-09-24 14:00:58 -05:00
m_modifier.SetInitialPercentageOrMoney(); //Modificar al riesgo inicial
2025-09-22 09:09:20 -05:00
}
}
//+------------------------------------------------------------------+
//| Modificador de riesgo 2 |
//+------------------------------------------------------------------+
class CDynamicRiskAlma : public CExtraModifications
2025-09-22 09:09:20 -05:00
{
private:
double multiplier;
public:
CDynamicRiskAlma(double mul)
2025-09-24 14:00:58 -05:00
: CExtraModifications(LP_GMLPO) { multiplier = mul; m_modifier_name = "Dynamic Risk Alma v2"; }
2025-09-22 09:09:20 -05:00
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override;
};
//+------------------------------------------------------------------+
void CDynamicRiskAlma::OnClosePosition(ModifierOnOpenCloseStruct & on_open_close_struct)
{
if(on_open_close_struct.profit_total < 0)
{
2025-09-24 14:00:58 -05:00
double r = m_modifier.GetPercentage() * multiplier;
m_modifier.SetPercentage(r);
2025-09-22 09:09:20 -05:00
}
else
{
2025-09-24 14:00:58 -05:00
m_modifier.SetInitialPercentageOrMoney();
2025-09-22 09:09:20 -05:00
}
}
//+------------------------------------------------------------------+
//| Clase para modificar riesgo 3 |
//+------------------------------------------------------------------+
inline double FunctionKevin(double defect_risk, double loss_percentage, double constante)
{
return (defect_risk * MathExp(loss_percentage / constante));
}
//+------------------------------------------------------------------+
class CMathDynamicRisk : public CExtraModifications
2025-09-22 09:09:20 -05:00
{
private:
double constante;
double balance_inicial;
public:
CMathDynamicRisk(double constante_)
2025-09-24 14:00:58 -05:00
: CExtraModifications(LP_GMLPO) { constante = constante_; m_modifier_name = "Modifier By Kevin"; }
2025-09-22 09:09:20 -05:00
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override;
void OnInitModifier(ModfierInitInfo &on_init) override;
};
//+------------------------------------------------------------------+
void CMathDynamicRisk::OnInitModifier(ModfierInitInfo & on_init)
{
2025-09-24 14:00:58 -05:00
balance_inicial = on_init.balance;
2025-09-22 09:09:20 -05:00
}
//+------------------------------------------------------------------+
void CMathDynamicRisk::OnClosePosition(ModifierOnOpenCloseStruct & on_open_close_struct)
{
double percentage_ = CalcularPorcentaje(on_open_close_struct.profit_total, balance_inicial);
if(percentage_ < 0.00)
{
2025-09-24 14:00:58 -05:00
double new_percentage = FunctionKevin(m_modifier.GetInitialPercentageOrMoney(), percentage_, constante);
m_modifier.SetPercentage(NormalizeDouble(new_percentage, 2));
2025-09-22 09:09:20 -05:00
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CDynamicFOptimal : public CExtraModifications
2025-09-22 09:09:20 -05:00
{
private:
int num_pos; // N<EFBFBD>mero total de posiciones cerradas
double profit[]; // Profits de operaciones ganadoras
double loss[]; // P<EFBFBD>rdidas (valor absoluto)
double sum_profit; // Suma acumulada de profits
double sum_loss; // Suma acumulada de p<EFBFBD>rdidas
double avg_profit; // Promedio de profits
double avg_loss; // Promedio de p<EFBFBD>rdidas
double rr; // Relaci<EFBFBD>n ganancia/p<EFBFBD>rdida (R)
double win_probability; // Probabilidad de ganar (p)
int min_trades; // M<EFBFBD>nimo de operaciones para Kelly
double kelly_fraction; // Fracci<EFBFBD>n de Kelly (por ejemplo, 0.5)
int max_data; // M<EFBFBD>ximo n<EFBFBD>mero de operaciones a almacenar
bool enable_kelly_mod; // Agregar como variable de clase
public:
CDynamicFOptimal(int min_trades_, int max_data_to_update, double kelly_fraction_) : CExtraModifications(LP_GMLPO)
2025-09-22 09:09:20 -05:00
{
2025-09-24 14:00:58 -05:00
m_modifier_name = "Optimal F Modifier";
2025-09-22 09:09:20 -05:00
num_pos = 0;
sum_profit = 0.0;
sum_loss = 0.0;
avg_profit = 0.0;
avg_loss = 0.0;
rr = 0.0;
win_probability = 0.0;
min_trades = MathMax(min_trades_, 10);
kelly_fraction = MathMax(0.0, MathMin(kelly_fraction_, 1.0)); // Corregir
max_data = MathMax(max_data_to_update, 30);
enable_kelly_mod = true;
ArrayResize(profit, 0);
ArrayResize(loss, 0);
}
void TrimOldData()
{
int total_data = ArraySize(profit) + ArraySize(loss);
if(total_data <= max_data)
return;
while(total_data > max_data)
{
if(ArraySize(profit) > 0 && (ArraySize(loss) == 0 || profit[0] <= loss[0]))
{
sum_profit -= profit[0];
ArrayRemove(profit, 0, 1);
int win_count = ArraySize(profit);
avg_profit = win_count > 0 ? sum_profit / win_count : 0.0;
num_pos--; // Decrementar
}
else
if(ArraySize(loss) > 0)
{
sum_loss -= loss[0];
ArrayRemove(loss, 0, 1);
int loss_count = ArraySize(loss);
avg_loss = loss_count > 0 ? sum_loss / loss_count : 0.0;
num_pos--; // Decrementar
}
total_data = ArraySize(profit) + ArraySize(loss);
}
}
void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct) override
{
double profit_value = on_open_close_struct.position.profit;
num_pos++;
if(profit_value == 0)
{
2025-09-24 14:00:58 -05:00
Print("Break-even operation detected");
return;
2025-09-22 09:09:20 -05:00
}
if(profit_value > 0)
{
AddArrayNoVerification(profit, profit_value, 0);
sum_profit += profit_value;
int win_count = ArraySize(profit);
avg_profit = win_count > 0 ? sum_profit / win_count : 0.0;
}
else
{
double val = fabs(profit_value);
AddArrayNoVerification(loss, val, 0);
sum_loss += fabs(profit_value);
int loss_count = ArraySize(loss);
avg_loss = loss_count > 0 ? sum_loss / loss_count : 0.0;
}
TrimOldData();
int win_count = ArraySize(profit);
int total_data = ArraySize(profit) + ArraySize(loss);
win_probability = total_data > 0 ? (double)win_count / total_data : 0.0;
rr = avg_loss > 0 ? avg_profit / avg_loss : 0.0;
PrintFormat("%s: Debug | win_count: %d, loss_count: %d, avg_profit: %.2f, avg_loss: %.2f", __FUNCTION__, ArraySize(profit), ArraySize(loss), avg_profit, avg_loss);
if(enable_kelly_mod && num_pos >= min_trades && rr > 0 && win_probability > 0)
{
double kelly = (win_probability * (rr + 1) - 1) / rr;
kelly = MathMax(0.0, MathMin(kelly, 1.0));
double adjusted_kelly = kelly * kelly_fraction;
2025-09-24 14:00:58 -05:00
m_modifier.SetPercentage(adjusted_kelly * 100.0);
2025-09-22 09:09:20 -05:00
PrintFormat("%s: Info | Kelly ajustado: %+.2f, Probabilidad: %.2f, R: %.2f, Operaciones: %d", __FUNCTION__, adjusted_kelly * 100.0, win_probability, rr, num_pos);
}
}
};
//+------------------------------------------------------------------+
//| Clase manager para modidicadores |
//+------------------------------------------------------------------+
class CManagerModificator : public CManagerBase<CExtraModifications>
2025-09-22 09:09:20 -05:00
{
public:
CManagerModificator() : CManagerBase<CExtraModifications>(false) { }
2025-09-22 09:09:20 -05:00
inline void OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct);
inline void OnOpenPosition(ModifierOnOpenCloseStruct &on_open_close_struct);
inline void OnNewDay();
};
//+------------------------------------------------------------------+
inline void CManagerModificator::OnClosePosition(ModifierOnOpenCloseStruct &on_open_close_struct)
{
for(int i = 0; i < total; i++)
items[i].OnClosePosition(on_open_close_struct);
}
//+------------------------------------------------------------------+
inline void CManagerModificator::OnOpenPosition(ModifierOnOpenCloseStruct &on_open_close_struct)
{
for(int i = 0; i < total; i++)
items[i].OnOpenPosition(on_open_close_struct);
}
//+------------------------------------------------------------------+
inline void CManagerModificator::OnNewDay(void)
{
for(int i = 0; i < total; i++)
items[i].OnNewDay();
}
/*
ADVERTENCIA al momeneto de a<EFBFBD>adir items a esta clase, esta si son dinamicos las eliminara cuidado.
2025-09-24 14:00:58 -05:00
WARNING when adding items to this class, if they are dynamic, be careful.
2025-09-22 09:09:20 -05:00
*/
//+------------------------------------------------------------------+
#endif
//+------------------------------------------------------------------+