Logo

OptionPLMatrix

Updated 2023-11-17 15:13:47.720000

Syntax

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

Description

Use the table-valued function OptionPLMatrix to generate a result set of profit (loss) by varying two inputs into the theoretical value of the option. For example, you could generate a result set that shows the profit (loss) based on changes in the price of the underlying asset and the volatility.

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.

@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": "0c6638b6-3a71-4bcc-bbfb-ba165c9064b1", "colName": "idx_row", "colDatatype": "int", "colDesc": "The row index into a zero-based 2-dimensional array"}, {"id": "6fcd085f-dc48-4180-8c96-d2e29afff5f9", "colName": "idx_col", "colDatatype": "int", "colDesc": "The column index into a zero-based 2-dimensional array"}, {"id": "dfe72946-1dbe-47ea-8f8f-cb06e618f2d5", "colName": "row", "colDatatype": "float", "colDesc": "The value of the row in @Row units"}, {"id": "374babb5-618e-4d3a-b952-9533f615850e", "colName": "col", "colDatatype": "float", "colDesc": "The value of the column in @Col unit"}, {"id": "8d3e440f-15e7-4646-bf99-9db5162b2d00", "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 @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_OPTIONPLMATRIX stored procedure.

@Row cannot be the same as @Col.

For matrix calculations of other option values use OPTIONMATRIX.

Examples

In this example, we are going to calculate how the changes in the underlying and volatility will affect the profit (loss) 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 @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;

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. We will then subtract out the theoretical value of the option where the underlying is 105 and the volatility is .20, which were provided as inputs to the function.

