Sure, here is a simple Python script that calculates the interest rate delta of an interest rate swap:
python
def calculate_ir_swap_delta(notional, swap_length, coupon_frequency, fixed_rate, floating_rate, discount_curve):
"""
Calculates the interest rate delta of an interest rate swap.
Parameters:
notional (float): The notional amount of the swap.
swap_length (float): The length of the swap in years.
coupon_frequency (int): The number of times the coupon is paid per year.
fixed_rate (float): The fixed interest rate of the swap.
floating_rate (float): The floating interest rate of the swap.
discount_curve (List[float]): A list of discount factors corresponding to the tenors of the swap.
Returns:
float: The interest rate delta of the swap.
"""
# Calculate the present value of the fixed leg
fixed_leg_pv = 0.0
for i in range(coupon_frequency * swap_length):
tenor = i / coupon_frequency
discount_factor = discount_curve[int(tenor * len(discount_curve))]
fixed_leg_pv += notional * fixed_rate / coupon_frequency * discount_factor
# Calculate the present value of the floating leg
floating_leg_pv = 0.0
for i in range(coupon_frequency * swap_length):
tenor = i / coupon_frequency
discount_factor = discount_curve[int(tenor * len(discount_curve))]
floating_leg_pv += notional * floating_rate / coupon_frequency * discount_factor
# Calculate the interest rate delta
ir_swap_delta = (floating_leg_pv - fixed_leg_pv) / (floating_rate * notional)
return ir_swap_delta
# Example usage
notional = 1000000.0
swap_length = 5.0
coupon_frequency = 2
fixed_rate = 0.02
floating_rate = 0.03
discount_curve = [1.0, 0.99, 0.98, 0.97, 0.96, 0.95]
ir_swap_delta = calculate_ir_swap_delta(notional, swap_length, coupon_frequency, fixed_rate, floating_rate, discount_curve)
print("Interest rate delta of the swap: ", ir_swap_delta)
The calculate_ir_swap_delta function takes in the notional amount, swap length, coupon frequency, fixed and floating interest rates, and a discount curve. It calculates the present value of the fixed and floating legs of the swap, and then calculates the interest rate delta as the change in the present value of the floating leg with respect to the floating rate.
In the example usage, we assume a notional amount of 1 million, a swap length of 5 years, semi-annual coupon payments, a fixed rate of 2%, a floating rate of 3%, and a discount curve with semi-annual tenors. The resulting interest rate delta is approximately 16.67, which means that the swap value increases by approximately 16.67 for every 0.01 increase in the floating rate.