Logo

BinomialPriceNGreeks

Updated 2023-11-16 15:59:39.620000

Syntax

SELECT *
FROM [westclintech].[wct].[BinomialPriceNGreeks] (
  <@CallPut, nvarchar(4000),>
 ,<@AssetPrice, float,>
 ,<@StrikePrice, float,>
 ,<@TimeToMaturity, float,>
 ,<@RiskFreeRate, float,>
 ,<@DividendRate, float,>
 ,<@Volatility, float,>
 ,<@nSteps, int,>
 ,<@AmEur, nvarchar(4000),>)

Description

Use the table-valued function BinomialPriceNGreeks to calculate the price, delta, gamma, theta, vega, rho and lambda of European or American options using the Binomial Tree option pricing formula.

Arguments

@nSteps

the number of steps in the binomial tree. @nSteps is an expression of type int or of a type that can be implicitly converted to int.

@DividendRate

the annualized, continuously compounded dividend rate over the life of the option. For currency options, @DividendRate should be the foreign risk-free interest rate. @DividendRate 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. @RiskFreeRate 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.

@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.

@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.

@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.

@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.

@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.

Return Type

table

{"columns": [{"field": "colName", "headerName": "Name", "header": "name"}, {"field": "colDatatype", "headerName": "Type", "header": "type"}, {"field": "colDesc", "headerName": "Description", "header": "description", "minWidth": 1000}], "rows": [{"id": "00490e8d-e57b-44b3-8f1b-e8d35bbb5ee4", "colName": "Price", "colDatatype": "float", "colDesc": "the theoretical value of the option"}, {"id": "41a7c5d7-ee94-4a5a-9f46-2eda83cb13ba", "colName": "Delta", "colDatatype": "float", "colDesc": "the sensitivity to small changes in the asset price; the first derivative of the option with respect to price"}, {"id": "5794298c-fe60-4329-8704-8a43b3065718", "colName": "Gamma", "colDatatype": "float", "colDesc": "The rate of change in Delta with respect to small changes in the asset price; the second derivative of the option with respect to price."}, {"id": "2cc6d5cd-7e44-45d4-b8e9-37a932ebfdc8", "colName": "Theta", "colDatatype": "float", "colDesc": "The sensitivity to small changes in time; the first derivative of the option with respect to time."}, {"id": "841f1e6a-62a1-4b73-8cb7-e368da010b99", "colName": "Vega", "colDatatype": "float", "colDesc": "The sensitivity to small changes in volatility; the first derivative of the option with respect to volatility."}, {"id": "b8c3b520-16a0-40bf-9e43-5da19fa27ab6", "colName": "Rho", "colDatatype": "float", "colDesc": "The sensitivity to small changes in the risk-free rate; the first derivative of the option with respect to the risk-free rate."}, {"id": "973d5a4b-d93c-47a5-8ae8-787e8896d2a9", "colName": "Lambda", "colDatatype": "float", "colDesc": "Delta multiplied by the asset price divided by the theoretical value. If the theoretical value is zero, then lambda is set to zero."}]}

Remarks

@TimeToMaturity must be greater than zero (@TimeToMaturity > 0).

@AssetPrice must be greater than zero (@AssetPrice > 0).

@StrikePrice must be greater than zero (@StrikePrice > 0).

@Price must be greater than zero.

If @DividendRate is NULL an error will be returned.

If @RiskFreeRate is NULL an error will be returned.

Examples

Calculate the price and Greeks 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%. All column values have been cast as money data types to make the resultant table easier to read.

SELECT Cast(k.Price as money) as Price,
       Cast(k.Delta as money) as Delta,
       Cast(k.Gamma as money) as Gamma,
       Cast(k.Theta as money) as Theta,
       Cast(k.Vega as money) as Vega,
       Cast(k.Rho as money) as Rho,
       Cast(k.Lambda as money) as Lambda
  FROM wct.BinomialPriceNGreeks('C', --PutCall
                                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
                                100, --Number of steps
                                'A' --American/European
    ) k;

This produces the following result.

