Hola,
Soy MasterComander, aunque de "master" no tengo mucho.
Estoy trabajando en un script y necesito corregirlo, para que me permita ejecutar una alerta sobre el trailing stop que he implementado en la estrategia, justo en el momento en que aparezca en el gráfico, además de cubrir otros eventos relevantes.
He revisado toda la información oficial de Pine Script, visto videos en YouTube y consultado diversos recursos, pero sigo sin poder resolver los errores que me están bloqueando el avance del proyecto.
Agradecería vuestra ayuda y colaboración.
------------------------------
Errores de código:
1. El contador de Short y su cierre por trailing stop o stop loss (ambos basados en un multiplicador de ATR) siempre es #1. Por tanto, tengo errores en el calculo del contador.
2. No logra crear un "comentario" en el signal label que aparece en el grafico en el momento del cierre, que indique si dicho cierre es por trailing stop o por stop loss. Para ambos casos el comentario indica "Trailing".
3. ["Mas importante"] Se ejecuta la alerta del trailing stop o del stop loss cuando la vela se ha cerrado y no cuando sucede realmente, es decir, cuando se visualiza en el grafico (flecha morada). Las alertas de Short, Long y cierres tipo Cross (operación contraria a la anterior) se ejecutan perfectamente en el instante en que sucede la operación.
----------------------
Código con incidentes:
// === CONTADORES ===
var int longCount = 0, shortCount = 0
var int longEnCurso = na
var int shortEnCurso = na
var int longCerrado = na
var int shortCerrado = na
// === EJECUCIÓN ===
if longCond and strategy.position_size <= 0 and evitarDoble
if strategy.position_size < 0
shortCerrado := shortEnCurso
strategy.close("SHORT", comment="Cross SHORT #" + str.tostring(shortCerrado))
alert("CROSS SHORT #" + str.tostring(shortCerrado), alert.freq_once_per_bar)
longCount += 1
longEnCurso := longCount
strategy.entry("LONG", strategy.long, comment="LONG #" + str.tostring(longCount) + " | " + labelLong)
alert(" LONG #" + str.tostring(longCount), alert.freq_once_per_bar)
if shortCond and strategy.position_size >= 0 and evitarDoble
if strategy.position_size > 0
longCerrado := longEnCurso
strategy.close("LONG", comment="Cross LONG #" + str.tostring(longCerrado))
alert("CROSS LONG #" + str.tostring(longCerrado), alert.freq_once_per_bar)
shortCount += 1
shortEnCurso := shortCount
strategy.entry("SHORT", strategy.short, comment="SHORT #" + str.tostring(shortCount) + " | " + labelShort)
alert(" SHORT #" + str.tostring(shortCount), alert.freq_once_per_bar)
// === ALERTA DE TRAILING SOLO UNA VEZ POR POSICIÓN ===
var bool trailingAlertaEmitidaLong = false
var bool trailingAlertaEmitidaShort = false
if activarTrailing
// LONG
if strategy.position_size > 0 and not trailingAlertaEmitidaLong
trailingAlertaEmitidaLong := true
trailingAlertaEmitidaShort := false
alert(" TRAILING activado (solo alerta): LONG #" + str.tostring(longEnCurso), alert.freq_once_per_bar)
// SHORT
if strategy.position_size < 0 and not trailingAlertaEmitidaShort
trailingAlertaEmitidaShort := true
trailingAlertaEmitidaLong := false
alert(" TRAILING activado (solo alerta): SHORT #" + str.tostring(shortEnCurso), alert.freq_once_per_bar)
// RESET si no hay posición
if strategy.position_size == 0
trailingAlertaEmitidaLong := false
trailingAlertaEmitidaShort := false
// === ALERTAS DE CIERRE TRAILING (Solo al ejecutarse el trailing) ===
cerrado = strategy.closedtrades > 0 and strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index
if cerrado
comentario = strategy.closedtrades.exit_comment(strategy.closedtrades - 1)
precio = strategy.closedtrades.exit_price(strategy.closedtrades - 1)
if str.contains(comentario, "Trailing LONG")
alert(" TRAILING ejecutado: LONG #" + str.tostring(longCerrado) + " | Precio: " + str.tostring(precio), alert.freq_once_per_bar)
if str.contains(comentario, "Trailing SHORT")
alert(" TRAILING ejecutado: SHORT #" + str.tostring(shortCerrado) + " | Precio: " + str.tostring(precio), alert.freq_once_per_bar)
Error Alertas Trailing Stop
El espacio del Foro donde compartir indicadores y estrategias creados con el lenguaje de la plataforma TradingView.
-
- Mensajes: 1
- Registrado: 16 Abr 2025 16:57
Ir a
- Los Mercados
- ↳ Acciones Españolas
- ↳ Acciones Extranjeras
- ↳ Futuros y Opciones
- ↳ Forex
- ↳ Criptomonedas
- ↳ Renta Fija
- Las Técnicas
- ↳ Trading en General
- ↳ Sistemas de Trading
- ↳ Diarios de Trading
- ↳ Análisis Económico y Fundamental
- ↳ Psicología y Trading
- Plataformas de Trading
- ↳ Metatrader 4
- ↳ Expert Advisors
- ↳ Indicadores
- ↳ Scripts
- ↳ Metatrader 5
- ↳ Expert Advisors
- ↳ Indicadores
- ↳ Scripts
- ↳ Ninja Trader
- ↳ Estrategias
- ↳ Indicadores
- ↳ TradeStation
- ↳ Estrategias
- ↳ Indicadores
- ↳ PaintBars
- ↳ ShowMes
- ↳ Funciones
- ↳ MultiCharts
- ↳ Estrategias
- ↳ Indicadores
- ↳ Pine Script
- ↳ Expert Advisor Studio
- ↳ Python
- ↳ R
- ↳ ProRealTime
- ↳ Sistemas
- ↳ Indicadores
- ↳ Screeners
- ↳ Visual Chart
- ↳ Sistemas
- ↳ Indicadores
- ↳ Estudios
- ↳ AmiBroker
- ↳ Sistemas
- ↳ Indicadores
- ↳ Exploradores
- ↳ Comentarios
- ↳ Funciones
- ↳ Metastock
- ↳ Sistemas
- ↳ Indicadores
- ↳ Exploradores
- ↳ Asesores Expertos
- ↳ cTrader
- ↳ cBots
- ↳ Indicadores
- ↳ JForex
- ↳ Software
- Recursos para Traders
- ↳ Brokers
- ↳ Fiscalidad
- ↳ Data Feeds e Históricos
- ↳ Libros y Revistas de Trading
- ↳ Tutoriales
- ↳ Artículos Interesantes
- ↳ Páginas Web
- ↳ Cursos y Servicios
- ↳ Vídeos y Webinars
- Comunidad X-Trader.net
- ↳ Novedades de la Web
- ↳ Site Feedback
- ↳ Eventos
- ↳ 10 Años de X-Trader.net
- ↳ 20 Años de X-Trader.net
- ↳ Clasificados