Login     Register

        Contact Us     Search

XLeratorDB/financial-options Documentation

SQL Server Binomial Tree function for equity option pricing


BinomialDiscreteDividends

Updated: 15 Dec 2013


Use BinomialDiscreteDividends to calculate the price and Greeks of an 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].[BinomialDiscreteDividends](
  <@CallPut, nvarchar(4000),>
 ,<@AmEur, nvarchar(4000),>
 ,<@AssetPrice, float,>
 ,<@StrikePrice, float,>
 ,<@TimeToMaturity, float,>
 ,<@RiskFreeRate, float,>
 ,<@DividendRate, float,>
 ,<@Dividend_RangeQuery, nvarchar(max),>
 ,<@Volatility, float,>
 ,<@NumberOfSteps, int,>
 ,<@RV, nvarchar(4000),>)
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.
@Volatility
the volatility of the relative price change of the underlying asset. @Volatility 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.
 
@RV
identifies the calculation to be performed. @RV is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar. @RV is not case-sensitive. The following values are acceptable for @RV.

@RV
Returns
'P','PRICE'
Price
'D','DELTA'
Delta
'G','GAMMA'
Gamma
'T','THETA'
Theta
'V','VEGA'
Vega
'R','RHO'
Rho
'L','LAMBDA'
Lambda
'DDDV','VANNA','DVEGADSPOT','DDELTADVOL'
DdeltaDvol
'DVV','DDELTADVOLDVOL'
DdeltaDvolDvol
'DT','CHARM','DDELTADTIME'
DdeltaDtime
'GV','ZOMMA','DGAMMADVOL'
DgammaDvol
'GP','GAMMAP'
GammaP
'DVDV','VOMMA','VOLGA','DVEGADVOL'
DvegaDvol
'VP','VEGAP'
VegaP
'S','SPEED','DGAMMADSPOT'
DgammaDspot
'DX','DELTAX'
Delta X
'RND','RISKNEUTRALDENSITY'
Risk Neutral Density
'VVV','ULTIMA','DVOMMADVOL'
DvommaDvol
'VT','VETA','DVEGADTIME'
DvegaDtime
'GT','COLOR','DGAMMADTIME'
DgammaDtime

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)
·         If @RV is NULL, then @RV is set to 'P'.
·         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.
·         To get the implied volatility (given price), use BinomialDiscreteDividendsIV.
·         Use the table-valued function BinomialDiscreteDividendsTree to see the underlying price, intrinsic value, and option value for each node on the binomial tree.
·         Use the table-valued function BinomialDiscreteDividendsPriceNGreeks to calculate the price, delta, gamma, theta, vega, rho, and lambda in a single SELECT statement faster than calculating them individually in BinomialDiscreteDividends.
·         To calculate the price and Greeks using proportional dividends (where the dividend is specified with a date and a percentage) use ProportionalDividends.
Example
Calculate the price for an American call option expiring in one year with asset price of 478, a strike price of 500 and a volatility of 30%. 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.BinomialDiscreteDividends(
       '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
      ,0.30       --Volatility
      ,100        --Number of Steps
      ,'P'        --Return Value
      ) as [Price]
This produces the following result.
                 Price
----------------------
      46.4420843957928
To calculate the delta for the same option:
SELECT wct.BinomialDiscreteDividends(
       '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
      ,0.30       --Volatility
      ,100        --Number of Steps
      ,'D'        --Return Value
      ) as [Delta]
This produces the following result.
                 Delta
----------------------
     0.501614073818007
In this example, using the same data, we calculate the price for all the combinations of @AmEur and @CallPut.
SELECT n.z
,m.AE
,wct.BinomialDiscreteDividends(
       n.z        --Put/Call
      ,m.AE       --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
      ,0.30       --Volatility
      ,100        --Number of Steps
      ,'P'        --Return Value
      ) as [Price]
