MQLArticles/Account/RM/LossProfit/Manager.mqh
Nique_372 Nique_372 87640cc853
2026-11-07 21:17:58 -05:00
352 lines
12 KiB
MQL5
//+------------------------------------------------------------------+
//| Manager.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
#ifndef MQLARTICLES_ACCOUNT_RM_LOSSPROFIT_MANAGERS_MQH
#define MQLARTICLES_ACCOUNT_RM_LOSSPROFIT_MANAGERS_MQH
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include "Basic\\MaxLosses.mqh"
#include "Basic\\MaxProfits.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| Manager class to work with different poses |
//+------------------------------------------------------------------+
// NOTA:
// Esta clase solo sirve de manager... y es dueña de sus punteros no shared ptr ni nada de eso
// aparte asume mucho, y tambien no propaga logs..
//---
#ifndef LOSSPROFIT_MANAGER_MAX_ITEMS
#define LOSSPROFIT_MANAGER_MAX_ITEMS (32)
#endif // LOSSPROFIT_MANAGER_MAX_ITEMS
//---
//#define TSN_MQLART_RM_LPMAN_INV_IDX (LOSSPROFIT_MANAGER_MAX_ITEMS-1)
//#define TSN_MQLART_RM_LPMAN_FINALS (TSN_MQLART_RM_LPMAN_INV_IDX)
#define TSN_MQLART_RM_LPMAN_TL_MAXS (LOSSPROFIT_MANAGER_MAX_ITEMS>>1)
//---
enum ENUM_TSNMQLART_RM_LPMAN_R
{
//---
TSNMQLART_RM_LPMAN_R_OK = 0,
//--- add
TSNMQLART_RM_LPMAN_R_INV_POINTER,
TSNMQLART_RM_LPMAN_R_ALREDY_IS_ADD,
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// 1. Solo peude tener 1 tipo de perdida en el manager no mas..
// osea solo peudes tener un MDL en el manager no dos limites
// 2. tamaños fijos de arrays para evitar el overhead de una rray dinamico
// como el 1# punto se cumple (maximo podemos tener X puntos donde x se sabe en compilacion) por lo que podemos
// tener ya el array al maximo tamaño que podra tener este..
// 3. Una vez que ya se agrega NO se peude quitar
//---
class CLossProfitManager
{
private:
//---
CLossProfit* m_losses_profits[LOSSPROFIT_MANAGER_MAX_ITEMS]; // todos los punteros de losses profits
int m_losses_profits_s;
//--- [type]=>puntero final
// por ahora lo hare asi quizas si me rente hashmap por ahora creo que no da... asi que esto
CLossProfit* m_type_to_ptr[LOSSPROFIT_MANAGER_MAX_ITEMS];
//--- Optimizaicon extra
// Tambien para iterar rapido sobre profit\losses
// tenemos arrays fijos
//-
CLossProfit* m_profits[TSN_MQLART_RM_LPMAN_TL_MAXS];
int m_profits_s;
int m_i_psup;
//-
CLossProfit* m_losses[TSN_MQLART_RM_LPMAN_TL_MAXS];
int m_losses_s;
int m_i_lsup;
public:
~CLossProfitManager();
CLossProfitManager();
//--- Clear
void Clear();
//--- Add
bool Add(CLossProfit* new_loss_profit); // Add CLossProfit
//--- Is Superated
// Index
__forceinline int GetLastIndexSuperatedProfit() const { return m_i_psup; }
__forceinline int GetLastIndexSuperatedLoss() const { return m_i_lsup; }
// Ptr
const CLossProfit* const GetLastLossSuperated() const { return m_losses[m_i_lsup]; }
const CLossProfit* const GetLastProfitSuperated() const { return m_profits[m_i_psup]; }
// Type
__forceinline int GetLastLossSuperatedType() const { return m_losses[m_i_lsup].m_type; }
__forceinline int GetLastProfitSuperatedType() const { return m_profits[m_i_psup].m_type; }
// Main
__forceinline bool MaxProfitIsSuperated();
__forceinline bool MaxLossIsSuperated();
//--- On
void SetNewChossenBalanceForDynamicsAndSet(double _chosen_balance, bool update);
void SetValues(); // Setear valores
void SetValuesAndDynamic(); // Valores y dinamicos
void CheckDynamic();
//---
void Summary() const;
//---
__forceinline int Size() const { return m_losses_profits_s; }
CLossProfit* At(const int idx) const { return m_losses_profits[idx]; }
CLossProfit* Att(const int type) const { return m_type_to_ptr[type]; }
//---
static void FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out);
};
//+------------------------------------------------------------------+
//| Contructor y Destructor |
//+------------------------------------------------------------------+
CLossProfitManager::CLossProfitManager()
: m_losses_profits_s(0), m_profits_s(0), m_losses_s(0), m_i_psup(-1), m_i_lsup(-1)
{
for(int i = 0; i < LOSSPROFIT_MANAGER_MAX_ITEMS; i++)
{
m_type_to_ptr[i] = NULL;
m_losses_profits[i] = NULL;
}
}
//+------------------------------------------------------------------+
CLossProfitManager::~CLossProfitManager(void)
{
#ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog(FUNCION_ACTUAL, INFO_TEXT, "Eliminado en CLossProfitManager");
#endif
Clear(); // Eliminamos todos los punteros
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfitManager::Summary(void) const
{
Print("------------");
Print("Number of losses to revise: ", m_losses_s);
Print("Number of profits to revise: ", m_profits_s);
Print("Total losses and profits: ", Size());
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
__forceinline bool CLossProfitManager::MaxProfitIsSuperated(void)
{
for(m_i_psup = 0; m_i_psup < m_profits_s; m_i_psup++)
if(m_profits[m_i_psup].IsSuperated())
return true;
return false;
}
//+------------------------------------------------------------------+
__forceinline bool CLossProfitManager::MaxLossIsSuperated(void)
{
for(m_i_lsup = 0; m_i_lsup < m_losses_s; m_i_lsup++)
if(m_losses[m_i_lsup].IsSuperated())
return true;
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfitManager::SetValues()
{
for(int i = 0; i < m_losses_profits_s; i++)
{
m_losses_profits[i].Set(); // Setea el valor
m_losses_profits[i].SetSavedValue();
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfitManager::CheckDynamic(void)
{
for(int i = 0; i < m_losses_profits_s; i++)
{
if(m_losses_profits[i].IsDynamicMode()) // Check si es dinamico
m_losses_profits[i].CheckAndModifyThePercentage(); // Modificamos
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfitManager::SetValuesAndDynamic()
{
for(int i = 0; i < m_losses_profits_s; i++)
{
m_losses_profits[i].Set(); // Setea el valor
if(m_losses_profits[i].IsDynamicMode()) // Check si es dinamico
m_losses_profits[i].CheckAndModifyThePercentage(); // Modificamos
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfitManager::SetNewChossenBalanceForDynamicsAndSet(double _chosen_balance, bool update)
{
if(update)
{
for(int i = 0; i < m_losses_profits_s; i++)
{
m_losses_profits[i].Set();
if(m_losses_profits[i].IsDynamicMode()) // Check si es dinamico
{
m_losses_profits[i].SetDynamic(_chosen_balance); // Modificamos
m_losses_profits[i].CheckAndModifyThePercentage();
}
}
}
else
{
for(int i = 0; i < m_losses_profits_s; i++)
{
m_losses_profits[i].Set();
if(m_losses_profits[i].IsDynamicMode()) // Check si es dinamico
{
m_losses_profits[i].SetDynamic(_chosen_balance); // Modificamos
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfitManager::Clear(void)
{
//---
for(int i = 0; i < m_losses_profits_s; i++)
{
m_type_to_ptr[m_losses_profits[i].m_type] = NULL; // Limpiamos el type
if(CheckPointer(m_losses_profits[i]) == POINTER_DYNAMIC)
delete m_losses_profits[i];
m_losses_profits[i] = NULL; // limpaimos el original
}
//---
m_losses_profits_s = 0;
m_profits_s = 0;
m_losses_s = 0;
}
//+------------------------------------------------------------------+
//| Function that is executed every time a new item is added |
//+------------------------------------------------------------------+
// 1. No chekea duplicados (doble ejeucion si lo haces mal)
// 2. Tampoco valida si ya se agrego muchos elemtnos (+32=error fuera de rango)
// 3. El tipo debde de estar en rango ojo con eso. si no da error
//---
bool CLossProfitManager::Add(CLossProfit * new_loss_profit)
{
//---
if(!CheckPointer(new_loss_profit))
return CErrorTrace::FailInit<CLossProfitManager>(TSNMQLART_RM_LPMAN_R_INV_POINTER, 0);
//---
const int type = new_loss_profit.m_type;
if(m_type_to_ptr[type] != NULL) // Chekeamos si se agrego
return CErrorTrace::FailInit<CLossProfitManager>(TSNMQLART_RM_LPMAN_R_ALREDY_IS_ADD, type);
//--- Add (tipo)
m_type_to_ptr[type] = new_loss_profit;
//--- Add (normal)
m_losses_profits[m_losses_profits_s++] = new_loss_profit;
const ENUM_LOSS_PROFIT typelp = new_loss_profit.m_type_loss_or_profit;
if(typelp == T_LOSS)
m_losses[m_losses_s++] = new_loss_profit;
else
if(typelp == T_PROFIT)
m_profits[m_profits_s++] = new_loss_profit;
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
static void CLossProfitManager::FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out)
{
TSNSTD_ERRMAN_PUSHINI(out, trace_idx)
switch(trace[trace_idx].code)
{
case TSNMQLART_RM_LPMAN_R_OK:
TSNSTD_ERRMAN_PUSHOUT(out, "Not error", trace_idx)
break;
case TSNMQLART_RM_LPMAN_R_INV_POINTER:
TSNSTD_ERRMAN_PUSHOUT(out, "Invalid pointer", trace_idx)
break;
case TSNMQLART_RM_LPMAN_R_ALREDY_IS_ADD:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("CLossProfit* with type [%d] Alredy is add in CLossPrfotiManager", int(trace[trace_idx].info)), trace_idx)
break;
default:
TSNSTD_ERRMAN_PUSHOUT(out, "Unknow error", trace_idx)
break;
}
}
//---
/*
ADVERTENCIA:
- Al momento de añadir items a esta clase, esta si son dinamicos las eliminara cuidado.
- El unico que maneja esta clase es CRiskManagement, por lo que no se puede remover elementos
WARNINGS:
- When adding items to this class, if they are dynamic, be careful.
- The only one that handles this class is CRiskManagement, so elements cannot be removed.
*/
}
#endif // MQLARTICLES_ACCOUNT_RM_LOSSPROFIT_MANAGERS_MQH
//+------------------------------------------------------------------+