//+------------------------------------------------------------------+ //| SHL.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_TSL_INT_SHL_MQH #define MQLARTICLES_POSMGMT_TSL_INT_SHL_MQH //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #include "Def.mqh" #include #include //+------------------------------------------------------------------+ //| 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 #define TSNMQLA_TSL_SWING_NPARAMS 7 //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #define TSNMQLA_TRAILINGSTOP_SWING_BULL (0) #define TSNMQLA_TRAILINGSTOP_SWING_BEAR (1) //--- #ifndef TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R #define TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R (16) #endif // TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R //--- Layout // [aleatorio(12bit)|type(1bit)<<12|index(31bit)<<13bit|offstart(20bit)<<44] //--- #define TSNMQLA_TRAILINGSTOP_SWING_BITMASK_ALEATORIO (0xfff) #define TSNMQLA_TRAILINGSTOP_SWING_BITSTART_TYPE (12) #define TSNMQLA_TRAILINGSTOP_SWING_BITMASK_TYPE (1) #define TSNMQLA_TRAILINGSTOP_SWING_BITSTART_IDX (13) #define TSNMQLA_TRAILINGSTOP_SWING_BITMASK_IDX (0x7fffffff) #define TSNMQLA_TRAILINGSTOP_SWING_BITSTART_OFFS (44) #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) <=RESERVE) \ { \ RESERVE += (RESERVE >> 1) + 8; \ ArrayResize(ARR, RESERVE, RESERVE); \ } \ //--- #define TSNMQLA_TRAILING_STOP_GET_SWING_NAME(ID) m_names.Substr(int((ID)>>TSNMQLA_TRAILINGSTOP_SWING_BITSTART_OFFS),MQLARTICLES_CONV_MAXDIG_UL) //--- #define TSNMQLA_TRAILINGSTOP_SWING_IDX(SIZE, IDX) ((SIZE)-(IDX)-1) //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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; //--- static Xoshiro256 s_rand; //--- bool IsSwingLow(const int idx, double& price); bool IsSwingHigh(const int idx, double& price); public: CTrailingStopSwing(); ~CTrailingStopSwing(); //--- void OnNewBar() override final; void DeleteAll() override final; bool SetVariables(const MqlParam ¶ms[]) override final; //--- static const string s_ws; }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ TSNMQLA_TRAILINGSTOP_REGISTER_CREATOR(CTrailingStopSwing, SimpleSwingHL, TRAILING_STOP_MODE_BASIC_SWINGS) //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CTrailingStopSwing::CTrailingStopSwing() : CTrailingStop(TSNMQLA_TSL_SWING_NPARAMS, TSL_EXECUTION_ON_BAR, TRAILING_STOP_MODE_BASIC_SWINGS), //--- m_swings_bajistas_r(TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R), m_swings_alcistas_r(TSNMQLA_TRAILINGSTOP_SWING_DEFAULT_R), m_swings_alcistas_s(0), m_swings_bajistas_s(0), m_curr_swing_b_price(0.0), m_curr_swing_a_price(DBL_MAX) { 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++) ObjectDelete(m_chart_id, TSNMQLA_TRAILING_STOP_GET_SWING_NAME(m_swings_alcistas[i].id)); for(int i = 0; i < m_swings_bajistas_s; i++) ObjectDelete(m_chart_id, TSNMQLA_TRAILING_STOP_GET_SWING_NAME(m_swings_bajistas[i].id)); //--- m_swings_alcistas_s = 0; m_swings_bajistas_s = 0; //--- m_curr_swing_b_price = 0.0; m_curr_swing_a_price = DBL_MAX; //--- StringSetLength(m_names, 0); } //+------------------------------------------------------------------+ void CTrailingStopSwing::OnInitOpenPosition(void) { m_base_id = s_rand.nextUInt64() & TSNMQLA_TRAILINGSTOP_SWING_BITMASK_ALEATORIO; // rotamos. 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) { //--- const int start = get_all_swing ? m_range : (m_lenth + 1); double p; //--- // itera aumentado en 1... hasta m_lenth for(int i = start ; i > m_lenth; i--) { if(IsSwingHigh(i, p)) { //--- int s_off = ReqOffStr(); // offset en la "cinta de " //--- TSNMQLA_TRAILINGSTOP_SWING_NEWEL(m_swings_alcistas_s, m_swings_alcistas_r, m_swings_alcistas) m_swings_alcistas[m_swings_alcistas_s].price = p; m_swings_alcistas[m_swings_alcistas_s].time = iTime(m_symbol, m_timeframe, i); const ulong id = TSNMQLA_TRAILINGSTOP_SWING_MAKEI_ID(m_base_id, TSNMQLA_TRAILINGSTOP_SWING_BULL, m_swings_alcistas_s, s_off); m_swings_alcistas[m_swings_alcistas_s].id = id; //--- STRCONV_VALTOBUFFER_ADDDIG(s_off, id) CValueToBuffer::ULLToString(id, m_names, s_off); // lo llenamos de str //--- DrawSwing(m_swings_alcistas[m_swings_alcistas_s], "H", TSNMQLA_TRAILINGSTOP_SWING_BULL); m_swings_alcistas_s++; //--- 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 m_curr_swing_a_price = m_swings_alcistas[fmax(m_swings_alcistas_s - 1 - m_index, 0)].price; } else if(IsSwingLow(i, p)) { //--- int s_off = ReqOffStr(); // offset en la "cinta de " //--- TSNMQLA_TRAILINGSTOP_SWING_NEWEL(m_swings_bajistas_s, m_swings_bajistas_r, m_swings_bajistas) m_swings_bajistas[m_swings_bajistas_s].price = p; m_swings_bajistas[m_swings_bajistas_s].time = iTime(m_symbol, m_timeframe, i); const ulong id = TSNMQLA_TRAILINGSTOP_SWING_MAKEI_ID(m_base_id, TSNMQLA_TRAILINGSTOP_SWING_BEAR, m_swings_bajistas_s, s_off); m_swings_bajistas[m_swings_bajistas_s].id = id; //--- STRCONV_VALTOBUFFER_ADDDIG(s_off, id) CValueToBuffer::ULLToString(id, m_names, s_off); // lo llenamos de str //--- DrawSwing(m_swings_bajistas[m_swings_bajistas_s], "L", TSNMQLA_TRAILINGSTOP_SWING_BEAR); m_swings_bajistas_s++; //--- m_curr_swing_b_price = m_swings_bajistas[fmax(m_swings_bajistas_s - 1 - m_index, 0)].price; } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool CTrailingStopSwing::SetVariables(const MqlParam & params[]) { //--- 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; //--- Checkeo para evitar invalidos if(m_index < 0) return CErrorTrace::FailInit(TSN_TRAILING_TYPES_BASIC_ERR_SHL_INVALID_INDEX, m_index); if(m_range <= 0) return CErrorTrace::FailInit(TSN_TRAILING_TYPES_BASIC_ERR_SHL_INVALID_RANGE, m_range); if(m_lenth <= 0 || m_lenth >= m_range) return CErrorTrace::FailInit(TSN_TRAILING_TYPES_BASIC_ERR_SHL_INVALID_LENGHT, m_lenth); //--- Ahora si return true; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CTrailingStopSwing::OnNewBar(void) { GetSwings(false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ __forceinline void CTrailingStopSwing::DrawSwing(const Swing& s, const string& txt, const uchar dir) const { 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); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // 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) Algo similar para swing high */ 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; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // Nota usaria arrays pero creo que no vale la pensa noramlte lenth rosa los 8-14 // 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. 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 //--- for(; i <= idx + m_lenth; i++) if(price >= iLow(m_symbol, m_timeframe, i)) return false; //--- return true; } //+------------------------------------------------------------------+ // // [7][6][5][high][3][2][1][0] // end <----------- start (de mas actual a ma sprevio, vale?¿) // SOLO CON VELAS CERRADAS 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 //--- for(; i <= idx + m_lenth; i++) if(price <= iHigh(m_symbol, m_timeframe, i)) return false; //--- return true; // Condición cumplida } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // len=20 const string CTrailingStopSwing::s_ws = " "; Xoshiro256 CTrailingStopSwing::s_rand; //--- } #endif // MQLARTICLES_POSMGMT_TSL_INT_SHL_MQH //+------------------------------------------------------------------+