An Interest Rate Swap (IRS) is a financial derivative contract where two parties agree to exchange interest rate payments. Typically, one party pays a fixed interest rate and receives a floating interest rate, while the other party does the opposite. The "swap rate" refers to the fixed rate that makes the present value of the fixed leg equal to the present value of the expected floating leg at the initiation of the contract.
This calculator helps estimate the implied fixed rate for an IRS. It works by assuming a set of future expected floating rates and calculating the present value of receiving those floating rates. Then, it finds the fixed rate that makes the present value of paying that fixed rate equal to the present value of receiving the floating rates.
function calculateSwapRate() {
var notionalPrincipal = parseFloat(document.getElementById("notionalPrincipal").value);
var daysInPeriod = parseInt(document.getElementById("daysInPeriod").value);
var frequency = parseInt(document.getElementById("frequency").value);
var discountRate = parseFloat(document.getElementById("discountRate").value);
var numberOfPeriods = parseInt(document.getElementById("numberOfPeriods").value);
var expectedFloatingRatesString = document.getElementById("expectedFloatingRates").value;
var resultElement = document.getElementById("result");
resultElement.innerHTML = ""; // Clear previous results
if (isNaN(notionalPrincipal) || notionalPrincipal <= 0 ||
isNaN(daysInPeriod) || daysInPeriod <= 0 ||
isNaN(frequency) || frequency <= 0 ||
isNaN(discountRate) || discountRate < 0 ||
isNaN(numberOfPeriods) || numberOfPeriods <= 0) {
resultElement.innerHTML = "Please enter valid positive numbers for principal, days, frequency, discount rate, and number of periods.";
return;
}
var expectedFloatingRates = [];
try {
expectedFloatingRates = expectedFloatingRatesString.split(',').map(function(rate) {
var parsedRate = parseFloat(rate.trim());
if (isNaN(parsedRate) || parsedRate < 0) {
throw new Error("Invalid floating rate format.");
}
return parsedRate;
});
} catch (e) {
resultElement.innerHTML = "Please enter valid floating rates separated by commas (e.g., 0.04,0.042).";
return;
}
if (expectedFloatingRates.length < numberOfPeriods) {
resultElement.innerHTML = "You must provide at least as many expected floating rates as the number of payment periods.";
return;
}
var totalFloatingPV = 0;
var totalFixedPVFactor = 0;
var yearFraction = daysInPeriod / 360; // Common convention for OIS/IRS for day count
for (var i = 0; i < numberOfPeriods; i++) {
// Calculate discount factor for period i+1
// We assume the first discount factor applies to the end of the first period,
// which is used to discount the floating rate received at the end of period 1.
var discountFactor = 1 / Math.pow(1 + discountRate, yearFraction * (i + 1));
// Present Value of receiving floating rate in period i+1
var pvFloatingPayment = notionalPrincipal * expectedFloatingRates[i] * yearFraction * discountFactor;
totalFloatingPV += pvFloatingPayment;
// Present Value of the discount factor for the fixed leg
// The fixed leg has a payment of FixedRate * Notional * YearFraction
// So, PV(FixedLeg) = Sum(FixedRate * Notional * YearFraction * DiscountFactor_i)
// PV(FixedLeg) = FixedRate * Notional * YearFraction * Sum(DiscountFactor_i)
// To find FixedRate, we need Sum(DiscountFactor_i)
totalFixedPVFactor += discountFactor;
}
// The swap rate is found when PV(Fixed Leg) = PV(Floating Leg)
// FixedRate * Notional * YearFraction * Sum(DiscountFactor_i) = totalFloatingPV
// FixedRate = totalFloatingPV / (Notional * YearFraction * Sum(DiscountFactor_i))
var swapRate = totalFloatingPV / (notionalPrincipal * yearFraction * totalFixedPVFactor);
resultElement.innerHTML = "Estimated Fixed Swap Rate: " + (swapRate * 100).toFixed(4) + "%";
}
.calculator-container {
font-family: Arial, sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 600px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-container p {
color: #555;
line-height: 1.6;
margin-bottom: 15px;
}
.calculator-inputs {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.input-group {
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input[type="number"],
.input-group input[type="text"] {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
margin-top: 10px;
}
button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
background-color: #e7f3fe;
border: 1px solid #b3e0ff;
border-radius: 4px;
text-align: center;
font-size: 18px;
color: #333;
}
.calculator-result strong {
color: #2a73d2;
}