2026-10-27 18:07:28 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| 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:
|
2026-10-27 19:06:42 -05:00 | | | // 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..
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //---
|
| | | #ifndef LOSSPROFIT_MANAGER_MAX_ITEMS
|
| | | #define LOSSPROFIT_MANAGER_MAX_ITEMS (32)
|
| | | #endif // LOSSPROFIT_MANAGER_MAX_ITEMS
|
| | |
|
| | | //---
|
2026-10-27 19:06:42 -05:00 | | | //#define TSN_MQLART_RM_LPMAN_INV_IDX (LOSSPROFIT_MANAGER_MAX_ITEMS-1)
|
| | | //#define TSN_MQLART_RM_LPMAN_FINALS (TSN_MQLART_RM_LPMAN_INV_IDX)
|
2026-10-27 18:07:28 -05:00 | | | #define TSN_MQLART_RM_LPMAN_TL_MAXS (LOSSPROFIT_MANAGER_MAX_ITEMS>>1)
|
| | |
|
| | | //---
|
| | | enum ENUM_TSNMQLART_RM_LPMAN_R
|
| | | {
|
| | | //---
|
2026-10-28 13:32:56 -05:00 | | | TSNMQLART_RM_LPMAN_R_OK = 0,
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //--- 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;
|
| | |
|
2026-10-27 19:06:42 -05:00 | | | //--- [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];
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //--- 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
|
2026-11-05 12:56:42 -05:00 | | | bool Add(CLossProfit* new_loss_profit); // Add CLossProfit
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //--- 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;
|
| | |
|
| | | //---
|
2026-10-27 19:06:42 -05:00 | | | __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]; }
|
2026-10-29 12:18:24 -05:00 | | |
|
| | | //---
|
2026-11-06 10:40:44 -05:00 | | | static void FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out);
|
2026-10-27 18:07:28 -05:00 | | | };
|
| | |
|
| | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| 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++)
|
2026-10-27 19:06:42 -05:00 | | | {
|
| | | m_type_to_ptr[i] = NULL;
|
2026-10-27 18:07:28 -05:00 | | | m_losses_profits[i] = NULL;
|
2026-10-27 19:06:42 -05:00 | | | }
|
2026-10-27 18:07:28 -05:00 | | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | 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);
|
2026-10-27 19:06:42 -05:00 | | | Print("Total losses and profits: ", Size());
|
2026-10-27 18:07:28 -05:00 | | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | __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();
|
| | | }
|
| | | }
|
| | |
|
2026-11-07 21:17:58 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | 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
|
| | | }
|
| | | }
|
| | |
|
2026-10-27 18:07:28 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | 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
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
2026-11-07 21:17:58 -05:00 | | |
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | void CLossProfitManager::Clear(void)
|
| | | {
|
| | | //---
|
2026-10-27 19:06:42 -05:00 | | | 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
|
| | | }
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //---
|
| | | m_losses_profits_s = 0;
|
| | | m_profits_s = 0;
|
| | | m_losses_s = 0;
|
| | | }
|
| | |
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| Function that is executed every time a new item is added |
|
| | | //+------------------------------------------------------------------+
|
2026-10-27 19:06:42 -05:00 | | | // 1. No chekea duplicados (doble ejeucion si lo haces mal)
|
2026-10-27 18:07:28 -05:00 | | | // 2. Tampoco valida si ya se agrego muchos elemtnos (+32=error fuera de rango)
|
2026-10-27 19:06:42 -05:00 | | | // 3. El tipo debde de estar en rango ojo con eso. si no da error
|
| | |
|
| | | //---
|
2026-11-05 12:56:42 -05:00 | | | bool CLossProfitManager::Add(CLossProfit * new_loss_profit)
|
2026-10-27 18:07:28 -05:00 | | | {
|
| | | //---
|
| | | if(!CheckPointer(new_loss_profit))
|
2026-11-06 10:40:44 -05:00 | | | return CErrorTrace::FailInit<CLossProfitManager>(TSNMQLART_RM_LPMAN_R_INV_POINTER, 0);
|
2026-10-27 18:07:28 -05:00 | | |
|
| | | //---
|
| | | const int type = new_loss_profit.m_type;
|
2026-10-29 12:18:24 -05:00 | | | if(m_type_to_ptr[type] != NULL) // Chekeamos si se agrego
|
2026-11-06 10:40:44 -05:00 | | | return CErrorTrace::FailInit<CLossProfitManager>(TSNMQLART_RM_LPMAN_R_ALREDY_IS_ADD, type);
|
2026-10-27 18:07:28 -05:00 | | |
|
2026-10-29 12:18:24 -05:00 | | | //--- Add (tipo)
|
2026-10-27 19:06:42 -05:00 | | | m_type_to_ptr[type] = new_loss_profit;
|
2026-10-27 18:07:28 -05:00 | | |
|
2026-10-29 12:18:24 -05:00 | | | //--- Add (normal)
|
2026-10-27 18:07:28 -05:00 | | | m_losses_profits[m_losses_profits_s++] = new_loss_profit;
|
2026-10-28 13:32:56 -05:00 | | | const ENUM_LOSS_PROFIT typelp = new_loss_profit.m_type_loss_or_profit;
|
2026-10-27 18:07:28 -05:00 | | | 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;
|
| | |
|
| | |
|
| | | //---
|
2026-11-05 12:56:42 -05:00 | | | return true;
|
2026-10-27 18:07:28 -05:00 | | | }
|
| | |
|
2026-10-29 12:18:24 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-11-06 10:40:44 -05:00 | | | static void CLossProfitManager::FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out)
|
2026-10-29 12:18:24 -05:00 | | | {
|
2026-11-05 12:56:42 -05:00 | | | TSNSTD_ERRMAN_PUSHINI(out, trace_idx)
|
| | | switch(trace[trace_idx].code)
|
2026-10-29 12:18:24 -05:00 | | | {
|
| | | case TSNMQLART_RM_LPMAN_R_OK:
|
2026-11-05 12:56:42 -05:00 | | | TSNSTD_ERRMAN_PUSHOUT(out, "Not error", trace_idx)
|
2026-10-29 12:18:24 -05:00 | | | break;
|
| | | case TSNMQLART_RM_LPMAN_R_INV_POINTER:
|
2026-11-05 12:56:42 -05:00 | | | TSNSTD_ERRMAN_PUSHOUT(out, "Invalid pointer", trace_idx)
|
2026-10-29 12:18:24 -05:00 | | | break;
|
| | | case TSNMQLART_RM_LPMAN_R_ALREDY_IS_ADD:
|
2026-11-05 12:56:42 -05:00 | | | TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("CLossProfit* with type [%d] Alredy is add in CLossPrfotiManager", int(trace[trace_idx].info)), trace_idx)
|
2026-10-29 12:18:24 -05:00 | | | break;
|
| | | default:
|
2026-11-05 12:56:42 -05:00 | | | TSNSTD_ERRMAN_PUSHOUT(out, "Unknow error", trace_idx)
|
2026-10-29 12:18:24 -05:00 | | | break;
|
| | | }
|
| | | }
|
| | |
|
2026-10-27 18:07:28 -05:00 | | | //---
|
| | | /*
|
| | | 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
|
| | | //+------------------------------------------------------------------+
|