Logo

SQL Server BjerksundStenslandPriceNGreeks Function

Updated 2023-11-16 16:35:33.717000

Description

Use the table-valued function BjerksundStenslandPriceNGreeks to calculate the price and other derivatives of an American option using the Bjerksund & Stensland 2002 option pricing formula.

Syntax

SELECT *
FROM [westclintech].[wct].[BjerksundStenslandPriceNGreeks] (
  <@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 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.

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 volatility; the third derivative, twice to asset price and once to volatility.
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).

@Price must be greater than zero.

If @DividendRate is NULL then @DividendRate = 0.

If @RiskFreeRate is NULL then @RiskFreeRate = 0.

Examples

Calculate the price and Greeks for a put 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.BjerksundStenslandPriceNGreeks(   'P',                                                --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                                                 --Price
                                       ) k;

Here is the resultant table.

PriceDeltaGammaThetaVegaRhoLambdaGammaPDdeltaDtimeDdeltaDvolDdeltaDvolDvolDgammaDvolDvegaDvolVegaPPhiRho2DgammaDspotDeltaXRiskNeutralDensityDvommaDvolDgammaDtimeDvegaDtimeForwardPriceForwardPoints
4.25309729423103-0.4856189539736990.0385625753551722-0.01862723574662080.209275569006806-0.129024971219138-11.36091713347910.0383697624783963-0.0002506434633662690.00144320289052757-4.27457486296134E-05-0.00198143297325259-8.10906897186214E-060.4185511380136120.12022472119142-0.0006431228882775030.5257218321119690.0381881193334266-6.06312401885134E-060.000185778669160692-0.10173587926436599.91795756586940.417957565869372

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

SELECT n.*
FROM wct.BjerksundStenslandPriceNGreeks(   'P',                                                --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                                                 --Price
                                       ) 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.253097294231
Delta-0.485618954151334
Gamma0.0385703913252655
Theta-0.0186272357466564
Vega0.209275569007161
Rho-0.129024971220559
Lambda-11.3609171376349
GammaP0.0383775393686392
DdeltaDtime-0.000250644436712482
DdeltaDvol0.00144318512695918
DdeltaDvolDvol-4.27457528928699E-05
DgammaDvol-0.00198143290219832
DvegaDvol-8.10871370049426E-06
VegaP0.418551138014323
PhiRho20.120224721192841
DgammaDspot-0.000643179731696364
DeltaX0.525721831898807
RiskNeutralDensity0.0381909615043696
DvommaDvol-6.06312403661491E-06
DgammaDtime0.000185778630226844
DvegaDtime-0.101735880237712
ForwardPrice99.9179575658694
ForwardPoints0.417957565869372

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 o f 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 date*/
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.BjerksundStenslandPriceNGreeks(   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
                                                  ) 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.06060.08450.019927.3097
P99.002012-09-22 00:00:00.0000.152.564-0.7470.115-0.02090.0675-0.0245-28.1882
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.3142-0.91370.0579-0.01130.0322-0.0182-16.6365
P98.002012-10-20 00:00:00.0000.182.8239-0.54170.0694-0.02020.1348-0.0539-18.5597
P99.002012-10-20 00:00:00.0000.244.2143-0.57190.0508-0.02810.1339-0.0591-13.1299
C100.002012-10-20 00:00:00.0000.211.81110.3770.0526-0.0350.13040.043720.1391
P101.002012-10-20 00:00:00.0000.255.6361-0.660.0469-0.02650.1246-0.0637-11.3316
P102.002012-10-20 00:00:00.0000.256.3454-0.7040.0453-0.02430.1172-0.0644-10.7359
P98.002012-11-17 00:00:00.0000.173.1341-0.51940.0601-0.01380.1703-0.0789-16.0348
C99.002012-11-17 00:00:00.0000.243.63740.47730.0381-0.03450.17340.086312.696
P100.002012-11-17 00:00:00.0000.174.3404-0.63830.061-0.0120.158-0.0843-14.2293
P101.002012-11-17 00:00:00.0000.256.2894-0.61260.0391-0.02090.1647-0.0953-9.4244
P102.002012-11-17 00:00:00.0000.226.4887-0.67890.0443-0.01590.1524-0.0934-10.1239
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.9908-0.63570.0388-0.01310.1929-0.1286-8.7993
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.206.1762-0.5310.0344-0.00860.2686-0.1887-8.3196
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.164.052-0.47340.0429-0.00630.2701-0.1665-11.3038
P99.002013-03-22 00:00:00.0000.257.0533-0.48010.0254-0.0120.2784-0.2034-6.5869
P100.002013-03-22 00:00:00.0000.237.0514-0.51330.0285-0.01040.2764-0.2034-7.0439
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.166.4279-0.65070.0483-0.0050.24-0.1699-9.7956

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.BjerksundStenslandPriceNGreeks(   '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.00157135.968189662.800710548.11353136.381
126.50113838.207583095.322315731.77144406.4977
126.0074467.405273981.219520596.57335657.3932
125.5040219.187362710.967124188.43796576.4603
125.0011972.340350157.539125608.9556904.887
124.50-9927.060237537.293524446.22286539.4324
124.00-25762.46526090.686921027.94385579.8336
123.50-36369.136516733.661416274.5554284.1527
122.00-48513.05592587.41563956.15981016.2707