Value-at-Risk 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Yuhui_w

//@version=5
indicator("VaR", timeframe = "D")

percentile = input(1.0 , title = "Percentile Threashold")
length = input(250, title = "Length")
in_percent = input(true, title = "Result in Percentage")

rt_formula = close/close[1] - 1

return_ = in_percent ? rt_formula * 100 : rt_formula

VaR = ta.percentile_linear_interpolation(return_, length, percentile)
plot(return_, style = plot.style_histogram, title = "Return")
plot(VaR, style = plot.style_stepline, title = "VaR")

