Logo

SQL Server BlackScholesMertonPriceNGreeks Function

Updated 2023-11-16 16:47:08.450000

Description

Use the table-valued function BlackScholesMertonPriceNGreeks to calculate the price and other derivatives of a European option using the Black-Scholes-Merton option pricing formula.

Syntax

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

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 continuously compounded risk-free zero coupon 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 continuously compounded dividend zero-coupon 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.

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.
GammaPfloatGamma multiplied by asset price divided by strike price.
DdeltaDtimefloatThe instantaneous change in delta over the passage of time; the second derivative, once to asset price and once to time.
DdeltaDvolfloatThe sensitivity of delta with respect to volatility; the second derivative, once to asset price and once to volatility.
DdeltaDvolDvolfloatThe second derivative of delta with respect to volatility; the third derivative, once to asset price and twice to volatility.
DgammaDvolfloatThe rate of change in gamma with respect to changes in volatil
DvegaDvolfloatThe rate of change to vega as the volatility changes; the second derivative with respect to volatility.
VegaPfloatThe percentage change in theoretical value for a 10 per cent change in volatility.
PhiRho2floatThe sensitivity to a change in the dividend yield (foreign interest rate for a currency option); the first derivative with respect to dividend yield.
DgammaDspotfloatThe rate of change in gamma with respect to change in the asset price; the third derivative with respect to price.
DeltaXfloatThe sensitivity to a change in the strike price; the first derivative with respect to strike price.
RiskNeutralDensityfloatThe sensitivity of DeltaX; the second derivative with respect to strike price.
DvommaDvolfloatThe sensitivity of DvegaDvol to changes in volatility; the third derivative, twice to asset price and once to volatility.
DgammaDtimefloatThe sensitivity of Gamma to the passage of time; the third derivative, twice to asset price and once to time.
DvegaDtimefloatThe sensitivity of Vega to the passage of time; the second derivative, once to volatility and once to time.
ForwardPricefloatThe value of the underlying asset at the expiration date of the option.
ForwardPointsfloatThe difference between the ForwardPrice and the asset price.

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

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

If @DividendRate is NULL then @DividendRate = 0.

If @RiskFreeRate is NULL then @RiskFreeRate = 0.

For American-style options see BJERKSUNDSTENSLANDPRICENGREEKS.

Examples

Calculate the price and Greeks for a 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.

SELECT *
FROM wct.BlackScholesMertonPriceNGreeks(   'C',                                                --Put/Call
                                           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
                                       ) k;

Here is the resultant table.

PriceDeltaGammaThetaVegaRhoLambdaGammaPDdeltaDtimeDdeltaDvolDdeltaDvolDvolDgammaDvolDvegaDvolVegaPPhiRho2DgammaDspotDeltaXRiskNeutralDensityDvommaDvolDgammaDtimeDvegaDtimeForwardPriceForwardPoints
4.150028010729810.5172630074627640.0378316108959817-0.0224105824489930.2093332862103610.13223011843395312.40176430363270.0376424528415018-0.0002658840584352010.00120640336138375-1.56126607450338E-05-0.00189675260903188-2.86185329331919E-050.418666572420722-0.143827456785511-0.000542407991020922-0.4731764123181520.0374542405772943-1.55316913764812E-060.000187137587406527-0.10168005547594199.91795756586940.417957565869372

In this SELECT we unpivot the columns returned by the function for ease of viewing the results.

SELECT n.*
FROM wct.BlackScholesMertonPriceNGreeks(   'C',                                                --Put/Call
                                           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
                                       ) k
    CROSS APPLY
(
    VALUES
        ('Price', Price),
        ('Delta', Delta),
        ('Gamma', Gamma),
        ('Theta', Theta),
        ('Vega', Vega),
        ('Rho', Rho),
        ('Lambda', Lambda),
        ('GammaP', GammaP),
        ('DdeltaDtime', DdeltaDtime),
        ('DdeltaDvol', DdeltaDvol),
        ('DdeltaDvolDvol', DdeltaDvolDvol),
        ('DgammaDvol', DgammaDvol),
        ('DvegaDvol', DvegaDvol),
        ('VegaP', VegaP),
        ('PhiRho2', PhiRho2),
        ('DgammaDspot', DgammaDspot),
        ('DeltaX', DeltaX),
        ('RiskNeutralDensity', RiskNeutralDensity),
        ('DvommaDvol', DvommaDvol),
        ('DgammaDtime', DgammaDtime),
        ('DvegaDtime', DvegaDtime),
        ('ForwardPrice', ForwardPrice),
        ('ForwardPoints', ForwardPoints)
) n ([Return Value], Value);

This produces the following result.

