Logo

OptionMatrix

Updated 2023-11-17 15:02:31.763000

Syntax

SELECT *
FROM [westclintech].[wct].[OptionMatrix] (
  <@CallPut, nvarchar(4000),>
 ,<@AssetPrice, float,>
 ,<@StrikePrice, float,>
 ,<@TimeToMaturity, float,>
 ,<@RiskFreeRate, float,>
 ,<@DividendRate, float,>
 ,<@Volatility, float,>
 ,<@ReturnValue, nvarchar(4000),>
 ,<@AmEur, nvarchar(4000),>
 ,<@Row, nvarchar(4000),>
 ,<@RowStep, float,>
 ,<@RowNumSteps, int,>
 ,<@Col, nvarchar(4000),>
 ,<@ColStep, float,>
 ,<@ColNumSteps, int,>)

Description

Use the table-valued function OptionMatrix to generate a result set of return values by varying two inputs into the calculated value. For example, you could generate a result set that shows how a change in the underlying and a change in the volatility affect the price.

Arguments

@RowNumSteps

Identifies the number of times that the initial row value is incremented and/or decremented. @RowNumSteps 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.

@ColNumSteps

Identifies the number of times that the initial column value is incremented and/or decremented. @ColNumSteps is an expression of type int or of a type that can be implicitly converted to int.

@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. @ReturnValue is not case-sensitive. The following values are acceptable for @ReturnValue:

{
    "columns": [
        {
            "field": "@ReturnValue"
         },
        {
            "field": "Returns"
        }
    ],
    "rows": [
        {
            "@ReturnValue": "'P','PRICE'",
            "Returns": "Price"
        },
        {
            "@ReturnValue": "'D','DELTA'",
            "Returns": "Delta"
        },
        {
            "@ReturnValue": "'G','GAMMA'",
            "Returns": "Gamma"
        },
        {
            "@ReturnValue": "'T','THETA'",
            "Returns": "Theta"
        },
        {
            "@ReturnValue": "'V','VEGA'",
            "Returns": "Vega"
        },
        {
            "@ReturnValue": "'R','RHO'",
            "Returns": "Rho"
        },
        {
            "@ReturnValue": "'L','LAMBDA'",
            "Returns": "Lambda"
        }
    ]
}

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

@ColStep

Identifies the value by which the intial column value is incremented and/or decremented. In the case of time ('T') the row values are only decremented and the step value is assumed to be expressed in days. @ColStep is an expression of type float or of a type that can be implicitly converted to float.

@RowStep

Identifies the value by which the intial row value is incremented and/or decremented. In the case of time ('T') the row values are only decremented and the step value is assumed to be expressed in days. @RowStep 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.

@Row

Identifies the variable which is changing with each row. @Row is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar. The following values may be passed into @Row:

{
    "columns": [
        {
            "field": "column 1"
        }
    ],
    "rows": [
        {
            "column 1": "'S' , 'U' , 'ASSETP' , 'UNDERLYING'"
        },
        {
            "column 1": "'X' , 'K' , 'STRIKE'"
        },
        {
            "column 1": "'T' , 'TIME'"
        },
        {
            "column 1": "'R' , 'RF' , 'RISKFREE'"
        },
        {
            "column 1": "'D' , 'DIV' , 'DIVIDEND'"
        },
        {
            "column 1": "'V' , 'VOL' , 'VOLATILITY' , 'SIGMA'"
        }
    ]
}

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

@Col

Identifies the variable which is changing with each column. @Col is an expression of type nvarchar or of a type that can be implicitly converted to nvarchar. The following values may be passed into @Col:

{
    "columns": [
        {
            "field": "column 1"
        }
    ],
    "rows": [
        {
            "column 1": "'S' , 'U' , 'ASSETP' , 'UNDERLYING'"
        },
        {
            "column 1": "'X' , 'K' , 'STRIKE'"
        },
        {
            "column 1": "'T' , 'TIME'"
        },
        {
            "column 1": "'R' , 'RF' , 'RISKFREE'"
        },
        {
            "column 1": "'D' , 'DIV' , 'DIVIDEND'"
        },
        {
            "column 1": "'V' , 'VOL' , 'VOLATILITY' , 'SIGMA'"
        }
    ]
}

