0% found this document useful (0 votes)
6 views3 pages

Volatilidad Riesgo

The document is a TradingView Pine Script that creates a price range indicator with standard deviations. It calculates log returns and rolling standard deviations over a specified number of days, displaying upper and lower bounds for 68%, 95%, and 99.7% confidence levels. Optional labels can be shown on the most recent bar to indicate these confidence levels.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Volatilidad Riesgo

The document is a TradingView Pine Script that creates a price range indicator with standard deviations. It calculates log returns and rolling standard deviations over a specified number of days, displaying upper and lower bounds for 68%, 95%, and 99.7% confidence levels. Optional labels can be shown on the most recent bar to indicate these confidence levels.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//@version=6

indicator("Rango de Precios con Desviaciones", overlay=true)

// --- Inputs ---


length = input.int(40, minval=1, title="Número de Ruedas (Días)")
show_labels = input.bool(true, title="Mostrar Valores en la Barra Más Reciente")

// --- Cálculos ---


log_returns = math.log(close / close[1]) // Logaritmo de los retornos
rolling_std_dev = ta.stdev(log_returns, length) // Desviación estándar móvil
last_close = close

// Niveles de confianza (1, 2 y 3 desviaciones estándar)


upper_1 = last_close * math.exp(rolling_std_dev)
lower_1 = last_close * math.exp(-rolling_std_dev)

upper_2 = last_close * math.exp(2 * rolling_std_dev)


lower_2 = last_close * math.exp(-2 * rolling_std_dev)

upper_3 = last_close * math.exp(3 * rolling_std_dev)


lower_3 = last_close * math.exp(-3 * rolling_std_dev)

// --- Líneas horizontales dinámicas ---


var line upper_1_line = na
var line lower_1_line = na

var line upper_2_line = na


var line lower_2_line = na

var line upper_3_line = na


var line lower_3_line = na

if na(upper_1_line)
// Crear líneas al iniciar
upper_1_line := line.new(x1=bar_index, y1=upper_1, x2=bar_index + 1, y2=upper_1, color=color.new(color.green, 0),
width=1, extend=extend.right)
lower_1_line := line.new(x1=bar_index, y1=lower_1, x2=bar_index + 1, y2=lower_1, color=color.new(color.green, 0),
width=1, extend=extend.right)

upper_2_line := line.new(x1=bar_index, y1=upper_2, x2=bar_index + 1, y2=upper_2, color=color.new(color.blue, 0),


width=1, extend=extend.right)
lower_2_line := line.new(x1=bar_index, y1=lower_2, x2=bar_index + 1, y2=lower_2, color=color.new(color.blue, 0),
width=1, extend=extend.right)

upper_3_line := line.new(x1=bar_index, y1=upper_3, x2=bar_index + 1, y2=upper_3, color=color.new(color.red, 0),


width=1, extend=extend.right)
lower_3_line := line.new(x1=bar_index, y1=lower_3, x2=bar_index + 1, y2=lower_3, color=color.new(color.red, 0),
width=1, extend=extend.right)
else
// Actualizar líneas en cada barra nueva
line.set_y1(upper_1_line, upper_1)
line.set_y2(upper_1_line, upper_1)
line.set_y1(lower_1_line, lower_1)
line.set_y2(lower_1_line, lower_1)

line.set_y1(upper_2_line, upper_2)
line.set_y2(upper_2_line, upper_2)
line.set_y1(lower_2_line, lower_2)
line.set_y2(lower_2_line, lower_2)
line.set_y1(upper_3_line, upper_3)
line.set_y2(upper_3_line, upper_3)
line.set_y1(lower_3_line, lower_3)
line.set_y2(lower_3_line, lower_3)

// --- Etiquetas opcionales en la última barra ---


if show_labels and bar_index == last_bar_index
label.new(x=bar_index, y=upper_1, text="68%: " + str.tostring(upper_1), color=color.new(color.green, 80),
textcolor=color.white, style=label.style_label_down, yloc=yloc.price)
label.new(x=bar_index, y=lower_1, text="68%: " + str.tostring(lower_1), color=color.new(color.green, 80),
textcolor=color.white, style=label.style_label_up, yloc=yloc.price)

label.new(x=bar_index, y=upper_2, text="95%: " + str.tostring(upper_2), color=color.new(color.blue, 80),


textcolor=color.white, style=label.style_label_down, yloc=yloc.price)
label.new(x=bar_index, y=lower_2, text="95%: " + str.tostring(lower_2), color=color.new(color.blue, 80),
textcolor=color.white, style=label.style_label_up, yloc=yloc.price)

label.new(x=bar_index, y=upper_3, text="99.7%: " + str.tostring(upper_3), color=color.new(color.red, 80),


textcolor=color.white, style=label.style_label_down, yloc=yloc.price)
label.new(x=bar_index, y=lower_3, text="99.7%: " + str.tostring(lower_3), color=color.new(color.red, 80),
textcolor=color.white, style=label.style_label_up, yloc=yloc.price)

You might also like