OPEN-SOURCE SCRIPT

Calculadora Stop Promedio MNQ (10 Velas)

87
//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)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.