%% Calculate Cash Flow for Multiple FRA Instruments
% This example shows the workflow to price multiple FRA (forward rate agreement) instruments and then use cashflows to determine the cash flow for each of the FRA instruments.
%% Create FRA Instrument Object
% Use fininstrument to create a FRA instrument object for three FRA instruments.

FRAObj = fininstrument("FRA",'StartDate',datetime([2020,9,15 ; 2020,10,15 ; 2020,11,15]),'Maturity',datetime([2022,9,15 ; 2022,10,15 ; 2022,11,15]),'Rate',0.175)

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

Settle = datetime(2018,9,15);
Type = "zero";
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;
 
myRC = ratecurve('zero',Settle,ZeroDates,ZeroRates)

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

outPricer = finpricer("Discount", 'DiscountCurve',myRC)

%% Price FRA Instruments
% Use price to compute the prices and sensitivities for the three FRA instrument.

[Price, outPR] = price(outPricer, FRAObj,["all"])
outPR.Results

% Use cashflows for the three FRA instruments with a Settle date of  April 15, 2022. The specified Settle date must be before the instrument Maturity date.

CF = cashflows(FRAObj(1),datetime(2022,4,15))
CF = cashflows(FRAObj(2),datetime(2022,4,15))
CF = cashflows(FRAObj(3),datetime(2022,4,15))

% Copyright 2021 The MathWorks, Inc.