2026-09-12 19:41:52 -05:00
//+------------------------------------------------------------------+
//| Base.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
# property copyright " Copyright 2025, Niquel Mendoza. "
# property link " https://www.mql5.com/es/users/nique_372/news "
# property strict
# ifndef MQLARTICLES_POSMGMT_CONDPARTIALS_BASE_BASE_MQH
# define MQLARTICLES_POSMGMT_CONDPARTIALS_BASE_BASE_MQH
/ / + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
//| Include |
//+------------------------------------------------------------------+
# include "Defines.mqh"
2026-09-13 08:47:52 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-11-01 18:06:28 -05:00
namespace TSN
2026-09-13 08:47:52 -05:00
{
2026-09-12 19:41:52 -05:00
//+------------------------------------------------------------------+
//| Base class for conditional partials implementation |
//+------------------------------------------------------------------+
class CConditionalPartials : public CAccountGestor
{
protected :
//--- General
IConditionPartial * m_partial_condition ; // Pointer to IConditionPartial, this pointer will give the condition to close buy and sell positions partially
bool m_delete_partial_condition_in_dstctor ;
CTrade m_trade ; // CTrade instance, to apply partial closes
2026-11-02 15:21:28 -05:00
int m_tries ; // Number of tryes
2026-09-12 19:41:52 -05:00
//--- Tracked Positions
// Array of ConditionalPartialTrackedPosition type, this array will store the positions
// To which partial closes will be applied
ConditionalPartialTrackedPosition m_tracked_positions_arr [ ] ;
int m_tracked_positions_size ; // Number of positions being tracked
int m_tracked_positions_r ;
2026-10-30 13:45:16 -05:00
bool m_tracked_positions_remove [ ] ; // Parallel to m_tracked_positions_arr (same size): true = remove, always false between calls
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
// Volume
double m_volumes_to_close_arr [ ] ; // Array of percentages, that will close the trades in m_tracked_positions_arr
int m_volume_to_close_size ; // Number of partial closes planned for each trade
// Centinela:
// m_constant_partials=0 entonces valdra m_volume_to_close_size
// m_constant_partials=MQLART_CONDPARTIAL_CONSTANT entonces valdra 0
// La idea con esto es en caso sea constante no hace falta revisar si "se supero el numero de indices" por lo qeu idx=1 == 0 (siempre falso)
// De lo contraio si hace falta.. por lo que se revisa..
// Con constant partials solo se usa el (indice 0 y se "repite" para siempre... como volumebn vs usar un array que en cada parcial se cierra X..)
// Nota que por defecto No es constante.. aparte si es contaste: next_index_to_close ya no se usa o peirde el significaod ()
int m_centinela ;
2026-09-12 19:41:52 -05:00
//--- Symbol
string m_symbol ; // Symbol from which data like min_volume, step, etc. will be obtained
double m_symbol_bid ; // Symbol bid
double m_symbol_ask ; // Symbol ask
double m_symbol_min_volume ; // Minimum volume (opening or closing) for symbol m_symbol
double m_symbol_volume_step ; // Volume step for symbol m_symbol
int8_t m_symbol_digits ; // Digits of symbol "m_symbol"
2026-11-02 15:21:28 -05:00
2026-09-12 19:41:52 -05:00
//--- Parameters
// Boolean that will indicate to the class if when trades are opened (with the magic number, or magic number = 0), they will be added
// Automatically to the trackable positions array
bool m_auto_add_to_tracked_positions ;
2026-11-01 20:54:30 -05:00
TsnUniquePtr ( CDiff ) m_unique_m_min_distance_to_close_pos ;
2026-09-12 19:41:52 -05:00
CDiff * m_min_distance_to_close_pos ; // Pointer to CDiff, this will give the minimum distance between partial closes
ulong m_magic_number ; // Magic number
2026-11-02 15:21:28 -05:00
bool m_forze_to_close ; // Forze close total position
2026-09-12 19:41:52 -05:00
double m_min_distance_value ; // Minimum distance between partials (value)
2026-11-02 15:21:28 -05:00
char m_constant_partials ; // Partials is constant (only zero idx) ?
2026-09-12 19:41:52 -05:00
//--- Symbol Functions
void RefreshSymbolData ( ) ;
//--- Partials closures Functions
2026-11-02 15:21:28 -05:00
template < typename TPositionType >
void ClosePartial ( ) ;
2026-10-30 13:45:16 -05:00
2026-09-12 19:41:52 -05:00
public :
CConditionalPartials ( ) ;
~ CConditionalPartials ( ) ;
//--- Initialization function
bool Init ( ConditionalPartialConfig & config ) ;
//--- Add function
virtual void AddPositionToTrack ( ENUM_POSITION_TYPE position_type , ulong position_ticket , double open_price ) ;
bool AddPositionToTrack ( ulong position_ticket ) ;
//--- Remove
bool RemoveTrackedPosition ( ulong position_ticket ) ;
//--- Events functions
void OnOpenPosition ( const ROnOpenPosition & pos ) override final ;
void OnClosePosition ( const ROnClosePosition & pos , const int global_pos_index ) override final ;
//--- Execute function
void Execute ( const datetime & current_time ) ; // OnNewBar
2026-11-01 18:13:06 -05:00
//--- Config
2026-09-12 19:41:52 -05:00
// Condition
2026-11-01 18:13:06 -05:00
IConditionPartial * PartialCloseCondition ( ) { return m_partial_condition ; }
2026-09-12 19:41:52 -05:00
void PartialCloseCondition ( IConditionPartial * new_condition , bool delete_prev_ptr ) ;
// Magic number
2026-11-01 18:13:06 -05:00
ulong MagicNumber ( ) const { return m_magic_number ; }
2026-09-12 19:41:52 -05:00
void MagicNumber ( ulong new_value ) ;
// Minimum distance between partial closures
CDiff * MinDistBetweenPartialClosures ( ) { return m_min_distance_to_close_pos ; }
2026-11-02 10:55:13 -05:00
void MinDistBetweenPartialClosures ( CDiff * new_value ) ;
2026-09-12 19:41:52 -05:00
// Symbol
string Symbol ( ) { return m_symbol ; }
void Symbol ( string new_symbol ) ;
// Auto mode
2026-11-01 18:13:06 -05:00
bool IsAutoMode ( ) const { return m_auto_add_to_tracked_positions ; }
void AutoMode ( bool new_value ) { m_auto_add_to_tracked_positions = new_value ; }
// Tryes
int Tries ( ) const { return m_tries ; }
void Tries ( int v ) { m_tries = v ; }
2026-11-02 15:21:28 -05:00
// Force to close
bool ForzeToClose ( ) const { return m_forze_to_close ; }
void ForzeToClose ( bool new_val ) { m_forze_to_close = new_val ; }
// Constant
bool Constant ( ) const { return m_constant_partials = = MQLART_CONDPARTIAL_CONSTANT ; }
void Constant ( bool new_val ) ;
2026-09-12 19:41:52 -05:00
//--- Tracked positions size
2026-11-02 15:21:28 -05:00
int TrackedPositionsSize ( ) const { return m_tracked_positions_size ; }
//--- Errores
2026-11-06 10:40:44 -05:00
static void FormatTrace ( const TSNErrorFrame & trace [ ] , int & trace_idx , string & out ) ;
2026-09-12 19:41:52 -05:00
} ;
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CConditionalPartials : : CConditionalPartials ( )
:
2026-11-02 15:21:28 -05:00
//---
2026-09-12 19:41:52 -05:00
m_partial_condition ( NULL ) , m_symbol ( NULL ) , m_symbol_ask ( 0.00 ) ,
m_symbol_bid ( 0.00 ) , m_symbol_digits ( 0 ) , m_symbol_min_volume ( 0.00 ) , m_symbol_volume_step ( 0.00 ) ,
m_auto_add_to_tracked_positions ( true ) ,
m_min_distance_to_close_pos ( NULL ) , m_magic_number ( NOT_MAGIC_NUMBER ) , m_volume_to_close_size ( 0 ) , m_min_distance_value ( 0.00 ) ,
2026-11-02 15:21:28 -05:00
m_delete_partial_condition_in_dstctor ( true ) , m_forze_to_close ( false ) , m_centinela ( 0 ) ,
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
//---
2026-11-01 18:06:28 -05:00
m_tracked_positions_r ( CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE ) , m_tracked_positions_size ( 0 ) , m_tries ( MQLART_POSMGMT_CONDPARTIALS_TRYDEF )
2026-09-12 19:41:52 -05:00
{
2026-11-02 15:21:28 -05:00
//---
2026-09-12 19:41:52 -05:00
account_status . RegisterEvents ( & this , ACCOUNT_STATUS_REG_FLAG_ON_OPEN_POSITION | ACCOUNT_STATUS_REG_FLAG_ON_CLOSE_POSITION ) ;
//---
: : ArrayResize ( m_tracked_positions_arr , CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE , CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE ) ;
2026-10-30 13:45:16 -05:00
: : ArrayResize ( m_tracked_positions_remove , CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE , CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE ) ;
: : ArrayInitialize ( m_tracked_positions_remove , false ) ;
2026-09-12 19:41:52 -05:00
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CConditionalPartials : : ~ CConditionalPartials ( )
{
//--- Delete of event pool
if ( account_status . IsActive ( ) )
account_status . UnregisterEvents ( & this , ACCOUNT_STATUS_REG_FLAG_ON_OPEN_POSITION | ACCOUNT_STATUS_REG_FLAG_ON_CLOSE_POSITION ) ;
//--- Delete condition
if ( m_delete_partial_condition_in_dstctor & & CheckPointer ( m_partial_condition ) = = POINTER_DYNAMIC )
delete m_partial_condition ;
}
//+------------------------------------------------------------------+
//| Initialization function, the function sets the class parameters |
//| through the "config" variable. |
//| Inputs: |
//| - ConditionalPartialConfig& config: variable that will store the |
//| initial class configuration. |
//| Outputs: |
//| - bool: The function returns true on success, otherwise false. |
//+------------------------------------------------------------------+
bool CConditionalPartials ::Init ( ConditionalPartialConfig & config )
{
//--- Assignment
m_magic_number = config . magic_number ;
m_symbol = config . symbol ;
m_auto_add_to_tracked_positions = config . auto_mode ;
2026-11-02 15:21:28 -05:00
if ( m_magic_number ! = NOT_MAGIC_NUMBER )
m_trade . SetExpertMagicNumber ( m_magic_number ) ;
2026-09-12 19:41:52 -05:00
2026-11-01 20:54:30 -05:00
// Min distance
m_unique_m_min_distance_to_close_pos . Reset ( config . min_distance_to_close_pos ) ;
m_min_distance_to_close_pos = m_unique_m_min_distance_to_close_pos . m_value ;
2026-09-12 19:41:52 -05:00
// Partial condition
m_partial_condition = config . condition ;
m_delete_partial_condition_in_dstctor = config . delete_condition ;
2026-11-02 15:21:28 -05:00
// Extra
m_forze_to_close = config . forze_to_close ;
2026-09-12 19:41:52 -05:00
//--- Symbol refresh
RefreshSymbolData ( ) ;
2026-11-02 15:21:28 -05:00
//---
m_volume_to_close_size = CStringFunc : : ToArr < double , CStringConvert > (
config . str_percentage_volume_to_close , m_volumes_to_close_arr , ' , ' ) ;
if ( m_volume_to_close_size < 1 )
2026-09-12 19:41:52 -05:00
{
2026-11-07 09:34:18 -05:00
if ( m_volume_to_close_size = = 0 ) // string vacio
return CErrorTrace : : FailInit < CConditionalPartials > ( TSNQMLART_CONDPARTIAL_ERR_INVALID_NUM_VOLTOCLOSE_ARR , 0 ) ;
// < 0: valor mal formado, ToArr devuelve la posicion
return CErrorTrace : : FailInit < CConditionalPartials > ( TSNQMLART_CONDPARTIAL_ERR_MALFORMED_VOLTOCLOSE , ulong ( TSNSTD_STRFUNC_TOARR_REQPOS ( m_volume_to_close_size ) ) ) ;
2026-09-12 19:41:52 -05:00
}
2026-11-02 15:21:28 -05:00
//---
2026-09-12 19:41:52 -05:00
for ( int i = 0 ; i < m_volume_to_close_size ; i + + )
{
const double val = m_volumes_to_close_arr [ i ] ;
2026-11-02 15:21:28 -05:00
if ( val > 100.0 | | val < 0.00001 )
2026-09-12 19:41:52 -05:00
{
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CConditionalPartials > ( TSNQMLART_CONDPARTIAL_ERR_INVALID_VOLTOCLOSE_MUSTBE_0_100 , CBitsReinterprete : : ToBasic < ulong , double > ( val ) ) ;
2026-09-12 19:41:52 -05:00
}
m_volumes_to_close_arr [ i ] / = 100.00 ;
}
2026-11-02 15:21:28 -05:00
//---
Constant ( config . constant ) ;
2026-09-12 19:41:52 -05:00
//--- Print array
2026-11-02 15:21:28 -05:00
# ifdef CONDITIONAL_PARTIAL_MORE_INFO
ArrayPrint ( m_volumes_to_close_arr , 2 , " | " , 0 , m_volume_to_close_size ) ;
# endif // CONDITIONAL_PARTIAL_MORE_INFO
2026-09-12 19:41:52 -05:00
//--- Finish
return true ;
}
//+-------------------------------------------------------------------------------------------+
//| Function that executes the main logic of the class |
//| Inputs: |
//| datetime current_time: Variable of datetime type, which must |
//| store the current time. |
//| Outputs: the function returns nothing. |
//| Notes: |
//| - The function can be called on each tick or each bar, this |
//| will depend on the Execute function of the m_partial_condition variable, |
//| for example if the condition is an indicator, like RSI |
//| the logic could be executed on each candle, or for example if the condition |
//| evaluates a mitigation of an orderblock, in this case it would be executed on each tick |
//| |
//| - The define "CONDITIONAL_PARTIAL_EXECUTE_WHEN_THE_ARE_POSITIONS", |
//| allows conditional code compilation, if this define is defined |
//| the Execute(..) function of the m_partial_condition pointer, will only be called |
//| when there are open positions, otherwise if the define is not defined |
//| it will be called at all times. |
//+-------------------------------------------------------------------------------------------+
//#define CONDITIONAL_PARTIAL_EXECUTE_WHEN_THE_ARE_POSITIONS
//+------------------------------------------------------------------+
void CConditionalPartials : : Execute ( const datetime & current_time )
{
# ifdef CONDITIONAL_PARTIAL_EXECUTE_WHEN_THE_ARE_POSITIONS
if ( m_tracked_positions_size > 0 )
{
//--- Execution
m_partial_condition . Execute ( current_time ) ;
//--- Close conditions
const bool buy = m_partial_condition . CloseBuy ( ) ;
const bool sell = m_partial_condition . CloseSell ( ) ;
//--- Obtain min diff
if ( buy | | sell )
{
m_min_distance_to_close_pos . OnNewBar ( ) ;
m_min_distance_value = m_min_distance_to_close_pos . Diff ( ) ;
//--- Check buy condition
if ( buy )
{
m_symbol_bid = : : SymbolInfoDouble ( m_symbol , SYMBOL_BID ) ;
2026-11-02 15:21:28 -05:00
ClosePartial < MQLART_CONDPARTIAL_PBUY > ( ) ;
2026-09-12 19:41:52 -05:00
}
//--- Check sell condition
if ( sell )
{
m_symbol_ask = : : SymbolInfoDouble ( m_symbol , SYMBOL_ASK ) ;
2026-11-02 15:21:28 -05:00
ClosePartial < MQLART_CONDPARTIAL_PSELL > ( ) ;
2026-09-12 19:41:52 -05:00
}
}
}
# else
//--- Execution
m_partial_condition . Execute ( current_time ) ;
//--- Check conditions
if ( m_tracked_positions_size > 0 )
{
//--- Close conditions
const bool buy = m_partial_condition . CloseBuy ( ) ;
const bool sell = m_partial_condition . CloseSell ( ) ;
//--- Obtain min diff
if ( buy | | sell )
{
m_min_distance_to_close_pos . OnNewBar ( ) ;
m_min_distance_value = m_min_distance_to_close_pos . Diff ( ) ;
//--- Check buy condition
if ( buy )
{
m_symbol_bid = : : SymbolInfoDouble ( m_symbol , SYMBOL_BID ) ;
2026-11-02 15:21:28 -05:00
ClosePartial < MQLART_CONDPARTIAL_PBUY > ( ) ;
2026-09-12 19:41:52 -05:00
}
//--- Check sell condition
if ( sell )
{
m_symbol_ask = : : SymbolInfoDouble ( m_symbol , SYMBOL_ASK ) ;
2026-11-02 15:21:28 -05:00
ClosePartial < MQLART_CONDPARTIAL_PSELL > ( ) ;
2026-09-12 19:41:52 -05:00
}
}
}
# endif
}
//+------------------------------------------------------------------+
//| Function that executes every time a trade is closed or opened |
//+------------------------------------------------------------------+
void CConditionalPartials : : OnOpenPosition ( const ROnOpenPosition & pos )
{
2026-10-24 22:43:15 -05:00
if ( ( pos . position . magic = = m_magic_number | | m_magic_number = = NOT_MAGIC_NUMBER ) & & account_status . last_trans_symbol = = m_symbol )
2026-09-12 19:41:52 -05:00
{
2026-11-02 15:21:28 -05:00
# ifdef CONDITIONAL_PARTIAL_MORE_INFO
FastLog ( FUNCION_ACTUAL , INFO_TEXT , : : StringFormat ( " A trade has just been opened with ticket %I64u " , pos . position . ticket ) , FUNCION_ACTUAL ) ;
# endif // CONDITIONAL_PARTIAL_MORE_INFO
2026-09-12 19:41:52 -05:00
AddPositionToTrack ( pos . position . type , pos . position . ticket , pos . position . open_price ) ;
}
}
//+------------------------------------------------------------------+
void CConditionalPartials : : OnClosePosition ( const ROnClosePosition & pos , const int global_pos_index )
{
// Once the position is closed we verify if that ticket is in the internal array, if so the function
// will remove it.
RemoveTrackedPosition ( pos . position . ticket ) ;
}
//+-------------------------------------------------------------------------+
//| The function creates a new trackable position and adds it |
//| to the internal array. |
//| Inputs: |
//| - ENUM_POSITION_TYPE position_type: position type (buy or sell) |
//| - ulong position_ticket: Position ticket. |
//| - double open_price: Trade opening price. |
//| Outputs: The function returns no value. |
//| Notes: |
//| - The position ticket must be valid, |
//| the function will not check if the ticket exists. |
//+-------------------------------------------------------------------------+
void CConditionalPartials : : AddPositionToTrack ( ENUM_POSITION_TYPE position_type , ulong position_ticket , double open_price )
{
//---
if ( m_tracked_positions_size = = 0 ) // First position to be added to the tracked positions array
m_partial_condition . OnInitPartials ( open_price ) ;
//--- Obtain current value
m_min_distance_to_close_pos . OnNewBar ( ) ;
m_min_distance_value = m_min_distance_to_close_pos . Diff ( ) ;
//---
if ( m_tracked_positions_size > = m_tracked_positions_r )
{
2026-10-30 13:45:16 -05:00
const int old_r = m_tracked_positions_r ;
2026-09-12 19:41:52 -05:00
m_tracked_positions_r < < = 1 ;
ArrayResize ( m_tracked_positions_arr , m_tracked_positions_r , m_tracked_positions_r ) ;
2026-10-30 13:45:16 -05:00
ArrayResize ( m_tracked_positions_remove , m_tracked_positions_r , m_tracked_positions_r ) ;
for ( int i = old_r ; i < m_tracked_positions_r ; i + + ) // Iniciamos la cola nueva
m_tracked_positions_remove [ i ] = false ;
2026-09-12 19:41:52 -05:00
}
//--- Creation of a new position to track
2026-11-01 18:06:28 -05:00
m_tracked_positions_arr [ m_tracked_positions_size ] . try = m_tries ;
2026-09-12 19:41:52 -05:00
m_tracked_positions_arr [ m_tracked_positions_size ] . type = position_type ;
m_tracked_positions_arr [ m_tracked_positions_size ] . ticket = position_ticket ;
m_tracked_positions_arr [ m_tracked_positions_size ] . next_min_price = position_type = = POSITION_TYPE_BUY ?
open_price + m_min_distance_value : open_price - m_min_distance_value ;
2026-11-01 18:06:28 -05:00
m_tracked_positions_arr [ m_tracked_positions_size ] . next_index_to_close = 0 ;
//---
m_tracked_positions_size + + ;
2026-09-12 19:41:52 -05:00
}
//+-------------------------------------------------------------------------+
//| With a function similar to the AddPositionToTrack(...) function |
//| differing in that this overloaded function only requires the |
//| position ticket (the function internally already queries the |
//| position type, open_price, etc.). |
//| Inputs: |
//| - ulong position_ticket: Position ticket. |
//| Outputs: Returns true if a trackable position could be created with the |
//| ticket, otherwise false |
//| Notes: |
//| - If the position ticket doesn't exist, or is invalid (failure of |
//| PositionSelectByTicket) the function will not create a new trackable |
//| position with the ticket |
//+-------------------------------------------------------------------------+
bool CConditionalPartials : : AddPositionToTrack ( ulong position_ticket )
{
2026-11-02 15:21:28 -05:00
//---
: : ResetLastError ( ) ;
2026-09-12 19:41:52 -05:00
//--- Select
if ( ! : : PositionSelectByTicket ( position_ticket ) )
{
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CConditionalPartials > ( TSNQMLART_CONDPARTIAL_ERR_SELECT_POSITION , position_ticket , : : GetLastError ( ) ) ;
2026-09-12 19:41:52 -05:00
}
//--- Check symbol
2026-11-02 15:21:28 -05:00
if ( : : PositionGetString ( POSITION_SYMBOL ) ! = m_symbol )
2026-09-12 19:41:52 -05:00
{
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CConditionalPartials > ( TSNQMLART_CONDPARTIAL_ERR_INVALID_SYMBOL , position_ticket ) ;
2026-09-12 19:41:52 -05:00
}
//--- Add
AddPositionToTrack ( ( ENUM_POSITION_TYPE ) : : PositionGetInteger ( POSITION_TYPE ) , position_ticket , : : PositionGetDouble ( POSITION_PRICE_OPEN ) ) ;
return true ;
}
//+------------------------------------------------------------------+
//| Function that iterates over all positions to track (buy) |
//| and partially closes the trade |
//+------------------------------------------------------------------+
2026-11-02 15:21:28 -05:00
template < typename TPositionType >
void CConditionalPartials : : ClosePartial ( void )
2026-09-12 19:41:52 -05:00
{
2026-10-30 13:45:16 -05:00
//--- opt: solo un booleano como fastpath para eliminar o no (las marcas viven en m_tracked_positions_remove)
bool already_remove = false ;
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
//---
# define CONDITIONAL_PARTIAL_REMOVEPOS(IDX) m_tracked_positions_remove [ IDX ] = true ; already_remove = true ;
2026-09-12 19:41:52 -05:00
//--- Iteration
for ( int i = 0 ; i < m_tracked_positions_size ; i + + )
{
2026-11-02 15:21:28 -05:00
//---
if ( m_tracked_positions_arr [ i ] . type ! = ( sizeof ( TPositionType ) - 1 ) | |
( ( sizeof ( TPositionType ) = = sizeof ( MQLART_CONDPARTIAL_PBUY ) ) ?
( m_symbol_bid < m_tracked_positions_arr [ i ] . next_min_price ) : ( m_symbol_ask > m_tracked_positions_arr [ i ] . next_min_price ) ) )
2026-09-12 19:41:52 -05:00
continue ;
2026-11-02 15:21:28 -05:00
//---
2026-09-12 19:41:52 -05:00
if ( ! : : PositionSelectByTicket ( m_tracked_positions_arr [ i ] . ticket ) )
{
# ifdef CONDITIONAL_PARTIAL_MORE_INFO
2026-11-01 18:06:28 -05:00
FastLog ( FUNCION_ACTUAL , ERROR_TEXT , : : StringFormat ( " Ticket selection failed %I64u " , m_tracked_positions_arr [ i ] . ticket ) ) ;
# endif // CONDITIONAL_PARTIAL_MORE_INFO
//---
CONDITIONAL_PARTIAL_REMOVEPOS ( i )
2026-09-12 19:41:52 -05:00
continue ;
}
//---
2026-11-02 15:21:28 -05:00
int idx = m_tracked_positions_arr [ i ] . next_index_to_close + + > > m_constant_partials ;
2026-09-12 19:41:52 -05:00
//--- Initial calculation
2026-11-02 15:21:28 -05:00
const double lot_size = : : PositionGetDouble ( POSITION_VOLUME ) ;
double volume_to_close = m_volumes_to_close_arr [ idx + + ] * lot_size ;
2026-09-12 19:41:52 -05:00
//--- Check min volume to close
if ( volume_to_close < m_symbol_min_volume )
{
2026-11-02 15:21:28 -05:00
if ( m_forze_to_close )
volume_to_close = lot_size ;
else
{
2026-11-01 18:06:28 -05:00
# ifdef CONDITIONAL_PARTIAL_MORE_INFO
2026-11-02 15:21:28 -05:00
FastLog ( FUNCION_ACTUAL , WARNING_TEXT , : : StringFormat ( " Partial close of position %I64u skipped, volume to close %.*f is less than minimum volume %.*f " ,
m_tracked_positions_arr [ i ] . ticket ,
m_symbol_digits ,
volume_to_close ,
m_symbol_digits ,
m_symbol_min_volume ) ) ;
2026-11-01 18:06:28 -05:00
# endif // CONDITIONAL_PARTIAL_MORE_INFO
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
//---
2026-11-01 18:06:28 -05:00
CONDITIONAL_PARTIAL_REMOVEPOS ( i )
2026-11-02 15:21:28 -05:00
continue ;
2026-09-12 19:41:52 -05:00
}
}
//--- Round
2026-10-30 13:37:47 -05:00
volume_to_close = MQLART_POSMGMT_CONDPARTIALS_LOTROUND ( volume_to_close , m_symbol_volume_step ) ;
2026-09-12 19:41:52 -05:00
2026-11-01 18:06:28 -05:00
//--- Close
if ( ! m_trade . PositionClosePartial ( m_tracked_positions_arr [ i ] . ticket , volume_to_close ) )
{
//---
2026-09-12 19:41:52 -05:00
# ifdef CONDITIONAL_PARTIAL_MORE_INFO
2026-11-01 18:06:28 -05:00
FastLog ( FUNCION_ACTUAL , INFO_TEXT , : : StringFormat ( " Fallo al cerrar parcialem,nte = %.2f lotes de la posicion = %I64u " ,
volume_to_close , m_tracked_positions_arr [ i ] . ticket ) ) ;
# endif // CONDITIONAL_PARTIAL_MORE_INFO
2026-09-12 19:41:52 -05:00
2026-11-01 18:06:28 -05:00
//---
2026-11-02 15:21:28 -05:00
m_tracked_positions_arr [ i ] . next_index_to_close - - ; // rollback
2026-11-01 18:06:28 -05:00
if ( - - m_tracked_positions_arr [ i ] . try < 0 )
{
CONDITIONAL_PARTIAL_REMOVEPOS ( i )
}
continue ;
}
//---
# ifdef CONDITIONAL_PARTIAL_MORE_INFO
FastLog ( FUNCION_ACTUAL , INFO_TEXT , : : StringFormat ( " Partially closing %.2f lots of position %I64u " , volume_to_close , m_tracked_positions_arr [ i ] . ticket ) ) ;
# endif // CONDITIONAL_PARTIAL_MORE_INFO
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
//---
m_tracked_positions_arr [ i ] . next_min_price + = m_min_distance_value ;
2026-09-12 19:41:52 -05:00
//--- Check limit
2026-11-02 15:21:28 -05:00
if ( idx = = m_centinela ) // Limit reached
2026-09-12 19:41:52 -05:00
{
2026-11-01 18:06:28 -05:00
CONDITIONAL_PARTIAL_REMOVEPOS ( i )
2026-09-12 19:41:52 -05:00
}
}
2026-11-02 15:21:28 -05:00
//--- Check para eliminar
if ( ! already_remove )
return ;
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
//--- Eliminamos
int w = 0 ;
for ( int r = 0 ; r < m_tracked_positions_size ; r + + )
2026-09-12 19:41:52 -05:00
{
2026-11-02 15:21:28 -05:00
if ( ! m_tracked_positions_remove [ r ] )
2026-09-12 19:41:52 -05:00
{
2026-11-02 15:21:28 -05:00
if ( w ! = r )
m_tracked_positions_arr [ w ] = m_tracked_positions_arr [ r ] ;
w + + ;
2026-09-12 19:41:52 -05:00
}
2026-11-02 15:21:28 -05:00
else
m_tracked_positions_remove [ r ] = false ;
2026-09-12 19:41:52 -05:00
}
2026-11-02 15:21:28 -05:00
m_tracked_positions_size = w ; // opt: solo cambiamos el tamaño "virtual"
2026-09-12 19:41:52 -05:00
2026-11-02 15:21:28 -05:00
//---
# undef CONDITIONAL_PARTIAL_REMOVEPOS
2026-09-12 19:41:52 -05:00
}
2026-11-02 15:21:28 -05:00
2026-09-12 19:41:52 -05:00
//+----------------------------------------------------------------------------------+
//| The function sets a new partial close "condition", it changes |
//| the "address" of the m_partial_condition pointer to new_condition, and |
//| if necessary the previous pointer is deleted |
//| Inputs: |
//| - IConditionPartial *new_condition: Pointer to IConditionPartial. |
//| - bool delete_prev_ptr: Boolean indicating if the internal pointer is deleted |
//| Outputs: The function returns nothing. |
//+----------------------------------------------------------------------------------+
void CConditionalPartials : : PartialCloseCondition ( IConditionPartial * new_condition , bool delete_prev_ptr )
{
//--- Delete prev ptr
if ( delete_prev_ptr & & : : CheckPointer ( m_partial_condition ) = = POINTER_DYNAMIC )
{
delete m_partial_condition ;
}
//---
m_partial_condition = new_condition ;
}
//+------------------------------------------------------------------+
//| Sets a new internal pointer for the minimum distance |
//| between partials. |
//| Inputs: |
//| - CDiff *new_value: Pointer to CDiff. |
//| Outputs: The function returns no value |
//+------------------------------------------------------------------+
2026-11-01 20:54:30 -05:00
void CConditionalPartials : : MinDistBetweenPartialClosures ( CDiff * new_value )
2026-09-12 19:41:52 -05:00
{
//---
2026-11-01 20:54:30 -05:00
m_unique_m_min_distance_to_close_pos . Reset ( new_value ) ;
2026-09-12 19:41:52 -05:00
m_min_distance_to_close_pos = new_value ;
}
//+------------------------------------------------------------------+
//| Sets a new value for the magic number |
//| Inputs: |
//| - ulong new_value: new magic number value. |
//| Outputs: The function returns no value |
//+------------------------------------------------------------------+
void CConditionalPartials : : MagicNumber ( ulong new_value )
{
m_magic_number = new_value ;
m_trade . SetExpertMagicNumber ( new_value ) ;
}
//+------------------------------------------------------------------+
//| Sets a new symbol. |
//| Inputs: |
//| - string new_symbol: New symbol. |
//| Outputs: The function returns no value. |
//| Notes: |
//| - The function should only be called when there are no |
//| open positions, to avoid unexpected bugs with |
//| tracked positions from other symbols. |
//+------------------------------------------------------------------+
void CConditionalPartials : : Symbol ( string new_symbol )
{
m_symbol = new_symbol ;
RefreshSymbolData ( ) ;
}
//+------------------------------------------------------------------+
//| Removes a trackable position from the internal array |
//| Inputs: |
//| ulong position_ticket: Ticket of the trackable position to be |
//| removed |
//| Outputs: The function returns true, if the ticket was |
//| successfully removed, otherwise false (ticket does not |
//| exist). |
//+------------------------------------------------------------------+
bool CConditionalPartials : : RemoveTrackedPosition ( ulong position_ticket )
{
//---
int index = -1 ;
for ( int i = 0 ; i < m_tracked_positions_size ; i + + )
{
if ( m_tracked_positions_arr [ i ] . ticket = = position_ticket )
{
index = i ;
break ;
}
}
//---
2026-11-01 20:54:30 -05:00
if ( index = = -1 )
2026-11-02 15:21:28 -05:00
{
2026-11-06 10:40:44 -05:00
return CErrorTrace : : FailInit < CConditionalPartials > ( TSNQMLART_CONDPARTIAL_ERR_POSITION_NOT_FOUND , position_ticket ) ;
2026-11-02 15:21:28 -05:00
}
2026-11-01 20:54:30 -05:00
//---
if ( index ! = - - m_tracked_positions_size )
m_tracked_positions_arr [ index ] = m_tracked_positions_arr [ m_tracked_positions_size ] ;
//---
return true ; // Success
2026-09-12 19:41:52 -05:00
}
//+------------------------------------------------------------------+
//| "Re-queries" the values of minimum volume, volume step and |
//| symbol digits. |
//+------------------------------------------------------------------+
void CConditionalPartials : : RefreshSymbolData ( void )
{
m_symbol_digits = ( int8_t ) : : SymbolInfoInteger ( m_symbol , SYMBOL_DIGITS ) ;
m_symbol_min_volume = : : SymbolInfoDouble ( m_symbol , SYMBOL_VOLUME_MIN ) ;
m_symbol_volume_step = : : SymbolInfoDouble ( m_symbol , SYMBOL_VOLUME_STEP ) ;
}
2026-11-02 15:21:28 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CConditionalPartials : : Constant ( bool new_val )
{
m_constant_partials = MQLART_CONDPARTIAL_CONSTANT * new_val ;
m_centinela = new_val * m_volume_to_close_size ;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-11-06 10:40:44 -05:00
static void CConditionalPartials : : FormatTrace ( const TSNErrorFrame & trace [ ] , int & trace_idx , string & out )
2026-11-02 15:21:28 -05:00
{
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHINI ( out , trace_idx )
switch ( trace [ trace_idx ] . code )
2026-11-02 15:21:28 -05:00
{
case TSNQMLART_CONDPARTIAL_ERR_NOT :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , " Not errors " , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
case TSNQMLART_CONDPARTIAL_ERR_INVALID_NUM_VOLTOCLOSE_ARR :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , " It was not possible to obtain at least one volume from the provided string representing the array of volumes to be closed. " , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
2026-11-07 09:34:18 -05:00
case TSNQMLART_CONDPARTIAL_ERR_MALFORMED_VOLTOCLOSE :
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Malformed double found in the string-comma-separated volumes to close, expected position in the array = [%d] " ,
int ( trace [ trace_idx ] . info ) ) , trace_idx )
break ;
2026-11-02 15:21:28 -05:00
case TSNQMLART_CONDPARTIAL_ERR_INVALID_VOLTOCLOSE_MUSTBE_0_100 :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " The percentage of volume to be closed [%.4f] must be greater than 0.0 and less than or equal to 100.0. " ,
CBitsReinterprete : : ToBasic < double , ulong > ( trace [ trace_idx ] . info ) ) , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
case TSNQMLART_CONDPARTIAL_ERR_SELECT_POSITION :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Error in select position [ticket = %I64u] last err = %d " , trace [ trace_idx ] . info , trace [ trace_idx ] . mql_err ) , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
case TSNQMLART_CONDPARTIAL_ERR_INVALID_SYMBOL :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Invalid symbol [ticket = %I64u] (must ve equal at CConditionalPartials::Symbol()) " , trace [ trace_idx ] . info ) , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
case TSNQMLART_CONDPARTIAL_ERR_POSITION_NOT_FOUND :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , StringFormat ( " Position not found [ticket = %I64u] " , trace [ trace_idx ] . info ) , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
default :
2026-11-05 17:09:00 -05:00
TSNSTD_ERRMAN_PUSHOUT ( out , " Unknow error " , trace_idx )
2026-11-02 15:21:28 -05:00
break ;
}
}
//---
2026-11-01 18:06:28 -05:00
}
2026-09-12 19:41:52 -05:00
# endif // MQLARTICLES_POSMGMT_CONDPARTIALS_BASE_BASE_MQH
//+------------------------------------------------------------------+