Advertisement
Guest User

High Frequency Historical Volatility

a guest
Dec 7th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. //@version=4
  2. study("High Frequency Historical Volatility (2)",precision=2)
  3. bgcolor(#000000c0)
  4. Annual = input(365,title = "Trading Days in one Year")
  5. Month = input(30,title = "Trading Days in one Month")
  6. // Security
  7. reso = input(defval="5", title="Historical Volatility Sampling Resolution")
  8.  
  9. //High Frequency Historical Volatility
  10. m = input(288, minval=1,title="Number of sampling returns per bar") //Default = 24*60(minutes in one bar(1 day))/5 (sampling resolution) = 288
  11. k = Annual*m
  12. n = Month*(m+1)
  13. //n = (Month-1)*(m+1)
  14. change = log(close / nz(close[1]))
  15. variance = sum(pow(change, 2), m)
  16. v = security(syminfo.tickerid, reso, variance)
  17. c2 = log(open/nz(close[1])) //Gap
  18.  
  19.  
  20. Hv = sqrt((k/n) * (sum(v+pow(c2,2),Month)))*100
  21.  
  22.  
  23. //Historical Volatility Percentile coloring
  24. malen = input (30, "Ma Length")
  25. sma = input(false, "Plot Moving Average")
  26. Columns = input(false,"Style Columns")
  27. ma= sma(Hv, malen)
  28. Hvp = percentrank(Hv, Annual)
  29.  
  30. Color= Hvp >= 95 ? color.new(color.red,0): Hvp >= 80 ? color.new(color.orange,0) : Hvp >= 70 ? color.new(color.yellow,0) : Hvp >= 30 ? color.new(color.blue,0) : Hvp >= 10 ? color.new(color.aqua,0) : Hvp >= 5 ?color.new(color.silver,0) : color.new(color.white,0)
  31.  
  32.  
  33.  
  34.  
  35. plot(Hv, color=Color,transp=0,linewidth=3,style= Columns?plot.style_columns : plot.style_line)
  36. plot(sma?ma:na, color=color.lime, transp=0, linewidth=2)
  37. hline(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement