Login     Register

        Contact Us     Search

XLeratorDB/financial-options Documentation

SQL Server Binomial Tree function for equity option implied volatility


BinomialDiscreteDividendsIV

Updated: 15 Dec 2013


Use the scalar function BinomialDiscreteDividendsIV to calculate the implied volatility of American or European option paying discrete dividends using a Cox Ross Rubinstein Binomial as described in Options, Futures, and Other Derivatives, 8th Edition, by John C. Hull.
Syntax
SELECT [wctOptions].[wct].[BinomialDiscreteDividendsIV](
  <@CallPut, nvarchar(4000),>
 ,<@AmEur, nvarchar(4000),>
 ,<@AssetPrice, float,>
 ,<@StrikePrice, float,>
 ,<@TimeToMaturity, float,>
 ,<@RiskFreeRate, float,>
 ,<@DividendRate, float,>
 ,<@Dividend_RangeQuery, nvarchar(max),>
 ,<@Price, float,>
 ,<@NumberOfSteps, int,>)
Arguments
@CallPut
identifies the option as being a call ('C') or a put ('P'). @CallPut is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar.
@AmEur
identifies the option as being American ('A') or European ('E'). @AmEur is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar.
@AssetPrice
the price of the underlying asset. @AssetPrice is an expression of type float or of a type that can be implicitly converted to float.
@StrikePrice
the exercise price of the option. @StrikePrice is an expression of type float or of a type that can be implicitly converted to float.
@TimeToMaturity
the time to expiration of the option, expressed in years. @TimeToMaturity is an expression of type float or of a type that can be implicitly converted to float.
@RiskFreeRate
the annualized, continuously compounded zero-coupon risk-free rate over the life of the option. @RiskFreeRate is an expression of type float or of a type that can be implicitly converted to float.
@DividendRate
the annualized, continuously compounded zero-coupon dividend rate over the life of the option, used in addition to the discrete dividends. @DividendRate is an expression of type float or of a type that can be implicitly converted to float.
@Dividend_RangeQuery
a string containing an SQL statement which, when executed, provides the function with the times and amounts of the dividends to be used in the calculation. The results of the SQL must contain exactly two columns, the first being the time value, as a float or as a value that implicitly converts to float, and the second being the dividend amount as float, or as a value that implicitly converts to float. @Dividend_RangeQuery is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar.
@Price
the price of the option. @Price is an expression of type float or of a type that can be implicitly converted to float.
@NumberOfSteps
the number of steps in the binomial tree. @NumberOfSteps is an expression of type int or of a type that can be implicitly converted to int.
Return Type
float
Remarks
·         @Volatility must be greater than zero (@Volatility > 0).
·         @TimeToMaturity must be greater than zero (@TimeToMaturity > 0).
·         @AssetPrice must be greater than zero (@AssetPrice > 0).
·         @StrikePrice must be greater than zero (@StrikePrice > 0).
·         @NumberOfSteps must be greater than 1 (@NumberOfSteps > 1)
·         Negative time values returned by @Dividend_RangeQuery are ignored.
·         Time values returned by @Dividend_RangeQuery that are greater than @TimeToMaturity are ignored.
·         If @RiskFreeRate is NULL then @RiskFreeRate is set to zero.
·         If @DividendRate is NULL then @DividendRate is set to zero.
·         If @NumberOfSteps is NULL then @NumberOfSteps is set to 100.
·         To calculate the price and/or Greeks of the option use BinomialDiscreteDividends.
·         Use the table-value function BinomialDiscreteDividendsTree to see the underlying price, intrinsic value, and option value for each node on the binomial tree.
·         To calculate the implied volatility using proportional dividends (where the dividend is specified with a time and a percentage rather than a monetary value) use ProportionalDividendsIV.
Examples
Calculate the volatility for an American call option expiring in one year with an underlying asset value of 478, a strike price of 500 and a price of 46.44. The risk-free rate is 2.75%. Dividends will be paid quarterly in the following amounts.

T
Div
0.25
4.00
0.50
4.10
0.75
4.20
1.00
4.30


