MQLArticles/PosMgmt/ConditionalPartial/Base/Base.mqh
Nique_372 Nique_372 e4715a4e1c
2026-11-07 09:34:18 -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"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| 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
int m_tries; // Number of tryes
//--- 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;
bool m_tracked_positions_remove[]; // Parallel to m_tracked_positions_arr (same size): true = remove, always false between calls
// 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;
//--- 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"
//--- 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;
TsnUniquePtr(CDiff) m_unique_m_min_distance_to_close_pos;
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
bool m_forze_to_close; // Forze close total position
double m_min_distance_value; // Minimum distance between partials (value)
char m_constant_partials; // Partials is constant (only zero idx) ?
//--- Symbol Functions
void RefreshSymbolData();
//--- Partials closures Functions
template <typename TPositionType>
void ClosePartial();
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
//--- Config
// Condition
IConditionPartial* PartialCloseCondition() { return m_partial_condition; }
void PartialCloseCondition(IConditionPartial* new_condition, bool delete_prev_ptr);
// Magic number
ulong MagicNumber() const { return m_magic_number; }
void MagicNumber(ulong new_value);
// Minimum distance between partial closures
CDiff* MinDistBetweenPartialClosures() { return m_min_distance_to_close_pos; }
void MinDistBetweenPartialClosures(CDiff* new_value);
// Symbol
string Symbol() { return m_symbol; }
void Symbol(string new_symbol);
// Auto mode
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; }
// 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);
//--- Tracked positions size
int TrackedPositionsSize() const { return m_tracked_positions_size; }
//--- Errores
static void FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CConditionalPartials::CConditionalPartials()
:
//---
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),
m_delete_partial_condition_in_dstctor(true), m_forze_to_close(false), m_centinela(0),
//---
m_tracked_positions_r(CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE), m_tracked_positions_size(0), m_tries(MQLART_POSMGMT_CONDPARTIALS_TRYDEF)
{
//---
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);
::ArrayResize(m_tracked_positions_remove, CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE, CONDITIONAL_PARTIAL_ARR_MAIN_RESERVE);
::ArrayInitialize(m_tracked_positions_remove, false);
}
//+------------------------------------------------------------------+
//| 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;
if(m_magic_number != NOT_MAGIC_NUMBER)
m_trade.SetExpertMagicNumber(m_magic_number);
// 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;
// Partial condition
m_partial_condition = config.condition;
m_delete_partial_condition_in_dstctor = config.delete_condition;
// Extra
m_forze_to_close = config.forze_to_close;
//--- Symbol refresh
RefreshSymbolData();
//---
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)
{
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)));
}
//---
for(int i = 0; i < m_volume_to_close_size; i++)
{
const double val = m_volumes_to_close_arr[i];
if(val > 100.0 || val < 0.00001)
{
return CErrorTrace::FailInit<CConditionalPartials>(TSNQMLART_CONDPARTIAL_ERR_INVALID_VOLTOCLOSE_MUSTBE_0_100, CBitsReinterprete::ToBasic<ulong, double>(val));
}
m_volumes_to_close_arr[i] /= 100.00;
}
//---
Constant(config.constant);
//--- Print array
#ifdef CONDITIONAL_PARTIAL_MORE_INFO
ArrayPrint(m_volumes_to_close_arr, 2, " | ", 0, m_volume_to_close_size);
#endif // CONDITIONAL_PARTIAL_MORE_INFO
//--- 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);
ClosePartial<MQLART_CONDPARTIAL_PBUY>();
}
//--- Check sell condition
if(sell)
{
m_symbol_ask = ::SymbolInfoDouble(m_symbol, SYMBOL_ASK);
ClosePartial<MQLART_CONDPARTIAL_PSELL>();
}
}
}
#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);
ClosePartial<MQLART_CONDPARTIAL_PBUY>();
}
//--- Check sell condition
if(sell)
{
m_symbol_ask = ::SymbolInfoDouble(m_symbol, SYMBOL_ASK);
ClosePartial<MQLART_CONDPARTIAL_PSELL>();
}
}
}
#endif
}
//+------------------------------------------------------------------+
//| Function that executes every time a trade is closed or opened |
//+------------------------------------------------------------------+
void CConditionalPartials::OnOpenPosition(const ROnOpenPosition &pos)
{
if((pos.position.magic == m_magic_number || m_magic_number == NOT_MAGIC_NUMBER) && account_status.last_trans_symbol == m_symbol)
{
#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
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)
{
const int old_r = m_tracked_positions_r;
m_tracked_positions_r <<= 1;
ArrayResize(m_tracked_positions_arr, m_tracked_positions_r, m_tracked_positions_r);
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;
}
//--- Creation of a new position to track
m_tracked_positions_arr[m_tracked_positions_size].try = m_tries;
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;
m_tracked_positions_arr[m_tracked_positions_size].next_index_to_close = 0;
//---
m_tracked_positions_size++;
}
//+-------------------------------------------------------------------------+
//| 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)
{
//---
::ResetLastError();
//--- Select
if(!::PositionSelectByTicket(position_ticket))
{
return CErrorTrace::FailInit<CConditionalPartials>(TSNQMLART_CONDPARTIAL_ERR_SELECT_POSITION, position_ticket, ::GetLastError());
}
//--- Check symbol
if(::PositionGetString(POSITION_SYMBOL) != m_symbol)
{
return CErrorTrace::FailInit<CConditionalPartials>(TSNQMLART_CONDPARTIAL_ERR_INVALID_SYMBOL, position_ticket);
}
//--- 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 |
//+------------------------------------------------------------------+
template <typename TPositionType>
void CConditionalPartials::ClosePartial(void)
{
//--- opt: solo un booleano como fastpath para eliminar o no (las marcas viven en m_tracked_positions_remove)
bool already_remove = false;
//---
#define CONDITIONAL_PARTIAL_REMOVEPOS(IDX) m_tracked_positions_remove[IDX] = true; already_remove = true;
//--- Iteration
for(int i = 0; i < m_tracked_positions_size; i++)
{
//---
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)))
continue;
//---
if(!::PositionSelectByTicket(m_tracked_positions_arr[i].ticket))
{
#ifdef CONDITIONAL_PARTIAL_MORE_INFO
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)
continue;
}
//---
int idx = m_tracked_positions_arr[i].next_index_to_close++ >> m_constant_partials;
//--- Initial calculation
const double lot_size =::PositionGetDouble(POSITION_VOLUME);
double volume_to_close = m_volumes_to_close_arr[idx++] * lot_size;
//--- Check min volume to close
if(volume_to_close < m_symbol_min_volume)
{
if(m_forze_to_close)
volume_to_close = lot_size;
else
{
#ifdef CONDITIONAL_PARTIAL_MORE_INFO
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));
#endif // CONDITIONAL_PARTIAL_MORE_INFO
//---
CONDITIONAL_PARTIAL_REMOVEPOS(i)
continue;
}
}
//--- Round
volume_to_close = MQLART_POSMGMT_CONDPARTIALS_LOTROUND(volume_to_close, m_symbol_volume_step);
//--- Close
if(!m_trade.PositionClosePartial(m_tracked_positions_arr[i].ticket, volume_to_close))
{
//---
#ifdef CONDITIONAL_PARTIAL_MORE_INFO
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
//---
m_tracked_positions_arr[i].next_index_to_close--; // rollback
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
//---
m_tracked_positions_arr[i].next_min_price += m_min_distance_value;
//--- Check limit
if(idx == m_centinela) // Limit reached
{
CONDITIONAL_PARTIAL_REMOVEPOS(i)
}
}
//--- Check para eliminar
if(!already_remove)
return;
//--- Eliminamos
int w = 0;
for(int r = 0; r < m_tracked_positions_size; r++)
{
if(!m_tracked_positions_remove[r])
{
if(w != r)
m_tracked_positions_arr[w] = m_tracked_positions_arr[r];
w++;
}
else
m_tracked_positions_remove[r] = false;
}
m_tracked_positions_size = w; // opt: solo cambiamos el tamaño "virtual"
//---
#undef CONDITIONAL_PARTIAL_REMOVEPOS
}
//+----------------------------------------------------------------------------------+
//| 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 |
//+------------------------------------------------------------------+
void CConditionalPartials::MinDistBetweenPartialClosures(CDiff *new_value)
{
//---
m_unique_m_min_distance_to_close_pos.Reset(new_value);
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;
}
}
//---
if(index == -1)
{
return CErrorTrace::FailInit<CConditionalPartials>(TSNQMLART_CONDPARTIAL_ERR_POSITION_NOT_FOUND, position_ticket);
}
//---
if(index != --m_tracked_positions_size)
m_tracked_positions_arr[index] = m_tracked_positions_arr[m_tracked_positions_size];
//---
return true; // Success
}
//+------------------------------------------------------------------+
//| "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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CConditionalPartials::Constant(bool new_val)
{
m_constant_partials = MQLART_CONDPARTIAL_CONSTANT * new_val;
m_centinela = new_val * m_volume_to_close_size;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
static void CConditionalPartials::FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out)
{
TSNSTD_ERRMAN_PUSHINI(out, trace_idx)
switch(trace[trace_idx].code)
{
case TSNQMLART_CONDPARTIAL_ERR_NOT:
TSNSTD_ERRMAN_PUSHOUT(out, "Not errors", trace_idx)
break;
case TSNQMLART_CONDPARTIAL_ERR_INVALID_NUM_VOLTOCLOSE_ARR:
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)
break;
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;
case TSNQMLART_CONDPARTIAL_ERR_INVALID_VOLTOCLOSE_MUSTBE_0_100:
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)
break;
case TSNQMLART_CONDPARTIAL_ERR_SELECT_POSITION:
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)
break;
case TSNQMLART_CONDPARTIAL_ERR_INVALID_SYMBOL:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Invalid symbol [ticket = %I64u] (must ve equal at CConditionalPartials::Symbol())", trace[trace_idx].info), trace_idx)
break;
case TSNQMLART_CONDPARTIAL_ERR_POSITION_NOT_FOUND:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Position not found [ticket = %I64u]", trace[trace_idx].info), trace_idx)
break;
default:
TSNSTD_ERRMAN_PUSHOUT(out, "Unknow error", trace_idx)
break;
}
}
//---
}
#endif // MQLARTICLES_POSMGMT_CONDPARTIALS_BASE_BASE_MQH
//+------------------------------------------------------------------+