@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": "90882a04-6fab-4ede-96af-ad2454acec17", "colName": "idx_row", "colDatatype": "int", "colDesc": "The row index into a zero-based 2-dimensional array"}, {"id": "b91a73e6-dd5b-44c7-a952-c43666433348", "colName": "idx_col", "colDatatype": "int", "colDesc": "The column index into a zero-based 2-dimensional array"}, {"id": "a0be0df5-5d74-4ac0-b0bb-1d5656e3420d", "colName": "row", "colDatatype": "float", "colDesc": "The value of the row in @Row units"}, {"id": "31d06b60-a6f0-4c6f-be37-fb971cb36b92", "colName": "col", "colDatatype": "float", "colDesc": "The value of the column in @Col unit"}, {"id": "9c8379b8-b617-43af-b323-469ea7beab00", "colName": "val", "colDatatype": "float", "colDesc": "The return value, calculated using row and col"}]}

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 an error will be returned.

If @RiskFreeRate is NULL an error will be returned.

@RowNumSteps must be greater than zero.

@ColNumSteps must be greater than zero.

European options are calculated using Black-Scholes-Merton.

American options are calculated using Bjerksund & Stensland 2002.

For results automatically formatted into a ‘matrix’ format, use the SP_OPTIONMATRIX stored procedure.

@Row cannot be the same as @Col.

For matrix P&L calculations use OPTIONPLMATRIX.

Examples

In this example, we are going to calculate how the changes in the underlying and volatility will affect the price of a Call option where the underlying is valued at 105, the strike price is 100, the option expires on 2013-06-21 and today’s date is 2012-09-04. The continuously compounded risk free rate is 2% and the continuously compounded dividend rate is 1.25%. The volatility is 20%. We have put the initial values of the option into variables simply to make the SQL easier to read.

DECLARE @rv as char(1);
DECLARE @s as float;
DECLARE @x as float;
DECLARE @t as float;
DECLARE @r as float;
DECLARE @d as float;
DECLARE @v as float;
DECLARE @z as char(1);
SET @z = 'C'; --Call/Put;
SET @s = 105; --Underlying;
SET @x = 100; --Strike;
SET @t = datediff(d, '2012-09-04', '2013-06-21') / cast(365 as float); --Time;
SET @r = .02; --RiskFree;
SET @d = .0125; --Dividend;
SET @v = .20; --Volatility;
SET @rv = 'P'; --ReturnValue;

Now we will invoke the table-valued function, specifying that we want the rows to move the underlying 3 steps in increments of 0.5 and the columns to move the volatility in 2 steps in increments of 0.01. This means that we will calculate new price values where the underlying prices are 103.5, 104.0, 104.5, 105.0, 105.5, 106, and 106.5 and where the volatilities are .18, .19, .20, .21, and .22.

SELECT *
FROM wct.OptionMatrix(@z, @s, @x, @t, @r, @d, @v, @rv, 'E', 'UNDERLYING', 0.5, 3,
          'VOL', .01, 2);

This produces the following result.

