2026-10-27 18:07:28 -05:00
//+------------------------------------------------------------------+
//| Base.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_BASES_BASE_MQH
# define MQLARTICLES_ACCOUNT_RM_LOSSPROFIT_BASES_BASE_MQH
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
//| Include |
//+------------------------------------------------------------------+
# include "Def.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
2026-10-28 10:48:27 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Clase base qeu represtna una maxima perdida\ganancia.. aparte de tambien representar a GLMPO (dinero ariessgoad por op)
// Esta clase base no implemta el caculol solo da una clase base..
2026-10-27 18:07:28 -05:00
//+------------------------------------------------------------------+
//| CLossProfit Class |
//+------------------------------------------------------------------+
class CLossProfit : public CAccountGestor
{
public :
const bool m_active_hook_on_superated ;
const int m_type ; // Type of maximum loss or profit.
2026-10-28 10:48:27 -05:00
const ENUM_LOSS_PROFIT m_type_loss_or_profit ; // Tipo (Loss\Profit\GMLPO)
2026-10-27 18:07:28 -05:00
protected :
2026-10-27 22:04:41 -05:00
//--- Principales
double m_value ;
double m_calculation_value ;
//---
2026-11-07 21:17:58 -05:00
uint8_t m_dync_type ; // Function for dynamic risk
2026-10-28 10:48:27 -05:00
double m_initial_calc_value ; // Initial calc value
2026-10-27 18:07:28 -05:00
double m_saved_value ; // Valor base
2026-10-28 10:48:27 -05:00
FuncionLossProfitSuperate m_f_check_sup ; // Funcion custom que chekea si se ha superado
FTSNMQLArtRmPctgVal m_f_calc_val ; // Calcular valor
2026-10-27 18:07:28 -05:00
const ulong m_magic_number ; // Numero magico para filtrar
//---
2026-10-28 10:48:27 -05:00
void * m_ptr_f ; // Ptr al superar
2026-10-27 18:07:28 -05:00
//--- Dynamic risk
Dynamic_LossProfit m_dynamic_lp ; // Structure containing the % to exceed and risks to apply
double m_chosen_balance ; // Chosen initial balance
int m_index_cambio ; // Change index between positives and negatives
int m_size_dynamic ; // Size of changes
bool m_min_val_is ; // Minimum value exceeded
bool m_max_val_is ; // Maximum value exceeded
int m_pos_derecha ; // Current position on right
int m_pos_izquierda ; // Current position on left
double m_new_balance_to_overcome ; // Next balance to overcome
// Cache for dynamic risk parameters
string m_cache_percentages_to_activate ;
string m_cache_risks_to_be_applied ;
//---
2026-11-05 12:56:42 -05:00
bool ProccesDynamic ( double & balance_to_activate_the_risk [ ] , double & risk_to_be_adjusted [ ] ) ;
2026-10-27 18:07:28 -05:00
//---
virtual void OnSuperated ( ) { }
//---
static CHashMapFastP ( double , double , _fibbo ) s_balance_risk_map ; // HashMap for dynamic risk (percentages to exceed and new risk percentage)
2026-11-07 21:17:58 -05:00
//---
void CheckPositives ( ) ;
void CheckNegatives ( ) ;
void CheckNegativesAndPositives ( ) ;
2026-10-27 18:07:28 -05:00
public :
//--- Constructor
2026-10-28 10:48:27 -05:00
CLossProfit ( const int type , const bool act_hook , const ulong magic , const ENUM_LOSS_PROFIT tt ) ;
2026-10-27 18:07:28 -05:00
~ CLossProfit ( ) { }
2026-10-28 10:48:27 -05:00
//--- Inits (setteo base)
void Init ( double _calc_value , FuncionLossProfitSuperate fsup , FTSNMQLArtRmPctgVal fval ) ;
2026-11-05 12:56:42 -05:00
bool SetDynamic ( const string & percentages_to_activate , const string & risks_to_be_applied , double _chosen_balance ) ;
bool SetDynamic ( double _chosen_balance ) ;
2026-10-28 10:48:27 -05:00
void SetPtrF ( void * ptr ) { m_ptr_f = ptr ; }
2026-10-27 18:07:28 -05:00
//--- Dynamic risk
// Verifies and checks if the risk can be modified (only works in dynamic risk)
__forceinline void CheckAndModifyThePercentage ( ) ;
// Returns true if the risk is dynamic
2026-11-07 21:17:58 -05:00
inline bool IsDynamicMode ( ) const { return m_dync_type < TSNMQLART_ACCST_RMLP_DYNCTYPE_INV ; }
2026-10-27 18:07:28 -05:00
//--- General getters
// Returns true if the "maximum loss or profit" has been exceeded
bool IsSuperated ( ) ;
inline string Name ( ) const { return TSN_MQLART_RM_LP_NAME ( m_type ) ; } // Nombre
//--- Functions to work with the (Percentage | Value | Profit) of the "maximum loss or profit"
// Set value with calc value
2026-11-02 22:03:44 -05:00
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
__forceinline void Set ( )
{
m_value = m_f_calc_val ( m_calculation_value , m_ptr_f ) ;
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Valor calculado para = %s, es = %.2f " , Name ( ) , m_value ) ) ;
}
# else
2026-10-28 10:48:27 -05:00
__forceinline void Set ( ) { m_value = m_f_calc_val ( m_calculation_value , m_ptr_f ) ; }
2026-11-02 22:03:44 -05:00
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
// Initial calc value (percentage or money)
inline double GetInitialCalcValue ( ) const { return m_initial_calc_value ; }
2026-10-27 22:04:41 -05:00
inline void SetInitialCalcValue ( ) { m_calculation_value = m_initial_calc_value ; } // Setter
2026-10-27 18:07:28 -05:00
// Current value
2026-10-28 10:48:27 -05:00
__forceinline double GetValue ( ) const { return m_value ; }
void SetValue ( double val ) { m_value = val ; }
2026-10-27 18:07:28 -05:00
// Calc value if mode is:
// percentage: represet the percentage
// money: fixed money
2026-10-27 22:04:41 -05:00
inline double GetCalcValue ( ) const { return m_calculation_value ; }
void SetCalcValue ( double val ) { m_calculation_value = val ; }
2026-10-27 18:07:28 -05:00
// Saved value
inline double GetSavedValue ( ) const { return m_saved_value ; }
2026-10-27 22:04:41 -05:00
void SetSavedValue ( ) { m_saved_value = m_value ; }
2026-10-27 18:07:28 -05:00
// Magic number
__forceinline ulong MagicNumber ( ) const { return m_magic_number ; }
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
} ;
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
2026-10-28 10:48:27 -05:00
CLossProfit : : CLossProfit ( const int type , const bool act_hook , const ulong magic , const ENUM_LOSS_PROFIT tt )
2026-10-27 18:07:28 -05:00
:
//--- Basico
2026-10-28 10:48:27 -05:00
m_type ( type ) , m_type_loss_or_profit ( tt ) , m_magic_number ( magic ) ,
//---
m_initial_calc_value ( 0.00 ) , m_saved_value ( 0.00 ) , m_ptr_f ( NULL ) ,
2026-10-27 18:07:28 -05:00
//--- Hook
m_active_hook_on_superated ( act_hook ) ,
//--- Riesgo dinamico
2026-11-07 21:17:58 -05:00
m_min_val_is ( false ) , m_max_val_is ( false ) , m_pos_derecha ( 0 ) , m_pos_izquierda ( 0 ) , m_dync_type ( TSNMQLART_ACCST_RMLP_DYNCTYPE_INV ) ,
m_index_cambio ( 0 ) , m_chosen_balance ( 0.00 ) ,
2026-10-27 18:07:28 -05:00
m_new_balance_to_overcome ( 0.00 ) , m_cache_percentages_to_activate ( NULL ) , m_cache_risks_to_be_applied ( NULL ) ,
2026-10-28 10:48:27 -05:00
//--- Extra ptr funcs
m_f_check_sup ( NULL ) , m_f_calc_val ( NULL )
2026-10-27 18:07:28 -05:00
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CLossProfit : : IsSuperated ( void )
{
2026-10-28 10:48:27 -05:00
if ( m_f_check_sup ( m_value , m_saved_value , m_ptr_f ) )
2026-10-27 18:07:28 -05:00
{
if ( m_active_hook_on_superated )
OnSuperated ( ) ;
return true ;
}
return false ;
}
2026-10-28 10:48:27 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CLossProfit ::Init ( double _calc_value , FuncionLossProfitSuperate fsup , FTSNMQLArtRmPctgVal fval )
{
//---
m_initial_calc_value = _calc_value ;
m_calculation_value = _calc_value ;
m_value = 0.00 ;
//---
m_f_calc_val = fval ;
m_f_check_sup = fsup ;
}
2026-10-27 18:07:28 -05:00
//+------------------------------------------------------------------+
//| Establecer el riesgo dinamico |
//+------------------------------------------------------------------+
2026-11-05 12:56:42 -05:00
bool CLossProfit : : SetDynamic ( double _chosen_balance )
2026-10-27 18:07:28 -05:00
{
if ( m_cache_percentages_to_activate = = NULL | | m_cache_risks_to_be_applied = = NULL )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_INVALID_CACHE , 0 ) ; // fallo de cache
2026-11-05 12:56:42 -05:00
// devolvemos el de abajo
2026-10-27 19:06:42 -05:00
return SetDynamic ( m_cache_percentages_to_activate , m_cache_risks_to_be_applied , _chosen_balance ) ;
2026-10-27 18:07:28 -05:00
}
//+------------------------------------------------------------------+
2026-11-05 12:56:42 -05:00
bool CLossProfit : : SetDynamic ( const string & percentages_to_activate , const string & risks_to_be_applied , double _chosen_balance )
2026-10-27 18:07:28 -05:00
{
//---
2026-11-07 21:17:58 -05:00
m_dync_type = TSNMQLART_ACCST_RMLP_DYNCTYPE_INV ;
2026-10-27 18:07:28 -05:00
//---
2026-11-02 21:51:46 -05:00
m_size_dynamic = TSN : : CStringFunc : : ToArr < double , TSN : : CStringConvert > ( percentages_to_activate , m_dynamic_lp . balance_to_activate_the_risk , ' , ' ) ;
if ( m_size_dynamic < 1 )
2026-11-07 09:32:25 -05:00
return m_size_dynamic = = 0 ?
CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_INVALID_SIZE , 0 ) :
CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_MALFORMED_VALUES , ( ulong ) TSNSTD_STRFUNC_TOARR_REQPOS ( m_size_dynamic ) | 0ULL < < 32 ) ;
// resize
2026-11-02 21:51:46 -05:00
ArrayResize ( m_dynamic_lp . balance_to_activate_the_risk , m_size_dynamic ) ;
2026-10-27 18:07:28 -05:00
//---
2026-11-02 21:51:46 -05:00
m_size_dynamic = TSN : : CStringFunc : : ToArr < double , TSN : : CStringConvert > ( risks_to_be_applied , m_dynamic_lp . risk_to_be_adjusted , ' , ' ) ;
if ( m_size_dynamic < 1 )
2026-11-07 09:32:25 -05:00
return m_size_dynamic = = 0 ?
CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_INVALID_SIZE , 0 ) :
CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_MALFORMED_VALUES , ( ulong ) TSNSTD_STRFUNC_TOARR_REQPOS ( m_size_dynamic ) | 1ULL < < 32 ) ;
// resize
2026-11-02 21:51:46 -05:00
ArrayResize ( m_dynamic_lp . risk_to_be_adjusted , m_size_dynamic ) ;
2026-10-27 18:07:28 -05:00
//---
if ( m_dynamic_lp . balance_to_activate_the_risk .Size ( ) ! = m_dynamic_lp . risk_to_be_adjusted .Size ( ) )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_SIZE_NOT_EQ_ARRS ,
2026-11-07 09:32:25 -05:00
ulong ( m_dynamic_lp . balance_to_activate_the_risk .Size ( ) ) | ulong ( m_dynamic_lp . risk_to_be_adjusted .Size ( ) ) < < 32
) ;
2026-10-27 18:07:28 -05:00
//--- Cache values
m_cache_percentages_to_activate = percentages_to_activate ;
m_cache_risks_to_be_applied = risks_to_be_applied ;
//---
2026-10-29 08:11:18 -05:00
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , " Arrays before revision " ) ;
CRmFunctions : : PrintArrayAsTable ( m_dynamic_lp . balance_to_activate_the_risk ,
" Negative percentages to modify the risk " , " balance " ) ;
CRmFunctions : : PrintArrayAsTable ( m_dynamic_lp . risk_to_be_adjusted ,
" Risk to be adjusted " , " new risk " ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
//---
m_chosen_balance = _chosen_balance ;
//---
2026-11-02 21:51:46 -05:00
return ProccesDynamic ( m_dynamic_lp . balance_to_activate_the_risk , m_dynamic_lp . risk_to_be_adjusted ) ;
2026-10-27 18:07:28 -05:00
}
//+------------------------------------------------------------------+
2026-11-05 12:56:42 -05:00
bool CLossProfit : : ProccesDynamic ( double & balance_to_activate_the_risk [ ] , double & risk_to_be_adjusted [ ] )
2026-10-27 18:07:28 -05:00
{
//---
s_balance_risk_map . Clear ( ) ;
//---
# define TSN_MQLART_RM_LP_PDYN_F_POS ( 1 )
# define TSN_MQLART_RM_LP_PDYN_F_NEG ( 2 )
# define TSN_MQLART_RM_LP_PDYN_F_ALL ( TSN_MQLART_RM_LP_PDYN_F_POS | TSN_MQLART_RM_LP_PDYN_F_NEG )
//--- Agrupamos de apares.. y un clean previo antes del sort
uint f = 0 ;
for ( int i = 0 ; i < m_size_dynamic ; i + + )
{
//---
const double v = balance_to_activate_the_risk [ i ] ;
2026-11-02 21:51:46 -05:00
if ( v = = 0.0 | | v > 100.0 | | v < -100.0 )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_INVALID_VALUE_DYNC_PCTG , CBitsReinterprete : : ToBasic < ulong , double > ( v ) ) ;
2026-10-27 18:07:28 -05:00
//---
if ( s_balance_risk_map . ReserveSlot ( v ) )
{
const uint p = s_balance_risk_map . m_last_pos_tbl ;
s_balance_risk_map . m_table [ p ] . key = v ;
s_balance_risk_map . m_table [ p ] . value = risk_to_be_adjusted [ i ] ;
}
else
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CLossProfit > ( TSNMQLART_RM_LP_RES_LPDYNC_ERR_DYN_PCTG_ADD_HASHMAP , 0 ) ;
2026-10-27 18:07:28 -05:00
//---
f | = TSN_MQLART_RM_LP_PDYN_F_POS + ( v < 0.0 ) ;
}
//--- Sort
ArraySort ( balance_to_activate_the_risk ) ;
//---
for ( int i = 0 ; i < m_size_dynamic ; i + + )
{
//---
2026-11-05 12:56:42 -05:00
const double prev_value_bal = balance_to_activate_the_risk [ i ] ; // balance
double value ; // riesgo a cambiar
2026-10-27 18:07:28 -05:00
s_balance_risk_map . TryGet ( prev_value_bal , value ) ;
//---
2026-11-05 12:56:42 -05:00
// Caluclomaso ya el porcentaje "final" dado qeu el previo era [0-100]
risk_to_be_adjusted [ i ] = TSNMQLART_RISKM_NORM_PERCERNTAGE ( value ) ;
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
// buscmaos aquel indice en el cual ya este pos acutal sea pos pero el previo neg (este indice seira como el cambio)
if ( i < ( m_size_dynamic - 1 ) & & f = = TSN_MQLART_RM_LP_PDYN_F_ALL & & ( balance_to_activate_the_risk [ i ] < 0.0 & & balance_to_activate_the_risk [ i + 1 ] > 0.0 ) )
m_index_cambio = i ;
//---
// Tambien calculoasmo el balance a "superar"
balance_to_activate_the_risk [ i ] = ( m_chosen_balance + ( m_chosen_balance * ( prev_value_bal / 100.0 ) ) ) ;
2026-10-27 18:07:28 -05:00
}
//---
m_min_val_is = false ;
m_max_val_is = false ;
//---
if ( f = = TSN_MQLART_RM_LP_PDYN_F_NEG ) // Solo negativos
{
2026-11-07 21:17:58 -05:00
m_pos_izquierda = m_size_dynamic - 1 ; // desde arriba a abajo
2026-10-27 18:07:28 -05:00
m_new_balance_to_overcome = MAX_VAL_DEFINES_CLOSS_PROFIT ;
2026-11-07 21:17:58 -05:00
m_dync_type = TSNMQLART_ACCST_RMLP_DYNCTYPE_NEG ;
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " [Elegido = Negativo] izq = %d " , m_pos_izquierda ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
else
if ( f = = TSN_MQLART_RM_LP_PDYN_F_POS ) // Solo positivos
{
2026-11-07 21:17:58 -05:00
m_pos_derecha = 0 ; // empeiza en el primer indice
2026-10-27 18:07:28 -05:00
m_new_balance_to_overcome = MIN_VAL_DEFINES_CLOSE_PROFIT ;
2026-11-07 21:17:58 -05:00
m_dync_type = TSNMQLART_ACCST_RMLP_DYNCTYPE_POS ;
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , " [Elegido = Positivo] pos drch = 0 " ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
else // combinacion
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_pos_derecha = m_index_cambio + 1 ; // Sumamos 1 para que sea pos
m_pos_izquierda = m_index_cambio ; // Tal cual apunta abajo
m_dync_type = TSNMQLART_ACCST_RMLP_DYNCTYPE_NEG_POS ;
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " [Elegido = Pos y Neg] pos drch = %d, pos izqr " , m_pos_derecha , m_pos_izquierda ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
//---
2026-10-29 08:11:18 -05:00
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , " Arrays ready " ) ;
CRmFunctions : : PrintArrayAsTable ( balance_to_activate_the_risk ,
2026-11-07 21:17:58 -05:00
" Percentages to modify the risk " , " balance " ) ;
//---
double temp [ ] ;
ArrayResize ( temp , m_size_dynamic ) ;
for ( int i = 0 ; i < m_size_dynamic ; i + + )
temp [ i ] = risk_to_be_adjusted [ i ] * 100.0 ;
CRmFunctions : : PrintArrayAsTable ( temp ,
2026-10-29 08:11:18 -05:00
" Risk to be adjusted " , " new risk " ) ;
2026-11-07 21:17:58 -05:00
2026-10-29 08:11:18 -05:00
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-11-02 21:51:46 -05:00
//---
2026-11-05 12:56:42 -05:00
return true ;
2026-10-27 18:07:28 -05:00
}
//+------------------------------------------------------------------+
//| Checks and modifies the percentage |
//+------------------------------------------------------------------+
2026-11-07 21:17:58 -05:00
void CLossProfit : : CheckAndModifyThePercentage ( void )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
//--- despacho
switch ( m_dync_type )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
case TSNMQLART_ACCST_RMLP_DYNCTYPE_NEG :
{
CheckNegatives ( ) ;
break ;
}
case TSNMQLART_ACCST_RMLP_DYNCTYPE_POS :
{
CheckPositives ( ) ;
break ;
}
case TSNMQLART_ACCST_RMLP_DYNCTYPE_NEG_POS :
{
CheckNegativesAndPositives ( ) ;
break ;
}
default :
{
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , FATAL_ERROR_TEXT , StringFormat ( " Risk %s is not allowed to use dynamic risk " , TSN_MQLART_RM_LP_NAME ( m_type ) ) ) ;
Remover ( ) ;
2026-10-27 19:06:42 -05:00
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-11-07 21:17:58 -05:00
break ;
}
}
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
2026-10-27 18:07:28 -05:00
//+------------------------------------------------------------------+
//| Funciones estaticas para chekear y modficar el porcentaje |
//+------------------------------------------------------------------+
2026-11-07 21:17:58 -05:00
// Aqui la idea es ir subiendo hasta que no se pueda mas luego cuando caemos tambien..
// 1. en caso el min value aun no se supere (vamos bien) entonces veriamso si almenos e account eq de ahora
// es mayor a la posicon izquieda en caso sea cierto hace falta saber por cuanto? aárte tenemoque que indenficair "donde cae"
// haste la cuenta de que tenemos 3 nivlees para modicar eisgo en un blanace de 10k y por lo tnato 3 riesogs (+default) que usar
// 10k 10.5k 11k 12k
// <- inicial ] [ 0 ] [ 1 ] [ 2 ->
// entonces en los niveles o cajas que tineen a "infinito" por debajo del balnce base o por encima usaran ya un resigo "fijo"
// con esto en mente continuemso, last seira 3-1 = 2 esto dado qeu en cada "paso" buscarmeos rnago asi que para no salirnos de rango eso
// por ejemplo el primer paso buscamos si equity cae en la "caja #0" osea si es mayor 10.5k pero menor o iqual 11k
// En caso digmoas caiga en ese rango salimos con cayo_en_rango=True (se consiguio) si se consiguo entonces
// seteamos el calcvalue con el riesgo configuradon con indice 0 (1-1) nota que restmoas uno dad que ahora mismo pozizq pro el post inc
// aumento
// ahora el balance a superar "por abajo" lo volvemos a configurar esta vez en el "piso" osea 10.5k en este caso ...
// y en caso cayo_en_rango=false solo se da en las siguintes condicion
// asumeindo qeu size_dynacmis siempre es valido cuando se llama osea miimo como un valor
// entonces:
// - size_dynamic=1: con ese valor last=0 por lo qeu no iteraria y cayo_en_rango=false (usaria el maximo)
// - size_dynamic>1 && se superaron todos los valores posibles por lo tanto m_pos_derecha=size-1
void CLossProfit : : CheckPositives ( )
2026-10-27 18:07:28 -05:00
{
//---
2026-11-01 21:32:38 -05:00
const double account_equity = AccountInfoDouble ( ACCOUNT_EQUITY ) ;
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
if ( ! m_max_val_is & & account_equity > m_dynamic_lp . balance_to_activate_the_risk [ m_pos_derecha ] ) //toca aumentar riesgo o parar
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Se ha superado el balance %.2f (idx=%d) " ,
m_dynamic_lp . balance_to_activate_the_risk [ m_pos_derecha ] , m_pos_derecha ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
//---
bool cayo_en_rango = false ;
const int last = m_size_dynamic - 1 ;
2026-10-27 18:07:28 -05:00
2026-11-07 21:17:58 -05:00
//---
while ( m_pos_derecha < last )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
if ( m_dynamic_lp . balance_to_activate_the_risk [ + + m_pos_derecha ] > = account_equity )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
cayo_en_rango = true ;
2026-10-27 18:07:28 -05:00
break ;
}
}
2026-11-07 21:17:58 -05:00
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " El indice correspondiente es (idx=%d) " ,
m_pos_derecha ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
//---
if ( cayo_en_rango )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_new_balance_to_overcome = m_dynamic_lp . balance_to_activate_the_risk [ m_pos_derecha - 1 ] ;
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_derecha - 1 ] ;
2026-10-27 18:07:28 -05:00
}
else
2026-11-07 21:17:58 -05:00
{
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_derecha ] ; //llegamos al maximo asignamos el maximo posible
m_new_balance_to_overcome = m_dynamic_lp . balance_to_activate_the_risk [ m_pos_derecha ] ;
m_max_val_is = true ; //llegamos al maximo
}
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Nuevo calc value = %.2f | Nuevo 'piso' = %.2f " ,
m_calculation_value , m_new_balance_to_overcome ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
//---
return ;
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
//--- Abajo
if ( account_equity < m_new_balance_to_overcome ) //toca disminir riesgo o parar
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
bool cayo_en_rango = false ;
while ( m_pos_derecha > 0 )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
if ( account_equity > = m_dynamic_lp . balance_to_activate_the_risk [ - - m_pos_derecha ] )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
cayo_en_rango = true ;
2026-10-27 18:07:28 -05:00
break ;
}
}
2026-11-07 21:17:58 -05:00
//---
if ( cayo_en_rango )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_derecha ] ;
m_new_balance_to_overcome = m_dynamic_lp . balance_to_activate_the_risk [ m_pos_derecha ] ;
m_pos_derecha + + ; // buscamos nuevo "techo"
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
else // cayo por debajo del nivel 0, m_pos_derecha es 0 aqui por lo tanto se sigue buscando se nivel
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_new_balance_to_overcome = MIN_VAL_DEFINES_CLOSE_PROFIT ;
m_calculation_value = m_initial_calc_value ; //n os pasamos del miinimo entonces aisgnamos el porcentake inicial
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
m_max_val_is = false ; //Reseteamos, ahora si es pobible aumentar el riesgo
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Nuevo calc value = %.2f | Nuevo 'piso' = %.2f | poz drc = %d " ,
m_calculation_value , m_new_balance_to_overcome , m_pos_derecha ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
}
//+------------------------------------------------------------------+
2026-11-07 21:17:58 -05:00
void CLossProfit : : CheckNegatives ( )
2026-10-27 18:07:28 -05:00
{
//---
2026-11-01 21:32:38 -05:00
const double account_equity = AccountInfoDouble ( ACCOUNT_EQUITY ) ;
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
if ( ! m_min_val_is & & account_equity < m_dynamic_lp . balance_to_activate_the_risk [ m_pos_izquierda ] ) //toca aumentar riesgo o parar
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Se ha superado el balance %.2f (idx=%d) " ,
m_dynamic_lp . balance_to_activate_the_risk [ m_pos_izquierda ] , m_pos_izquierda ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
2026-11-07 21:17:58 -05:00
//---
bool cayo_en_rango = false ;
while ( m_pos_izquierda > 0 )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
if ( account_equity > = m_dynamic_lp . balance_to_activate_the_risk [ - - m_pos_izquierda ] )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
cayo_en_rango = true ;
2026-10-27 18:07:28 -05:00
break ;
}
}
2026-11-07 21:17:58 -05:00
//---
int i = m_pos_izquierda ;
if ( cayo_en_rango )
i + + ; // el cuerpo apunta abajo (el real) pero este i que sera el que usamos apunta a previous
2026-10-27 18:07:28 -05:00
else
2026-11-07 21:17:58 -05:00
m_min_val_is = true ;
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ i ] ;
m_new_balance_to_overcome = m_dynamic_lp . balance_to_activate_the_risk [ i ] ; //arriba
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Nuevo calc value = %.2f | idx = %d " ,
m_calculation_value , m_pos_izquierda ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
//---
return ;
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
//--- Recuperandose
if ( account_equity > m_new_balance_to_overcome ) //toca disminir riesgo o parar
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
const int last = m_size_dynamic - 1 ;
bool cayo_en_rango = false ;
2026-10-27 18:07:28 -05:00
2026-11-07 21:17:58 -05:00
while ( m_pos_izquierda < last )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
if ( m_dynamic_lp . balance_to_activate_the_risk [ + + m_pos_izquierda ] > = account_equity )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
cayo_en_rango = true ;
2026-10-27 18:07:28 -05:00
break ;
}
}
2026-11-07 21:17:58 -05:00
//---
if ( cayo_en_rango )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_izquierda ] ; //usa el riesgo de aqui
m_new_balance_to_overcome = m_dynamic_lp . balance_to_activate_the_risk [ m_pos_izquierda - - ] ;
// pos izq qeuda apuntabadno abajr
2026-10-27 18:07:28 -05:00
}
else //nos pasmaos entonces, riesgo inicial
{
2026-11-07 21:17:58 -05:00
m_calculation_value = m_initial_calc_value ; // En este caso si o si es porcentaje
m_new_balance_to_overcome = MAX_VAL_DEFINES_CLOSS_PROFIT ;
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
//---
m_min_val_is = false ; //llegamos al maximo
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Nuevo calc value = %.2f | Nuevo 'techo' = %.2f | idx= %d " ,
m_calculation_value , m_new_balance_to_overcome , m_pos_izquierda ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
}
//+------------------------------------------------------------------+
2026-11-07 21:17:58 -05:00
// 0 1 2
// 9k 9.5k 10k 13k
// [ ] [ ] [ ]
//
void CLossProfit : : CheckNegativesAndPositives ( )
2026-10-27 18:07:28 -05:00
{
//---
2026-11-01 21:32:38 -05:00
const double account_equity = AccountInfoDouble ( ACCOUNT_EQUITY ) ;
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
if ( ! m_min_val_is & & account_equity < m_dynamic_lp . balance_to_activate_the_risk [ m_pos_izquierda ] )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
if ( m_max_val_is )
m_pos_derecha + + ; //size + 1, y luego en while ya se ve la direfencia de 1
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
bool cayo_en_rango = false ;
2026-10-27 18:07:28 -05:00
//---
2026-11-07 21:17:58 -05:00
while ( m_pos_izquierda > 0 )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_pos_derecha - - ; // reducimos tambien pero este va "uno adelantado" en caso sea "pico"
if ( account_equity > = m_dynamic_lp . balance_to_activate_the_risk [ - - m_pos_izquierda ] )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
cayo_en_rango = true ;
2026-10-27 18:07:28 -05:00
break ;
}
}
//---
2026-11-07 21:17:58 -05:00
if ( cayo_en_rango ) // Solo si estabamos en almeons un proceso de bajada
{
// en caso venimos de haber subido fuera de indexcambio > y pos izq es igual a idx cambio entonces usremos el "normal calc value"
if ( m_pos_izquierda = = m_index_cambio )
m_calculation_value = m_initial_calc_value ;
else // de lo contrario
// ahora en caso estemos por debajo usamos +1 dado que izq se qeudo apuntao abajo...
// si estmaos en linea "positiva" usamos tal cual
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_izquierda + ( m_pos_izquierda < m_index_cambio ) ] ;
}
2026-10-27 18:07:28 -05:00
else
2026-11-07 21:17:58 -05:00
{
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_izquierda ] ;
m_min_val_is = true ; //llegamos al minimo
m_pos_derecha = m_pos_izquierda ; // ahora esperamos superar esto para cambiar..
}
//--- reset
m_max_val_is = false ;
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Nuevo calc value = %.2f | pos izq = %d | pos drc = %d " ,
m_calculation_value , m_pos_izquierda , m_pos_derecha ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
2026-11-07 21:17:58 -05:00
//--- En caso se supere
// 0 1 2 3
// 8k 9k 10k 11k 12k
//.. ] [ ] [ ] [ ] [ ...
if ( ! m_max_val_is & & account_equity > m_dynamic_lp . balance_to_activate_the_risk [ m_pos_derecha ] ) //toca disminir riesgo o parar
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
if ( m_min_val_is )
m_pos_izquierda - - ; //-1
2026-10-27 18:07:28 -05:00
2026-11-07 21:17:58 -05:00
//---
const int last = m_size_dynamic - 1 ;
bool cayo_en_rango = false ;
2026-10-27 18:07:28 -05:00
2026-11-07 21:17:58 -05:00
//---
while ( m_pos_derecha < last )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
m_pos_izquierda + + ;
if ( m_dynamic_lp . balance_to_activate_the_risk [ + + m_pos_derecha ] > = account_equity )
2026-10-27 18:07:28 -05:00
{
2026-11-07 21:17:58 -05:00
cayo_en_rango = true ;
2026-10-27 18:07:28 -05:00
break ;
}
}
2026-11-07 21:17:58 -05:00
//---
if ( cayo_en_rango )
{
if ( m_pos_izquierda = = m_index_cambio ) // si cae aqui entonces usamso el normal
m_calculation_value = m_initial_calc_value ;
else // no cae ahi entonces el riesgo de los arrays
// 1. en caso pos dereca sea mayor a index cambio entonces esta en su "fase" - 1
// 2. de lo conrtario tal cual esta bien dado qeu apunta al siguiente
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_derecha - ( m_pos_derecha > m_index_cambio ) ] ;
}
else //nos pasmaos entonces, riesgo inicial
{
m_calculation_value = m_dynamic_lp . risk_to_be_adjusted [ m_pos_derecha ] ; //maximo
m_max_val_is = true ; //llegamos al maximo
m_pos_izquierda = m_pos_derecha ; // mismo
}
//--- reset
m_min_val_is = false ;
//---
# ifdef MORE_INFO_LOSS_PROFIT_DEFINE
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " Nuevo calc value = %.2f | pos izq = %d | pos drc = %d " ,
m_calculation_value , m_pos_izquierda , m_pos_derecha ) ) ;
# endif // MORE_INFO_LOSS_PROFIT_DEFINE
2026-10-27 18:07:28 -05:00
}
//---
2026-11-07 21:17:58 -05:00
//PrintFormat("%d - %d - cambio en: %d",m_pos_izquierda,m_pos_derecha, m_index_cambio);
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 CLossProfit : : 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_LP_RES_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_LP_RES_LPDYNC_ERR_INVALID_SIZE :
2026-11-05 12:56:42 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Set dynamic error, invalid size [%s] (must be > 0) " , ( trace [ trace_idx ] . info ? " to_be_apllied " : " percentage_to_active " ) ) , trace_idx )
2026-10-29 12:18:24 -05:00
break ;
2026-11-07 09:32:25 -05:00
case TSNMQLART_RM_LP_RES_LPDYNC_ERR_MALFORMED_VALUES :
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Malformed double find in parse str-comma-separated value [%s], expected position int arr = [%d] " ,
( ( trace [ trace_idx ] . info > > 32 ) = = 0 ? " percentage to active " : " risk to be applied " ) , int ( trace [ trace_idx ] . info ) ) , trace_idx )
break ;
2026-10-29 12:18:24 -05:00
case TSNMQLART_RM_LP_RES_LPDYNC_ERR_SIZE_NOT_EQ_ARRS :
2026-11-05 12:56:42 -05:00
{
const ulong v = trace [ trace_idx ] . info ;
const int bal_s = int ( v ) ;
const int risk_ajusted_s = int ( v > > 32 ) ;
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Dynamic LP Error, arrays must be equal [balance active size = %d | risk ajusted size = %d] " , bal_s , risk_ajusted_s ) , trace_idx )
2026-10-29 12:18:24 -05:00
break ;
2026-11-05 12:56:42 -05:00
}
2026-10-29 12:18:24 -05:00
case TSNMQLART_RM_LP_RES_LPDYNC_ERR_INVALID_CACHE :
2026-11-05 12:56:42 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , " Invalid cache, first call SetDynamic with full parameters " , trace_idx )
2026-10-29 12:18:24 -05:00
break ;
2026-11-02 21:51:46 -05:00
case TSNMQLART_RM_LP_RES_LPDYNC_ERR_INVALID_VALUE_DYNC_PCTG :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Invalid dinamyc str-comma-separetd, An invalid value [%.4f] was found for the balance percentage to be modified, must be in [-100.0, 100.0] and != 0.0. "
2026-11-05 12:56:42 -05:00
, CBitsReinterprete : : ToBasic < double , ulong > ( trace [ trace_idx ] . info ) ) , trace_idx )
2026-11-02 21:51:46 -05:00
break ;
case TSNMQLART_RM_LP_RES_LPDYNC_ERR_DYN_PCTG_ADD_HASHMAP :
2026-11-05 12:56:42 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , " Fatal error, could not add hashmap pctg balance " , trace_idx )
2026-11-02 21:51:46 -05:00
break ;
2026-10-29 12:18:24 -05:00
default :
2026-11-05 12:56:42 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , " Unkwnow error " , trace_idx )
2026-10-29 12:18:24 -05:00
break ;
}
}
2026-10-27 18:07:28 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CHashMapFastP ( double , double , _fibbo ) CLossProfit : : s_balance_risk_map ( 64 ) ;
//---
}
//+------------------------------------------------------------------+
# endif // MQLARTICLES_ACCOUNT_RM_LOSSPROFIT_BASES_BASE_MQH