{"columns":[{"field":"Price","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Delta","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Gamma","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Theta","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Vega","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Rho","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Lambda","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Price":"4.1547","Delta":"0.5172","Gamma":"0.038","Theta":"-0.0225","Vega":"0.209","Rho":"0.1322","Lambda":"12.3873"}]}

In the following SELECT, we will populate a derived table with some option information and then calculate the price and Greeks for options contained therein. For demonstration purposes we have made lots of simplifying assumptions about the US and contra interest rates and the implied volatility. This just demonstrates the structure of the TSQL statement.

SET NOCOUNT ON;
/*Create a temporary table to store data*/
CREATE TABLE #opt (
    z char(1),
    s money,
    ex datetime,
    vol money);
/*Put data in the table*/
INSERT INTO #opt
VALUES ('C', 98, '2012-09-22', 0.23);
INSERT INTO #opt
VALUES ('P', 99, '2012-09-22', 0.15);
INSERT INTO #opt
VALUES ('C', 100, '2012-09-22', 0.15);
INSERT INTO #opt
VALUES ('C', 101, '2012-09-22', 0.21);
INSERT INTO #opt
VALUES ('P', 102, '2012-09-22', 0.19);
INSERT INTO #opt
VALUES ('P', 98, '2012-10-20', 0.18);
INSERT INTO #opt
VALUES ('P', 99, '2012-10-20', 0.24);
INSERT INTO #opt
VALUES ('C', 100, '2012-10-20', 0.21);
INSERT INTO #opt
VALUES ('P', 101, '2012-10-20', 0.25);
INSERT INTO #opt
VALUES ('P', 102, '2012-10-20', 0.25);
INSERT INTO #opt
VALUES ('P', 98, '2012-11-17', 0.17);
INSERT INTO #opt
VALUES ('C', 99, '2012-11-17', 0.24);
INSERT INTO #opt
VALUES ('P', 100, '2012-11-17', 0.17);
INSERT INTO #opt
VALUES ('P', 101, '2012-11-17', 0.25);
INSERT INTO #opt
VALUES ('P', 102, '2012-11-17', 0.22);
INSERT INTO #opt
VALUES ('C', 98, '2012-12-22', 0.21);
INSERT INTO #opt
VALUES ('C', 99, '2012-12-22', 0.19);
INSERT INTO #opt
VALUES ('C', 100, '2012-12-22', 0.21);
INSERT INTO #opt
VALUES ('C', 101, '2012-12-22', 0.15);
INSERT INTO #opt
VALUES ('P', 102, '2012-12-22', 0.22);
INSERT INTO #opt
VALUES ('C', 98, '2013-03-16', 0.24);
INSERT INTO #opt
VALUES ('C', 99, '2013-03-16', 0.17);
INSERT INTO #opt
VALUES ('P', 100, '2013-03-16', 0.2);
INSERT INTO #opt
VALUES ('C', 101, '2013-03-16', 0.19);
INSERT INTO #opt
VALUES ('C', 102, '2013-03-16', 0.21);
INSERT INTO #opt
VALUES ('P', 98, '2013-03-22', 0.16);
INSERT INTO #opt
VALUES ('P', 99, '2013-03-22', 0.25);
INSERT INTO #opt
VALUES ('P', 100, '2013-03-22', 0.23);
INSERT INTO #opt
VALUES ('C', 101, '2013-03-22', 0.22);
INSERT INTO #opt
VALUES ('P', 102, '2013-03-22', 0.16);
/*Select data*/
SELECT O.z,
       O.s as Strike,
       O.ex as exDate,
       O.vol as Volatility,
       Cast(k.Price as money) as Price,
       Cast(k.Delta as money) as Delta,
       Cast(k.Gamma as money) as Gamma,
       Cast(k.Theta as money) as Theta,
       Cast(k.Vega as money) as Vega,
       Cast(k.Rho as money) as Rho,
       Cast(k.Lambda as money) as Lambda
  FROM #OPT O
 CROSS APPLY wct.BinomialPriceNGreeks(O.z, --PutCall
                                      96.76, --Asset Price
                                      O.s, --Strike Price
                                      datediff(d, '2012-09-04', O.ex) / 365.0000, --Time-to-expiry
                                      0.0569, --Risk Free Rate
                                      0.00284, --Dividend Rate
                                      O.vol, --Volatility
                                      100, --Number of Steps
                                      'E' --American/European
       ) k;
