Beobachten
1
0
Fork
Du hast bereits einen Fork von MQLArticles erstellt
0
MQLArticles/PosMgmt/TSL/Int/SHL.mqh
427 Zeilen
15 KiB
MQL5
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+
//| SHL.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com"
#property strict
Nique_372
2026-10-24 12:47:47 -05:00
#ifndef MQLARTICLES_POSMGMT_TSL_INT_SHL_MQH
#define MQLARTICLES_POSMGMT_TSL_INT_SHL_MQH
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-24 17:50:41 -05:00
#include "Def.mqh"
Nique_372
2026-10-24 11:58:18 -05:00
#include <TSN\\MQLArticles\\Utils\\Objectos 2D.mqh>
Nique_372
2026-10-25 11:11:50 -05:00
#include <TSN\\Random\\Xoshiro256.mqh>
Nique_372
2026-10-24 12:47:47 -05:00
//+------------------------------------------------------------------+
//| Indices de MqlParam para CTrailingStopSwing::SetVariables |
//+------------------------------------------------------------------+
#define TSNMQLA_TSL_SWING_PARAM_TIMEFRAME 0 // ENUM_TIMEFRAMES
#define TSNMQLA_TSL_SWING_PARAM_INDEX 1 // int: indice
#define TSNMQLA_TSL_SWING_PARAM_CHART_ID 2 // long: id del grafico
#define TSNMQLA_TSL_SWING_PARAM_CLR_BULL 3 // color de los swings alcistas
#define TSNMQLA_TSL_SWING_PARAM_CLR_BEAR 4 // color de los swings bajistas
#define TSNMQLA_TSL_SWING_PARAM_LENGTH 5 // int: longitud (m_lenth)
#define TSNMQLA_TSL_SWING_PARAM_RANGE 6 // int: rango
Nique_372
2026-11-05 17:09:00 -05:00
#define TSNMQLA_TSL_SWING_NPARAMS 7
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_BULL (0)
#define TSNMQLA_TRAILINGSTOP_SWING_BEAR (1)
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
#ifndef TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R
#define TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R (16)
#endif // TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R
Nique_372
2026-10-24 11:58:18 -05:00
//--- Layout
// [aleatorio(12bit)|type(1bit)<<12|index(31bit)<<13bit|offstart(20bit)<<44]
//---
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_BITMASK_ALEATORIO (0xfff)
Nique_372
2026-10-24 11:58:18 -05:00
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_BITSTART_TYPE (12)
#define TSNMQLA_TRAILINGSTOP_SWING_BITMASK_TYPE (1)
Nique_372
2026-10-24 11:58:18 -05:00
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_BITSTART_IDX (13)
#define TSNMQLA_TRAILINGSTOP_SWING_BITMASK_IDX (0x7fffffff)
Nique_372
2026-10-24 11:58:18 -05:00
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_BITSTART_OFFS (44)
Nique_372
2026-10-24 11:58:18 -05:00
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_MAKEI_ID(BASEID, TYPE, IDX, OFFS) \
(BASEID | ulong(TYPE) << TSNMQLA_TRAILINGSTOP_SWING_BITSTART_TYPE | ulong(IDX) << TSNMQLA_TRAILINGSTOP_SWING_BITSTART_IDX | ulong(OFFS) <<TSNMQLA_TRAILINGSTOP_SWING_BITSTART_OFFS)
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_NEWEL(SIZE, RESERVE, ARR) \
Nique_372
2026-10-24 11:58:18 -05:00
if(SIZE>=RESERVE) \
{ \
RESERVE += (RESERVE >> 1) + 8; \
ArrayResize(ARR, RESERVE, RESERVE); \
} \
//---
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILING_STOP_GET_SWING_NAME(ID) m_names.Substr(int((ID)>>TSNMQLA_TRAILINGSTOP_SWING_BITSTART_OFFS),MQLARTICLES_CONV_MAXDIG_UL)
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
#define TSNMQLA_TRAILINGSTOP_SWING_IDX(SIZE, IDX) ((SIZE)-(IDX)-1)
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CTrailingStopSwing : public CTrailingStop
{
protected:
//---
struct pack(8) Swing
{
datetime time;
double price;
ulong id;
};
//---
string m_names;
//---
Swing m_swings_alcistas[];
int m_swings_alcistas_s;
int m_swings_alcistas_r;
Swing m_swings_bajistas[];
int m_swings_bajistas_s;
int m_swings_bajistas_r;
//---
double m_curr_swing_a_price;
double m_curr_swing_b_price;
//---
ulong m_base_id;
//---
int m_index;
int m_range;
int m_lenth;
long m_chart_id;
color m_clr_swings[2];
//---
inline double GetPricePositionSell(double curr_price) const override final;
inline double GetPricePositionBuy(double curr_price) const override final;
//---
void GetSwings(bool get_all_swing);
void OnInitOpenPosition() override final;
void OnCloseAllPosition() override final;
//---
int ReqOffStr();
__forceinline void DrawSwing(const Swing& s, const string& txt, const uchar dir) const;
Nique_372
2026-10-25 11:11:50 -05:00
//---
static Xoshiro256 s_rand;
Nique_372
2026-10-25 14:15:46 -05:00
//---
bool IsSwingLow(const int idx, double& price);
bool IsSwingHigh(const int idx, double& price);
Nique_372
2026-10-24 11:58:18 -05:00
public:
Nique_372
2026-10-24 17:50:41 -05:00
CTrailingStopSwing();
Nique_372
2026-10-24 11:58:18 -05:00
~CTrailingStopSwing();
//---
void OnNewBar() override final;
void DeleteAll() override final;
bool SetVariables(const MqlParam &params[]) override final;
//---
static const string s_ws;
};
Nique_372
2026-10-24 17:50:41 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
TSNMQLA_TRAILINGSTOP_REGISTER_CREATOR(CTrailingStopSwing, SimpleSwingHL, TRAILING_STOP_MODE_BASIC_SWINGS)
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-10-24 17:50:41 -05:00
CTrailingStopSwing::CTrailingStopSwing()
Nique_372
2026-11-05 17:09:00 -05:00
: CTrailingStop(TSNMQLA_TSL_SWING_NPARAMS, TSL_EXECUTION_ON_BAR, TRAILING_STOP_MODE_BASIC_SWINGS),
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
m_swings_bajistas_r(TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R), m_swings_alcistas_r(TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R),
Nique_372
2026-11-02 16:42:09 -05:00
m_swings_alcistas_s(0), m_swings_bajistas_s(0), m_curr_swing_b_price(0.0), m_curr_swing_a_price(DBL_MAX)
Nique_372
2026-10-24 11:58:18 -05:00
{
ArrayResize(m_swings_alcistas, m_swings_alcistas_r, m_swings_alcistas_r);
ArrayResize(m_swings_bajistas, m_swings_bajistas_r, m_swings_bajistas_r);
}
//+------------------------------------------------------------------+
CTrailingStopSwing::~CTrailingStopSwing()
{
DeleteAll();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CTrailingStopSwing::DeleteAll(void)
{
//---
for(int i = 0; i < m_swings_alcistas_s; i++)
Nique_372
2026-10-24 12:47:47 -05:00
ObjectDelete(m_chart_id, TSNMQLA_TRAILING_STOP_GET_SWING_NAME(m_swings_alcistas[i].id));
Nique_372
2026-10-24 11:58:18 -05:00
for(int i = 0; i < m_swings_bajistas_s; i++)
Nique_372
2026-10-24 12:47:47 -05:00
ObjectDelete(m_chart_id, TSNMQLA_TRAILING_STOP_GET_SWING_NAME(m_swings_bajistas[i].id));
Nique_372
2026-10-24 11:58:18 -05:00
//---
m_swings_alcistas_s = 0;
m_swings_bajistas_s = 0;
Nique_372
2026-11-02 16:42:09 -05:00
//---
m_curr_swing_b_price = 0.0;
m_curr_swing_a_price = DBL_MAX;
//---
Nique_372
2026-10-24 11:58:18 -05:00
StringSetLength(m_names, 0);
}
//+------------------------------------------------------------------+
void CTrailingStopSwing::OnInitOpenPosition(void)
Nique_372
2026-10-25 14:15:46 -05:00
{
Nique_372
2026-11-01 16:14:20 -05:00
m_base_id = s_rand.nextUInt64() & TSNMQLA_TRAILINGSTOP_SWING_BITMASK_ALEATORIO; // rotamos.
Nique_372
2026-10-24 11:58:18 -05:00
GetSwings(true);
}
//+------------------------------------------------------------------+
void CTrailingStopSwing::OnCloseAllPosition(void)
{
DeleteAll();
}
//+------------------------------------------------------------------+
int CTrailingStopSwing::ReqOffStr(void)
{
const int l = StringLen(m_names);;
m_names += s_ws; // llena de zeros
return l; // solo retona el icio
}
//+------------------------------------------------------------------+
void CTrailingStopSwing::GetSwings(bool get_all_swing)
{
//---
Nique_372
2026-10-25 21:02:28 -05:00
const int start = get_all_swing ? m_range : (m_lenth + 1);
Nique_372
2026-10-24 11:58:18 -05:00
double p;
//---
Nique_372
2026-10-25 21:02:28 -05:00
// itera aumentado en 1... hasta m_lenth
for(int i = start ; i > m_lenth; i--)
Nique_372
2026-10-24 11:58:18 -05:00
{
Nique_372
2026-10-25 14:15:46 -05:00
if(IsSwingHigh(i, p))
Nique_372
2026-10-24 11:58:18 -05:00
{
//---
int s_off = ReqOffStr(); // offset en la "cinta de "
//---
Nique_372
2026-10-24 12:47:47 -05:00
TSNMQLA_TRAILINGSTOP_SWING_NEWEL(m_swings_alcistas_s, m_swings_alcistas_r, m_swings_alcistas)
Nique_372
2026-10-24 11:58:18 -05:00
m_swings_alcistas[m_swings_alcistas_s].price = p;
Nique_372
2026-10-25 14:15:46 -05:00
m_swings_alcistas[m_swings_alcistas_s].time = iTime(m_symbol, m_timeframe, i);
Nique_372
2026-10-24 12:47:47 -05:00
const ulong id = TSNMQLA_TRAILINGSTOP_SWING_MAKEI_ID(m_base_id, TSNMQLA_TRAILINGSTOP_SWING_BULL, m_swings_alcistas_s, s_off);
Nique_372
2026-10-24 11:58:18 -05:00
m_swings_alcistas[m_swings_alcistas_s].id = id;
//---
Nique_372
2026-10-24 12:47:47 -05:00
STRCONV_VALTOBUFFER_ADDDIG(s_off, id)
CValueToBuffer::ULLToString(id, m_names, s_off); // lo llenamos de str
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
DrawSwing(m_swings_alcistas[m_swings_alcistas_s], "H", TSNMQLA_TRAILINGSTOP_SWING_BULL);
Nique_372
2026-10-24 11:58:18 -05:00
m_swings_alcistas_s++;
Nique_372
2026-11-05 17:09:00 -05:00
//--- dado qeu m_index esta en "serie" osea un index=0 quieredecir el "mas reciente" entonces lo "normalizamos"
// ahora bien puede dar fuera de rango asiq ue "truncamos" a como minimo 0 (osea el mas antiguo) digmoas si tengmos tan solo 1 swing y user nos pide idx=1 (previo)
// daria 1-1-1 (-1) array out range asi que truncamos en 0..
// lo mismo aplica para bajistas
Nique_372
2026-11-05 17:23:04 -05:00
m_curr_swing_a_price = m_swings_alcistas[fmax(m_swings_alcistas_s - 1 - m_index, 0)].price;
Nique_372
2026-10-24 11:58:18 -05:00
}
else
Nique_372
2026-10-25 14:15:46 -05:00
if(IsSwingLow(i, p))
Nique_372
2026-10-24 11:58:18 -05:00
{
//---
int s_off = ReqOffStr(); // offset en la "cinta de "
//---
Nique_372
2026-10-24 12:47:47 -05:00
TSNMQLA_TRAILINGSTOP_SWING_NEWEL(m_swings_bajistas_s, m_swings_bajistas_r, m_swings_bajistas)
Nique_372
2026-10-24 11:58:18 -05:00
m_swings_bajistas[m_swings_bajistas_s].price = p;
Nique_372
2026-10-25 14:15:46 -05:00
m_swings_bajistas[m_swings_bajistas_s].time = iTime(m_symbol, m_timeframe, i);
Nique_372
2026-10-24 12:47:47 -05:00
const ulong id = TSNMQLA_TRAILINGSTOP_SWING_MAKEI_ID(m_base_id, TSNMQLA_TRAILINGSTOP_SWING_BEAR, m_swings_bajistas_s, s_off);
Nique_372
2026-10-25 14:15:46 -05:00
m_swings_bajistas[m_swings_bajistas_s].id = id;
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
STRCONV_VALTOBUFFER_ADDDIG(s_off, id)
CValueToBuffer::ULLToString(id, m_names, s_off); // lo llenamos de str
Nique_372
2026-10-24 11:58:18 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
DrawSwing(m_swings_bajistas[m_swings_bajistas_s], "L", TSNMQLA_TRAILINGSTOP_SWING_BEAR);
Nique_372
2026-10-24 11:58:18 -05:00
m_swings_bajistas_s++;
//---
Nique_372
2026-11-05 17:23:04 -05:00
m_curr_swing_b_price = m_swings_bajistas[fmax(m_swings_bajistas_s - 1 - m_index, 0)].price;
Nique_372
2026-10-24 11:58:18 -05:00
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CTrailingStopSwing::SetVariables(const MqlParam & params[])
{
Nique_372
2026-11-05 17:09:00 -05:00
//---
Nique_372
2026-10-24 12:47:47 -05:00
m_timeframe = (ENUM_TIMEFRAMES)params[TSNMQLA_TSL_SWING_PARAM_TIMEFRAME].integer_value;
m_index = (int)params[TSNMQLA_TSL_SWING_PARAM_INDEX].integer_value;
m_chart_id = params[TSNMQLA_TSL_SWING_PARAM_CHART_ID].integer_value;
m_clr_swings[TSNMQLA_TRAILINGSTOP_SWING_BULL] = (color)params[TSNMQLA_TSL_SWING_PARAM_CLR_BULL].integer_value;
m_clr_swings[TSNMQLA_TRAILINGSTOP_SWING_BEAR] = (color)params[TSNMQLA_TSL_SWING_PARAM_CLR_BEAR].integer_value;
m_lenth = (int)params[TSNMQLA_TSL_SWING_PARAM_LENGTH].integer_value;
m_range = (int)params[TSNMQLA_TSL_SWING_PARAM_RANGE].integer_value;
Nique_372
2026-11-05 17:09:00 -05:00
//--- Checkeo para evitar invalidos
if(m_index < 0)
Nique_372
2026-11-06 10:40:44 -05:00
return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_SHL_INVALID_INDEX, m_index);
Nique_372
2026-11-05 17:09:00 -05:00
if(m_range <= 0)
Nique_372
2026-11-06 10:40:44 -05:00
return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_SHL_INVALID_RANGE, m_range);
Nique_372
2026-11-05 17:09:00 -05:00
if(m_lenth <= 0 || m_lenth >= m_range)
Nique_372
2026-11-06 10:40:44 -05:00
return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_SHL_INVALID_LENGHT, m_lenth);
Nique_372
2026-11-05 17:09:00 -05:00
//--- Ahora si
Nique_372
2026-10-24 11:58:18 -05:00
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CTrailingStopSwing::OnNewBar(void)
{
GetSwings(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
__forceinline void CTrailingStopSwing::DrawSwing(const Swing& s, const string& txt, const uchar dir) const
{
Nique_372
2026-10-24 12:47:47 -05:00
CNativeoGObjs::TextCreate(m_chart_id, TSNMQLA_TRAILING_STOP_GET_SWING_NAME(s.id), 0, s.time, s.price, txt, "Arial", 9, m_clr_swings[dir], 0.00, ANCHOR_CENTER);
Nique_372
2026-10-24 11:58:18 -05:00
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-11-02 16:42:09 -05:00
// Solo se llaman cuando hay posiciones trackendanso si no las hay no se llaman..
// la idea:
/*
Nomralmte detectaremos swing high\lows
en caso de las operaciones en venta en caso el precio rompa el "mas cercano" por ejemplo
swing low
price : entonces pondremos el SL en dicho swing low (hiremos bajando)
Nique_372
2026-11-05 17:09:00 -05:00
Algo similar para swing high
Nique_372
2026-11-02 16:42:09 -05:00
*/
Nique_372
2026-10-24 11:58:18 -05:00
inline double CTrailingStopSwing::GetPricePositionSell(double curr_price) const
{
return m_curr_swing_b_price > curr_price ? m_curr_swing_b_price : 0.0;
}
//+------------------------------------------------------------------+
inline double CTrailingStopSwing::GetPricePositionBuy(double curr_price) const
{
return m_curr_swing_a_price < curr_price ? m_curr_swing_a_price : 0.0;
}
Nique_372
2026-10-25 14:15:46 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Nique_372
2026-11-02 16:42:09 -05:00
// Nota usaria arrays pero creo que no vale la pensa noramlte lenth rosa los 8-14
Nique_372
2026-11-02 15:21:28 -05:00
// Aparte se llama a cada barra no acada tick asi que no hay un ahorro por "cache" siempre tendmores qeu volver a consultar
// Auqnue quizas mejore pero creo que un poco el usar arrays.. habra que ver.
Nique_372
2026-10-25 14:15:46 -05:00
bool CTrailingStopSwing::IsSwingLow(const int idx, double& price)
{
//---
price = iLow(m_symbol, m_timeframe, idx);
//---
int i = idx - m_lenth;
for(; i < idx; i++)
if(price >= iLow(m_symbol, m_timeframe, i))
return false;
//---
i++; // == idx
//---
Nique_372
2026-10-25 21:02:28 -05:00
for(; i <= idx + m_lenth; i++)
Nique_372
2026-10-25 14:15:46 -05:00
if(price >= iLow(m_symbol, m_timeframe, i))
return false;
//---
return true;
}
//+------------------------------------------------------------------+
Nique_372
2026-10-25 21:02:28 -05:00
//
// [7][6][5][high][3][2][1][0]
// end <----------- start (de mas actual a ma sprevio, vale?¿)
// SOLO CON VELAS CERRADAS
Nique_372
2026-10-25 14:15:46 -05:00
bool CTrailingStopSwing::IsSwingHigh(const int idx, double& price)
{
//---
price = iHigh(m_symbol, m_timeframe, idx);
//---
int i = idx - m_lenth;
for(; i < idx; i++)
if(price <= iHigh(m_symbol, m_timeframe, i))
return false;
//---
i++; // == idx
//---
Nique_372
2026-10-25 21:02:28 -05:00
for(; i <= idx + m_lenth; i++)
Nique_372
2026-10-25 14:15:46 -05:00
if(price <= iHigh(m_symbol, m_timeframe, i))
return false;
//---
return true; // Condición cumplida
}
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// len=20
const string CTrailingStopSwing::s_ws = " ";
Nique_372
2026-10-25 11:11:50 -05:00
Xoshiro256 CTrailingStopSwing::s_rand;
Nique_372
2026-10-24 11:58:18 -05:00
//---
}
Nique_372
2026-10-24 12:47:47 -05:00
#endif // MQLARTICLES_POSMGMT_TSL_INT_SHL_MQH
Nique_372
2026-10-24 11:58:18 -05:00
//+------------------------------------------------------------------+