MQLArticles/RM/RiskManagementBases.mqh

193 lines
15 KiB
MQL5

2025-09-22 09:09:20 -05:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| RiskManagementBases.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
2025-11-10 09:33:30 -05:00
#property copyright "Copyright 2025, Leo."
2025-09-22 09:09:20 -05:00
#property link "https://www.mql5.com"
#property strict
#ifndef MQLARTICLES_RM_RISKMGMTBASES_MQH
#define MQLARTICLES_RM_RISKMGMTBASES_MQH
#include "AccountStatus.mqh"
//--- Risk management classes
class CRiskManagemetBase; // Base class
class CRiskManagemet; // Parent class
2025-11-26 07:03:20 -05:00
class CRiskManagemetPersonal; // Class for personal risk management
2025-09-22 09:09:20 -05:00
class CRiskManagemetPropFirm; // Class for prop firm risk management
class CRiskPointer; // Class to obtain a pointer based on the selected risk management
//---
class CLossProfit; // Class to handle maximum losses or gains
class CLossProfitManager; // Container class to manage CLossProfit objects
2026-02-14 17:00:45 -05:00
2025-11-26 07:03:20 -05:00
2025-09-22 09:09:20 -05:00
2026-01-28 13:02:22 -05:00
//---
2025-09-22 09:09:20 -05:00
#define RISK_MANAGEMENT_RESERVE_POS 10
2026-02-19 11:33:02 -05:00
//+------------------------------------------------------------------+
//| Base Class CRiskManagement |
2025-09-22 09:09:20 -05:00
//+------------------------------------------------------------------+
class CRiskManagemetBase : public CAccountGestor
{
protected:
CTrade* trade; //Instancia de ctrade
Position open_positions[]; //Tickets abiertos por el maic
ulong magic_number; //Numero magico
double daily_profit, weekly_profit, monthly_profit, gross_profit; //Profits de la cuenta
2026-02-14 17:00:45 -05:00
2026-02-06 08:24:16 -05:00
datetime last_day_time;
2025-11-26 07:03:20 -05:00
datetime last_weekly_time;
2025-09-22 09:09:20 -05:00
2025-11-26 07:03:20 -05:00
datetime last_monthly_time;
2025-11-23 08:04:30 -05:00
datetime init_trade_time;
2025-11-26 07:03:20 -05:00
double account_profit; //Ganancia de la cuenta NETA, no confundir con ganancia BRUTA desde que inicio la cuenta
2025-09-22 09:09:20 -05:00
2025-11-26 07:03:20 -05:00
bool positions_open;
2025-09-22 09:09:20 -05:00
void GetPositionsProfit(); // Function to get total profit from open positions
public:
CRiskManagemetBase(const ulong magic_);
2025-11-26 07:03:20 -05:00
~CRiskManagemetBase();
//---
virtual double GetValorWithApplied(double percentage_, int type, ENUM_APPLIED_PERCENTAGES applied) const = 0; // Function to get value based on max loss or gain type
//--- Function to set general class parameters
2025-09-22 09:09:20 -05:00
virtual void SetGeneralParameters(MqlParam &params[]);
//--- Getters
2025-11-23 08:04:30 -05:00
2025-09-22 09:09:20 -05:00
//- Magic number
2025-11-23 08:04:30 -05:00
2025-09-22 09:09:20 -05:00
inline ulong MagicNumber() const { return this.magic_number; }
2025-11-23 08:04:30 -05:00
//--- General profit retrieval
inline double GrossProfit() const { return this.gross_profit; }
2025-09-22 09:09:20 -05:00
inline double WeeklyProfit() const { return this.weekly_profit; }
inline double DailyProfit() const { return this.daily_profit; }
inline double MontlyProfit() const { return monthly_profit; }
//--- Functions to work with open positions
int GetPositions(uint flags) const; // Get total open positions filtered by flags
inline int16_t GetPositionsTotal() const { return (int16_t)open_positions.Size(); }
void CloseAllPositions(uint flags = FLAG_CLOSE_ALL_LOSS | FLAG_CLOSE_ALL_PROFIT | FLAG_POSITION_TYPE_BUY | FLAG_POSITION_TYPE_SELL);
void CloseAllOrders(uint flags);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CRiskManagemetBase::CRiskManagemetBase(const ulong magic_)
:
daily_profit(0.00), weekly_profit(0.00), monthly_profit(0.00), gross_profit(0.00), last_day_time(0), last_weekly_time(0), last_monthly_time(0)
, init_trade_time(0), account_profit(0.00), positions_open(false)
{
//---
ArrayResize(open_positions, 0, RISK_MANAGEMENT_RESERVE_POS);
2026-02-06 08:24:16 -05:00
2025-09-22 09:09:20 -05:00
//---
trade = new CTrade();
2026-02-06 08:24:16 -05:00
2025-09-22 09:09:20 -05:00
//---
2026-02-06 08:24:16 -05:00
2025-09-22 09:09:20 -05:00
trade.SetExpertMagicNumber(magic_);
if(magic_ == NOT_MAGIC_NUMBER)
LogWarning("No magic number has been chosen, taking into account all the magic numbers and the user's trades", FUNCION_ACTUAL);
2026-02-06 08:24:16 -05:00
2025-09-22 09:09:20 -05:00
this.magic_number = magic_;
}
2026-02-06 08:24:16 -05:00
2025-09-22 09:09:20 -05:00
//+------------------------------------------------------------------+
2025-11-10 09:33:30 -05:00
//| Destructor |
2025-09-22 09:09:20 -05:00
//+------------------------------------------------------------------+