/*Clean up table*/
DROP TABLE #opt;

This produces the following result.

{"columns":[{"field":"z"},{"field":"Strike","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"exDate","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Volatility","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Price","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Delta","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Gamma","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Theta","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Vega","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Rho","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Lambda","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"z":"C","Strike":"98.00","exDate":"2012-09-22 00:00:00.000","Volatility":"0.23","Price":"1.5321","Delta":"0.4317","Gamma":"0.0798","Theta":"-0.0601","Vega":"0.0835","Rho":"0.0198","Lambda":"27.2637"},{"z":"P","Strike":"99.00","exDate":"2012-09-22 00:00:00.000","Volatility":"0.15","Price":"2.521","Delta":"-0.7225","Gamma":"0.1042","Theta":"-0.0193","Vega":"0.0713","Rho":"-0.0357","Lambda":"-27.7319"},{"z":"C","Strike":"100.00","exDate":"2012-09-22 00:00:00.000","Volatility":"0.15","Price":"0.3213","Delta":"0.185","Gamma":"0.0833","Theta":"-0.0266","Vega":"0.0624","Rho":"0.0087","Lambda":"55.7107"},{"z":"C","Strike":"101.00","exDate":"2012-09-22 00:00:00.000","Volatility":"0.21","Price":"0.4969","Delta":"0.2005","Gamma":"0.0622","Theta":"-0.038","Vega":"0.0613","Rho":"0.0093","Lambda":"39.0483"},{"z":"P","Strike":"102.00","exDate":"2012-09-22 00:00:00.000","Volatility":"0.19","Price":"5.2087","Delta":"-0.8785","Gamma":"0.0495","Theta":"-0.0095","Vega":"0.0429","Rho":"-0.0445","Lambda":"-16.3205"},{"z":"P","Strike":"98.00","exDate":"2012-10-20 00:00:00.000","Volatility":"0.18","Price":"2.7643","Delta":"-0.5245","Gamma":"0.0649","Theta":"-0.019","Vega":"0.139","Rho":"-0.0674","Lambda":"-18.3591"},{"z":"P","Strike":"99.00","exDate":"2012-10-20 00:00:00.000","Volatility":"0.24","Price":"4.1618","Delta":"-0.558","Gamma":"0.048","Theta":"-0.0268","Vega":"0.1346","Rho":"-0.0733","Lambda":"-12.9729"},{"z":"C","Strike":"100.00","exDate":"2012-10-20 00:00:00.000","Volatility":"0.21","Price":"1.8125","Delta":"0.3766","Gamma":"0.0528","Theta":"-0.035","Vega":"0.127","Rho":"0.0436","Lambda":"20.106"},{"z":"P","Strike":"101.00","exDate":"2012-10-20 00:00:00.000","Volatility":"0.25","Price":"5.5534","Delta":"-0.6412","Gamma":"0.0436","Theta":"-0.0249","Vega":"0.1274","Rho":"-0.0852","Lambda":"-11.172"},{"z":"P","Strike":"102.00","exDate":"2012-10-20 00:00:00.000","Volatility":"0.25","Price":"6.2284","Delta":"-0.6827","Gamma":"0.0417","Theta":"-0.0227","Vega":"0.1291","Rho":"-0.0911","Lambda":"-10.6052"},{"z":"P","Strike":"98.00","exDate":"2012-11-17 00:00:00.000","Volatility":"0.17","Price":"3.0422","Delta":"-0.4941","Gamma":"0.0541","Theta":"-0.0125","Vega":"0.1755","Rho":"-0.1031","Lambda":"-15.7167"},{"z":"C","Strike":"99.00","exDate":"2012-11-17 00:00:00.000","Volatility":"0.24","Price":"3.6311","Delta":"0.4769","Gamma":"0.0383","Theta":"-0.0346","Vega":"0.1699","Rho":"0.0862","Lambda":"12.7068"},{"z":"P","Strike":"100.00","exDate":"2012-11-17 00:00:00.000","Volatility":"0.17","Price":"4.1809","Delta":"-0.5985","Gamma":"0.0524","Theta":"-0.0102","Vega":"0.163","Rho":"-0.1259","Lambda":"-13.8504"},{"z":"P","Strike":"101.00","exDate":"2012-11-17 00:00:00.000","Volatility":"0.25","Price":"6.1515","Delta":"-0.5902","Gamma":"0.0359","Theta":"-0.0193","Vega":"0.1742","Rho":"-0.1282","Lambda":"-9.2834"},{"z":"P","Strike":"102.00","exDate":"2012-11-17 00:00:00.000","Volatility":"0.22","Price":"6.312","Delta":"-0.6451","Gamma":"0.0389","Theta":"-0.0139","Vega":"0.1648","Rho":"-0.1393","Lambda":"-9.8885"},{"z":"C","Strike":"98.00","exDate":"2012-12-22 00:00:00.000","Volatility":"0.21","Price":"4.5931","Delta":"0.5339","Gamma":"0.0359","Theta":"-0.0272","Vega":"0.2107","Rho":"0.1406","Lambda":"11.2477"},{"z":"C","Strike":"99.00","exDate":"2012-12-22 00:00:00.000","Volatility":"0.19","Price":"3.6944","Delta":"0.4939","Gamma":"0.0399","Theta":"-0.025","Vega":"0.2067","Rho":"0.1317","Lambda":"12.9365"},{"z":"C","Strike":"100.00","exDate":"2012-12-22 00:00:00.000","Volatility":"0.21","Price":"3.70","Delta":"0.4639","Gamma":"0.0358","Theta":"-0.0263","Vega":"0.2094","Rho":"0.123","Lambda":"12.1316"},{"z":"C","Strike":"101.00","exDate":"2012-12-22 00:00:00.000","Volatility":"0.15","Price":"2.0675","Delta":"0.3869","Gamma":"0.0484","Theta":"-0.0192","Vega":"0.2042","Rho":"0.1056","Lambda":"18.1071"},{"z":"P","Strike":"102.00","exDate":"2012-12-22 00:00:00.000","Volatility":"0.22","Price":"6.7402","Delta":"-0.5964","Gamma":"0.0334","Theta":"-0.0111","Vega":"0.1988","Rho":"-0.1925","Lambda":"-8.5625"},{"z":"C","Strike":"98.00","exDate":"2013-03-16 00:00:00.000","Volatility":"0.24","Price":"7.4659","Delta":"0.5695","Gamma":"0.0233","Theta":"-0.0242","Vega":"0.2759","Rho":"0.2519","Lambda":"7.3807"},{"z":"C","Strike":"99.00","exDate":"2013-03-16 00:00:00.000","Volatility":"0.17","Price":"5.0212","Delta":"0.5415","Gamma":"0.0333","Theta":"-0.0193","Vega":"0.2825","Rho":"0.2506","Lambda":"10.4356"},{"z":"P","Strike":"100.00","exDate":"2013-03-16 00:00:00.000","Volatility":"0.20","Price":"5.8236","Delta":"-0.4827","Gamma":"0.0284","Theta":"-0.0068","Vega":"0.275","Rho":"-0.2777","Lambda":"-8.0206"},{"z":"C","Strike":"101.00","exDate":"2013-03-16 00:00:00.000","Volatility":"0.19","Price":"4.7036","Delta":"0.4851","Gamma":"0.0299","Theta":"-0.02","Vega":"0.2811","Rho":"0.2233","Lambda":"9.9784"},{"z":"C","Strike":"102.00","exDate":"2013-03-16 00:00:00.000","Volatility":"0.21","Price":"4.8521","Delta":"0.4662","Gamma":"0.027","Theta":"-0.0212","Vega":"0.2836","Rho":"0.2129","Lambda":"9.2962"},{"z":"P","Strike":"98.00","exDate":"2013-03-22 00:00:00.000","Volatility":"0.16","Price":"3.7663","Delta":"-0.4204","Gamma":"0.0343","Theta":"-0.0046","Vega":"0.2799","Rho":"-0.2423","Lambda":"-10.8015"},{"z":"P","Strike":"99.00","exDate":"2013-03-22 00:00:00.000","Volatility":"0.25","Price":"6.7833","Delta":"-0.4488","Gamma":"0.0222","Theta":"-0.0103","Vega":"0.2836","Rho":"-0.2737","Lambda":"-6.4015"},{"z":"P","Strike":"100.00","exDate":"2013-03-22 00:00:00.000","Volatility":"0.23","Price":"6.7043","Delta":"-0.4741","Gamma":"0.0244","Theta":"-0.0087","Vega":"0.2884","Rho":"-0.2866","Lambda":"-6.8428"},{"z":"C","Strike":"101.00","exDate":"2013-03-22 00:00:00.000","Volatility":"0.22","Price":"5.6755","Delta":"0.4983","Gamma":"0.0254","Theta":"-0.022","Vega":"0.282","Rho":"0.232","Lambda":"8.4955"},{"z":"P","Strike":"102.00","exDate":"2013-03-22 00:00:00.000","Volatility":"0.16","Price":"5.8326","Delta":"-0.5547","Gamma":"0.0347","Theta":"-0.0025","Vega":"0.2742","Rho":"-0.3243","Lambda":"-9.2015"}]}

