Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

IV Rank - thinkScript Chart Study

By: ChrisBaker97 on Sep 17th, 2013  |  syntax: None  |  size: 3.10 KB  |  views: 426  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # IV_Rank - IMPLIED VOLATILITY RANK - Adds a colored label to the chart showing IV Rank
  2. #
  3. # This study simply places a label on the chart showing the current IV
  4. # Rank, otherwise known as IV Percentile. In addition, the label is
  5. # color-coded to hint at the IV Rank, where lower values appear red and
  6. # higher values are green, suggesting greater premium available to be
  7. # sold at a higher IV Rank.
  8. #
  9. # For a more complex presentation of IV Rank in the context of past implied
  10. # volatility, see my other thinkScript: http://pastebin.com/0Vumd8Gt
  11. #
  12. # By: Chris Baker <ChrisBaker97@gmail.com> @ChrisBaker97
  13. # Latest version maintained at: http://pastebin.com/jRDkHvXE
  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. # This version of the IV Rank study adds dynamic color-coding, for quick
  29. # identification of high- or low-IV Rank. The code has also been
  30. # streamlined for faster execution and ease of interpretation. There is
  31. # also no need to specify a period for the study - the entire aggregation
  32. # period on the chart is used.
  33. #
  34. # The user input brightness can range from 0 to 100 and controls the
  35. # intensity of the plot's red-green color scheme. A lower brightness will
  36. # show up better on a light background, and vice versa.
  37.  
  38. input brightness = 80 ;                 # overall brightness of IV display
  39.  
  40. declare upper ;
  41. declare hide_on_intraday ;              # IV shows N/A for intraday aggregations, so we hide it
  42.  
  43. def ivClean = if isNaN(impVolatility()) # if IV data doesn't exist for a particular period ...
  44.     then ivClean[1]                     # ... set it to the previous value so it won't taint the hi/lo
  45.     else impVolatility() * 100 ;
  46.  
  47. def ivHi = HighestAll(ivClean) ;        # highest IV over range
  48. def ivLo =  LowestAll(ivClean) ;        # lowest IV over range
  49.  
  50. def ivRange = ivHi - ivLo ;             # IV range from low to high
  51.  
  52. def ivRank = Round( 100 * (ivClean - ivLo) / ivRange, 1) ;  # IV rank
  53.  
  54. # Define a color level from 0 to 255 based on current IV%
  55. def level = ivRank * 2.55;
  56.  
  57. # Check bounds and convert brightness input to an intensity factor between 0.2 and 1.0
  58. def intensity = if      brightness <   0 then 0.2
  59.                 else if brightness > 100 then 1.0
  60.                 else                          0.2 + (brightness * 0.008) ;
  61.  
  62. # Calculate red and green color levels (modified by intensity) for the color function
  63. def rLvl = intensity * (255 - level) ;
  64. def gLvl = intensity * (level) ;
  65.  
  66. # Add label, colored according to current IV Rank.
  67. AddLabel(yes, "IV Rank: " + ivRank + "%", CreateColor(rLvl, gLvl, 0)) ;