Value At Risk Channel [AstrideUnicorn]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AstrideUnicorn

//@version=5

indicator(title ="Value At Risk Channel",  shorttitle="VaR Channel" , timeframe = "D", overlay = true)

// Script inputs
Percentile = input(5.0 , title = "Probability")
Window = input(200, title = "Window")

// Calculate the reurns
Returns = (close - close[1])/close[1]

// Calculate the calues of VaR - positive and negative returns in the percentiles
// determined by the Percentile parameter
VarLower = ta.percentile_linear_interpolation(Returns, Window, Percentile)
VarUpper = ta.percentile_linear_interpolation(Returns, Window, 100 - Percentile)

// Calculate the upper and lower lines of the VaR Channel
LowerLine = close*(1+VarLower)
UpperLine = close*(1+VarUpper)

// Plot upper and lower lines
plot(LowerLine, style = plot.style_stepline, linewidth = 3, title = "Upper line", color = color.blue)
plot(UpperLine, style = plot.style_stepline, linewidth = 3, title = "Lower line", color = color.blue)





