%% Remove Instrument From a Portfolio
% Use addInstrument to add instruments to an empty portfolio and then remove an instrument from the portfolio using removeInstrument.
%% Create FixedBond Instrument Objects
% Use fininstrument to create two FixedBond instrument objects.

FixB1 = fininstrument("FixedBond", 'Maturity',datetime(2022,9,15),'CouponRate',0.045,'Name',"fixed_bond1")
FixB2 = fininstrument("FixedBond", 'Maturity',datetime(2022,9,15),'CouponRate',0.035,'Name',"fixed_bond2")

%
%% 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 for FixedBond Instruments
% Use finpricer to create a Discount pricer object and use the ratecurve object for the 'DiscountCurve' name-value pair argument.

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

%% Add Instruments to finportfolio Object
% Create an empty finportflio object using finportfolio and then use addInstrument to add the two FixedBond instruments to the portfolio.

f1 = finportfolio;
f1 = addInstrument(f1,FixB1)
f1 = addInstrument(f1,FixB2)

%% Remove Instrument from finportfolio Object
% Use removeInstrument to remove the first FixedBond instrument from the portfolio.

f1 = removeInstrument(f1,1)

%% Set Pricer for Portfolio
% Use setPricer to set the Discount pricer for the portfolio and then use pricePortfolio to calculate the price and sensitivities for the single instrument in the portfolio.

f1 = setPricer(f1,DiscountPricer)
[PortPrice,InstPrice,PortSens,InstSens] = pricePortfolio(f1)

% Copyright 2019 The MathWorks, Inc.