SELECT *
FROM wct.OptionPLMatrix(@z, @s, @x, @t, @r, @d, @v, '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":"-1.64080586718804"},{"idx_row":"0","idx_col":"1","row":"103.5","col":"0.19","val":"-1.29539849756088"},{"idx_row":"0","idx_col":"2","row":"103.5","col":"0.2","val":"-0.949085002213835"},{"idx_row":"0","idx_col":"3","row":"103.5","col":"0.21","val":"-0.602018535773226"},{"idx_row":"0","idx_col":"4","row":"103.5","col":"0.22","val":"-0.254326196597994"},{"idx_row":"1","idx_col":"0","row":"104","col":"0.18","val":"-1.32613332988574"},{"idx_row":"1","idx_col":"1","row":"104","col":"0.19","val":"-0.982509081163634"},{"idx_row":"1","idx_col":"2","row":"104","col":"0.2","val":"-0.637721964048261"},{"idx_row":"1","idx_col":"3","row":"104","col":"0.21","val":"-0.291960470767997"},{"idx_row":"1","idx_col":"4","row":"104","col":"0.22","val":"0.0546190919242093"},{"idx_row":"2","idx_col":"0","row":"104.5","col":"0.18","val":"-1.00591796730666"},{"idx_row":"2","idx_col":"1","row":"104.5","col":"0.19","val":"-0.664349095992549"},{"idx_row":"2","idx_col":"2","row":"104.5","col":"0.2","val":"-0.321336407476835"},{"idx_row":"2","idx_col":"3","row":"104.5","col":"0.21","val":"0.0228933133940643"},{"idx_row":"2","idx_col":"4","row":"104.5","col":"0.22","val":"0.368152030340461"},{"idx_row":"3","idx_col":"0","row":"105","col":"0.18","val":"-0.680248061340414"},{"idx_row":"3","idx_col":"1","row":"105","col":"0.19","val":"-0.340997775599938"},{"idx_row":"3","idx_col":"2","row":"105","col":"0.2","val":"0"},{"idx_row":"3","idx_col":"3","row":"105","col":"0.21","val":"0.342477540773224"},{"idx_row":"3","idx_col":"4","row":"105","col":"0.22","val":"0.686212792724994"},{"idx_row":"4","idx_col":"0","row":"105.5","col":"0.18","val":"-0.34921483648538"},{"idx_row":"4","idx_col":"1","row":"105.5","col":"0.19","val":"-0.0125368626415181"},{"idx_row":"4","idx_col":"2","row":"105.5","col":"0.2","val":"0.326213442467953"},{"idx_row":"4","idx_col":"3","row":"105.5","col":"0.21","val":"0.666725088673203"},{"idx_row":"4","idx_col":"4","row":"105.5","col":"0.22","val":"1.00873995999709"},{"idx_row":"5","idx_col":"0","row":"106","col":"0.18","val":"-0.0129122013398231"},{"idx_row":"5","idx_col":"1","row":"106","col":"0.19","val":"0.320949601525101"},{"idx_row":"5","idx_col":"2","row":"106","col":"0.2","val":"0.657228128837545"},{"idx_row":"5","idx_col":"3","row":"106","col":"0.21","val":"0.995567131765007"},{"idx_row":"5","idx_col":"4","row":"106","col":"0.22","val":"1.33567064094265"},{"idx_row":"6","idx_col":"0","row":"106.5","col":"0.18","val":"0.328563507504093"},{"idx_row":"6","idx_col":"1","row":"106.5","col":"0.19","val":"0.659375485187226"},{"idx_row":"6","idx_col":"2","row":"106.5","col":"0.2","val":"0.992966464285587"},{"idx_row":"6","idx_col":"3","row":"106.5","col":"0.21","val":"1.32893328461522"},{"idx_row":"6","idx_col":"4","row":"106.5","col":"0.22","val":"1.66694059180434"}]}

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

We will do that in the following example, and we will assume that we are long 100 contracts and our notional exposure is 10,000,000.

SELECT row,
       col,
       Cast(val * 10000000 as money) as val
FROM wct.OptionPLMatrix(@z, @s, @x, @t, @r, @d, @v, '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":"-16408058.6719"},{"row":"103.5","col":"0.19","val":"-12953984.9756"},{"row":"103.5","col":"0.2","val":"-9490850.0221"},{"row":"103.5","col":"0.21","val":"-6020185.3577"},{"row":"103.5","col":"0.22","val":"-2543261.966"},{"row":"104","col":"0.18","val":"-13261333.2989"},{"row":"104","col":"0.19","val":"-9825090.8116"},{"row":"104","col":"0.2","val":"-6377219.6405"},{"row":"104","col":"0.21","val":"-2919604.7077"},{"row":"104","col":"0.22","val":"546190.9192"},{"row":"104.5","col":"0.18","val":"-10059179.6731"},{"row":"104.5","col":"0.19","val":"-6643490.9599"},{"row":"104.5","col":"0.2","val":"-3213364.0748"},{"row":"104.5","col":"0.21","val":"228933.1339"},{"row":"104.5","col":"0.22","val":"3681520.3034"},{"row":"105","col":"0.18","val":"-6802480.6134"},{"row":"105","col":"0.19","val":"-3409977.756"},{"row":"105","col":"0.2","val":"0.00"},{"row":"105","col":"0.21","val":"3424775.4077"},{"row":"105","col":"0.22","val":"6862127.9272"},{"row":"105.5","col":"0.18","val":"-3492148.3649"},{"row":"105.5","col":"0.19","val":"-125368.6264"},{"row":"105.5","col":"0.2","val":"3262134.4247"},{"row":"105.5","col":"0.21","val":"6667250.8867"},{"row":"105.5","col":"0.22","val":"10087399.60"},{"row":"106","col":"0.18","val":"-129122.0134"},{"row":"106","col":"0.19","val":"3209496.0153"},{"row":"106","col":"0.2","val":"6572281.2884"},{"row":"106","col":"0.21","val":"9955671.3177"},{"row":"106","col":"0.22","val":"13356706.4094"},{"row":"106.5","col":"0.18","val":"3285635.075"},{"row":"106.5","col":"0.19","val":"6593754.8519"},{"row":"106.5","col":"0.2","val":"9929664.6429"},{"row":"106.5","col":"0.21","val":"13289332.8462"},{"row":"106.5","col":"0.22","val":"16669405.918"}]}

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 * 10000000 as money) as val
    FROM wct.OptionPLMatrix(@z, @s, @x, @t, @r, @d, @v, '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":"-16408058.6719","0.19":"-12953984.9756","0.20":"-9490850.0221","0.21":"-6020185.3577","0.22":"-2543261.966"},{"row":"104","0.18":"-13261333.2989","0.19":"-9825090.8116","0.20":"-6377219.6405","0.21":"-2919604.7077","0.22":"546190.9192"},{"row":"104.5","0.18":"-10059179.6731","0.19":"-6643490.9599","0.20":"-3213364.0748","0.21":"228933.1339","0.22":"3681520.3034"},{"row":"105","0.18":"-6802480.6134","0.19":"-3409977.756","0.20":"0.00","0.21":"3424775.4077","0.22":"6862127.9272"},{"row":"105.5","0.18":"-3492148.3649","0.19":"-125368.6264","0.20":"3262134.4247","0.21":"6667250.8867","0.22":"10087399.60"},{"row":"106","0.18":"-129122.0134","0.19":"3209496.0153","0.20":"6572281.2884","0.21":"9955671.3177","0.22":"13356706.4094"},{"row":"106.5","0.18":"3285635.075","0.19":"6593754.8519","0.20":"9929664.6429","0.21":"13289332.8462","0.22":"16669405.918"}]}

Of course, this required that we know the column-values before running the SQL. The SP_OPTIONPLMATRIX stored procedure will automatically figure out the column headings for you and execute the SQL by call 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 * 10000000 as money) as val
    FROM wct.OptionPLMatrix(@z, @s, @x, @t, @r, @d, @v, '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":"-13261333.2989","105":"-6802480.6134","106":"-129122.0134","row":"0.18","103.5":"-16408058.6719","104.5":"-10059179.6731","105.5":"-3492148.3649","106.5":"3285635.075"},{"104":"-9825090.8116","105":"-3409977.756","106":"3209496.0153","row":"0.19","103.5":"-12953984.9756","104.5":"-6643490.9599","105.5":"-125368.6264","106.5":"6593754.8519"},{"104":"-6377219.6405","105":"0.00","106":"6572281.2884","row":"0.2","103.5":"-9490850.0221","104.5":"-3213364.0748","105.5":"3262134.4247","106.5":"9929664.6429"},{"104":"-2919604.7077","105":"3424775.4077","106":"9955671.3177","row":"0.21","103.5":"-6020185.3577","104.5":"228933.1339","105.5":"6667250.8867","106.5":"13289332.8462"},{"104":"546190.9192","105":"6862127.9272","106":"13356706.4094","row":"0.22","103.5":"-2543261.966","104.5":"3681520.3034","105.5":"10087399.60","106.5":"16669405.918"}]}