In this example, we translate some of the Greek values into monetary values in order to anticipate how changes in volatility, underlying, and time might affect the profit (or loss) on a long call position with a notional value of 10,000,000, which was bought at 0.50.

DECLARE @notional as float;
DECLARE @position as float;
SET @position = .50;
SET @notional = 10000000;

SELECT n.S as Spot,
       CAST((k.Price - @position) * @notional / 100 as money) as [P/L],
       CAST(k.Delta * @notional / 100 as money) as Delta,
       CAST(k.Gamma * @notional / 100 as money) as Gamma,
       CAST(k.Vega * @notional / 100 as money) as Vega
  FROM (   SELECT 122
           UNION ALL
           SELECT 123.50
           UNION ALL
           SELECT 124
           UNION ALL
           SELECT 124.50
           UNION ALL
           SELECT 125
           UNION ALL
           SELECT 125.5
           UNION ALL
           SELECT 126
           UNION ALL
           SELECT 126.50
           UNION ALL
           SELECT 127) n(s)
 CROSS APPLY wct.BinomialPriceNGreeks('C',
                                      n.s, --Asset price    
                                      125, --Strike   
                                      Cast(7 as float) / cast(365 as float), --Time
                                      0.000335699, --Continuously Compounded EURIBOR
                                      0.001869966, --Continuously Compounded LIBOR
                                      .09, --Volatility
                                      100, --Number of Steps
                                      'A' --American/European
       ) k
 ORDER BY 1 DESC;

