forked from nique_372/MQLArticles
//+------------------------------------------------------------------+
| |
//| Fixed.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_FIXED_MQH
| |
#define MQLARTICLES_POSMGMT_TSL_INT_FIXED_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "Def.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| Indices de MqlParam para CTrailingStopFixed::SetVariables |
| |
//+------------------------------------------------------------------+
| |
#define TSNMQLA_TSL_FIXED_PARAM_START 0 // int: puntos para empezar
| |
#define TSNMQLA_TSL_FIXED_PARAM_STEP 1 // int: paso en puntos
| |
#define TSNMQLA_TSL_FIXED_NPARAMS 2 // numero de parametros
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| Trailing Stop by Fixed Points |
| |
//+------------------------------------------------------------------+
| |
class CTrailingStopFixed : public CTrailingStop
| |
{
| |
private:
| |
//---
| |
int m_start;
| |
int m_step;
| |
double m_res;
| |
| |
//---
| |
void Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price) override final;
| |
| |
public:
| |
CTrailingStopFixed()
| |
: CTrailingStop(TSNMQLA_TSL_FIXED_NPARAMS, TSL_EXECUTION_ON_TICK, TRAILING_STOP_MODE_BASIC_POINTS) { }
| |
~CTrailingStopFixed() {}
| |
| |
//---
| |
void TrailingStop(void) override final;
| |
| |
//---
| |
bool SetVariables(const MqlParam ¶ms[]) override final;
| |
};
| |
| |
TSNMQLA_TRAILINGSTOP_REGISTER_CREATOR(CTrailingStopFixed, SimpleFixedPoints, TRAILING_STOP_MODE_BASIC_POINTS)
| |
| |
//+------------------------------------------------------------------+
| |
bool CTrailingStopFixed::SetVariables(const MqlParam & params[])
| |
{
| |
//---
| |
m_start = (int)params[TSNMQLA_TSL_FIXED_PARAM_START].integer_value;
| |
m_step = (int)params[TSNMQLA_TSL_FIXED_PARAM_STEP].integer_value;
| |
m_res = m_point_value * m_step;
| |
| |
//---
| |
if(m_res < m_trade_stops_levels)
| |
{
| |
// Si el paso es mas pequeño que el trade_stop_levels, entonces dara error y no podemos modificar para nada el sl
| |
return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_FIXED_STEP_LOWER_STOPLEVEL, m_step);
| |
}
| |
| |
//---
| |
return true;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
void CTrailingStopFixed::Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price)
| |
{
| |
TSNMQLA_TRAILING_CHECK_R
| |
m_trailings[m_trailings_s].extra = false;
| |
m_trailings[m_trailings_s].open_price = open_price;
| |
m_trailings[m_trailings_s].last_stop_loss_price = sl_price;
| |
m_trailings[m_trailings_s].ticket = ticket;
| |
m_trailings[m_trailings_s].pos_type = pos_type;
| |
m_trailings[m_trailings_s].price_a_superar = pos_type == POSITION_TYPE_BUY ?
| |
open_price + (m_start * m_point_value) : open_price - (m_start * m_point_value);
| |
m_trailings_s++;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
void CTrailingStopFixed::TrailingStop(void)
| |
{
| |
//---
| |
if(m_trailings_s < 1)
| |
return;
| |
| |
//---
| |
const double ask = SymbolInfoDouble(m_symbol, SYMBOL_ASK);
| |
const double bid = SymbolInfoDouble(m_symbol, SYMBOL_BID);
| |
| |
//---
| |
for(int i = 0; i < m_trailings_s; i++)
| |
{
| |
if(m_trailings[i].pos_type == POSITION_TYPE_BUY)
| |
{
| |
if(m_trailings[i].extra) // Si ya se ha superado star
| |
{
| |
//const double s_temp = bid - m_trade_stops_levels; // Miinimo valor de SL
| |
// Creo que no hace falta checker stop levels sabemos que si o si sera menor a m_tick_bid - res (y res es menor a stoplevels)
| |
const double stoplosnuevo = bid - m_res;
| |
if(stoplosnuevo > m_trailings[i].last_stop_loss_price)
| |
{
| |
//--- Seleccion de ticket
| |
#ifndef GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
| |
ResetLastError();
| |
#endif // GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
| |
//---
| |
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].last_stop_loss_price = stoplosnuevo;
| |
}
| |
}
| |
else // No se ha superado
| |
{
| |
if(m_trailings[i].price_a_superar + m_trade_stops_levels >= bid)
| |
continue; // NO lo hizo
| |
m_trailings[i].extra = true; // Se ha superado
| |
}
| |
}
| |
else
| |
{
| |
if(m_trailings[i].extra)
| |
{
| |
// const double s_temp = ask + m_trade_stops_levels;
| |
const double stoplosnuevo = ask + m_res;
| |
if(stoplosnuevo < m_trailings[i].last_stop_loss_price)
| |
{
| |
//--- Seleccion de ticket
| |
#ifndef GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
| |
ResetLastError();
| |
#endif // GENBYLEO_TRAILINGSTOP_DESACTIVE_ERR_LOGS
| |
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].last_stop_loss_price = stoplosnuevo;
| |
}
| |
}
| |
else
| |
{
| |
if(m_trailings[i].price_a_superar - m_trade_stops_levels <= ask)
| |
continue;
| |
m_trailings[i].extra = true; // Se ha superado
| |
}
| |
}
| |
}
| |
}
| |
}
| |
//---
| |
#endif // MQLARTICLES_POSMGMT_TSL_INT_FIXED_MQH
|