The number of steps is 100.
SELECT wct.BinomialDiscreteDividendsIV(
       'C'        --Put/Call
      ,'A'        --American/European
      ,478        --Asset Price
      ,500        --Strike Price
      ,1          --Time-to-maturity
      ,.0275      --Risk-free rate
      ,NULL       --Dividend rate
      ,'SELECT *
       FROM (VALUES
            (0.25,4.00),
            (0.50,4.10),
            (0.75,4.20),
            (1.00,4.30)
            )n(T,D)'--Discrete Dividends
      ,46.44      --Price
      ,100        --Number of Steps
      ) as [Volatility]

This produces the following result.
            Volatility
----------------------
              0.299989


In this example we look at how to use dates in the function, using the
YEARFRAC and CALCDATE functions from XLeratorDB/financial. We will use the GETDATE() function as the start date for the calculations. The option expires on November 27th, 2014 with dividends payable on February 6th, May 6th, August 6th, and November 6th, 2014.
SELECT wct.BinomialDiscreteDividendsIV(
       'C'
      ,'A'
      ,478
      ,500
      ,wct.YEARFRAC(GETDATE(),'2014-11-27',NULL)
      ,.0275           
      ,NULL
      ,'SELECT *
       FROM (VALUES
(wct.YEARFRAC(GETDATE(),wct.CALCDATE(2014,2,6),NULL),4.00),
(wct.YEARFRAC(GETDATE(),wct.CALCDATE(2014,5,6),NULL),4.10),
(wct.YEARFRAC(GETDATE(),wct.CALCDATE(2014,8,6),NULL),4.20),
(wct.YEARFRAC(GETDATE(),wct.CALCDATE(2014,11,6),NULL),4.30)
            )n(T,D)'
      ,44.69           
      ,100       
      ) as [Volatility]

This produces the following result on 2013-12-10. You results will be different.
            Volatility
----------------------
              0.299992


In this example, we use 
@date_start as a datetime variable to establish the starting point for calculating the time-to-expiry and @date_start_string as a string variable to be used in @Dividend_RangeQuery.
DECLARE @date_start as datetime = cast('2013-12-10' as datetime)
DECLARE @date_start_string as varchar(max) = '''' + convert(varchar,@date_start,112) + ''''
SELECT wct.BinomialDiscreteDividendsIV(
       'C'
      ,'A'
      ,478
      ,500
      ,wct.YEARFRAC(@date_start,'2014-11-27',NULL)
      ,.0275           
      ,NULL
      ,'SELECT *
       FROM (VALUES
(wct.YEARFRAC(' + @date_start_string + ', wct.CALCDATE(2014,2,6),NULL),4.00),
(wct.YEARFRAC(' + @date_start_string + ', wct.CALCDATE(2014,5,6),NULL),4.10),
(wct.YEARFRAC(' + @date_start_string + ', wct.CALCDATE(2014,8,6),NULL),4.20),
(wct.YEARFRAC(' + @date_start_string + ', wct.CALCDATE(2014,11,6),NULL),4.30)
            )n(T,D)'
      ,44.69           
      ,100       
      ) as Volatility
 
This produces the following result.
            Volatility
----------------------
              0.299992


In this example we put some equity options trades into a table, with a separate table containing the closing price and prices for each ticker and a third table containing the dividend information. This example shows one way to combine the information from multiples tables to calculate the theoretical value of the option. This example holds the risk-free rate constant, which you wouldn't want to do in practice and we will add a discount factor table and calculate the interpolated risk-free rate in another example.
/*Put dividend information into a table*/
SELECT *
INTO #div
FROM (VALUES
 ('ABC','2013-12-15',4)
,('ABC','2014-03-15',4.05)
,('ABC','2014-06-15',4.1)
,('ABC','2014-09-15',4.15)
,('ABC','2014-12-15',4.2)
,('DEF','2014-01-17',1)
,('DEF','2014-04-17',1.05)
,('DEF','2014-07-17',1.1)
,('DEF','2014-10-17',1.15)
,('DEF','2015-01-17',1.2)
,('GHI','2014-02-17',1.25)
,('GHI','2014-05-17',1.3)
,('GHI','2014-08-17',1.35)
,('GHI','2014-11-17',1.4)
,('GHI','2015-02-17',1.45)
)d(ticker,date_div,amt_div)
 
/*Put underlying price information into a table*/
SELECT *
INTO #prices
FROM (VALUES
       ('ABC',495)
      ,('DEF',125)
      ,('GHI',62.5)
      )p(ticker,price)
 