FROM (VALUES ('P'),('C'))n(z)
CROSS APPLY(VALUES ('A'),('E'))m(AE)
This produces the following result.
z    AE                    Price
---- ---- ----------------------
P    A          69.8455172828263
P    E          69.8455172828263
C    A          46.4420843957928
C    E          45.0939475380429
 
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.BinomialDiscreteDividends(
       '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)'
      ,0.30      
      ,100       
      ,'P'       
      ) as [Price]
This produces the following result on 2013-12-10. You results will be different.
                 Price
----------------------
      44.6914440159369
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.BinomialDiscreteDividends(
       '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)'
      ,0.30      
      ,100       
      ,'P'       
      ) as [Price]
This produces the following result.
                 Price
----------------------
      44.6914440159369
In this example we put some equity options trades into a table, with a separate table containing the closing price and volatilities 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 price information into a table*/
SELECT *
INTO #prices
FROM (VALUES
       ('ABC',495,0.35)
      ,('DEF',125,0.3)
      ,('GHI',62.5,0.25)
      )p(ticker,price,vol)
 
/*put the options into a table*/
SELECT *
INTO #options
FROM (VALUES
       (1,'ABC','2014-08-09',470,'C')
      ,(2,'ABC','2014-08-20',480,'P')
      ,(3,'ABC','2014-11-10',490,'P')
      ,(4,'ABC','2014-09-14',500,'C')
      ,(5,'DEF','2014-11-14',100,'C')
      ,(6,'DEF','2014-12-12',110,'C')
      ,(7,'DEF','2014-08-11',120,'P')
      ,(8,'DEF','2014-08-26',130,'C')
      ,(9,'GHI','2014-08-04',50,'C')
      ,(10,'GHI','2014-12-28',55,'C')
      ,(11,'GHI','2014-10-09',60,'P')
      ,(12,'GHI','2014-11-14',65,'C')
      )o(rn,ticker,expiry,strike,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
,A.strike
,A.expiry
,B.vol
,wct.BinomialDiscreteDividends(
       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)) + ''''
      ,B.vol           
      ,100       
      ,'P'       
      ) as [Price]
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
price
strike
expiry
vol
Price
1
ABC
C
495
470
2014-08-09
0.35
65.43249321
2
ABC
P
495
480
2014-08-20
0.35
50.02983479
3
ABC
P
495
490
2014-11-10
0.35
63.89661264
4
ABC
C
495
500
2014-09-14
0.35
55.87327188
5
DEF
C
125
100
2014-11-14
0.3
28.13292336
6
DEF
C
125
110
2014-12-12
0.3
21.85635434
7
DEF
P
125
120
2014-08-11
0.3
9.865945077
8
DEF
C
125
130
2014-08-26
0.3
10.06775696
9
GHI
C
62.5
50
2014-08-04
0.25
12.9316739
10
GHI
C
62.5
55
2014-12-28
0.25
8.97155636
11
GHI
P
62.5
60
2014-10-09
0.25
5.404846596
12
GHI
C
62.5
65
2014-11-14
0.25
3.914550829

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 BinomialDiscreteDividend 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 price information into a table*/
SELECT *
INTO #prices
FROM (VALUES
       ('ABC',495,0.35)
      ,('DEF',125,0.3)
      ,('GHI',62.5,0.25)
      )p(ticker,price,vol)
 
/*put the options into a table*/
SELECT *
INTO #options
FROM (VALUES
       (1,'ABC','2014-08-09',470,'C')
      ,(2,'ABC','2014-08-20',480,'P')
      ,(3,'ABC','2014-11-10',490,'P')
      ,(4,'ABC','2014-09-14',500,'C')
      ,(5,'DEF','2014-11-14',100,'C')
      ,(6,'DEF','2014-12-12',110,'C')
      ,(7,'DEF','2014-08-11',120,'P')
      ,(8,'DEF','2014-08-26',130,'C')
      ,(9,'GHI','2014-08-04',50,'C')
      ,(10,'GHI','2014-12-28',55,'C')
      ,(11,'GHI','2014-10-09',60,'P')
      ,(12,'GHI','2014-11-14',65,'C')
      )o(rn,ticker,expiry,strike,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
 
SELECT A.rn
,A.ticker
,A.z
,B.price
,A.strike
,A.expiry
,B.vol
,wct.BinomialDiscreteDividends(
       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)) + ''''
      ,B.vol           
      ,100       
      ,'P'       
      ) as [Price]
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
price
strike
expiry
vol
Price
1
ABC
C
495
470
2014-08-09
0.35
65.43249321
2
ABC
P
495
480
2014-08-20
0.35
50.02983479
3
ABC
P
495
490
2014-11-10
0.35
63.89661264
4
ABC
C
495
500
2014-09-14
0.35
55.87327188
5
DEF
C
125
100
2014-11-14
0.3
28.13292336
6
DEF
C
125
110
2014-12-12
0.3
21.85635434
7
DEF
P
125
120
2014-08-11
0.3
9.865945077
8
DEF
C
125
130
2014-08-26
0.3
10.06775696
9
GHI
C
62.5
50
2014-08-04
0.25
12.9316739
10
GHI
C
62.5
55
2014-12-28
0.25
8.97155636
11
GHI
P
62.5
60
2014-10-09
0.25
5.404846596
12
GHI
C
62.5
65
2014-11-14
0.25
3.914550829

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 price information into a table*/
SELECT *
INTO #prices
FROM (VALUES
       ('ABC',495,0.35)
      ,('DEF',125,0.3)
      ,('GHI',62.5,0.25)
      )p(ticker,price,vol)
 
