%% Price an Interest-Rate Swap
% Price an interest-rate swap with a fixed receiving leg and a floating paying leg. Payments are made once a year, and the notional principal amount is $100. The values for the remaining arguments are:
% Coupon rate for fixed leg: 0.06 (6%)
% Spread for floating leg: 20 basis points
% Swap settlement date: Jan. 01, 2004
% Swap maturity date: Jan. 01, 2006
% Based on the information above, set the required arguments and build the LegRate, LegType, and LegReset matrices:

Settle = '01-Jan-2004';
Maturity = '01-Jan-2006';
Basis = 0; 
Principal = 100;
LegRate = [0.06 20]; % [CouponRate Spread] 
LegType = [1 0]; % [Fixed Float] 
LegReset = [1 1]; % Payments once per year

%
% Price the swap using the BKTree included in the MAT-file deriv.mat. BKTree contains the time and forward-rate information needed to price the instrument.

load deriv.mat;

%
% Use swapbybk to price of the swap.

Price = swapbybk(BKTree, LegRate,... 
Settle, Maturity, LegReset, Basis, Principal, LegType)

%
% Using the previous data, calculate the swap rate, which is the coupon rate for the fixed leg, such that the swap price at time = 0 is zero.

LegRate = [NaN 20]; 

[Price, PriceTree, SwapRate] = swapbybk(BKTree, LegRate,... 
Settle, Maturity, LegReset, Basis, Principal, LegType)

% Copyright 2015 The MathWorks, Inc.