Logo

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

colNamecolDatatypecolDesc
nodeintThe node within the step
stepnointThe step number
underlyingintthe underlying value of the undelying asset at this step and node in the tree
intrinsicfloatthe intrinsic value of the option at this step and node in the tree
pricefloatthe 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.

nodestepnounderlyingintrinsicprice
0099.504.10331126546237
0196.228349289281902.39274557660086
11102.8828829874022.882882987401625.83283543020053
0293.064273436583301.18123638510604
1299.503.61710563855476
22106.3807800180846.380780018084188.07370383125967
0390.004235282500200.444198036800946
1396.228349289281901.92578461228176
23102.8828829874022.882882987401625.326677765603
33109.9976014342629.9976014342619910.8528420254086
0487.044813972654600.0969012568653358
1493.064273436583300.794907303249054
2499.503.06831200084976
34106.3807800180846.380780018084187.60997152588158
44113.73739052518613.737390525186114.1351051153859
0584.182701133479100
1590.004235282500200.194724293659481
2596.228349289281901.40101823393864
35102.8828829874022.882882987401624.75303909409562
45109.9976014342629.9976014342619910.4994174902749
55117.60432804718717.604328047187217.8171663328575
0681.414697173745200
1687.044813972654600
2693.064273436583300.391300915672107
3699.502.42077991522551
46106.3807800180846.380780018084187.11020212856249
56113.73739052518613.737390525186113.9288564115091
66121.60273689739521.602736897394621.7581035454753
0778.737707707701200
1784.182701133479100
2790.004235282500200
3796.228349289281900.786324108452473
47102.8828829874022.882882987401624.07166648962555
57109.9976014342629.9976014342619910.18221376055
67117.60432804718717.604328047187217.7225719623691
77125.73708694635625.737086946356125.8519225017229
0876.148740095819600
1881.414697173745200
2887.044813972654600
3893.064273436583300
4899.501.5801281795407
58106.3807800180846.380780018084186.5886799160665
68113.73739052518613.737390525186113.8173291874142
78121.60273689739521.602736897394621.6804778795424
88130.01200003496230.012000034961830.0873913596105
0973.644900099289200
1978.737707707701200
2984.182701133479100
3990.004235282500200
4996.228349289281900
59102.8828829874022.882882987401623.17528743801645
69109.9976014342629.9976014342619910.0381077745853
79117.60432804718717.604328047187217.6437716040886
89125.73708694635625.737086946356125.7753942245699
99134.43225514125734.432255141256734.4693475630574
01071.223388644509200
11076.148740095819600
21081.414697173745200
31087.044813972654600
41093.064273436583300
51099.500
610106.3807800180846.380780018084186.38078001808418
710113.73739052518613.737390525186113.7373905251861
810121.60273689739521.602736897394621.6027368973946
910130.01200003496230.012000034961830.0120000349618
1010139.00279372291939.002793722918939.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.

stepno012345678910
04.1NULLNULLNULLNULLNULLNULLNULLNULLNULLNULL
12.395.83NULLNULLNULLNULLNULLNULLNULLNULLNULL
21.183.628.07NULLNULLNULLNULLNULLNULLNULLNULL
30.441.935.3310.85NULLNULLNULLNULLNULLNULLNULL
40.10.793.077.6114.14NULLNULLNULLNULLNULLNULL
500.191.44.7510.517.82NULLNULLNULLNULLNULL
6000.392.427.1113.9321.76NULLNULLNULLNULL
70000.794.0710.1817.7225.85NULLNULLNULL
800001.586.5913.8221.6830.09NULLNULL
9000003.1810.0417.6425.7834.47NULL
100000006.3813.7421.630.0139