Logo

SQL Server BinomialPriceNGreeks Function

Updated 2023-11-16 15:59:39.620000

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.

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),>)

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

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

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

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

@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
Pricefloatthe theoretical value of the option
Deltafloatthe sensitivity to small changes in the asset price; the first derivative of the option with respect to price
GammafloatThe rate of change in Delta with respect to small changes in the asset price; the second derivative of the option with respect to price.
ThetafloatThe sensitivity to small changes in time; the first derivative of the option with respect to time.
VegafloatThe sensitivity to small changes in volatility; the first derivative of the option with respect to volatility.
RhofloatThe sensitivity to small changes in the risk-free rate; the first derivative of the option with respect to the risk-free rate.
LambdafloatDelta 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.

PriceDeltaGammaThetaVegaRhoLambda
4.15470.51720.038-0.02250.2090.132212.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.

zStrikeexDateVolatilityPriceDeltaGammaThetaVegaRhoLambda
C98.002012-09-22 00:00:00.0000.231.53210.43170.0798-0.06010.08350.019827.2637
P99.002012-09-22 00:00:00.0000.152.521-0.72250.1042-0.01930.0713-0.0357-27.7319
C100.002012-09-22 00:00:00.0000.150.32130.1850.0833-0.02660.06240.008755.7107
C101.002012-09-22 00:00:00.0000.210.49690.20050.0622-0.0380.06130.009339.0483
P102.002012-09-22 00:00:00.0000.195.2087-0.87850.0495-0.00950.0429-0.0445-16.3205
P98.002012-10-20 00:00:00.0000.182.7643-0.52450.0649-0.0190.139-0.0674-18.3591
P99.002012-10-20 00:00:00.0000.244.1618-0.5580.048-0.02680.1346-0.0733-12.9729
C100.002012-10-20 00:00:00.0000.211.81250.37660.0528-0.0350.1270.043620.106
P101.002012-10-20 00:00:00.0000.255.5534-0.64120.0436-0.02490.1274-0.0852-11.172
P102.002012-10-20 00:00:00.0000.256.2284-0.68270.0417-0.02270.1291-0.0911-10.6052
P98.002012-11-17 00:00:00.0000.173.0422-0.49410.0541-0.01250.1755-0.1031-15.7167
C99.002012-11-17 00:00:00.0000.243.63110.47690.0383-0.03460.16990.086212.7068
P100.002012-11-17 00:00:00.0000.174.1809-0.59850.0524-0.01020.163-0.1259-13.8504
P101.002012-11-17 00:00:00.0000.256.1515-0.59020.0359-0.01930.1742-0.1282-9.2834
P102.002012-11-17 00:00:00.0000.226.312-0.64510.0389-0.01390.1648-0.1393-9.8885
C98.002012-12-22 00:00:00.0000.214.59310.53390.0359-0.02720.21070.140611.2477
C99.002012-12-22 00:00:00.0000.193.69440.49390.0399-0.0250.20670.131712.9365
C100.002012-12-22 00:00:00.0000.213.700.46390.0358-0.02630.20940.12312.1316
C101.002012-12-22 00:00:00.0000.152.06750.38690.0484-0.01920.20420.105618.1071
P102.002012-12-22 00:00:00.0000.226.7402-0.59640.0334-0.01110.1988-0.1925-8.5625
C98.002013-03-16 00:00:00.0000.247.46590.56950.0233-0.02420.27590.25197.3807
C99.002013-03-16 00:00:00.0000.175.02120.54150.0333-0.01930.28250.250610.4356
P100.002013-03-16 00:00:00.0000.205.8236-0.48270.0284-0.00680.275-0.2777-8.0206
C101.002013-03-16 00:00:00.0000.194.70360.48510.0299-0.020.28110.22339.9784
C102.002013-03-16 00:00:00.0000.214.85210.46620.027-0.02120.28360.21299.2962
P98.002013-03-22 00:00:00.0000.163.7663-0.42040.0343-0.00460.2799-0.2423-10.8015
P99.002013-03-22 00:00:00.0000.256.7833-0.44880.0222-0.01030.2836-0.2737-6.4015
P100.002013-03-22 00:00:00.0000.236.7043-0.47410.0244-0.00870.2884-0.2866-6.8428
C101.002013-03-22 00:00:00.0000.225.67550.49830.0254-0.0220.2820.2328.4955
P102.002013-03-22 00:00:00.0000.165.8326-0.55470.0347-0.00250.2742-0.3243-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.

SpotP/LDeltaGammaVega
127.00157322.582289996.82911171.36033353.3322
126.50113915.001583247.026415993.61844646.9353
126.0074513.531974049.192720749.91295726.694
125.5040369.790762726.100224276.79546635.1617
125.0011830.638550168.774625811.616888.2779
124.50-9789.529737549.853424517.23966600.5949
124.00-25731.681826076.094221104.62565619.4685
123.50-36386.195716681.001216305.09874577.8491
122.00-48527.69952548.47283906.00681232.1331