OPEN-SOURCE SCRIPT
Calculadora Stop Promedio MNQ (10 Velas)

//version=5
indicator("Calculadora Stop Promedio MNQ (10 Velas) - Corregido", overlay=true, max_lines_count=50, max_labels_count=50)
// ---------- Parámetros ----------
len = input.int(10, "Velas para promedio", minval=1)
tick_size = input.float(0.25, "Tick size (MNQ = 0.25)")
show_label = input.bool(true, "Mostrar etiqueta")
show_line = input.bool(true, "Mostrar línea")
// ---------- Protección: esperar suficientes barras ----------
enoughBars = bar_index >= (len - 1)
// ---------- Cálculo stop promedio (alto - bajo) ----------
var float stopPromedio = na
if enoughBars
float suma = 0.0
for i = 0 to len - 1
suma += high - low
stopPromedio := suma / len
else
stopPromedio := na
// ---------- Conversion a ticks ----------
stopTicks = not na(stopPromedio) ? stopPromedio / tick_size : na
// ---------- Nivel donde se colocaría el stop (visual) ----------
yStop = not na(stopPromedio) ? close - stopPromedio : na
// ---------- Crear / actualizar línea ----------
// Usamos xloc.bar_index para coordenadas por barras
var line stopLine = na
if show_line and not na(yStop)
if na(stopLine)
stopLine := line.new(x1 = bar_index - 1, y1 = yStop, x2 = bar_index, y2 = yStop, xloc = xloc.bar_index, extend = extend.none, color = color.red, width = 2)
else
line.set_xy1(stopLine, bar_index - 1, yStop)
line.set_xy2(stopLine, bar_index, yStop)
line.set_color(stopLine, color.red)
else
// Si ocultamos o no hay yStop, borramos línea si existe
if not na(stopLine)
line.delete(stopLine)
stopLine := na
// ---------- Crear / actualizar etiqueta ----------
var label stopLabel = na
if show_label and not na(yStop) and enoughBars
txt = "Stop promedio: " + str.tostring(stopTicks, format.volume) + " ticks"
if na(stopLabel)
stopLabel := label.new(x = bar_index, y = yStop, text = txt, xloc = xloc.bar_index, style = label.style_label_left, color = color.new(color.red, 0), textcolor = color.white, size = size.small)
else
label.set_xy(stopLabel, bar_index, yStop)
label.set_text(stopLabel, txt)
else
if not na(stopLabel)
label.delete(stopLabel)
stopLabel := na
// ---------- Output en Data Window (opcional) ----------
plotchar(stopPromedio, title="Stop promedio (price units)", char=' ', display=display.none)
plotchar(stopTicks, title="Stop (ticks)", char=' ', display=display.none)
indicator("Calculadora Stop Promedio MNQ (10 Velas) - Corregido", overlay=true, max_lines_count=50, max_labels_count=50)
// ---------- Parámetros ----------
len = input.int(10, "Velas para promedio", minval=1)
tick_size = input.float(0.25, "Tick size (MNQ = 0.25)")
show_label = input.bool(true, "Mostrar etiqueta")
show_line = input.bool(true, "Mostrar línea")
// ---------- Protección: esperar suficientes barras ----------
enoughBars = bar_index >= (len - 1)
// ---------- Cálculo stop promedio (alto - bajo) ----------
var float stopPromedio = na
if enoughBars
float suma = 0.0
for i = 0 to len - 1
suma += high - low
stopPromedio := suma / len
else
stopPromedio := na
// ---------- Conversion a ticks ----------
stopTicks = not na(stopPromedio) ? stopPromedio / tick_size : na
// ---------- Nivel donde se colocaría el stop (visual) ----------
yStop = not na(stopPromedio) ? close - stopPromedio : na
// ---------- Crear / actualizar línea ----------
// Usamos xloc.bar_index para coordenadas por barras
var line stopLine = na
if show_line and not na(yStop)
if na(stopLine)
stopLine := line.new(x1 = bar_index - 1, y1 = yStop, x2 = bar_index, y2 = yStop, xloc = xloc.bar_index, extend = extend.none, color = color.red, width = 2)
else
line.set_xy1(stopLine, bar_index - 1, yStop)
line.set_xy2(stopLine, bar_index, yStop)
line.set_color(stopLine, color.red)
else
// Si ocultamos o no hay yStop, borramos línea si existe
if not na(stopLine)
line.delete(stopLine)
stopLine := na
// ---------- Crear / actualizar etiqueta ----------
var label stopLabel = na
if show_label and not na(yStop) and enoughBars
txt = "Stop promedio: " + str.tostring(stopTicks, format.volume) + " ticks"
if na(stopLabel)
stopLabel := label.new(x = bar_index, y = yStop, text = txt, xloc = xloc.bar_index, style = label.style_label_left, color = color.new(color.red, 0), textcolor = color.white, size = size.small)
else
label.set_xy(stopLabel, bar_index, yStop)
label.set_text(stopLabel, txt)
else
if not na(stopLabel)
label.delete(stopLabel)
stopLabel := na
// ---------- Output en Data Window (opcional) ----------
plotchar(stopPromedio, title="Stop promedio (price units)", char=' ', display=display.none)
plotchar(stopTicks, title="Stop (ticks)", char=' ', display=display.none)
Script de código abierto
Siguiendo fielmente el espíritu de TradingView, el creador de este script lo ha publicado en código abierto, permitiendo que otros traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Puede utilizarlo de forma gratuita, pero tenga en cuenta que la publicación de este código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.
Script de código abierto
Siguiendo fielmente el espíritu de TradingView, el creador de este script lo ha publicado en código abierto, permitiendo que otros traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Puede utilizarlo de forma gratuita, pero tenga en cuenta que la publicación de este código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.