2026-10-24 11:58:18 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| Fixed.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_FIXED_MQH
|
| | | #define MQLARTICLES_POSMGMT_TSL_INT_FIXED_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 CTrailingStopFixed::SetVariables |
|
| | | //+------------------------------------------------------------------+
|
| | | #define TSNMQLA_TSL_FIXED_PARAM_START 0 // int: puntos para empezar
|
| | | #define TSNMQLA_TSL_FIXED_PARAM_STEP 1 // int: paso en puntos
|
2026-11-05 17:23:04 -05:00 | | | #define TSNMQLA_TSL_FIXED_NPARAMS 2 // numero de parametros
|
2026-10-24 12:47:47 -05:00 | | |
|
2026-10-24 11:58:18 -05:00 | | | //+------------------------------------------------------------------+
|
| | | //| |
|
| | | //+------------------------------------------------------------------+
|
| | | 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:
|
2026-10-24 17:50:41 -05:00 | | | CTrailingStopFixed()
|
2026-11-05 17:23:04 -05:00 | | | : CTrailingStop(TSNMQLA_TSL_FIXED_NPARAMS, TSL_EXECUTION_ON_TICK, TRAILING_STOP_MODE_BASIC_POINTS) { }
|
2026-10-24 11:58:18 -05:00 | | | ~CTrailingStopFixed() {}
|
| | |
|
| | | //---
|
| | | void TrailingStop(void) override final;
|
| | |
|
| | | //---
|
| | | bool SetVariables(const MqlParam ¶ms[]) override final;
|
| | | };
|
| | |
|
2026-10-24 17:50:41 -05:00 | | | TSNMQLA_TRAILINGSTOP_REGISTER_CREATOR(CTrailingStopFixed, SimpleFixedPoints, TRAILING_STOP_MODE_BASIC_POINTS)
|
| | |
|
2026-10-24 11:58:18 -05:00 | | | //+------------------------------------------------------------------+
|
| | | bool CTrailingStopFixed::SetVariables(const MqlParam & params[])
|
| | | {
|
| | | //---
|
2026-10-24 12:47:47 -05:00 | | | m_start = (int)params[TSNMQLA_TSL_FIXED_PARAM_START].integer_value;
|
| | | m_step = (int)params[TSNMQLA_TSL_FIXED_PARAM_STEP].integer_value;
|
2026-10-24 11:58:18 -05:00 | | | 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
|
2026-11-06 10:40:44 -05:00 | | | return CErrorTrace::FailInit<CBasicTSLFormater>(TSN_TRAILING_TYPES_BASIC_ERR_FIXED_STEP_LOWER_STOPLEVEL, m_step);
|
2026-10-24 11:58:18 -05:00 | | | }
|
| | |
|
| | | //---
|
| | | return true;
|
| | | }
|
| | |
|
| | | //+------------------------------------------------------------------+
|
| | | void CTrailingStopFixed::Add(ulong ticket, double open_price, ENUM_POSITION_TYPE pos_type, double sl_price)
|
| | | {
|
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].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
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | //---
|
2026-10-24 12:47:47 -05:00 | | | #endif // MQLARTICLES_POSMGMT_TSL_INT_FIXED_MQH
|