Skip to content

Swaps

This module contains examples of interest rate swaps.

Swap dataclass

Bases: EventsMixin

In a Vanilla Swap, at the end of each period the holder pays a fixed rate and receives a floating rate. In this simple version the floating rate payment is replaced by receiving notional at the beginning of the period and paying the notional at the end of the period.

Parameters:

Name Type Description Default
ccy str

the currency of the swap.

required
dates List[datetime]

the period datetimes of the swap, including the inception and maturity.

required
strike_rate float

the strike rate of the swaption (in units, i.e. 0.02 means 200 bps).

required
track str

an optional identifier for the contract.

''

Examples:

>>> dates = pd.bdate_range(datetime(2023, 12, 31), datetime(2024, 12, 31), freq="2QE")
>>> Swap("USD", dates, strike_rate = 0.03).print_events()
      time op  quantity unit track
12/31/2023  +     1.000  USD  .swp
06/30/2024  +    -1.015  USD  .swp
06/30/2024  +     1.000  USD  .swp
12/31/2024  +    -1.015  USD  .swp
Source code in qablet_contracts\ir\swap.py
@dataclass
class Swap(EventsMixin):
    """In a **Vanilla Swap**, at the end of each period the holder pays a fixed rate and receives a floating rate.
    In this simple version the floating rate payment is replaced by receiving notional at the beginning of the period
    and paying the notional at the end of the period.

    Args:
        ccy: the currency of the swap.
        dates: the period datetimes of the swap, including the inception and maturity.
        strike_rate: the strike rate of the swaption (in units, i.e. 0.02 means 200 bps).
        track: an optional identifier for the contract.

    Examples:
        >>> dates = pd.bdate_range(datetime(2023, 12, 31), datetime(2024, 12, 31), freq="2QE")
        >>> Swap("USD", dates, strike_rate = 0.03).print_events()
              time op  quantity unit track
        12/31/2023  +     1.000  USD  .swp
        06/30/2024  +    -1.015  USD  .swp
        06/30/2024  +     1.000  USD  .swp
        12/31/2024  +    -1.015  USD  .swp
    """

    ccy: str
    dates: List[datetime]
    strike_rate: float
    track: str = ""

    def events(self):
        events = []
        # payment events
        for start, end in zip(self.dates[0:-1], self.dates[1:]):
            events.extend(
                simple_swap_period(
                    self.ccy, start, end, self.strike_rate, self.track + ".swp"
                )
            )

        return events

simple_swap_period(ccy, start, end, fixed_rate, track='')

Simple representation of a swap period, paying fixed, receiving floating rate.

Parameters:

Name Type Description Default
ccy str

the currency of the swap.

required
start datetime

the start of the period.

required
end datetime

the end of the period.

required
fixed_rate float

the fixed annual rate of the swap.

required
track str

an optional identifier for the contract.

''
Source code in qablet_contracts\ir\swap.py
def simple_swap_period(
    ccy: str,
    start: datetime,
    end: datetime,
    fixed_rate: float,
    track: str = "",
) -> list:
    """Simple representation of a swap period, paying fixed, receiving floating rate.

    Args:
        ccy: the currency of the swap.
        start: the start of the period.
        end: the end of the period.
        fixed_rate: the fixed annual rate of the swap.
        track: an optional identifier for the contract.
    """
    return [
        {
            "track": track,
            "time": start,
            "op": "+",
            "quantity": 1,
            "unit": ccy,
        },
        {
            "track": track,
            "time": end,
            "op": "+",
            "quantity": -1 - fixed_rate * dcf(end, start),
            "unit": ccy,
        },
    ]