Advertisement
ChrisBaker97

Implied Volatility w/ IV Rank - thinkScript Chart Study

Oct 23rd, 2013
3,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # IV_with_ivRank - IMPLIED VOLATILITY WITH IV RANK - Plots IV over the chart period with IV rank at right
  2. #
  3. # This combined study shows implied volatility graphically over time, as
  4. # well as the current IV rank (a.k.a IV percentile) at the right of the
  5. # chart. In addition, the chart and its labels are color-coded to hint at
  6. # the IV rank, where lower values appear red and higher values are green,
  7. # suggesting greater premium available to be sold at a higher IV rank.
  8. #
  9. # For a simpler study which only places an IV Rank label on the primary
  10. # chart, see my other thinkScript: http://pastebin.com/jRDkHvXE
  11. #
  12. # By: Chris Baker <ChrisBaker97@gmail.com> @ChrisBaker97
  13. # Latest version maintained at: http://pastebin.com/0Vumd8Gt
  14. # More thinkScripts at: http://pastebin.com/u/ChrisBaker97
  15. #
  16. # Credit goes to Allen Everhart (http://www.smalldoginvestor.com) for the
  17. # original idea and implementation of IV percentile as a chart label.
  18. #
  19. # Portions of this code are derived from the IV_percentile Scan tab
  20. # thinkScript included with the thinkorswim platform by TD Ameritrade.
  21. #
  22. # This thinkScript is designed for use in the Charts tab.
  23. #
  24. # This work is licensed under the Creative Commons Attribution-ShareAlike
  25. # 3.0 Unported License. To view a copy of this license, visit:
  26. # http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
  27. #
  28. # I created this study to provide more context around the current IV rank
  29. # numerical value. While useful, the IV rank simply shows the percentile
  30. # level of implied volatility over the aggregation period, and this can
  31. # be skewed by outliers — high or low IV for a brief period of time. This
  32. # visual depiction shows IV cycles over time, and allows one to get a
  33. # better feel for how the current IV level compares to historical trends.
  34. #
  35. # The user input brightness can range from 0 to 100 and controls the
  36. # intensity of the plot's red-green color scheme. A lower brightness will
  37. # show up better on a light background, and vice versa.
  38.  
  39. input brightness = 80 ; # overall brightness of IV display
  40.  
  41. declare lower ;
  42. declare hide_on_intraday ;
  43.  
  44. def ivClean = if isNaN(impVolatility()) # if IV data doesn't exist for a particular period...
  45. then ivClean[1] # ...set it to the previous value so it won't taint the hi/low
  46. else impVolatility() * 100 ;
  47.  
  48. def ivHi = HighestAll(ivClean) ; # highest IV over range
  49. def ivLo = LowestAll(ivClean) ; # lowest IV over range
  50.  
  51. def ivRange = ivHi - ivLo ; # IV range from low to high
  52.  
  53. def ivRank = Round( 100 * (ivClean - ivLo) / ivRange, 1) ; # IV rank
  54.  
  55. # Define a color level from 0 to 255 based on current IV%
  56. def level = ivRank * 2.55;
  57.  
  58. # Check bounds and convert brightness input to an intensity factor between 0.2 and 1.0
  59. def intensity = if brightness < 0 then 0.2
  60. else if brightness > 100 then 1.0
  61. else 0.2 + (brightness * 0.008) ;
  62.  
  63. # Calculate red and green color levels (modified by intensity) for the color function
  64. def rLvl = intensity * (255 - level) ;
  65. def gLvl = intensity * (level) ;
  66.  
  67. # Plot raw implied volatility, colored according to IV Rank at each point.
  68. plot iv = if isNaN(impVolatility()) # check to make sure we're not out of data ...
  69. then double.NaN # ... and if we are, plot nothing ...
  70. else Round( ivClean, 1) ; # ... else plot the raw IV
  71. iv.AssignValueColor(CreateColor(rLvl, gLvl, 0)) ; # color according to IV Rank
  72. iv.SetLineWeight(2) ;
  73.  
  74. # Plot high and low raw IV bubbles.
  75. AddChartBubble(ivClean == ivHi and ! isNaN(impVolatility()),
  76. ivHi,"IV Hi: " + round(ivHi,1) + "%",Color.LIGHT_GRAY,up = yes) ;
  77. AddChartBubble(ivClean == ivLo and ! isNaN(impVolatility()),
  78. ivLo,"IV Lo: " + round(ivLo,1) + "%",Color.LIGHT_GRAY,up = no) ;
  79.  
  80. # If the chart is set to extend to the right, we need to count how many bars
  81. # there are before we run out of data, in order to properly position the
  82. # IV Rank bubble horizontally.
  83. def maxBars = HighestAll(if isNan(imp_volatility()) then 0 else BarNumber());
  84.  
  85. # Plot a bubble at the last data point, showing the present IV Rank.
  86. AddChartBubble(BarNumber() == maxBars,iv,"IV Rank: " + Round(ivRank,1) + "%",iv.TakeValueColor(),up = yes) ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement