2026-10-24 11:58:18 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| Atr.mqh |
|
| | | //| Copyright 2026, Niquel Mendoza. |
|
| | | //| https://www.mql5.com |
|
| | | //+------------------------------------------------------------------+
|
| | | #property copyright "Copyright 2026, Niquel Mendoza."
|
| | | #property link "https://www.mql5.com"
|
| | | #property strict
|
| | |
|
2026-10-24 12:47:47 -05:00 | | | #ifndef MQLARTICLES_POSMGMT_TSL_INT_ATR_MQH
|
| | | #define MQLARTICLES_POSMGMT_TSL_INT_ATR_MQH
|
2026-10-24 11:58:18 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
2026-10-24 17:50:41 -05:00 | | | #include "Def.mqh"
|
2026-10-24 11:58:18 -05:00 | | |
|
2026-10-24 12:47:47 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| Indices de MqlParam para CTrailingStopATR::SetVariables |
|
| | | //+------------------------------------------------------------------+
|
| | | #define TSNMQLA_TSL_ATR_PARAM_PERIOD 0 // int: periodo del ATR
|
| | | #define TSNMQLA_TSL_ATR_PARAM_TIMEFRAME 1 // ENUM_TIMEFRAMES
|
| | | #define TSNMQLA_TSL_ATR_PARAM_MULTIPLIER 2 // double: multiplicador del ATR
|
| | | #define TSNMQLA_TSL_ATR_PARAM_INDEX 3 // int: indice del ATR
|
2026-11-05 17:23:04 -05:00 | | | #define TSNMQLA_TSL_ATR_NPARAMS 4 // numero de parametros
|
2026-10-24 12:47:47 -05:00 | | |
|
2026-10-24 11:58:18 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | namespace TSN
|
| | | {
|
| | | //+------------------------------------------------------------------+
|
| | | //| Clase derivada para Trailing Stop basado en ATR |
|
| | | //+------------------------------------------------------------------+
|
| | | class CTrailingStopATR : public CTrailingStop
|
| | | {
|
| | | private:
|
| | | double m_atr_mul;
|
2026-10-25 09:14:49 -05:00 | | | CAtrOptimized* m_atr;
|
2026-10-24 11:58:18 -05:00 | | |
|
| | | //---
|
| | | void Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price) override final;
|
| | |
|
| | | public:
|
| | | // Constructor: inicializa con valores por defecto
|
2026-10-24 17:50:41 -05:00 | | | CTrailingStopATR()
|
2026-11-05 17:23:04 -05:00 | | | : CTrailingStop(TSNMQLA_TSL_ATR_NPARAMS, TSL_EXECUTION_ON_TICK, TRAILING_STOP_MODE_BASIC_ATR),
|
2026-10-24 11:58:18 -05:00 | | | m_atr_mul(1.5) {}
|
| | |
|
| | | //---
|
| | | bool SetVariables(const MqlParam ¶ms[]) override final;
|
| | |
|
| | | //---
|
| | | void TrailingStop() override final;
|
| | | };
|
2026-10-25 09:14:49 -05:00 | | |
|
2026-10-24 17:50:41 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | TSNMQLA_TRAILINGSTOP_REGISTER_CREATOR(CTrailingStopATR, SimpleAtr, TRAILING_STOP_MODE_BASIC_ATR)
|
2026-10-24 11:58:18 -05:00 | | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | void CTrailingStopATR::Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price)
|
| | | {
|
| | | //---
|
2026-10-25 09:14:49 -05:00 | | | const double atr_val = m_atr.GetAtrValue(TimeCurrent());
|
2026-10-24 12:47:47 -05:00 | | | TSNMQLA_TRAILING_CHECK_R
|
2026-10-24 11:58:18 -05:00 | | |
|
| | | //---
|
| | | m_trailings[m_trailings_s].open_price = open_price;
|
| | | m_trailings[m_trailings_s].ticket = ticket;
|
| | | m_trailings[m_trailings_s].pos_type = pos_type;
|
| | | m_trailings[m_trailings_s].price_base = open_price;
|
| | | m_trailings[m_trailings_s++].price_a_superar = pos_type == POSITION_TYPE_BUY ? open_price + (atr_val * m_atr_mul) : open_price - (atr_val * m_atr_mul);
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | bool CTrailingStopATR::SetVariables(const MqlParam ¶ms[])
|
| | | {
|
| | | //---
|
2026-10-24 12:47:47 -05:00 | | | const int atr_p = (int)params[TSNMQLA_TSL_ATR_PARAM_PERIOD].integer_value;
|
| | | m_timeframe = (ENUM_TIMEFRAMES)params[TSNMQLA_TSL_ATR_PARAM_TIMEFRAME].integer_value;
|
| | | m_atr_mul = params[TSNMQLA_TSL_ATR_PARAM_MULTIPLIER].double_value;
|
| | | const int index = (int)params[TSNMQLA_TSL_ATR_PARAM_INDEX].integer_value;
|
2026-10-25 09:14:49 -05:00 | | | m_atr = CAtrEmbCreator::Create(m_timeframe, m_symbol, index, atr_p);
|
2026-11-05 17:23:04 -05:00 | | |
|
| | | //---
|
| | | if(CheckPointer(m_atr) != POINTER_DYNAMIC)
|
2026-11-06 10:40:44 -05:00 | | | return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_ATR_INVALID_PTR, ulong(uint(atr_p)) | ulong(uint(index)) << 32);
|
2026-11-05 17:23:04 -05:00 | | |
|
2026-10-24 11:58:18 -05:00 | | | //---
|
2026-11-05 17:23:04 -05:00 | | | return true;
|
2026-10-24 11:58:18 -05:00 | | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | void CTrailingStopATR::TrailingStop(void)
|
| | | {
|
| | | //---
|
| | | if(m_trailings_s < 1)
|
| | | return;
|
| | |
|
| | | //---
|
| | | const double ask = SymbolInfoDouble(m_symbol, SYMBOL_ASK);
|
| | | const double bid = SymbolInfoDouble(m_symbol, SYMBOL_BID);
|
| | |
|
| | | //---
|
2026-10-25 09:14:49 -05:00 | | | const double atr_val = m_atr.GetAtrValue(TimeCurrent());
|
2026-10-24 11:58:18 -05:00 | | |
|
| | | //---
|
| | | for(int i = 0; i < m_trailings_s; i++)
|
| | | {
|
| | | if(m_trailings[i].pos_type == POSITION_TYPE_BUY)
|
| | | {
|
| | | const double stoplosnuevo = m_trailings[i].price_base + (atr_val * m_atr_mul);
|
| | | const double s_temp = bid - m_trade_stops_levels;
|
| | |
|
| | | if(stoplosnuevo >= s_temp)
|
| | | continue;
|
| | |
|
| | | ResetLastError();
|
| | | if(!PositionSelectByTicket(m_trailings[i].ticket))
|
| | | {
|
| | | #ifndef GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | FastLog(FUNCION_ACTUAL, ERROR_TEXT,
|
| | | StringFormat("Error inselecting position = %I64u, last err = %d", m_trailings[i].ticket, ::GetLastError()));
|
| | | #endif // GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | continue;
|
| | | }
|
| | |
|
| | | if(!m_trade.PositionModify(m_trailings[i].ticket, stoplosnuevo, PositionGetDouble(POSITION_TP)))
|
| | | {
|
| | | #ifndef GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | FastLog(FUNCION_ACTUAL, ERROR_TEXT,
|
| | | StringFormat("Error in modify position [BUY] = %I64u, new stop = %.*f, curr price = %.*f",
|
| | | m_trailings[i].ticket, m_digits, stoplosnuevo, m_digits, bid));
|
| | | #endif // GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | continue;
|
| | | }
|
| | |
|
| | |
|
| | | //---
|
| | | m_trailings[i].price_base = stoplosnuevo;
|
| | | }
|
| | | else
|
| | | {
|
| | | const double stoplosnuevo = m_trailings[i].price_base - (atr_val * m_atr_mul);
|
| | | const double s_temp = ask + m_trade_stops_levels;
|
| | |
|
| | | if(stoplosnuevo <= s_temp)
|
| | | continue;
|
| | |
|
| | | ResetLastError();
|
| | | if(!PositionSelectByTicket(m_trailings[i].ticket))
|
| | | {
|
| | | #ifndef GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | FastLog(FUNCION_ACTUAL, ERROR_TEXT,
|
| | | StringFormat("Error inselecting position = %I64u, last err = %d", m_trailings[i].ticket, ::GetLastError()));
|
| | | #endif // GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | continue;
|
| | | }
|
| | |
|
| | | if(!m_trade.PositionModify(m_trailings[i].ticket, stoplosnuevo, PositionGetDouble(POSITION_TP)))
|
| | | {
|
| | | #ifndef GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | FastLog(FUNCION_ACTUAL, ERROR_TEXT,
|
| | | StringFormat("Error in modify position [SELL] = %I64u, new stop = %.*f, curr price = %.*f",
|
| | | m_trailings[i].ticket, m_digits, stoplosnuevo, m_digits, ask));
|
| | | #endif // GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
|
| | | continue;
|
| | | }
|
| | |
|
| | | //---
|
| | | m_trailings[i].price_base = stoplosnuevo;
|
| | | }
|
| | | }
|
| | | }
|
| | | //---
|
| | | }
|
2026-10-24 12:47:47 -05:00 | | | #endif // MQLARTICLES_POSMGMT_TSL_INT_ATR_MQH
|
2026-10-24 11:58:18 -05:00 | | | //+------------------------------------------------------------------+
|