{"columns":[{"field":"idx_row","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"idx_col","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"row","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"col","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"idx_row":"0","idx_col":"0","row":"103.5","col":"0.18","val":"8.65125863951278"},{"idx_row":"0","idx_col":"1","row":"103.5","col":"0.19","val":"8.99666600913994"},{"idx_row":"0","idx_col":"2","row":"103.5","col":"0.2","val":"9.34297950448698"},{"idx_row":"0","idx_col":"3","row":"103.5","col":"0.21","val":"9.69004597092759"},{"idx_row":"0","idx_col":"4","row":"103.5","col":"0.22","val":"10.0377383101028"},{"idx_row":"1","idx_col":"0","row":"104","col":"0.18","val":"8.96593117681508"},{"idx_row":"1","idx_col":"1","row":"104","col":"0.19","val":"9.30955542553718"},{"idx_row":"1","idx_col":"2","row":"104","col":"0.2","val":"9.65434254265256"},{"idx_row":"1","idx_col":"3","row":"104","col":"0.21","val":"10.0001040359328"},{"idx_row":"1","idx_col":"4","row":"104","col":"0.22","val":"10.346683598625"},{"idx_row":"2","idx_col":"0","row":"104.5","col":"0.18","val":"9.28614653939416"},{"idx_row":"2","idx_col":"1","row":"104.5","col":"0.19","val":"9.62771541070827"},{"idx_row":"2","idx_col":"2","row":"104.5","col":"0.2","val":"9.97072809922398"},{"idx_row":"2","idx_col":"3","row":"104.5","col":"0.21","val":"10.3149578200949"},{"idx_row":"2","idx_col":"4","row":"104.5","col":"0.22","val":"10.6602165370413"},{"idx_row":"3","idx_col":"0","row":"105","col":"0.18","val":"9.6118164453604"},{"idx_row":"3","idx_col":"1","row":"105","col":"0.19","val":"9.95106673110088"},{"idx_row":"3","idx_col":"2","row":"105","col":"0.2","val":"10.2920645067008"},{"idx_row":"3","idx_col":"3","row":"105","col":"0.21","val":"10.634542047474"},{"idx_row":"3","idx_col":"4","row":"105","col":"0.22","val":"10.9782772994258"},{"idx_row":"4","idx_col":"0","row":"105.5","col":"0.18","val":"9.94284967021544"},{"idx_row":"4","idx_col":"1","row":"105.5","col":"0.19","val":"10.2795276440593"},{"idx_row":"4","idx_col":"2","row":"105.5","col":"0.2","val":"10.6182779491688"},{"idx_row":"4","idx_col":"3","row":"105.5","col":"0.21","val":"10.958789595374"},{"idx_row":"4","idx_col":"4","row":"105.5","col":"0.22","val":"11.3008044666979"},{"idx_row":"5","idx_col":"0","row":"106","col":"0.18","val":"10.279152305361"},{"idx_row":"5","idx_col":"1","row":"106","col":"0.19","val":"10.6130141082259"},{"idx_row":"5","idx_col":"2","row":"106","col":"0.2","val":"10.9492926355384"},{"idx_row":"5","idx_col":"3","row":"106","col":"0.21","val":"11.2876316384658"},{"idx_row":"5","idx_col":"4","row":"106","col":"0.22","val":"11.6277351476435"},{"idx_row":"6","idx_col":"0","row":"106.5","col":"0.18","val":"10.6206280142049"},{"idx_row":"6","idx_col":"1","row":"106.5","col":"0.19","val":"10.951439991888"},{"idx_row":"6","idx_col":"2","row":"106.5","col":"0.2","val":"11.2850309709864"},{"idx_row":"6","idx_col":"3","row":"106.5","col":"0.21","val":"11.620997791316"},{"idx_row":"6","idx_col":"4","row":"106.5","col":"0.22","val":"11.9590050985052"}]}

If we were going to populate a 2-dimensional array with the calculated prices, then Array(0,0) would contain 8.65125863951278 and Array(6,4) would contain 11.9590050985052. If we are not interested in the idx_row and idx_col columns, we can explicitly select the columns that we want.

SELECT row,
       col,
       Cast(val as money) as val
FROM wct.OptionMatrix(@z, @s, @x, @t, @r, @d, @v, @rv, 'E', 'UNDERLYING', 0.5, 3,
          'VOL', .01, 2);

This produces the following result.

{"columns":[{"field":"row","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"col","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"row":"103.5","col":"0.18","val":"8.6513"},{"row":"103.5","col":"0.19","val":"8.9967"},{"row":"103.5","col":"0.2","val":"9.343"},{"row":"103.5","col":"0.21","val":"9.69"},{"row":"103.5","col":"0.22","val":"10.0377"},{"row":"104","col":"0.18","val":"8.9659"},{"row":"104","col":"0.19","val":"9.3096"},{"row":"104","col":"0.2","val":"9.6543"},{"row":"104","col":"0.21","val":"10.0001"},{"row":"104","col":"0.22","val":"10.3467"},{"row":"104.5","col":"0.18","val":"9.2861"},{"row":"104.5","col":"0.19","val":"9.6277"},{"row":"104.5","col":"0.2","val":"9.9707"},{"row":"104.5","col":"0.21","val":"10.315"},{"row":"104.5","col":"0.22","val":"10.6602"},{"row":"105","col":"0.18","val":"9.6118"},{"row":"105","col":"0.19","val":"9.9511"},{"row":"105","col":"0.2","val":"10.2921"},{"row":"105","col":"0.21","val":"10.6345"},{"row":"105","col":"0.22","val":"10.9783"},{"row":"105.5","col":"0.18","val":"9.9428"},{"row":"105.5","col":"0.19","val":"10.2795"},{"row":"105.5","col":"0.2","val":"10.6183"},{"row":"105.5","col":"0.21","val":"10.9588"},{"row":"105.5","col":"0.22","val":"11.3008"},{"row":"106","col":"0.18","val":"10.2792"},{"row":"106","col":"0.19","val":"10.613"},{"row":"106","col":"0.2","val":"10.9493"},{"row":"106","col":"0.21","val":"11.2876"},{"row":"106","col":"0.22","val":"11.6277"},{"row":"106.5","col":"0.18","val":"10.6206"},{"row":"106.5","col":"0.19","val":"10.9514"},{"row":"106.5","col":"0.2","val":"11.285"},{"row":"106.5","col":"0.21","val":"11.621"},{"row":"106.5","col":"0.22","val":"11.959"}]}

