{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Calendars\n", "\n", "The `Calendar` can be built in three ways:\n", "\n", "- using the constructor `Calendar` passing the arguments\n", "- with the `Calendar.load(name=\"\")` passing the name of a calendar delivered with the package (`B3`, `ANBIMA`, `Actual`) or calendars from `pandas_market_calendar` package\n", "- with the `Calendar.load(filename=\"\")` passing a file that describes a valid calendar\n", "\n", "## Calendars from file\n", "\n", "The file describing the calendar is a text file with the weekdays and holidays that represents the nonworking days." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saturday\n", "Sunday\n", "2001-01-01\n", "2002-01-01\n", "2012-12-25\n", "2013-01-01\n" ] } ], "source": [ "! type Test.cal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here Saturday and Sunday are nonworking week days and the other lines bring the holidays." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: Test\n", "Start: 2001-01-01\n", "End: 2013-01-01\n", "Weekdays: Saturday, Sunday\n", "Holidays: 4\n", "Financial: True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bizdays import Calendar\n", "\n", "Calendar.load(filename='Test.cal')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Predefined Calendars\n", "\n", "`bizdays` comes with some predefined calendars: `ANBIMA`, `B3`, `Actual`, that can be directly loaded by its names.\n", "\n", "`ANBIMA` is the standard calendar used in the brazilian fixed income markets." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: ANBIMA\n", "Start: 2000-01-01\n", "End: 2078-12-25\n", "Weekdays: Saturday, Sunday\n", "Holidays: 948\n", "Financial: True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Calendar.load(\"ANBIMA\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Actual` is a calendar with no holiday and nonworking days." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: Actual\n", "Start: 1970-01-01\n", "End: 2071-01-01\n", "Weekdays: \n", "Holidays: 0\n", "Financial: True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Calendar.load(\"Actual\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calendars from `pandas_market_calendars`\n", "\n", "The package [`pandas_market_calendars`](https://github.com/rsheftel/pandas_market_calendars) comes with many predefined calendars." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ASX',\n", " 'BMF',\n", " 'B3',\n", " 'BSE',\n", " 'NSE',\n", " 'CFE',\n", " 'CBOE_Futures',\n", " 'CBOE_Equity_Options',\n", " 'CBOE_Index_Options',\n", " 'CME_Equity',\n", " 'CBOT_Equity',\n", " 'CME_Agriculture',\n", " 'CBOT_Agriculture',\n", " 'COMEX_Agriculture',\n", " 'NYMEX_Agriculture',\n", " 'CME_Rate',\n", " 'CBOT_Rate',\n", " 'CME_InterestRate',\n", " 'CBOT_InterestRate',\n", " 'CME_Bond',\n", " 'CBOT_Bond',\n", " 'CMEGlobex_Livestock',\n", " 'CMEGlobex_Live_Cattle',\n", " 'CMEGlobex_Feeder_Cattle',\n", " 'CMEGlobex_Lean_Hog',\n", " 'CMEGlobex_Port_Cutout',\n", " 'CME Globex Cryptocurrencies',\n", " 'CME Globex Crypto',\n", " 'CMEGlobex_EnergyAndMetals',\n", " 'CMEGlobex_Energy',\n", " 'CMEGlobex_CrudeAndRefined',\n", " 'CMEGlobex_NYHarbor',\n", " 'CMEGlobex_HO',\n", " 'HO',\n", " 'CMEGlobex_Crude',\n", " 'CMEGlobex_CL',\n", " 'CL',\n", " 'CMEGlobex_Gas',\n", " 'CMEGlobex_RB',\n", " 'RB',\n", " 'CMEGlobex_MicroCrude',\n", " 'CMEGlobex_MCL',\n", " 'MCL',\n", " 'CMEGlobex_NatGas',\n", " 'CMEGlobex_NG',\n", " 'NG',\n", " 'CMEGlobex_Dutch_NatGas',\n", " 'CMEGlobex_TTF',\n", " 'TTF',\n", " 'CMEGlobex_LastDay_NatGas',\n", " 'CMEGlobex_NN',\n", " 'NN',\n", " 'CMEGlobex_CarbonOffset',\n", " 'CMEGlobex_CGO',\n", " 'CGO',\n", " 'C-GEO',\n", " 'CMEGlobex_NGO',\n", " 'NGO',\n", " 'CMEGlobex_GEO',\n", " 'GEO',\n", " 'CMEGlobex_Metals',\n", " 'CMEGlobex_PreciousMetals',\n", " 'CMEGlobex_Gold',\n", " 'CMEGlobex_GC',\n", " 'GC',\n", " 'CMEGlobex_SilverCMEGlobex_SI',\n", " 'SI',\n", " 'CMEGlobex_Platinum',\n", " 'CMEGlobex_PL',\n", " 'PL',\n", " 'CMEGlobex_BaseMetals',\n", " 'CMEGlobex_Copper',\n", " 'CMEGlobex_HG',\n", " 'HG',\n", " 'CMEGlobex_Aluminum',\n", " 'CMEGlobex_ALI',\n", " 'ALI',\n", " 'CMEGlobex_QC',\n", " 'QC',\n", " 'CMEGlobex_FerrousMetals',\n", " 'CMEGlobex_HRC',\n", " 'HRC',\n", " 'CMEGlobex_BUS',\n", " 'BUS',\n", " 'CMEGlobex_TIO',\n", " 'TIO',\n", " 'CME Globex Equity',\n", " 'CMEGlobex_FX',\n", " 'CME_FX',\n", " 'CME_Currency',\n", " 'CME Globex Fixed Income',\n", " 'CME Globex Interest Rate Products',\n", " 'EUREX',\n", " 'HKEX',\n", " 'ICE',\n", " 'ICEUS',\n", " 'NYFE',\n", " 'NYSE',\n", " 'stock',\n", " 'NASDAQ',\n", " 'BATS',\n", " 'DJIA',\n", " 'DOW',\n", " 'IEX',\n", " 'Investors_Exchange',\n", " 'JPX',\n", " 'LSE',\n", " 'OSE',\n", " 'SIFMAUS',\n", " 'SIFMA_US',\n", " 'Capital_Markets_US',\n", " 'Financial_Markets_US',\n", " 'Bond_Markets_US',\n", " 'SIFMAUK',\n", " 'SIFMA_UK',\n", " 'Capital_Markets_UK',\n", " 'Financial_Markets_UK',\n", " 'Bond_Markets_UK',\n", " 'SIFMAJP',\n", " 'SIFMA_JP',\n", " 'Capital_Markets_JP',\n", " 'Financial_Markets_JP',\n", " 'Bond_Markets_JP',\n", " 'SIX',\n", " 'SSE',\n", " 'TASE',\n", " 'TSX',\n", " 'TSXV',\n", " 'AIXK',\n", " 'ASEX',\n", " 'BVMF',\n", " 'CMES',\n", " 'IEPA',\n", " 'XAMS',\n", " 'XASX',\n", " 'XBKK',\n", " 'XBOG',\n", " 'XBOM',\n", " 'XBRU',\n", " 'XBSE',\n", " 'XBUD',\n", " 'XBUE',\n", " 'XCBF',\n", " 'XCSE',\n", " 'XDUB',\n", " 'XFRA',\n", " 'XETR',\n", " 'XHEL',\n", " 'XHKG',\n", " 'XICE',\n", " 'XIDX',\n", " 'XIST',\n", " 'XJSE',\n", " 'XKAR',\n", " 'XKLS',\n", " 'XKRX',\n", " 'XLIM',\n", " 'XLIS',\n", " 'XLON',\n", " 'XMAD',\n", " 'XMEX',\n", " 'XMIL',\n", " 'XMOS',\n", " 'XNYS',\n", " 'XNZE',\n", " 'XOSL',\n", " 'XPAR',\n", " 'XPHS',\n", " 'XPRA',\n", " 'XSAU',\n", " 'XSES',\n", " 'XSGO',\n", " 'XSHG',\n", " 'XSTO',\n", " 'XSWX',\n", " 'XTAE',\n", " 'XTAI',\n", " 'XTKS',\n", " 'XTSE',\n", " 'XWAR',\n", " 'XWBO',\n", " 'us_futures',\n", " '24/7',\n", " '24/5']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas_market_calendars as mcal\n", "\n", "mcal.get_calendar_names()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These calendars can be loaded in bizdays with the prefix `PMC/` in its name.\n", "For example, use `PMC/NYSE` to load the NYSE calendar." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: PMC/NYSE\n", "Start: 1885-01-01\n", "End: 2200-12-25\n", "Weekdays: Saturday, Sunday\n", "Holidays: 3552\n", "Financial: True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Calendar.load(\"PMC/NYSE\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create your own Calendars\n", "\n", "`Calendar` can be called with no arguments and it returns a calendar without nonworking days." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: None\n", "Start: 1970-01-01\n", "End: 2071-01-01\n", "Weekdays: \n", "Holidays: 0\n", "Financial: True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Calendar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Naming the calendar is a good idea." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Calendar: actual\n", "Start: 1970-01-01\n", "End: 2071-01-01\n", "Weekdays: \n", "Holidays: 0\n", "Financial: True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Calendar(name='actual')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Financial Calendars\n", "\n", "`bizdays` was designed to be used with financial calculations, for this reason all calendars are financial by default.\n", "\n", "In finance, the amount of business days between two consecutive dates is 1, which indicates one day of compounding interest rates.\n", "\n", "For example." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal = Calendar()\n", "cal.bizdays('2021-01-01', '2021-01-02')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this calendar both dates are business days, but the return is one business day.\n", "\n", "This behavior can changed by setting the attribute `financial` to `False`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cal.financial = False\n", "cal.bizdays('2021-01-01', '2021-01-02')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.11 ('venv': venv)", "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.10.11" }, "vscode": { "interpreter": { "hash": "804a4a06e7ed5f90e0da5c1a0173147605cc28b78a3676d1fdeb0cd38c97fa0e" } } }, "nbformat": 4, "nbformat_minor": 2 }