Skip to content

Code Snippets

Installation

python
pip install linerapy

Object Creation

python
obj_dict = {
    'id': 'USD SOFR',
    'value date': datetime.date(2026, 3, 20),
    'currency': 'USD',
    'instruments': {
        'generator': ['USD SOFR SW', 'USD SOFR SW', 'USD SOFR SW', 'USD SOFR SW'],
        'tenor': ['1M', '1Y', '5Y', '10Y'],
        'quote': [3.65, 3.65, 3.65, 3.65],
        'in use': [True, True, True, True]
    }
}

yield_curve = linerapy.YieldCurve(obj_dict)
python
obj_dict = {
    'id': 'IRS_001',
    'effective date': datetime.date(2026, 3, 24),
    'termination date': datetime.date(2031, 3, 24),
    'schedule roll': 'Backward',
    'leg1': {
        'currency': 'USD',
        'notional': 10000,
        'is fixed': True,
        'is paid': True,
        'rate': 0.0365,
        'calendar': 'US',
        'frequency': '6M',
        'day count convention': 'ACT/360',
        'pay lag': 2,
        'accrual adjustment': 'Modified Following',
        'accrual calendar': 'US',
        'pay adjustment': 'Modified Following',
        'pay calendar': 'US'
    },
    'leg2': {
        'currency': 'USD',
        'notional': 10000,
        'is fixed': False,
        'is paid': False,
        'index': 'SOFR',
        'margin': 0.0025,
        'calendar': 'USD',
        'frequency': '3M',
        'day count convention': 'ACT/360',
        'pay lag': 2,
        'accrual adjustment': 'Modified Following',
        'accrual calendar': 'US',
        'pay adjustment': 'Modified Following',
        'pay calendar': 'US'
    }
}

irs = linerapy.IRS(obj_dict)

Portfolio Pricing

The snippeg below demonstrate the ease of portfolio pricing. For the complete code with the sample curve data and portfoli go to the GitHub repository.

python
if __name__ == '__main__':
    data_path = os.path.join(os.getcwd(), '..', 'data')
    yc_dict = load_yield_curves(os.path.join(data_path, 'yield_curves.csv'))

    yield_curves = {}
    for value_date in yc_dict.keys():
        yield_curve_data = yc_dict[value_date]

        for yield_curve_id in yield_curve_data.keys():
            yield_curve_dict = yield_curve_data[yield_curve_id]
            yield_curve_dict['id'] = yield_curve_id
            yield_curve_dict['value date'] = value_date

            yc = linerapy.YieldCurve(yield_curve_dict)

            yield_curves[yield_curve_id] = yc


    swaps = load_swaps(os.path.join(data_path, "swaps.csv"))

    print("Yield Curves: {}".format(len(yield_curves)))
    print("Swaps: {}".format(len(swaps)))

    curve_assignment = {
        "USD": "USD SOFR",
        "SGD": "SGD SORA",
        "GBP": "GBP SONIA",
        "CHF": "CHF SARON",
        "JPY": "JPY TONA"
    }

    for swap_id in swaps.keys():
        swap = swaps[swap_id]
        if swap['leg1']['currency'] != swap['leg1']['currency']:
            print("Cross currency swap is not supported.")
            continue

        yc = yield_curves[curve_assignment[swap['leg1']['currency']]]

        irs = linerapy.IRS(swap)
        result = irs.price(yc)
        print(result)