%% 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, 2000
% Swap maturity date: Jan. 01, 2003
% Based on the information above, set the required arguments and build the LegRate, LegType, and LegReset matrices:

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

%
% Load the file deriv.mat, which provides ZeroRateSpec, the interest-rate term structure needed to price the bond.

load deriv.mat;

%
% Use swapbyzero to compute the price of the swap.

Price = swapbyzero(ZeroRateSpec, 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, SwapRate] = swapbyzero(ZeroRateSpec, LegRate, Settle,...
Maturity, LegReset, Basis, Principal, LegType)

%
% In swapbyzero , if Settle is not on a reset date (and 'StartDate' is not specified), the effective date is assumed to be the previous reset date before Settle in order to compute the accrued interest and dirty price. In this example, the effective date is ( '15-Sep-2009' ), which is the previous reset date before the ( '08-Jun-2010' ) Settle date.
% Use swapbyzero with name-value pair arguments for LegRate, LegType, LatestFloatingRate, AdjustCashFlowsBasis, and BusinessDayConvention to calculate output for Price, SwapRate, AI, RecCF, RecCFDates, PayCF, and PayCFDates:

Settle = datenum('08-Jun-2010');
RateSpec = intenvset('Rates', [.005 .0075 .01 .014 .02 .025 .03]',...
'StartDates',Settle, 'EndDates',{'08-Dec-2010','08-Jun-2011',...
'08-Jun-2012','08-Jun-2013','08-Jun-2015','08-Jun-2017','08-Jun-2020'}');
Maturity = datenum('15-Sep-2020');
LegRate = [.025 50];
LegType = [1 0]; % fixed/floating
LatestFloatingRate = .005;
 
[Price, SwapRate, AI, RecCF, RecCFDates, PayCF,PayCFDates] = ...
swapbyzero(RateSpec, LegRate, Settle, Maturity,'LegType',LegType,...
'LatestFloatingRate',LatestFloatingRate,'AdjustCashFlowsBasis',true,...
'BusinessDayConvention','modifiedfollow')

% Copyright 2015 The MathWorks, Inc.