Logo

SQL Server StandardBarrier Function

Updated 2023-11-14 21:22:16.187000

Description

Use StandardBarrier to calculate the price or Greeks of a European-style Knock-In or Knock-Out option. StandardBarrier valuations are based on the formulae published by Mark Rubinstein and Eric Reiner in 1991.

Syntax

SELECT [westclintech].[wct].[StandardBarrier](
  <@CallPut, nvarchar(4000),>
 ,<@BarrierType, nvarchar(4000),>
 ,<@AssetPrice, float,>
 ,<@StrikePrice, float,>
 ,<@BarrierPrice, float,>
 ,<@Rebate, float,>
 ,<@TimeToMaturity, float,>
 ,<@RiskFreeRate, float,>
 ,<@DividendRate, float,>
 ,<@Volatility, float,>
 ,<@ReturnValue, 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.

@BarrierType

identifies the type of barrier as 'UI' (Up-and-In), 'UO' (Up-and-Out), 'DI' (Down-and-In), or 'DO' (Down-and-out). @BarrierType must be of a type nvarchar or of a type that implicitly converts 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.

@BarrierPrice

For a knock-in option, @BarrierPrice is the value at which the option comes into existence if the @AssetPrice crosses the barrier. For a knock-out option, @BarrierPrice is the value at which the option is extinguished if the @AssetPrice crosses the barrier. @BarrierPrice must be of a type float or of a type that implicitly converts to float.

@Rebate

An amount paid to the buyer of the option in the event that the barrier is never breached. @Rebate must be of a type float or of a type that implicitly converts 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 zero coupon risk-free 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 zero coupon dividend rate over the life of the option. For currency options @DividendRate should be the foreign risk-free zero coupon 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.

@ReturnValue

identifies the calculation to be performed. @ReturnValue is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar. For a full description of the return values, see STANDARDBARRIERPRICENGREEKS STANDARDBARRIERPRICENGREEKS. @ReturnValue is not case-sensitive. The following values are acceptable for @ReturnValue:

@ReturnValueReturns
'P','PRICE'Price
'D','DELTA'Delta
'G','GAMMA'Gamma
'T','THETA'Theta
'V','VEGA'Vega
'R','RHO'Rho
'L','LAMBDA'Lambda
'DDDV','VANNA','DVEGADSPOT','DDELTADVOL'DdeltaDvol
'DVV','DDELTADVOLDVOL'DdeltaDvolDvol
'DT','CHARM','DDELTADTIME'DdeltaDtime
'GV','ZOMMA','DGAMMADVOL'DgammaDvol
'GP','GAMMAP'GammaP
'DVDV','VOMMA','VOLGA','DVEGADVOL'DvegaDvol
'VP','VEGAP'VegaP
'PR2','PHIRHO2'PhiRho2
'S','SPEED','DGAMMADSPOT'DgammaDspot
'DX','DELTAX'Delta X
'GX','GAMMAX','DX','RND','RISKNEUTRALDENSITY'Risk Neutral Density
'VVV','ULTIMA','DVOMMADVOL'DvommaDvol
'VT','VETA','DVEGADTIME'DvegaDtime
'GT','COLOR','DGAMMADTIME'DgammaDtime
'FR','RHOFUTURESOPTIONS','FUTURESOPTIONSRHO'Futures Options Rho
'B','CARRYSENSITIVITY'Carry Sensitivity

Return Type

float

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

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

If @ReturnValue is NULL, then @ReturnValue is set to 'P'.

If @DividendRate is NULL then @DividendRate = 0.

If @RiskFreeRate is NULL @RiskFreeRate = 0.

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

@Rebate must be greater than or equal to zero (@Rebate >= 0).

If @Rebate is NULL, then @Rebate = 0.

@BarrierPrice assumes continuous monitoring.

To convert a non-continuous @BarrierPrice use the ADJUSTEDBARRIER function.

To calculate the price and all the Greeks use STANDARDBARRIERPRICENGREEKS.

Examples

A down-and-in call.

SELECT cast(wct.StandardBarrier(   'C',  --PutCall
                                   'DI', --BarrierType
                                   100,  --Asset Price
                                   90,   --Strike Price
                                   97,   --Barrier
                                   2,    --Rebate
                                   0.5,  --Time-to-expiry
                                   .10,  --Risk Free Rate
                                   .05,  --Dividend Rate
                                   .20,  --Volatility
                                   'P'   --Return Value
                               ) as money) as Price;

This produces the following result.

Price
8.5951

This SELECT statement reproduces the table for a down-and-in call included in the original paper on Barrier Options by Rubinstein and Reiner, though the numerical values are different.

SELECT *
FROM
(
    SELECT n.t,
           n3.Barrier,
           n2.Strike,
           cast(wct.StandardBarrier(   'C',        --PutCall
                                       'DI',       --BarrierType
                                       100,        --Asset Price
                                       n2.Strike,  --Strike Price
                                       n3.Barrier, --Barrier
                                       2,          --Rebate
                                       n.t,        --Time-to-expiry
                                       .10,        --Risk Free Rate
                                       .05,        --Dividend Rate
                                       .20,        --Volatility
                                       'P'         --Return Value
                                   ) as money) as Price
    FROM
    (
        VALUES
            (0.5),
            (0.75),
            (1.00),
            (1.25),
            (1.50)
    ) n (t)
        CROSS APPLY
    (
        VALUES
            (90),
            (100),
            (110)
    ) n2 (Strike)
        CROSS APPLY
    (
        VALUES
            (97),
            (100),
            (103)
    ) n3 (Barrier)
) D
PIVOT
(
    SUM(Price)
    for t in ([0.5], [0.75], [1.00], [1.25], [1.50])
) as P
ORDER BY 1,
         2;

This produces the following result.

BarrierStrike0.50.751.001.251.50
97908.595110.046111.306612.432413.4536
971003.95195.37196.6467.8078.8761
971101.62242.6223.64494.64615.6113
1009013.165214.605515.88517.037918.0873
1001006.71868.44949.940911.268512.4716
1001102.83474.35565.74557.02658.2156
1039013.165214.605515.88517.037918.0873
1031006.71868.44949.940911.268512.4716
1031102.83474.35565.74557.02658.2156

We can reproduce the table in its entirety with the following SQL.

SELECT *
FROM
(
    SELECT n.t,
           n5.Z,
           n4.BarrierType,
           n3.Barrier,
           n2.Strike,
           cast(wct.StandardBarrier(n5.Z, n4.BarrierType, 100, n2.Strike, n3.Barrier,
                     2, n.t, .10, .05, .20, 'P') as money) as Price
    FROM
    (
        VALUES
            (0.5),
            (0.75),
            (1.00),
            (1.25),
            (1.50)
    ) n (t)
        CROSS APPLY
    (
        VALUES
            (90),
            (100),
            (110)
    ) n2 (Strike)
        CROSS APPLY
    (
        VALUES
            (97),
            (100),
            (103)
    ) n3 (Barrier)
        CROSS APPLY
    (
        VALUES
            ('UI'),
            ('DI'),
            ('UO'),
            ('DO')
    ) n4 (BarrierType)
        CROSS APPLY
    (
        VALUES
            ('C'),
            ('P')
    ) n5 (Z)
) D
PIVOT
(
    SUM(Price)
    for t in ([0.5], [0.75], [1.00], [1.25], [1.50])
) as P
ORDER BY 1,
         2;

Here are the results of this query.

ZBarrierTypeBarrierStrike0.50.751.001.251.50
CDI97908.595110.046111.306612.432413.4536
CDI971003.95195.37196.6467.8078.8761
CDI971101.62242.6223.64494.64615.6113
CDI1009013.165214.605515.88517.037918.0873
CDI1001006.71868.44949.940911.268512.4716
CDI1001102.83474.35565.74557.02658.2156
CDI1039013.165214.605515.88517.037918.0873
CDI1031006.71868.44949.940911.268512.4716
CDI1031102.83474.35565.74557.02658.2156
CDO97906.53916.52026.53246.55346.5761
CDO971004.73575.03835.24885.40935.5379
CDO971103.18133.69444.05454.32834.5467
CDO100902.002.002.002.002.00
CDO1001002.002.002.002.002.00
CDO1001102.002.002.002.002.00
CDO103902.002.002.002.002.00
CDO1031002.002.002.002.002.00
CDO1031102.002.002.002.002.00
CUI979013.165214.605515.88517.037918.0873
CUI971006.71868.44949.940911.268512.4716
CUI971102.83474.35565.74557.02658.2156
CUI1009013.165214.605515.88517.037918.0873
CUI1001006.71868.44949.940911.268512.4716
CUI1001102.83474.35565.74557.02658.2156
CUI1039013.193714.682615.973217.126318.1724
CUI1031006.99618.664810.118611.420212.6039
CUI1031103.11554.57285.92447.17898.3485
CUO97902.002.002.002.002.00
CUO971002.002.002.002.002.00
CUO971102.002.002.002.002.00
CUO100902.002.002.002.002.00
CUO1001002.002.002.002.002.00
CUO1001102.002.002.002.002.00
CUO103901.94431.88951.87361.86921.869
CUO1031001.69541.75121.7841.8061.8218
CUO1031101.6921.74941.78291.80521.8212
PDI97901.60632.07972.45442.75032.9845
PDI971004.66845.19915.55745.8055.9752
PDI9711010.044110.242710.319910.324110.2785
PDI100901.24491.78292.19742.52132.7767
PDI1001004.31064.90435.30175.57695.768
PDI1001109.93910.087910.154710.159810.1191
PDI103901.24491.78292.19742.52132.7767
PDI1031004.31064.90435.30175.57695.768
PDI1031109.93910.087910.154710.159810.1191
PDO97901.60761.66411.6971.71891.7347
PDO971001.61121.6661.69821.71981.7353
PDO971101.86391.8061.78871.78371.783
PDO100902.002.002.002.002.00
PDO1001002.002.002.002.002.00
PDO1001102.002.002.002.002.00
PDO103902.002.002.002.002.00
PDO1031002.002.002.002.002.00
PDO1031102.002.002.002.002.00
PUI97901.24491.78292.19742.52132.7767
PUI971004.31064.90435.30175.57695.768
PUI971109.93910.087910.154710.159810.1191
PUI100901.24491.78292.19742.52132.7767
PUI1001004.31064.90435.30175.57695.768
PUI1001109.93910.087910.154710.159810.1191
PUI103900.86151.22011.54751.82922.0671
PUI1031002.77223.39363.84714.18574.4412
PUI1031107.00017.4937.80698.00728.1283
PUO97902.002.002.002.002.00
PUO971002.002.002.002.002.00
PUO971102.002.002.002.002.00
PUO100902.002.002.002.002.00
PUO1001002.002.002.002.002.00
PUO1001102.002.002.002.002.00
PUO103902.35622.52952.61172.64972.6637
PUO1031003.51123.47733.41643.34883.2809
PUO1031104.91174.56164.30954.11033.9448

To calculate the delta for the first option:

SELECT wct.StandardBarrier(   'C',  --PutCall
                              'DI', --BarrierType
                              100,  --Asset Price
                              90,   --Strike Price
                              97,   --Barrier
                              2,    --Rebate
                              0.5,  --Time-to-expiry
                              .10,  --Risk Free Rate
                              .05,  --Dividend Rate
                              .20,  --Volatility
                              'D'   --Return Value
                          ) as Delta;

This produces the following result.

Delta
-0.649025843078022

In this example we show all of the return values for an option (which can be more easily done using STANDARDBARRIERPRICENGREEKS).

SELECT n.rv as Description,
       wct.StandardBarrier('C', 'DI', 100, 90, 97, 2, 0.5, .10, .05, .20, n.rv) 
                 as Value
FROM
(
    SELECT 'Price'
    UNION ALL
    SELECT 'Delta'
    UNION ALL
    SELECT 'Gamma'
    UNION ALL
    SELECT 'GammaX'
    UNION ALL
    SELECT 'Theta'
    UNION ALL
    SELECT 'Vega'
    UNION ALL
    SELECT 'Rho'
    UNION ALL
    SELECT 'RhoFuturesOption'
    UNION ALL
    SELECT 'Lambda'
    UNION ALL
    SELECT 'DdeltaDvol'
    UNION ALL
    SELECT 'DdeltaDvolDvol'
    UNION ALL
    SELECT 'DdeltaDtime'
    UNION ALL
    SELECT 'DgammaDvol'
    UNION ALL
    SELECT 'GammaP'
    UNION ALL
    SELECT 'DvegaDvol'
    UNION ALL
    SELECT 'VegaP'
    UNION ALL
    SELECT 'PhiRho2'
    UNION ALL
    SELECT 'CarrySensitivity'
    UNION ALL
    SELECT 'DgammaDspot'
    UNION ALL
    SELECT 'DeltaX'
    UNION ALL
    SELECT 'DvommaDvol'
    UNION ALL
    SELECT 'DvegaDtime'
    UNION ALL
    SELECT 'DgammaDtime'
) n(rv);

This produces the following result.

DescriptionValue
Price8.59507043337689
Delta-0.649025843078022
Gamma0.052092730129516
GammaX0.0207577954824956
Theta-0.0173153378580135
Vega0.258665904443234
Rho0.156984606562283
RhoFuturesOption-0.0379626818828527
Lambda-7.55114048347627
DdeltaDvol0.0135552546964846
DdeltaDvolDvol-0.650285444002208
DdeltaDtime0.000306086723699144
DgammaDvol-0.00416152983184759
GammaP0.052092730129516
DvegaDvol-0.00199687541169169
VegaP0.517331808886468
PhiRho2-0.199959958365525
CarrySensitivity0.199959958365525
DgammaDspot12980.5178819264
DeltaX-0.582783834275347
DvommaDvol0.000564868690799969
DvegaDtime-0.0699760427380625
DgammaDtime-5.04370755206702E-05