/*put the options into a table*/
SELECT *
INTO #options
FROM (VALUES
       (1,'ABC','2014-08-09',470,'C')
      ,(2,'ABC','2014-08-20',480,'P')
      ,(3,'ABC','2014-11-10',490,'P')
      ,(4,'ABC','2014-09-14',500,'C')
      ,(5,'DEF','2014-11-14',100,'C')
      ,(6,'DEF','2014-12-12',110,'C')
      ,(7,'DEF','2014-08-11',120,'P')
      ,(8,'DEF','2014-08-26',130,'C')
      ,(9,'GHI','2014-08-04',50,'C')
      ,(10,'GHI','2014-12-28',55,'C')
      ,(11,'GHI','2014-10-09',60,'P')
      ,(12,'GHI','2014-11-14',65,'C')
      )o(rn,ticker,expiry,strike,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
 
SELECT A.rn
,A.ticker
,A.z
,B.price
,A.strike
,A.expiry
,C.rf
,B.vol
,wct.BinomialDiscreteDividends(
       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)) + ''''
      ,B.vol           
      ,100       
      ,'P'       
      ) as [Price]
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
price
strike
expiry
rf
vol
Price
1
ABC
C
495
470
2014-08-09
0.002531977
0.35
62.03014829
2
ABC
P
495
480
2014-08-20
0.00256042
0.35
53.93950488
3
ABC
P
495
490
2014-11-10
0.002733725
0.35
69.70808676
4
ABC
C
495
500
2014-09-14
0.002623764
0.35
52.25858779
5
DEF
C
125
100
2014-11-14
0.002741446
0.3
27.05726033
6
DEF
C
125
110
2014-12-12
0.002795504
0.3
20.62139325
7
DEF
P
125
120
2014-08-11
0.002537148
0.3
10.82848406
8
DEF
C
125
130
2014-08-26
0.002575936
0.3
9.29818039
9
GHI
C
62.5
50
2014-08-04
0.00251905
0.25
12.67660871
10
GHI
C
62.5
55
2014-12-28
0.002753961
0.25
8.560586874
11
GHI
P
62.5
60
2014-10-09
0.00267198
0.25
6.04699361
12
GHI
C
62.5
65
2014-11-14
0.002741446
0.25
3.518655567

 


Copyright 2008-2017 Westclintech LLC         Privacy Policy        Terms of Service