/*put the options into a table*/
SELECT *
INTO #options
FROM (VALUES
       (1,'ABC','2014-08-09',470,65.43,'C')
      ,(2,'ABC','2014-08-20',480,50.03,'P')
      ,(3,'ABC','2014-11-10',490,63.90,'P')
      ,(4,'ABC','2014-09-14',500,55.87,'C')
      ,(5,'DEF','2014-11-14',100,28.13,'C')
      ,(6,'DEF','2014-12-12',110,21.86,'C')
      ,(7,'DEF','2014-08-11',120,9.87,'P')
      ,(8,'DEF','2014-08-26',130,10.07,'C')
      ,(9,'GHI','2014-08-04',50,12.93,'C')
      ,(10,'GHI','2014-12-28',55,8.97,'C')
      ,(11,'GHI','2014-10-09',60,5.40,'P')
      ,(12,'GHI','2014-11-14',65,3.91,'C')
      )o(rn,ticker,expiry,strike,price,z)
 
/*Establish the start date for calculation purposes*/
DECLARE @date_start as datetime = cast('2013-12-10' as datetime)
DECLARE @date_start_string as varchar(max) = '''' + convert(varchar,@date_start,112) + ''''
SELECT A.rn
,A.ticker
,A.z
,B.price as underlying
,A.strike
,A.expiry
,A.price
,wct.BinomialDiscreteDividendsIV(
       A.z
      ,'A'
      ,B.price
      ,A.strike
      ,wct.YEARFRAC(@date_start,A.expiry,NULL)
      ,.0275           
      ,NULL
,'SELECT wct.YEARFRAC(' + @date_start_string + ',date_div,NULL),amt_div FROM #div WHERE ticker = ' + '''' + CAST(A.ticker as varchar(max)) + ''''
      ,A.price         
      ,100
      ) as [Volatility]
FROM #options A
INNER JOIN #prices B
ON A.ticker = B.ticker
 
DROP TABLE #div
DROP TABLE #prices
DROP TABLE #options
This produces the following result, which has been reformatted for presentation purposes.

rn
ticker
z
underlying
strike
expiry
price
Volatility
1
ABC
C
495
470
2014-08-09
65.43
0.349983
2
ABC
P
495
480
2014-08-20
50.03
0.350001
3
ABC
P
495
490
2014-11-10
63.9
0.350019
4
ABC
C
495
500
2014-09-14
55.87
0.34998
5
DEF
C
125
100
2014-11-14
28.13
0.299898
6
DEF
C
125
110
2014-12-12
21.86
0.30009
7
DEF
P
125
120
2014-08-11
9.87
0.300105
8
DEF
C
125
130
2014-08-26
10.07
0.300055
9
GHI
C
62.5
50
2014-08-04
12.93
0.249606
10
GHI
C
62.5
55
2014-12-28
8.97
0.249904
11
GHI
P
62.5
60
2014-10-09
5.4
0.249772
12
GHI
C
62.5
65
2014-11-14
3.91
0.249787



For large volumes of data having many options for a single ticker, it will be more efficient to calculate the time values of the dividends outside of the
BinomialDiscreteDividendIV function and simply use the pre-calculated values in @Dividend_RangeQuery.
/*Put dividend information into a table*/
SELECT *
INTO #div
FROM (VALUES
 ('ABC','2013-12-15',4)
,('ABC','2014-03-15',4.05)
,('ABC','2014-06-15',4.1)
,('ABC','2014-09-15',4.15)
,('ABC','2014-12-15',4.2)
,('DEF','2014-01-17',1)
,('DEF','2014-04-17',1.05)
,('DEF','2014-07-17',1.1)
,('DEF','2014-10-17',1.15)
,('DEF','2015-01-17',1.2)
,('GHI','2014-02-17',1.25)
,('GHI','2014-05-17',1.3)
,('GHI','2014-08-17',1.35)
,('GHI','2014-11-17',1.4)
,('GHI','2015-02-17',1.45)
)d(ticker,date_div,amt_div)
 
/*Put underlying price information into a table*/
SELECT *
INTO #prices
FROM (VALUES
       ('ABC',495)
      ,('DEF',125)
      ,('GHI',62.5)
      )p(ticker,price)
 