If we wanted to PIVOT the results, we could enter the following SQL.

SELECT row,
       [0.18],
       [0.19],
       [0.20],
       [0.21],
       [0.22]
FROM
(
    SELECT row,
           col,
           Cast(val as money) as val
    FROM wct.OptionMatrix(@z, @s, @x, @t, @r, @d, @v, @rv, 'E', 'UNDERLYING', 0.5,
              3, 'VOL', .01, 2)
) d
PIVOT
(
    sum(val)
    for col in ([0.18], [0.19], [0.20], [0.21], [0.22])
) as P
ORDER BY row;

This produces the following result

{"columns":[{"field":"row","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"0.18","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"0.19","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"0.20","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"0.21","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"0.22","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"row":"103.5","0.18":"8.6513","0.19":"8.9967","0.20":"9.343","0.21":"9.69","0.22":"10.0377"},{"row":"104","0.18":"8.9659","0.19":"9.3096","0.20":"9.6543","0.21":"10.0001","0.22":"10.3467"},{"row":"104.5","0.18":"9.2861","0.19":"9.6277","0.20":"9.9707","0.21":"10.315","0.22":"10.6602"},{"row":"105","0.18":"9.6118","0.19":"9.9511","0.20":"10.2921","0.21":"10.6345","0.22":"10.9783"},{"row":"105.5","0.18":"9.9428","0.19":"10.2795","0.20":"10.6183","0.21":"10.9588","0.22":"11.3008"},{"row":"106","0.18":"10.2792","0.19":"10.613","0.20":"10.9493","0.21":"11.2876","0.22":"11.6277"},{"row":"106.5","0.18":"10.6206","0.19":"10.9514","0.20":"11.285","0.21":"11.621","0.22":"11.959"}]}

Of course, this required that we know the column-values before running the SQL. The SP_OPTIONMATRIX stored procedure will automatically figure out the column headings for you and execute the SQL by calling the table-valued function.

Let’s say you wanted to swap the rows and columns.

SELECT row,
       [103.5],
       [104],
       [104.5],
       [105],
       [105.5],
       [106],
       [106.5]
FROM
(
    SELECT row,
           col,
           Cast(val as money) as val
    FROM wct.OptionMatrix(@z, @s, @x, @t, @r, @d, @v, @rv, 'E', 'VOL', .01, 2, 
              'UNDERLYING', 0.5, 3)
) d
PIVOT
(
    sum(val)
    for col in ([103.5], [104], [104.5], [105], [105.5], [106], [106.5])
) as P
ORDER BY row;

This produces the following result.

{"columns":[{"field":"row","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"103.5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"104","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"104.5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"105","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"105.5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"106","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"106.5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"104":"8.9659","105":"9.6118","106":"10.2792","row":"0.18","103.5":"8.6513","104.5":"9.2861","105.5":"9.9428","106.5":"10.6206"},{"104":"9.3096","105":"9.9511","106":"10.613","row":"0.19","103.5":"8.9967","104.5":"9.6277","105.5":"10.2795","106.5":"10.9514"},{"104":"9.6543","105":"10.2921","106":"10.9493","row":"0.2","103.5":"9.343","104.5":"9.9707","105.5":"10.6183","106.5":"11.285"},{"104":"10.0001","105":"10.6345","106":"11.2876","row":"0.21","103.5":"9.69","104.5":"10.315","105.5":"10.9588","106.5":"11.621"},{"104":"10.3467","105":"10.9783","106":"11.6277","row":"0.22","103.5":"10.0377","104.5":"10.6602","105.5":"11.3008","106.5":"11.959"}]}