Skip to content

Commit

Permalink
BOOK: add calendars NB (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
attack68 authored Feb 18, 2025
1 parent a43a0ba commit 37de223
Showing 1 changed file with 248 additions and 0 deletions.
248 changes: 248 additions & 0 deletions notebooks/coding_2/Calendars.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2362d250-6e1f-43d3-a853-4e53db61ef19",
"metadata": {},
"outputs": [],
"source": [
"from rateslib import *"
]
},
{
"cell_type": "markdown",
"id": "7c691656-ba4f-4278-8849-a75fc13b83f8",
"metadata": {},
"source": [
"# Timings\n",
"\n",
"Get a calendar straight from a hash table."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d8d2453-d45d-44a1-9794-31f9315f2de4",
"metadata": {},
"outputs": [],
"source": [
"%timeit get_calendar(\"ldn\")"
]
},
{
"cell_type": "markdown",
"id": "09dcad09-c185-48c3-9773-818f3af0d5db",
"metadata": {},
"source": [
"Construct a ``Cal`` directly from a list of holidays and week mask."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "375dc5c5-afbb-43a4-bd45-abb989ce3057",
"metadata": {},
"outputs": [],
"source": [
"cal = get_calendar(\"ldn\")\n",
"holidays = cal.holidays\n",
"%timeit Cal(holidays=holidays, week_mask=[5,6])"
]
},
{
"cell_type": "markdown",
"id": "e17cff9e-1685-491c-8b68-d43a5bd6f6d4",
"metadata": {},
"source": [
"Get a ``NamedCal`` parsed in Rust and converted to Python."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9096fea1-5cf6-4866-8f2d-2f549092be48",
"metadata": {},
"outputs": [],
"source": [
"%timeit get_calendar(\"ldn,tgt\", named=True)"
]
},
{
"cell_type": "markdown",
"id": "3a855b69-5996-41f0-af6a-98130274030b",
"metadata": {},
"source": [
"Get a ``NamedCal`` parsed and constructed in Python."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ca011bc-fcef-4e06-861c-8342693bfcb5",
"metadata": {},
"outputs": [],
"source": [
"%timeit get_calendar(\"ldn,tgt\", named=False)"
]
},
{
"cell_type": "markdown",
"id": "359e90af-641f-4753-a031-105a5fe0d54e",
"metadata": {},
"source": [
"Construct a ``UnionCal`` directly from multiple ``Cal``."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07ef7035-3406-4f6f-91fe-5232599ee91c",
"metadata": {},
"outputs": [],
"source": [
"c1 = Cal(holidays=get_calendar(\"ldn\", named=False).holidays, week_mask=[5,6])\n",
"c2 = Cal(holidays=get_calendar(\"tgt\", named=False).holidays, week_mask=[5,6])\n",
"\n",
"%timeit UnionCal([c1, c2])"
]
},
{
"cell_type": "markdown",
"id": "ad0bd111-3c5d-4f93-a097-ecdf3cdb6090",
"metadata": {},
"source": [
"Add a new calendar to ``defaults.calendars`` and fetch that directly."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3db567be-5110-41e5-a36e-c8a7c8fd9445",
"metadata": {},
"outputs": [],
"source": [
"defaults.calendars[\"ldn,tgt\"] = get_calendar(\"ldn,tgt\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1a030ff-3403-4b5d-a271-7154c62e5597",
"metadata": {},
"outputs": [],
"source": [
"%timeit get_calendar(\"ldn,tgt\")"
]
},
{
"cell_type": "markdown",
"id": "97dd17e2-5b84-4a9c-ba5d-017ba5815ee2",
"metadata": {},
"source": [
"# Tenor Manipulations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1afb68c-f364-47cb-8818-63736ec0a911",
"metadata": {},
"outputs": [],
"source": [
"add_tenor(dt(2001, 9, 28), \"-6m\", modifier=\"MF\", calendar=\"LDN\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "993f5d28-6945-44ea-a83d-d7a66a80256f",
"metadata": {},
"outputs": [],
"source": [
"add_tenor(dt(2001, 9, 28), \"-6m\", modifier=\"MF\", calendar=\"LDN\", roll=31)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bcddb24-17ab-46ca-a8c1-77c6e4fe2ac8",
"metadata": {},
"outputs": [],
"source": [
"add_tenor(dt(2001, 9, 28), \"-6m\", modifier=\"MF\", calendar=\"LDN\", roll=29)"
]
},
{
"cell_type": "markdown",
"id": "32a400ca-6e1c-4f0d-880f-bf256e6ce776",
"metadata": {},
"source": [
"# Associated Settlement Calendars"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f38317db-2936-496e-a29e-3a7a5fcaa6b2",
"metadata": {},
"outputs": [],
"source": [
"tgt_and_nyc = get_calendar(\"tgt,nyc\")\n",
"tgt_and_nyc.add_bus_days(dt(2009, 11, 10), 2, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adc402b7-de57-4bc8-afb0-652a61c150d9",
"metadata": {},
"outputs": [],
"source": [
"tgt_plus_nyc_settle = get_calendar(\"tgt|nyc\")\n",
"tgt_plus_nyc_settle.add_bus_days(dt(2009, 11, 10), 2, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79f4e7e4-c5ed-4d1e-ad55-669838a1b2ff",
"metadata": {},
"outputs": [],
"source": [
"tgt_plus_nyc_settle.add_bus_days(dt(2009, 11, 10), 1, settlement=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90585e34-914d-4292-a6e8-9d146ba432de",
"metadata": {},
"outputs": [],
"source": [
"tgt_plus_nyc_settle.add_bus_days(dt(2009, 11, 10), 1, settlement=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 37de223

Please sign in to comment.