Return ValueValue
Price4.15002801072982
Delta0.517263007462764
Gamma0.0378316108959817
Theta-0.022410582448993
Vega0.209333286210361
Rho0.132230118433953
Lambda12.4017643036327
GammaP0.0376424528415018
DdeltaDtime-0.000265884058435201
DdeltaDvol0.00120640336138375
DdeltaDvolDvol-1.56126607450338E-05
DgammaDvol-0.00189675260903188
DvegaDvol-2.86185329331919E-05
VegaP0.418666572420722
PhiRho2-0.143827456785512
DgammaDspot-0.000542407991020922
DeltaX-0.473176412318152
RiskNeutralDensity0.0374542405772943
DvommaDvol-1.55316913764812E-06
DgammaDtime0.000187137587406527
DvegaDtime-0.101680055475941
ForwardPrice99.9179575658694
ForwardPoints0.417957565869372

In the following SELECT, we will populate a derived table with some option information and then calculate the price and select 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.BlackScholesMertonPriceNGreeks(   O.z,                                        --Put/Call
                                                      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
                                                  ) k;
/*Clean up table*/
DROP TABLE #opt;

This produces the following result.

zStrikeexDateVolatilityPriceDeltaGammaThetaVegaRhoLambda
C98.002012-09-22 00:00:00.0000.231.530.43180.0795-0.05990.08450.019927.3097
P99.002012-09-22 00:00:00.0000.152.519-0.72240.104-0.01930.072-0.0357-27.7503
C100.002012-09-22 00:00:00.0000.150.32330.18610.0831-0.02660.05760.008755.7059
C101.002012-09-22 00:00:00.0000.210.49490.20070.0622-0.0380.06030.009339.2314
P102.002012-09-22 00:00:00.0000.195.209-0.8780.0495-0.00950.0434-0.0445-16.3094
P98.002012-10-20 00:00:00.0000.182.7705-0.5240.0644-0.01880.1367-0.0674-18.3014
P99.002012-10-20 00:00:00.0000.244.1557-0.55790.0479-0.02670.1355-0.0733-12.989
C100.002012-10-20 00:00:00.0000.211.81110.3770.0526-0.03490.13040.043720.1391
P101.002012-10-20 00:00:00.0000.255.5468-0.64110.0435-0.02480.1283-0.0852-11.184
P102.002012-10-20 00:00:00.0000.256.2355-0.68170.0415-0.02250.1225-0.091-10.5781
P98.002012-11-17 00:00:00.0000.173.0416-0.49370.0538-0.01240.1737-0.103-15.7054
C99.002012-11-17 00:00:00.0000.243.63740.47730.0381-0.03440.17340.086312.696
P100.002012-11-17 00:00:00.0000.174.1816-0.59790.0522-0.01010.1684-0.1258-13.8352
P101.002012-11-17 00:00:00.0000.256.1555-0.58960.0357-0.01920.1693-0.1281-9.2677
P102.002012-11-17 00:00:00.0000.226.3061-0.64480.0388-0.01390.1621-0.1393-9.8933
C98.002012-12-22 00:00:00.0000.214.58270.53420.0358-0.02720.210.140711.2803
C99.002012-12-22 00:00:00.0000.193.69780.49440.0397-0.02490.21080.131812.9366
C100.002012-12-22 00:00:00.0000.213.69020.46420.0358-0.02630.20990.123112.1709
C101.002012-12-22 00:00:00.0000.152.06220.38740.0483-0.01920.20240.105818.1769
P102.002012-12-22 00:00:00.0000.226.7389-0.5960.0333-0.01110.2046-0.1923-8.5574
C98.002013-03-16 00:00:00.0000.247.45240.56980.0232-0.02420.27590.25217.3984
C99.002013-03-16 00:00:00.0000.175.02770.54220.0331-0.01930.27860.250810.4339
P100.002013-03-16 00:00:00.0000.205.826-0.48220.0283-0.00670.28-0.2775-8.0087
C101.002013-03-16 00:00:00.0000.194.69210.48550.0298-0.020.28010.223610.0126
C102.002013-03-16 00:00:00.0000.214.84510.46670.0269-0.02110.27930.21329.3202
P98.002013-03-22 00:00:00.0000.163.7562-0.41980.0341-0.00460.2789-0.2419-10.8137
P99.002013-03-22 00:00:00.0000.256.7686-0.44840.0221-0.01020.2823-0.2734-6.4099
P100.002013-03-22 00:00:00.0000.236.7179-0.47350.0242-0.00860.284-0.2864-6.8206
C101.002013-03-22 00:00:00.0000.225.66540.49870.0253-0.0220.28460.23228.5172
P102.002013-03-22 00:00:00.0000.165.8302-0.5540.0345-0.00250.2819-0.324-9.1938

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.BlackScholesMertonPriceNGreeks(   '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
                                                  ) k
ORDER BY 1 DESC;

This produces the following result.

SpotP/LDeltaGammaVega
127.00157223.26389924.479111145.2983102.7502
126.50113849.872183167.904615945.70794404.2558
126.0074461.346373992.108220656.09265660.2674
125.5040212.400862707.066324198.43636578.4328
125.0011967.999250152.632125605.6696905.6385
124.50-9929.344437534.053724443.41246539.5554
124.00-25763.526426088.941821024.40685579.7508
123.50-36369.57816732.837716273.20814284.0525
122.00-48513.07172587.37473955.81711016.257