MQLArticles/PosMgmt/BE/Base/Def.mqh
Nique_372 Nique_372 1b2572d6ea
2026-11-06 10:40:44 -05:00
198 lines
8.2 KiB
MQL5
//+------------------------------------------------------------------+
//| Def.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_BASE_DEF_MQH
#define MQLARTICLES_POSMGMT_BE_BASE_DEF_MQH
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include "..\\..\\..\\Account\\MoneyStatus\\Manager.mqh"
#include "..\\..\\..\\Utils\\FA\\Atr\\All.mqh"
#include <TSN\\Std\\Basics\\ErrManager.mqh>
#include <TSN\\Std\\Misce\\BitReinterprete.mqh>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| Break Even Structs |
//+------------------------------------------------------------------+
struct position_be
{
ulong ticket; // Position Ticket
double breakeven_price; // Be price
double price_to_beat; // Price to exceed to reach break even
ENUM_POSITION_TYPE type; // Position type
int try; // Intentos
};
//---
enum ENUM_BREAKEVEN_TYPE
{
BREAKEVEN_TYPE_RR = 0, //By RR
BREAKEVEN_TYPE_FIXED_POINTS, //By FixedPoints
BREAKEVEN_TYPE_ATR, //By Atr
BREAKEVEN_T_TOTAL,
};
//---
const string g_breakevens_types_str[3]
{
"Breakeven by rr",
"Breakeven by fixed points",
"Breakeven by atr"
};
//---
struct BreakEvenParams
{
long integer_value;
double double_value;
CAtrOptimized* atr_pointer_value;
};
//--
#ifndef MQLARTICLES_BREAKEVEN_CHECK_RE
// Crece m_pos_be y su array paralelo de marcas (m_pos_be_remove) juntos; la cola nueva de marcas se inicia en false
#define MQLARTICLES_BREAKEVEN_CHECK_RE \
if(m_pos_be_s>=m_pos_be_r) \
{ \
const int old_r = m_pos_be_r; \
m_pos_be_r<<=1; \
ArrayResize(m_pos_be, m_pos_be_r,m_pos_be_r); \
ArrayResize(m_pos_be_remove, m_pos_be_r,m_pos_be_r); \
for(int i_re = old_r; i_re < m_pos_be_r; i_re++) \
m_pos_be_remove[i_re] = false; \
}
#endif // MQLARTICLES_BREAKEVEN_CHECK_RE
//---
#ifndef CBREAKEVEN_RESERVE_ARR
#define CBREAKEVEN_RESERVE_ARR 16
#endif // CBREAKEVEN_RESERVE_ARR
//---
#ifndef CBREAKEVEN_DEFAULT_POS_MOD_TRY
#define CBREAKEVEN_DEFAULT_POS_MOD_TRY (4)
#endif // CBREAKEVEN_DEFAULT_POS_MOD_TRY
//---
#define MQLARTICLES_BREAKEVEN_PROLOGO(TYPE, TICKET) \
MQLARTICLES_BREAKEVEN_CHECK_RE \
m_pos_be[m_pos_be_s].type = TYPE; \
m_pos_be[m_pos_be_s].ticket = TICKET; \
m_pos_be[m_pos_be_s].try = m_be_try;
//--- Errores propios de CBreakEvenBase (rango [0 - 99])
enum ENUM_TSNMQLART_BE_ERR
{
TSNMQLART_BE_ERR_NOT,
TSNMQLART_BE_ERR_SELECT_POSITION,
TSNMQLART_BE_ERR_POSITION_NOT_FOUND,
};
//--- Errores de los tipos de breakeven (Imp\), desde 100 para no pisar los de la base
#define TSNMQLART_BE_TYPES_ERR_START (100)
enum ENUM_TSNMQLART_BE_TYPES_ERR
{
TSNMQLART_BE_TYPES_ERR_NOT = TSNMQLART_BE_TYPES_ERR_START,
//--- commons
TSNMQLART_BE_TYPES_ERR_INV_SYMBOL,
TSNMQLART_BE_TYPES_ERR_INV_SL_PRICE,
TSNMQLART_BE_TYPES_ERR_SL_INSUFICCENT_DIST,
//--- atr
TSNMQLART_BE_TYPES_ERR_ATR_INVALID_EXTRABE_VAL,
TSNMQLART_BE_TYPES_ERR_ATR_PTR_INVALID,
//--- rr
TSNMQLART_BE_TYPES_ERR_RR_INVALID_ATPPTR_RR,
TSNMQLART_BE_TYPES_ERR_RR_INVALID_TYPE_EXTRA,
TSNMQLART_BE_TYPES_ERR_RR_INVALID_RR,
TSNMQLART_BE_TYPES_ERR_RR_INVALID_ATRMUL_OR_POINTS,
//--- fixed points
TSNMQLART_BE_TYPES_ERR_BPOINTS_INV_EPOINTSBE,
TSNMQLART_BE_TYPES_ERR_BPOINTS_EPOINTS_MUSTBE_MINOR_PNORM,
};
//+------------------------------------------------------------------+
//| Formater de los errores de los tipos de breakeven (Imp) |
//+------------------------------------------------------------------+
class CBasicBEFormater
{
public:
static void FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out);
};
//+------------------------------------------------------------------+
static void CBasicBEFormater::FormatTrace(const TSNErrorFrame &trace[], int &trace_idx, string &out)
{
TSNSTD_ERRMAN_PUSHINI(out, trace_idx)
switch(trace[trace_idx].code)
{
case TSNMQLART_BE_TYPES_ERR_NOT:
TSNSTD_ERRMAN_PUSHOUT(out, "Not error", trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_INV_SYMBOL:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Invalid symbol for the position [ticket = %I64u] (must be equal to the symbol of the breakeven)", trace[trace_idx].info), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_INV_SL_PRICE:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Invalid sl price [%.5f] (the position must have a stop loss)", CBitsReinterprete::ToBasic<double, ulong>(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_SL_INSUFICCENT_DIST:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("The distance between the open price and the stop loss [%.5f] is not enough to trigger the breakeven",
CBitsReinterprete::ToBasic<double, ulong>(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_ATR_INVALID_EXTRABE_VAL:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Atr breakeven, the extra atr multiplier [%.4f] must be lower than the atr multiplier",
CBitsReinterprete::ToBasic<double, ulong>(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_ATR_PTR_INVALID:
TSNSTD_ERRMAN_PUSHOUT(out, "Atr breakeven, invalid atr pointer", trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_RR_INVALID_ATPPTR_RR:
TSNSTD_ERRMAN_PUSHOUT(out, "RR breakeven, the extra type is by atr but the atr pointer is invalid", trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_RR_INVALID_TYPE_EXTRA:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("RR breakeven, invalid extra type [%d]", int(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_RR_INVALID_RR:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("RR breakeven, invalid rr coefficient [%.4f] (must be > 0)", CBitsReinterprete::ToBasic<double, ulong>(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_RR_INVALID_ATRMUL_OR_POINTS:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("RR breakeven, invalid extra atr multiplier or points [%.4f] (must be > 0)",
CBitsReinterprete::ToBasic<double, ulong>(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_BPOINTS_INV_EPOINTSBE:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Fixed points breakeven, invalid extra points [%d] (must be >= 0)", int(trace[trace_idx].info)), trace_idx)
break;
case TSNMQLART_BE_TYPES_ERR_BPOINTS_EPOINTS_MUSTBE_MINOR_PNORM:
TSNSTD_ERRMAN_PUSHOUT(out, StringFormat("Fixed points breakeven, extra points [%d] must be lower than points [%d]",
int(trace[trace_idx].info), int(trace[trace_idx].info >> 32)), trace_idx)
break;
default:
TSNSTD_ERRMAN_PUSHOUT(out, "Unknow error", trace_idx)
break;
}
}
//---
//#define TSNMQLART_POSMGMT_BE_ELOGS
//---
}
#endif // MQLARTICLES_POSMGMT_BE_BASE_DEF_MQH
//+------------------------------------------------------------------+