Advertisement
Guest User

HV MAD

a guest
Feb 26th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. //@version=4
  2. study("Pig Historical Volatility Mean Absolute Deviation",precision=2)
  3.  
  4. H = input(title="Historical Volatility", type=input.string, defval="Yang Zhang", options=["Close Centered","Close Zero Mean","Close Absolute Return","Parkinson","Rogers Satchell","Garman Klass","Garman Klass Yang Zhang Extension","EWMA","Yang Zhang"])
  5. Annual = input(365, "Annual")
  6. n = input(30, "Length")
  7. Plen = input (365, "Percentile Length")
  8. malen = input (30, "Ma Length")
  9. xPrice = log(close / close[1])
  10. sma = input(false, "Plot Moving Average")
  11. Columns = input(false,"Style Columns")
  12. PI= 2 * asin(1)
  13. //Percentrank Formula
  14. _percentrank(src, length) =>
  15. count = 0
  16. for i = 1 to length
  17. count := count + (src >= nz(src[i]) ? 1 : 0)
  18. pr = 100 * count / length
  19.  
  20.  
  21. //Close to Close Centered
  22. dev = xPrice - sma(xPrice, n)
  23. mad = sum(abs(dev), n) / (n)
  24. amad=sqrt(Annual*PI/2)*mad
  25. Hv = amad*100
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. //Historical Volatility Percentile
  33. Hvp=_percentrank(Hv, Plen)
  34.  
  35. 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)
  36.  
  37.  
  38. ma= sma(Hv, malen)
  39.  
  40. plot(Hv, color=Color,transp=0,linewidth=2,style= Columns?plot.style_columns : plot.style_line)
  41. plot(sma?ma:na, color=color.lime, transp=0, linewidth=2)
  42. hline(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement