Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Motgench
- //@version=4
- study("Pig Downside Upside Volatility",precision=2)
- sd(src, length) =>
- deviation = src - sma(src, length)
- variance = sum(pow(deviation, 2), length) / (length)
- sd=sqrt(variance)
- Annual = input(365, "Annual")
- n = input(30, "Length")
- xPrice = log(close/nz(close[1]))
- d = 0.0
- if xPrice < 0
- d := log(close/nz(close[1]))
- dhv = sd(d,n)* sqrt(Annual) * 100
- u = 0.0
- if xPrice > 0
- u := log(close/nz(close[1]))
- uhv = sd(u,n)*sqrt(Annual)*100
- nethv = uhv - dhv
- len = input(30,title="band length")
- sdnet = sd(nethv,len)
- sma = sma(nethv,len)
- upsd = sma + 1.96*sdnet
- downsd = sma - 1.96*sdnet
- Color = nethv > 0 ? color.green : color.red
- sn = input(false,"Show net volatility")
- sb = input(false,"Show band")
- plot(sn? na : dhv,color= color.red,title="downside volatility")
- plot(sn?na:uhv,color=color.green,title = "upside volatility")
- plot(sn?nethv:na,color=Color,title = "net volatility",style=plot.style_columns)
- hline(0)
- plot(sb and sn?upsd:na,color=color.aqua,title="upband")
- plot(sb and sn?downsd:na,color=color.aqua,title="downband")
- plot(sn?sma:na,color=color.white,title="mean")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement