2026-10-29 11:36:56 -05:00
//+------------------------------------------------------------------+
//| RR.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
# property copyright " Copyright 2026, Niquel Mendoza. "
# property link " https://www.mql5.com "
# property strict
# ifndef MQLARTICLES_POSMGMT_BE_IMP_RR_MQH
# define MQLARTICLES_POSMGMT_BE_IMP_RR_MQH
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
//| |
//+------------------------------------------------------------------+
# include "..\\Base\\BEBase.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| CBreakEvenRR Class |
//+------------------------------------------------------------------+
enum ENUM_TYPE_EXTRA_BE_BY_RRR
{
EXTRA_BE_RRR_BY_ATR , //By Atr
EXTRA_BE_RRR_BY_FIXED_POINTS //By Fixed Points
} ;
//---
# define TSNMQLART_POSMGMT_BE_RR_NPARAMS ( 4 )
//---
# define TSNMQLART_POSMGMT_BE_RR_I_TYPEEXTRA ( 0 )
# define TSNMQLART_POSMGMT_BE_RR_I_COEF_RR ( 1 )
# define TSNMQLART_POSMGMT_BE_RR_I_ATRPTR ( 2 )
# define TSNMQLART_POSMGMT_BE_RR_I_ATRMUL_OR_POINTS ( 3 )
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CBreakEvenRR : public CBreakEvenBase
{
private :
CAtrOptimized * m_atr_ultra ;
double m_coefficient_rr ; //Coefficient of rr
ENUM_TYPE_EXTRA_BE_BY_RRR m_type ;
double m_extra_value_be ; //Extra value that will be added to the opening price of the position to obtain the breakeven price
//Note: if the m_type is atr this will contain the atr multiplier, if not it will contain the value already multiplied by the point value
public :
CBreakEvenRR ( string symbol_ , ulong magic_ ) ;
bool Add ( ulong post_ticket , double open_price , double sl_price , ENUM_POSITION_TYPE
position_type , datetime open_time , const string & sym ) override final ;
//---
bool Set ( const BreakEvenParams & params [ ] ) override final ;
} ;
//+------------------------------------------------------------------+
//| Contructor |
//+------------------------------------------------------------------+
void CBreakEvenRR : : CBreakEvenRR ( string symbol_ , ulong magic_ )
: CBreakEvenBase ( symbol_ , magic_ , TSNMQLART_POSMGMT_BE_RR_NPARAMS ) ,
//---
m_coefficient_rr ( 1.0 ) ,
m_extra_value_be ( 100.0 ) ,
m_type ( EXTRA_BE_RRR_BY_FIXED_POINTS ) ,
m_atr_ultra ( NULL )
{
}
//+------------------------------------------------------------------+
//| Function to set break even values by rr without MqlParams |
//+------------------------------------------------------------------+
bool CBreakEvenRR : : Set ( const BreakEvenParams & params [ ] )
{
//---
m_type = ( ENUM_TYPE_EXTRA_BE_BY_RRR ) params [ TSNMQLART_POSMGMT_BE_RR_I_TYPEEXTRA ] . integer_value ;
if ( m_type ! = EXTRA_BE_RRR_BY_ATR & & m_type ! = EXTRA_BE_RRR_BY_FIXED_POINTS )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_RR_INVALID_TYPE_EXTRA , m_type ) ;
2026-10-29 11:36:56 -05:00
//---
m_coefficient_rr = params [ TSNMQLART_POSMGMT_BE_RR_I_COEF_RR ] . double_value ; ;
m_atr_ultra = params [ TSNMQLART_POSMGMT_BE_RR_I_ATRPTR ] . atr_pointer_value ;
m_extra_value_be = params [ TSNMQLART_POSMGMT_BE_RR_I_ATRMUL_OR_POINTS ] . double_value ;
//---
if ( m_type = = EXTRA_BE_RRR_BY_ATR )
{
if ( CheckPointer ( m_atr_ultra ) = = POINTER_INVALID )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_RR_INVALID_ATPPTR_RR , 0 ) ;
2026-10-29 11:36:56 -05:00
}
else
m_extra_value_be * = m_point_value ;
//---
if ( m_coefficient_rr < = 0.00 )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_RR_INVALID_RR , CBitsReinterprete : : ToBasic < ulong , double > ( m_coefficient_rr ) ) ;
2026-10-29 11:36:56 -05:00
if ( m_extra_value_be < = 0.00 )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_RR_INVALID_ATRMUL_OR_POINTS , CBitsReinterprete : : ToBasic < ulong , double > ( m_extra_value_be ) ) ;
2026-10-29 11:36:56 -05:00
return true ;
}
//+------------------------------------------------------------------+
//| Function to add an element to the positions array |
//+------------------------------------------------------------------+
bool CBreakEvenRR : : Add ( ulong post_ticket , double open_price , double sl_price , ENUM_POSITION_TYPE position_type , datetime open_time , const string & sym )
{
//---
if ( sym ! = m_symbol )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_INV_SYMBOL , post_ticket ) ;
2026-10-29 11:36:56 -05:00
//---
if ( sl_price < = DBL_EPSILON )
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_INV_SL_PRICE , CBitsReinterprete : : ToBasic < ulong , double > ( sl_price ) ) ;
2026-10-29 11:36:56 -05:00
//---
double val = m_extra_value_be ;
if ( m_type = = EXTRA_BE_RRR_BY_ATR )
val * = m_atr_ultra . GetAtrValue ( open_time ) ;
//---
const double diff = fabs ( open_price - sl_price ) ;
if ( ( diff * m_coefficient_rr ) < = val )
{
# ifdef TSNMQLART_POSMGMT_BE_ELOGS
FastLog ( FUNCION_ACTUAL , ERROR_TEXT , StringFormat ( " The distance from the opening price %f where the stoploss is located is greater than or equal to the price to trigger the breakeven " ,
m_extra_value_be ) ) ;
# endif // TSNMQLART_POSMGMT_BE_ELOGS
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CBasicBEFormater > ( TSNMQLART_BE_TYPES_ERR_SL_INSUFICCENT_DIST , CBitsReinterprete : : ToBasic < ulong , double > ( diff ) ) ;
2026-10-29 11:36:56 -05:00
}
//---
2026-11-01 17:28:51 -05:00
MQLARTICLES_BREAKEVEN_PROLOGO ( position_type , post_ticket )
//---
2026-10-29 11:36:56 -05:00
m_pos_be [ m_pos_be_s ] . breakeven_price = position_type = = POSITION_TYPE_BUY ? open_price + val : open_price - val ;
m_pos_be [ m_pos_be_s ] . price_to_beat = position_type = = POSITION_TYPE_BUY ? open_price + ( m_coefficient_rr * diff ) : open_price - ( m_coefficient_rr * diff ) ;
2026-11-01 17:28:51 -05:00
2026-10-29 11:36:56 -05:00
//---
# ifdef TSNMQLART_POSMGMT_BE_ELOGS
2026-10-29 12:18:18 -05:00
if ( m_type = = EXTRA_BE_RRR_BY_ATR )
2026-10-29 11:36:56 -05:00
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " El precio del nuevo stop loss sera %f, valor actual del atr[%d] = %f " ,
m_pos_be [ m_pos_be_s ] . breakeven_price , m_atr_ultra . Index ( ) , val ) ) ;
else
FastLog ( FUNCION_ACTUAL , INFO_TEXT , StringFormat ( " El precio del nuevo stop loss sera %f " ,
m_pos_be [ m_pos_be_s ] . breakeven_price ) ) ;
# endif // TSNMQLART_POSMGMT_BE_ELOGS
//---
m_pos_be_s + + ;
2026-10-29 17:44:49 -05:00
//---
2026-10-29 11:36:56 -05:00
return true ;
}
//---
}
# endif // MQLARTICLES_POSMGMT_BE_IMP_RR_MQH