This script retrieves swap rates every 10 minutes over the last 24 hours and plots them for visual analysis. Simply update the API key and run the script in any Python environment. For more details visit:
import requests
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
# API endpoint and headers
url = "https://api.bluegamma.io/v1/swap_rate"
headers = {"X-Api-Key": "your_api_key_here"} # Replace with your API key
# Function to generate time intervals over the past 24 hours
def generate_time_intervals(interval_minutes=10):
end_time = datetime.utcnow()
start_time = end_time - timedelta(days=1)
return [start_time + timedelta(minutes=i) for i in range(0, 24*60, interval_minutes)]
# Fetch swap rates for each time interval
swap_rates = []
for valuation_time in generate_time_intervals():
params = {
"index": "6M NIBOR",
"maturity_date": "10Y",
"payment_frequency": "1Y",
"fixed_leg_frequency": "6M",
"fixed_leg_day_count": "Thirty360BondBasis",
"valuation_time": valuation_time.isoformat(),
"start_date": valuation_time.date().isoformat() # Aligns with valuation date
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
swap_rates.append({
"time": valuation_time,
"rate": response.json().get("swap_rate")
})
# Plot the collected swap rates
times = [entry["time"] for entry in swap_rates if entry["rate"] is not None]
rates = [entry["rate"] for entry in swap_rates if entry["rate"] is not None]
plt.figure(figsize=(10, 6))
plt.plot(times, rates, marker='o', linestyle='-')
plt.xlabel("Time")
plt.ylabel("Swap Rate")
plt.title("Swap Rates over the Last 24 Hours (10-minute intervals)")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Instructions:
Set API Key: Replace "your_api_key_here" in headers.
Run the Code: The script fetches swap rates and plots them. Ensure matplotlib is installed (pip install matplotlib if needed).
Output: You’ll see a time series plot displaying swap rates over the past 24 hours, updated every 10 minutes.