Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # IV_with_ivRank - IMPLIED VOLATILITY WITH IV RANK - Plots IV over the chart period with IV rank at right
- #
- # This combined study shows implied volatility graphically over time, as
- # well as the current IV rank (a.k.a IV percentile) at the right of the
- # chart. In addition, the chart and its labels are color-coded to hint at
- # the IV rank, where lower values appear red and higher values are green,
- # suggesting greater premium available to be sold at a higher IV rank.
- #
- # For a simpler study which only places an IV Rank label on the primary
- # chart, see my other thinkScript: http://pastebin.com/jRDkHvXE
- #
- # By: Chris Baker <ChrisBaker97@gmail.com> @ChrisBaker97
- # Latest version maintained at: http://pastebin.com/0Vumd8Gt
- # More thinkScripts at: http://pastebin.com/u/ChrisBaker97
- #
- # Credit goes to Allen Everhart (http://www.smalldoginvestor.com) for the
- # original idea and implementation of IV percentile as a chart label.
- #
- # Portions of this code are derived from the IV_percentile Scan tab
- # thinkScript included with the thinkorswim platform by TD Ameritrade.
- #
- # This thinkScript is designed for use in the Charts tab.
- #
- # This work is licensed under the Creative Commons Attribution-ShareAlike
- # 3.0 Unported License. To view a copy of this license, visit:
- # http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
- #
- # I created this study to provide more context around the current IV rank
- # numerical value. While useful, the IV rank simply shows the percentile
- # level of implied volatility over the aggregation period, and this can
- # be skewed by outliers — high or low IV for a brief period of time. This
- # visual depiction shows IV cycles over time, and allows one to get a
- # better feel for how the current IV level compares to historical trends.
- #
- # The user input brightness can range from 0 to 100 and controls the
- # intensity of the plot's red-green color scheme. A lower brightness will
- # show up better on a light background, and vice versa.
- input brightness = 80 ; # overall brightness of IV display
- declare lower ;
- declare hide_on_intraday ;
- def ivClean = if isNaN(impVolatility()) # if IV data doesn't exist for a particular period...
- then ivClean[1] # ...set it to the previous value so it won't taint the hi/low
- else impVolatility() * 100 ;
- def ivHi = HighestAll(ivClean) ; # highest IV over range
- def ivLo = LowestAll(ivClean) ; # lowest IV over range
- def ivRange = ivHi - ivLo ; # IV range from low to high
- def ivRank = Round( 100 * (ivClean - ivLo) / ivRange, 1) ; # IV rank
- # Define a color level from 0 to 255 based on current IV%
- def level = ivRank * 2.55;
- # Check bounds and convert brightness input to an intensity factor between 0.2 and 1.0
- def intensity = if brightness < 0 then 0.2
- else if brightness > 100 then 1.0
- else 0.2 + (brightness * 0.008) ;
- # Calculate red and green color levels (modified by intensity) for the color function
- def rLvl = intensity * (255 - level) ;
- def gLvl = intensity * (level) ;
- # Plot raw implied volatility, colored according to IV Rank at each point.
- plot iv = if isNaN(impVolatility()) # check to make sure we're not out of data ...
- then double.NaN # ... and if we are, plot nothing ...
- else Round( ivClean, 1) ; # ... else plot the raw IV
- iv.AssignValueColor(CreateColor(rLvl, gLvl, 0)) ; # color according to IV Rank
- iv.SetLineWeight(2) ;
- # Plot high and low raw IV bubbles.
- AddChartBubble(ivClean == ivHi and ! isNaN(impVolatility()),
- ivHi,"IV Hi: " + round(ivHi,1) + "%",Color.LIGHT_GRAY,up = yes) ;
- AddChartBubble(ivClean == ivLo and ! isNaN(impVolatility()),
- ivLo,"IV Lo: " + round(ivLo,1) + "%",Color.LIGHT_GRAY,up = no) ;
- # If the chart is set to extend to the right, we need to count how many bars
- # there are before we run out of data, in order to properly position the
- # IV Rank bubble horizontally.
- def maxBars = HighestAll(if isNan(imp_volatility()) then 0 else BarNumber());
- # Plot a bubble at the last data point, showing the present IV Rank.
- AddChartBubble(BarNumber() == maxBars,iv,"IV Rank: " + Round(ivRank,1) + "%",iv.TakeValueColor(),up = yes) ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement