%% Bootstrap ratecurve Object from STIRFuture, Deposit, and Swap BootInstruments
% Create BootInstruments for multiple Deposit, STIRFuture, and Swap instruments and then use irbootstrap to create and display a ratecurve object.
%% Create Instruments
% Use fininstrument to create a Deposit instrument object.

Settle = datetime(2021,6,15); 
DepRates = [0.0016 0.0017 .00175]';
DepDates = Settle + calmonths([1 2 3]');

Deposits = fininstrument("Deposit","Maturity",DepDates,"Rate",DepRates,'Name',"deposit_instrument")

% Use fininstrument to create a STIRFuture instrument object.

FutureRates = [0.002 0.0025 0.0035]';
FutMat = [datetime(2021,9,15) datetime(2021,12,15) datetime(2022,3,16)]';
FutEndDates = [datetime(2021,12,15) datetime(2022,3,15) datetime(2022,6,15)]';

Futures = fininstrument("STIRFuture","Maturity",FutMat,"RateEndDate",FutEndDates,"QuotedPrice",100 - 100*FutureRates,'Name',"stir_future_instrument")

%
% Use fininstrument to create a Swap instrument object.

SwapRates = [.0063 0;.0108 0;.013 0;.015 0;0.017 0;.018 0;0.019 0];
SwapTimes = [2 5 7 10 15 20 30]';
SwapDates = datemnth(Settle,12*SwapTimes);

Swaps = fininstrument("Swap","Maturity",SwapDates,"LegRate",SwapRates,'Name',"swap_instrument")

% Define BootInstruments for the three instruments.

BootInstruments = [Deposits;Futures;Swaps];

%% Create ratecurve Object Using irbootstrap
% Use irbootstrap to create a ratecurve object.

ConvexityAdj = (1:3)'/10000;
ZeroCurve = irbootstrap(BootInstruments,Settle,'ConvexityAdjustment',ConvexityAdj,'InterpMethod','pchip')

%% Plot Bootstrapped Curve

PlottingDates = Settle + calmonths(1:360);
plot(PlottingDates,zerorates(ZeroCurve,PlottingDates))
xlabel('Maturity (Years)') 
ylabel('Zero Rate')
title('Bootstrapped Curve')

% Copyright 2021 The MathWorks, Inc.