/*put the options into a table*/
SELECT *
INTO #options
FROM (VALUES
       (1,'ABC','2014-08-09',470,65.43,'C')
      ,(2,'ABC','2014-08-20',480,50.03,'P')
      ,(3,'ABC','2014-11-10',490,63.90,'P')
      ,(4,'ABC','2014-09-14',500,55.87,'C')
      ,(5,'DEF','2014-11-14',100,28.13,'C')
      ,(6,'DEF','2014-12-12',110,21.86,'C')
      ,(7,'DEF','2014-08-11',120,9.87,'P')
      ,(8,'DEF','2014-08-26',130,10.07,'C')
      ,(9,'GHI','2014-08-04',50,12.93,'C')
      ,(10,'GHI','2014-12-28',55,8.97,'C')
      ,(11,'GHI','2014-10-09',60,5.40,'P')
      ,(12,'GHI','2014-11-14',65,3.91,'C')
      )o(rn,ticker,expiry,strike,price,z)
 
/*Establish the start date for calculation purposes*/
DECLARE @date_start as datetime = cast('2013-12-10' as datetime)
 
/*Put the #div values into another table with date converted to a
 fraction of a year*/
SELECT ticker
,wct.YEARFRAC(@date_start,date_div,NULL) as T
,amt_div as D
INTO #tdiv
FROM #div
 
/*Calculate the implied volatility*/
SELECT A.rn
,A.ticker
,A.z
,B.price as underlying
,A.strike
,A.expiry
,A.price
,wct.BinomialDiscreteDividendsIV(
       A.z
      ,'A'
      ,B.price
      ,A.strike
      ,wct.YEARFRAC(@date_start,A.expiry,NULL)
      ,.0275           
      ,NULL
,'SELECT T,D FROM #tdiv WHERE ticker = ' + '''' + CAST(A.ticker as varchar(max)) + ''''
      ,A.price         
      ,100
      ) as [Volatility]
FROM #options A
INNER JOIN #prices B
ON A.ticker = B.ticker
 
 
DROP TABLE #div
DROP TABLE #prices
DROP TABLE #options
DROP TABLE #tdiv
This produces the following result, which has been reformatted for presentation purposes.

rn
ticker
z
underlying
strike
expiry
price
Volatility
1
ABC
C
495
470
2014-08-09
65.43
0.349983
2
ABC
P
495
480
2014-08-20
50.03
0.350001
3
ABC
P
495
490
2014-11-10
63.9
0.350019
4
ABC
C
495
500
2014-09-14
55.87
0.34998
5
DEF
C
125
100
2014-11-14
28.13
0.299898
6
DEF
C
125
110
2014-12-12
21.86
0.30009
7
DEF
P
125
120
2014-08-11
9.87
0.300105
8
DEF
C
125
130
2014-08-26
10.07
0.300055
9
GHI
C
62.5
50
2014-08-04
12.93
0.249606
10
GHI
C
62.5
55
2014-12-28
8.97
0.249904
11
GHI
P
62.5
60
2014-10-09
5.4
0.249772
12
GHI
C
62.5
65
2014-11-14
3.91
0.249787



In this example, we will use the same inputs as above, with the exception that we will have a table of discount factors and use the
DFINTERP function to convert the discount factors into risk-free rates to be used in the function.
/*Put dividend information into a table*/
SELECT *
INTO #div
FROM (VALUES
 ('ABC','2013-12-15',4)
,('ABC','2014-03-15',4.05)
,('ABC','2014-06-15',4.1)
,('ABC','2014-09-15',4.15)
,('ABC','2014-12-15',4.2)
,('DEF','2014-01-17',1)
,('DEF','2014-04-17',1.05)
,('DEF','2014-07-17',1.1)
,('DEF','2014-10-17',1.15)
,('DEF','2015-01-17',1.2)
,('GHI','2014-02-17',1.25)
,('GHI','2014-05-17',1.3)
,('GHI','2014-08-17',1.35)
,('GHI','2014-11-17',1.4)
,('GHI','2015-02-17',1.45)
)d(ticker,date_div,amt_div)
 
/*Put underlying price information into a table*/
SELECT *
INTO #prices
FROM (VALUES
       ('ABC',495)
      ,('DEF',125)
      ,('GHI',62.5)
      )p(ticker,price)
 
