Advertisement
Guest User

Downside upside volatility

a guest
Feb 25th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Motgench
  3.  
  4. //@version=4
  5. study("Pig Downside Upside Volatility",precision=2)
  6.  
  7. sd(src, length) =>
  8. deviation = src - sma(src, length)
  9. variance = sum(pow(deviation, 2), length) / (length)
  10. sd=sqrt(variance)
  11.  
  12. Annual = input(365, "Annual")
  13. n = input(30, "Length")
  14. xPrice = log(close/nz(close[1]))
  15. d = 0.0
  16.  
  17. if xPrice < 0
  18. d := log(close/nz(close[1]))
  19.  
  20.  
  21. dhv = sd(d,n)* sqrt(Annual) * 100
  22.  
  23. u = 0.0
  24. if xPrice > 0
  25. u := log(close/nz(close[1]))
  26.  
  27. uhv = sd(u,n)*sqrt(Annual)*100
  28.  
  29. nethv = uhv - dhv
  30. len = input(30,title="band length")
  31. sdnet = sd(nethv,len)
  32. sma = sma(nethv,len)
  33. upsd = sma + 1.96*sdnet
  34. downsd = sma - 1.96*sdnet
  35. Color = nethv > 0 ? color.green : color.red
  36. sn = input(false,"Show net volatility")
  37. sb = input(false,"Show band")
  38. plot(sn? na : dhv,color= color.red,title="downside volatility")
  39. plot(sn?na:uhv,color=color.green,title = "upside volatility")
  40. plot(sn?nethv:na,color=Color,title = "net volatility",style=plot.style_columns)
  41. hline(0)
  42. plot(sb and sn?upsd:na,color=color.aqua,title="upband")
  43. plot(sb and sn?downsd:na,color=color.aqua,title="downband")
  44. plot(sn?sma:na,color=color.white,title="mean")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement