forked from nique_372/MQLArticles
//+------------------------------------------------------------------+
| |
//| Atr.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_ATR_MQH
| |
#define MQLARTICLES_POSMGMT_TSL_INT_ATR_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "Def.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| 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
| |
#define TSNMQLA_TSL_ATR_NPARAMS 4 // numero de parametros
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| Clase derivada para Trailing Stop basado en ATR |
| |
//+------------------------------------------------------------------+
| |
class CTrailingStopATR : public CTrailingStop
| |
{
| |
private:
| |
double m_atr_mul;
| |
CAtrOptimized* m_atr;
| |
| |
//---
| |
void Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price) override final;
| |
| |
public:
| |
// Constructor: inicializa con valores por defecto
| |
CTrailingStopATR()
| |
: CTrailingStop(TSNMQLA_TSL_ATR_NPARAMS, TSL_EXECUTION_ON_TICK, TRAILING_STOP_MODE_BASIC_ATR),
| |
m_atr_mul(1.5) {}
| |
| |
//---
| |
bool SetVariables(const MqlParam ¶ms[]) override final;
| |
| |
//---
| |
void TrailingStop() override final;
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
TSNMQLA_TRAILINGSTOP_REGISTER_CREATOR(CTrailingStopATR, SimpleAtr, TRAILING_STOP_MODE_BASIC_ATR)
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
void CTrailingStopATR::Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price)
| |
{
| |
//---
| |
const double atr_val = m_atr.GetAtrValue(TimeCurrent());
| |
TSNMQLA_TRAILING_CHECK_R
| |
| |
//---
| |
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[])
| |
{
| |
//---
| |
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;
| |
m_atr = CAtrEmbCreator::Create(m_timeframe, m_symbol, index, atr_p);
| |
| |
//---
| |
if(CheckPointer(m_atr) != POINTER_DYNAMIC)
| |
return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_ATR_INVALID_PTR, ulong(uint(atr_p)) | ulong(uint(index)) << 32);
| |
| |
//---
| |
return true;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
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);
| |
| |
//---
| |
const double atr_val = m_atr.GetAtrValue(TimeCurrent());
| |
| |
//---
| |
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;
| |
}
| |
}
| |
}
| |
//---
| |
}
| |
#endif // MQLARTICLES_POSMGMT_TSL_INT_ATR_MQH
| |
//+------------------------------------------------------------------+
|