SQL Server BinomialTree Function
Updated 2023-11-16 16:04:42.697000
Description
Use the table-valued function BinomialTree to return the option value, intrinsic value and underlying value for each node on a binomial tree for an American or European option.
Syntax
SELECT * FROM [westclintech].[wct].[BinomialTree](
<@CallPut, nvarchar(4000),>
,<@AssetPrice, float,>
,<@StrikePrice, float,>
,<@TimeToMaturity, float,>
,<@RiskFreeRate, float,>
,<@DividendRate, float,>
,<@Volatility, float,>
,<@nSteps, int,>
,<@AmEur, 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.
@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. @Strike 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. @Time is an expression of type float or of a type that can be implicitly converted to float.
@RiskFreeRate
the annualized, continuously compounded risk-free rate of return over the life of the option. @RiskFree is an expression of type float or of a type that can be implicitly converted to float.
@DividendRate
the annualized, continuously compounded dividend rate over the life of the option. For currency options, @Div should be the foreign risk-free interest rate. @Div is an expression of type float or of a type that can be implicitly converted to float.
@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.
@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.
Return Type
table
| colName | colDatatype | colDesc |
|---|---|---|
| node | int | The node within the step |
| stepno | int | The step number |
| underlying | int | the underlying value of the undelying asset at this step and node in the tree |
| intrinsic | float | the intrinsic value of the option at this step and node in the tree |
| price | float | the theoretical value of the option at this step and node in the tree |
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).
@Strike must be greater than zero (@Strike > 0).
If @DividendRate is NULL then @DividendRate is set to zero.
If @RiskFreeRate is NULL then @RiskFreeRate is set to zero.
Examples
Create the binomial tree for an American call option on 2012-09-04, expiring on 2012-12-15, with a current asset price of 99.5, a strike price of 100 and a volatility of 20%. The risk free rate is 2% and the dividend rate is 0.5%. The number of steps is 10.
SELECT *
FROM wct.BinomialTree('C', --@CallPut
99.5, --@AssetPrice
100, --@StrikePrice
datediff(d, '2012-09-04', '2012-12-15') / 365.0000, --@TimeToMaturity
.02, --@RiskFreeRate
.005, --@DividendRate
.20, --@Volatility
10, --@NumberOfSteps
'E' --@AmEur
);
This produces the following result.
| node | stepno | underlying | intrinsic | price |
|---|---|---|---|---|
| 0 | 0 | 99.5 | 0 | 4.10331126546237 |
| 0 | 1 | 96.2283492892819 | 0 | 2.39274557660086 |
| 1 | 1 | 102.882882987402 | 2.88288298740162 | 5.83283543020053 |
| 0 | 2 | 93.0642734365833 | 0 | 1.18123638510604 |
| 1 | 2 | 99.5 | 0 | 3.61710563855476 |
| 2 | 2 | 106.380780018084 | 6.38078001808418 | 8.07370383125967 |
| 0 | 3 | 90.0042352825002 | 0 | 0.444198036800946 |
| 1 | 3 | 96.2283492892819 | 0 | 1.92578461228176 |
| 2 | 3 | 102.882882987402 | 2.88288298740162 | 5.326677765603 |
| 3 | 3 | 109.997601434262 | 9.99760143426199 | 10.8528420254086 |
| 0 | 4 | 87.0448139726546 | 0 | 0.0969012568653358 |
| 1 | 4 | 93.0642734365833 | 0 | 0.794907303249054 |
| 2 | 4 | 99.5 | 0 | 3.06831200084976 |
| 3 | 4 | 106.380780018084 | 6.38078001808418 | 7.60997152588158 |
| 4 | 4 | 113.737390525186 | 13.7373905251861 | 14.1351051153859 |
| 0 | 5 | 84.1827011334791 | 0 | 0 |
| 1 | 5 | 90.0042352825002 | 0 | 0.194724293659481 |
| 2 | 5 | 96.2283492892819 | 0 | 1.40101823393864 |
| 3 | 5 | 102.882882987402 | 2.88288298740162 | 4.75303909409562 |
| 4 | 5 | 109.997601434262 | 9.99760143426199 | 10.4994174902749 |
| 5 | 5 | 117.604328047187 | 17.6043280471872 | 17.8171663328575 |
| 0 | 6 | 81.4146971737452 | 0 | 0 |
| 1 | 6 | 87.0448139726546 | 0 | 0 |
| 2 | 6 | 93.0642734365833 | 0 | 0.391300915672107 |
| 3 | 6 | 99.5 | 0 | 2.42077991522551 |
| 4 | 6 | 106.380780018084 | 6.38078001808418 | 7.11020212856249 |
| 5 | 6 | 113.737390525186 | 13.7373905251861 | 13.9288564115091 |
| 6 | 6 | 121.602736897395 | 21.6027368973946 | 21.7581035454753 |
| 0 | 7 | 78.7377077077012 | 0 | 0 |
| 1 | 7 | 84.1827011334791 | 0 | 0 |
| 2 | 7 | 90.0042352825002 | 0 | 0 |
| 3 | 7 | 96.2283492892819 | 0 | 0.786324108452473 |
| 4 | 7 | 102.882882987402 | 2.88288298740162 | 4.07166648962555 |
| 5 | 7 | 109.997601434262 | 9.99760143426199 | 10.18221376055 |
| 6 | 7 | 117.604328047187 | 17.6043280471872 | 17.7225719623691 |
| 7 | 7 | 125.737086946356 | 25.7370869463561 | 25.8519225017229 |
| 0 | 8 | 76.1487400958196 | 0 | 0 |
| 1 | 8 | 81.4146971737452 | 0 | 0 |
| 2 | 8 | 87.0448139726546 | 0 | 0 |
| 3 | 8 | 93.0642734365833 | 0 | 0 |
| 4 | 8 | 99.5 | 0 | 1.5801281795407 |
| 5 | 8 | 106.380780018084 | 6.38078001808418 | 6.5886799160665 |
| 6 | 8 | 113.737390525186 | 13.7373905251861 | 13.8173291874142 |
| 7 | 8 | 121.602736897395 | 21.6027368973946 | 21.6804778795424 |
| 8 | 8 | 130.012000034962 | 30.0120000349618 | 30.0873913596105 |
| 0 | 9 | 73.6449000992892 | 0 | 0 |
| 1 | 9 | 78.7377077077012 | 0 | 0 |
| 2 | 9 | 84.1827011334791 | 0 | 0 |
| 3 | 9 | 90.0042352825002 | 0 | 0 |
| 4 | 9 | 96.2283492892819 | 0 | 0 |
| 5 | 9 | 102.882882987402 | 2.88288298740162 | 3.17528743801645 |
| 6 | 9 | 109.997601434262 | 9.99760143426199 | 10.0381077745853 |
| 7 | 9 | 117.604328047187 | 17.6043280471872 | 17.6437716040886 |
| 8 | 9 | 125.737086946356 | 25.7370869463561 | 25.7753942245699 |
| 9 | 9 | 134.432255141257 | 34.4322551412567 | 34.4693475630574 |
| 0 | 10 | 71.2233886445092 | 0 | 0 |
| 1 | 10 | 76.1487400958196 | 0 | 0 |
| 2 | 10 | 81.4146971737452 | 0 | 0 |
| 3 | 10 | 87.0448139726546 | 0 | 0 |
| 4 | 10 | 93.0642734365833 | 0 | 0 |
| 5 | 10 | 99.5 | 0 | 0 |
| 6 | 10 | 106.380780018084 | 6.38078001808418 | 6.38078001808418 |
| 7 | 10 | 113.737390525186 | 13.7373905251861 | 13.7373905251861 |
| 8 | 10 | 121.602736897395 | 21.6027368973946 | 21.6027368973946 |
| 9 | 10 | 130.012000034962 | 30.0120000349618 | 30.0120000349618 |
| 10 | 10 | 139.002793722919 | 39.0027937229189 | 39.0027937229189 |
Since the purpose of the function is to show all the nodes it can return quite a number of rows. The number of row returned will be ((@NumberOfSteps + 2) / 2) * (@NumberOfSteps + 1).
In this example, we PIVOT the price values.
SELECT stepno,
[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9],
[10]
FROM ( SELECT k.stepno,
k.node,
ROUND(k.price, 2) as price
FROM wct.BinomialTree('C', --Call/Put
99.5, --Asset Price
100, --Strike Price
datediff(d, '2012-09-04', '2012-12-15') / 365.0000, --Time-to-expiry
.02, --Risk Free Rate
.005, --Dividend Rate
.20, --Volatility
10, --Number of Steps
'E') k ) d
PIVOT ( SUM(price)
FOR NODE IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10])) as P
ORDER BY stepno;
This produces the following result.
| stepno | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 4.1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 1 | 2.39 | 5.83 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | 1.18 | 3.62 | 8.07 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | 0.44 | 1.93 | 5.33 | 10.85 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 4 | 0.1 | 0.79 | 3.07 | 7.61 | 14.14 | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | 0 | 0.19 | 1.4 | 4.75 | 10.5 | 17.82 | NULL | NULL | NULL | NULL | NULL |
| 6 | 0 | 0 | 0.39 | 2.42 | 7.11 | 13.93 | 21.76 | NULL | NULL | NULL | NULL |
| 7 | 0 | 0 | 0 | 0.79 | 4.07 | 10.18 | 17.72 | 25.85 | NULL | NULL | NULL |
| 8 | 0 | 0 | 0 | 0 | 1.58 | 6.59 | 13.82 | 21.68 | 30.09 | NULL | NULL |
| 9 | 0 | 0 | 0 | 0 | 0 | 3.18 | 10.04 | 17.64 | 25.78 | 34.47 | NULL |
| 10 | 0 | 0 | 0 | 0 | 0 | 0 | 6.38 | 13.74 | 21.6 | 30.01 | 39 |