BlueGamma
  • Getting Started
  • Setting up your account
  • Features Overview
  • Interest Rate Swaps
    • Overview
    • Calculating a Swap Rate
    • Calculating the MtM of a Swap
    • Refinancing a Swap
    • Advanced
      • Download a Custom Table of Swap Rates
      • Benchmarking a Swap Rate with a Bank
    • FAQs
      • How Forward Rates Are Calculated
      • How Discount Factors Are Calculated
  • Forward Curves
    • Overview
    • Downloading a Forward Curve
    • Downloading Historic Forward Curves
    • Advanced
      • How to Access BRL Forward Curves and Download TLP Forecasts
    • FAQs
  • Government Bonds
    • Accessing Bond Yields
    • Accessing Forward Starting Bond Yields
  • Foreign Exchange
    • Downloading FX Forward Rates
  • Cross Currency
    • Overview
    • Pricing a Cross-Currency Swap
  • Integrations
    • Excel Add-in
      • Installation & Setup
      • Get Swap Rates
      • Get Discount Factors
      • Get Forward Rates
      • Get Swap Rate by ID
    • API
      • API Reference
      • How to Guides
        • Fetching a Swap Rate
        • Fetching Historical Swap Rates
        • Getting Forward Rates
        • Getting a Forward Curve
        • Getting Discount Factors
        • Validating BlueGamma API Data Against Bloomberg or Other Platforms
  • Accounts and Plans
    • Adding and removing seats
  • FAQs
    • Currency-Specific FAQs
    • Where does your data come from?
Powered by GitBook
On this page
  1. Integrations
  2. API
  3. How to Guides

Fetching Historical Swap Rates

PreviousFetching a Swap RateNextGetting Forward Rates

Last updated 1 month ago

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:

  1. Set API Key: Replace "your_api_key_here" in headers.

  2. Run the Code: The script fetches swap rates and plots them. Ensure matplotlib is installed (pip install matplotlib if needed).

  3. Output: You’ll see a time series plot displaying swap rates over the past 24 hours, updated every 10 minutes.

https://bluegamma.apidocumentation.com/reference