/*put the options into a table*/
SELECT *
INTO #options
FROM (VALUES
       (1,'ABC','2014-08-09',470,65.43,'C')
      ,(2,'ABC','2014-08-20',480,50.03,'P')
      ,(3,'ABC','2014-11-10',490,63.90,'P')
      ,(4,'ABC','2014-09-14',500,55.87,'C')
      ,(5,'DEF','2014-11-14',100,28.13,'C')
      ,(6,'DEF','2014-12-12',110,21.86,'C')
      ,(7,'DEF','2014-08-11',120,9.87,'P')
      ,(8,'DEF','2014-08-26',130,10.07,'C')
      ,(9,'GHI','2014-08-04',50,12.93,'C')
      ,(10,'GHI','2014-12-28',55,8.97,'C')
      ,(11,'GHI','2014-10-09',60,5.40,'P')
      ,(12,'GHI','2014-11-14',65,3.91,'C')
      )o(rn,ticker,expiry,strike,price,z)
 
/*Establish the start date for calculation purposes*/
DECLARE @date_start as datetime = cast('2013-12-10' as datetime)
 
/*Put the #div values into another table with date converted to a
 fraction of a year*/
SELECT ticker
,wct.YEARFRAC(@date_start,date_div,NULL) as T
,amt_div as D
INTO #tdiv
FROM #div
 
/*Put the discount factors into a table*/
SELECT wct.TENOR2DATE(tenor,@date_start,NULL,'') as date_df
,df
INTO #df
FROM (
   SELECT 'ON',0.999995555575309 UNION ALL
   SELECT 'TN',0.999991111170370 UNION ALL
   SELECT '1W',0.999956112706425 UNION ALL
   SELECT '2W',0.999916450742048 UNION ALL
   SELECT '1M',0.999804481000583 UNION ALL
   SELECT '2M',0.999574621744643 UNION ALL
   SELECT '3M',0.999241679910437 UNION ALL
   SELECT '6M',0.998800609148515 UNION ALL
   SELECT '9M',0.998022836090921 UNION ALL
   SELECT '1Y',0.997197057207847 UNION ALL
   SELECT '2Y',0.996311568695976
       )n(tenor, df)
 
/*Calculate the risk-free rate for each expiry date in the portfolio*/
SELECT expiry
,wct.DFINTERP(date_df,df,expiry,@date_start,'CC') as Rf
INTO #rates
FROM #df, #options
GROUP BY expiry
 
/*Calculate the implied volatility*/
SELECT A.rn
,A.ticker
,A.z
,B.price as underlying
,A.strike
,A.expiry
,A.price
,wct.BinomialDiscreteDividendsIV(
       A.z
      ,'A'
      ,B.price
      ,A.strike
      ,wct.YEARFRAC(@date_start,A.expiry,NULL)
      ,C.rf
      ,NULL
,'SELECT T,D FROM #tdiv WHERE ticker = ' + '''' + CAST(A.ticker as varchar(max)) + ''''
      ,A.price         
      ,100
      ) as [Volatility]
FROM #options A
INNER JOIN #prices B
ON A.ticker = B.ticker
INNER JOIN #rates C
ON A.expiry = C.expiry
 
DROP TABLE #div
DROP TABLE #prices
DROP TABLE #options
DROP TABLE #tdiv
DROP TABLE #df
DROP TABLE #rates

This produces the following result, which has been reformatted for presentation purposes.

rn
ticker
z
underlying
strike
expiry
price
Volatility
1
ABC
C
495
470
2014-08-09
65.43
0.372603
2
ABC
P
495
480
2014-08-20
50.03
0.325245
3
ABC
P
495
490
2014-11-10
63.9
0.318097
4
ABC
C
495
500
2014-09-14
55.87
0.371497
5
DEF
C
125
100
2014-11-14
28.13
0.337175
6
DEF
C
125
110
2014-12-12
21.86
0.330254
7
DEF
P
125
120
2014-08-11
9.87
0.275454
8
DEF
C
125
130
2014-08-26
10.07
0.319192
9
GHI
C
62.5
50
2014-08-04
12.93
0.295849
10
GHI
C
62.5
55
2014-12-28
8.97
0.276268
11
GHI
P
62.5
60
2014-10-09
5.4
0.21974
12
GHI
C
62.5
65
2014-11-14
3.91
0.268751

 


Copyright 2008-2024 Westclintech LLC         Privacy Policy        Terms of Service