Advertisement
JustUncleL

DVstdDev

Jul 26th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. //@version=3
  2.  
  3. study(title="Daily Historical Volatility StdDev Levels", shorttitle="HVStdDev", overlay = true)
  4.  
  5. //
  6. // Author : JustUncleL
  7. //
  8. // Description:
  9. // Plots Daily Standard deviation levels on price chart based on Historical Volatility.
  10. // The following uses the most common approach – calculating historical volatility as
  11. // standard deviation of logarithmic returns, based on daily closing/settlement prices.
  12. // Assets: Any Currency, Commodities also on stocks, some indices.
  13. // Time Frames: 5min to 60min.
  14. // This will also work on Daily Chart, by setting "DaystoExpire" to 21
  15. //
  16. // References:
  17. // - How To Use Standard Deviation In Your Trading Day
  18. // https://www.youtube.com/watch?v=i28w9HBmJdQ&feature=youtu.be
  19. // - Deviation Levels Indicator
  20. // https://www.youtube.com/watch?v=EjHDNIKQH5k&feature=youtu.be
  21. // - http://www.macroption.com/historical-volatility-calculation/
  22. // - Historical Volatility based Standard Deviation_V2 by ucsgears
  23. // - Historical Volatility Strategy by Hpotter
  24. //
  25. // Reviosions:
  26. //
  27. // 30-July-2017 : Original
  28. //
  29. //
  30.  
  31. // === INPUTS
  32. useDaily = input(true,title="Use Daily Data to Calculate HV, otherwise chart TF")
  33. //startDay = input("1700-1705",title="Time for Start of New Days Session")
  34. LookBack = input(21, minval=1)
  35. annual = input(252,minval=1)
  36. DaystoExpire_ = input (defval=0, minval=0, title="Calender Days to Expiry (0=Auto, default)")
  37. src = input(close,title="Settlement Source (close=default)")
  38. sLength_ = input(1,minval=1,title="Settlement Volume Weighted Average Length (1=Use end of day)")
  39. //
  40. stddev1 =input(true,title="Standard Deviation 1")
  41. stddev2 =input(true,title="Standard Deviation 2")
  42. stddev3 =input(false,title="Standard Deviation 3")
  43. //
  44. // === /INPUTs
  45.  
  46. //
  47. // Function to test Bar time in Range
  48. timeinrange(res, sess) => not na(time(res, sess))
  49.  
  50. // Test for new Daily Session or start of new month for Daily.
  51. start = security(tickerid,"D", time)
  52. newDay = isintraday ? hour(time)==hour(start) and minute(time)==minute(start) : dayofmonth(time)<dayofmonth(time[1])
  53. //newDay = isintraday ? timeinrange("1",startDay) : dayofmonth(time)<dayofmonth(time[1])
  54.  
  55. // Calculate Annualised Volatility
  56. sLength = isintraday ? sLength_ : 1
  57. hv = 0.0
  58. hv_ = useDaily? security(tickerid,"D", stdev(log(src / src[1]), LookBack) * sqrt(annual)) : stdev(log(src / src[1]), LookBack) * sqrt(annual)
  59. hv := newDay ? hv_ : nz(hv[1],hv_)
  60.  
  61. // get the Daily Settlement
  62. settlement = sLength==1? src[1] : vwma(src[1],sLength)
  63. settlement := newDay ? settlement : nz(settlement[1])
  64.  
  65. //--- debug
  66. //plotshape(hv_,location=location.bottom)
  67. //plotshape(hv,location=location.bottom)
  68. //plotshape(newDay,location=location.bottom)
  69. //--- /debug
  70.  
  71. //
  72. // Calculate STDdev over life of the option (generally this is one day for 5min to 60min TFs,
  73. // and 21 days for Daily TF)
  74. stdhv = 0.0
  75. DaystoExpire = DaystoExpire_==0? isintraday? 1 : 21 : DaystoExpire_
  76. stdhv := newDay? settlement*hv*sqrt(DaystoExpire/annual) : nz(stdhv[1])
  77.  
  78. // calculate StdDev lines ratios.
  79. stdhv05 = stdhv * 0.5
  80. stdhv07 = stdhv * 0.75
  81. stdhv1 = stdhv
  82. stdhv15= stdhv*1.5
  83. stdhv2 = stdhv*2.0
  84. stdhv3 = stdhv*3.0
  85.  
  86. // Plot the StdDev Levels.
  87. SettleM = plot(stddev1 ? settlement:na, color = newDay?na:purple, title = "Settlement",linewidth=4, transp=40)
  88. Stdhv05u = plot(stddev1 ? (settlement+stdhv05):na, color = newDay?na:orange, title = "+0.5 Stdev",linewidth=1, transp=20)
  89. Stdhv05d = plot(stddev1 ? (settlement-stdhv05):na, color = newDay?na:orange, title = "-0.5 Stdev",linewidth=1, transp=20)
  90. Stdhv07u = plot(stddev1 ? (settlement+stdhv07):na, color = newDay?na:red, title = "+0.75 Stdev",linewidth=1, transp=20)
  91. Stdhv07d = plot(stddev1 ? (settlement-stdhv07):na, color = newDay?na:red, title = "+0.75 Stdev",linewidth=1, transp=20)
  92. Stdhv1u = plot(stddev1 ? (settlement+stdhv1):na, color = newDay?na:lime, title = "+1 Stdev",linewidth=2, transp=20)
  93. Stdhv1d = plot(stddev1 ? (settlement-stdhv1):na, color = newDay?na:lime, title = "-1 Stdev",linewidth=2, transp=20)
  94. Stdhv15u = plot(stddev1 ? (settlement+stdhv15):na, color = newDay?na:green, title = "+1.5 Stdev",linewidth=1, transp=20)
  95. Stdhv15d = plot(stddev1 ? (settlement-stdhv15):na, color = newDay?na:green, title = "-1.5 Stdev",linewidth=1, transp=20)
  96. Stdhv2u = plot(stddev2 ? (settlement+stdhv2):na, color = newDay?na:blue, title = "+2 Stdev",linewidth=2, transp=20)
  97. Stdhv2d = plot(stddev2 ? (settlement-stdhv2):na, color = newDay?na:blue, title = "-2 Stdev",linewidth=2, transp=20)
  98. Stdhv3u = plot(stddev3 ? (settlement+stdhv3):na, color = newDay?na:gray, title = "+3 Stdev",linewidth=2, transp=20)
  99. Stdhv3d = plot(stddev3 ? (settlement-stdhv3):na, color = newDay?na:gray, title = "-3 Stdev",linewidth=2, transp=20)
  100.  
  101. // Colour in between levels.
  102. fill(Stdhv3d,Stdhv2d,color=gray,transp=95,title="-3 Stdev Fill")
  103. fill(Stdhv3u,Stdhv2u,color=gray,transp=95,title="+3 Stdev Fill")
  104. fill(Stdhv2d,Stdhv15d,color=blue,transp=95,title="-2 Stdev Fill")
  105. fill(Stdhv2u,Stdhv15u,color=blue,transp=95,title="+2 Stdev Fill")
  106. fill(Stdhv15d,Stdhv1d,color=green,transp=95,title="-1.5 Stdev Fill")
  107. fill(Stdhv15u,Stdhv1u,color=green,transp=95,title="+1.5 Stdev Fill")
  108. fill(Stdhv1d,Stdhv07d,color=lime,transp=95,title="-1 Stdev Fill")
  109. fill(Stdhv1u,Stdhv07u,color=lime,transp=95,title="+1 Stdev Fill")
  110. fill(Stdhv07d,Stdhv05d,color=red,transp=95,title="-0.75 Stdev Fill")
  111. fill(Stdhv07u,Stdhv05u,color=red,transp=95,title="+0.75 Stdev Fill")
  112. fill(Stdhv05d,SettleM,color=purple,transp=92,title="-0.5 Stdev Fill")
  113. fill(Stdhv05u,SettleM,color=purple,transp=92,title="+0.5 Stdev Fill")
  114.  
  115. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement