Logo

SQL Server OptionPLMatrix Function

Updated 2023-11-17 15:13:47.720000

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.

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

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.

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

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

column 1
'S' , 'U' , 'ASSETP' , 'UNDERLYING'
'X' , 'K' , 'STRIKE'
'T' , 'TIME'
'R' , 'RF' , 'RISKFREE'
'D' , 'DIV' , 'DIVIDEND'
'V' , 'VOL' , 'VOLATILITY' , 'SIGMA'

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

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

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

column 1
'S' , 'U' , 'ASSETP' , 'UNDERLYING'
'X' , 'K' , 'STRIKE'
'T' , 'TIME'
'R' , 'RF' , 'RISKFREE'
'D' , 'DIV' , 'DIVIDEND'
'V' , 'VOL' , 'VOLATILITY' , 'SIGMA'

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

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

Return Type

table

colNamecolDatatypecolDesc
idx_rowintThe row index into a zero-based 2-dimensional array
idx_colintThe column index into a zero-based 2-dimensional array
rowfloatThe value of the row in @Row units
colfloatThe value of the column in @Col unit
valfloatThe 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.

idx_rowidx_colrowcolval
00103.50.18-1.64080586718804
01103.50.19-1.29539849756088
02103.50.2-0.949085002213835
03103.50.21-0.602018535773226
04103.50.22-0.254326196597994
101040.18-1.32613332988574
111040.19-0.982509081163634
121040.2-0.637721964048261
131040.21-0.291960470767997
141040.220.0546190919242093
20104.50.18-1.00591796730666
21104.50.19-0.664349095992549
22104.50.2-0.321336407476835
23104.50.210.0228933133940643
24104.50.220.368152030340461
301050.18-0.680248061340414
311050.19-0.340997775599938
321050.20
331050.210.342477540773224
341050.220.686212792724994
40105.50.18-0.34921483648538
41105.50.19-0.0125368626415181
42105.50.20.326213442467953
43105.50.210.666725088673203
44105.50.221.00873995999709
501060.18-0.0129122013398231
511060.190.320949601525101
521060.20.657228128837545
531060.210.995567131765007
541060.221.33567064094265
60106.50.180.328563507504093
61106.50.190.659375485187226
62106.50.20.992966464285587
63106.50.211.32893328461522
64106.50.221.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.

rowcolval
103.50.18-16408058.6719
103.50.19-12953984.9756
103.50.2-9490850.0221
103.50.21-6020185.3577
103.50.22-2543261.966
1040.18-13261333.2989
1040.19-9825090.8116
1040.2-6377219.6405
1040.21-2919604.7077
1040.22546190.9192
104.50.18-10059179.6731
104.50.19-6643490.9599
104.50.2-3213364.0748
104.50.21228933.1339
104.50.223681520.3034
1050.18-6802480.6134
1050.19-3409977.756
1050.20.00
1050.213424775.4077
1050.226862127.9272
105.50.18-3492148.3649
105.50.19-125368.6264
105.50.23262134.4247
105.50.216667250.8867
105.50.2210087399.60
1060.18-129122.0134
1060.193209496.0153
1060.26572281.2884
1060.219955671.3177
1060.2213356706.4094
106.50.183285635.075
106.50.196593754.8519
106.50.29929664.6429
106.50.2113289332.8462
106.50.2216669405.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.

row0.180.190.200.210.22
103.5-16408058.6719-12953984.9756-9490850.0221-6020185.3577-2543261.966
104-13261333.2989-9825090.8116-6377219.6405-2919604.7077546190.9192
104.5-10059179.6731-6643490.9599-3213364.0748228933.13393681520.3034
105-6802480.6134-3409977.7560.003424775.40776862127.9272
105.5-3492148.3649-125368.62643262134.42476667250.886710087399.60
106-129122.01343209496.01536572281.28849955671.317713356706.4094
106.53285635.0756593754.85199929664.642913289332.846216669405.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.

row103.5104104.5105105.5106106.5
0.18-16408058.6719-13261333.2989-10059179.6731-6802480.6134-3492148.3649-129122.01343285635.075
0.19-12953984.9756-9825090.8116-6643490.9599-3409977.756-125368.62643209496.01536593754.8519
0.2-9490850.0221-6377219.6405-3213364.07480.003262134.42476572281.28849929664.6429
0.21-6020185.3577-2919604.7077228933.13393424775.40776667250.88679955671.317713289332.8462
0.22-2543261.966546190.91923681520.30346862127.927210087399.6013356706.409416669405.918