This produces the following result.

{"columns":[{"field":"Spot","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"P/L","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Delta","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Gamma","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Vega","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Spot":"127.00","P/L":"157322.5822","Delta":"89996.829","Gamma":"11171.3603","Vega":"3353.3322"},{"Spot":"126.50","P/L":"113915.0015","Delta":"83247.0264","Gamma":"15993.6184","Vega":"4646.9353"},{"Spot":"126.00","P/L":"74513.5319","Delta":"74049.1927","Gamma":"20749.9129","Vega":"5726.694"},{"Spot":"125.50","P/L":"40369.7907","Delta":"62726.1002","Gamma":"24276.7954","Vega":"6635.1617"},{"Spot":"125.00","P/L":"11830.6385","Delta":"50168.7746","Gamma":"25811.61","Vega":"6888.2779"},{"Spot":"124.50","P/L":"-9789.5297","Delta":"37549.8534","Gamma":"24517.2396","Vega":"6600.5949"},{"Spot":"124.00","P/L":"-25731.6818","Delta":"26076.0942","Gamma":"21104.6256","Vega":"5619.4685"},{"Spot":"123.50","P/L":"-36386.1957","Delta":"16681.0012","Gamma":"16305.0987","Vega":"4577.8491"},{"Spot":"122.00","P/L":"-48527.6995","Delta":"2548.4728","Gamma":"3906.0068","Vega":"1232.1331"}]}