%% Use Replicating Variance Swap Pricer and ratecurve to Price Variance Swap Instrument
% This example shows the workflow to price a VarianceSwap instrument when you use a ratecurve and a ReplicatingVarianceSwap pricing method.
%% Create VarianceSwap Instrument Object
% Use fininstrument to create a VarianceSwap instrument object.

VarianceSwapInst = fininstrument("VarianceSwap",'Maturity',datetime(2021,5,1),'Notional',150,'StartDate',datetime(2020,5,1),'RealizedVariance',0.05,'Strike',0.1,'Name',"variance_swap_instrument")

%
%% Create ratecurve Object
% Create a flat ratecurve object using ratecurve.

Settle = datetime(2020, 9, 15);
ZeroTimes = [calmonths(6) calyears([1 2 3 4 5 7 10 20 30])];
ZeroRates = [0.0052 0.0055 0.0061 0.0073 0.0094 0.0119 0.0168 0.0222 0.0293 0.0307]';
ZeroDates = Settle + ZeroTimes;
Basis = 1;
ZeroCurve = ratecurve("zero",Settle,ZeroDates,ZeroRates,'Basis',Basis)

%% Create ReplicatingVarianceSwap Pricer Object
% Use finpricer to create a ReplicatingVarianceSwap pricer object and use the ratecurve object for the 'DiscountCurve' name-value pair argument.

Strike = (50:5:135)';
Volatility = [.49;.45;.42;.38;.34;.31;.28;.25;.23;.21;.2;.21;.21;.22;.23;.24;.25;.26];
VolatilitySmile = table(Strike, Volatility);
SpotPrice = 100;
CallPutBoundary = 100;

outPricer =  finpricer("ReplicatingVarianceSwap",'DiscountCurve', ZeroCurve, 'VolatilitySmile', VolatilitySmile, ...
'SpotPrice', SpotPrice, 'CallPutBoundary', CallPutBoundary)

%% Price VarianceSwap Instrument
% Use price to compute the price and fair variance for the VarianceSwap instrument.

[Price, outPR] = price(outPricer,VarianceSwapInst,["all"])
outPR.Results
outPR.PricerData.ReplicatingPortfolio

% Copyright 2020 The MathWorks, Inc.