Git Product home page Git Product logo

python-bizdays's Introduction

Downloads Latest Version Supported Python versions

In several countries and markets, the accountability of the price of a financial instrument, mainly bonds and derivatives, involves the use of different rules to compute the way the days go by. In Brazil, several financial instruments pay interest according to the business days along their life cycle. So, having a way to compute the number of business days between 2 dates is fairly useful to price financial instruments. bizdays was created to make it easier.

bizdays computes business days between two dates based on the definition of nonworking days (usually holidays and weekends). It also computes other collateral effects like adjust dates for the next or previous business day, check whether a date is a business day, create sequences of business days, and much more.

Several financial libraries compute the holidays, giving no option to users set it by their own. Furtherly, the financial calendar is usually a small feature of a huge library, as quantlib, for example, and some users, including myself, don't want to put a hand in such a huge library only to use the financial calendar.

bizdays is a pure Python module without strong dependencies, what makes it appropriated for small projects.

python-bizdays's People

Contributors

fernandosavio avatar franklinwanderson avatar jwg4 avatar royopa avatar wilsonfreitas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-bizdays's Issues

Unusual results using offset method

I was testing biz days and found a strange. Here follows a little code fragment:

from bizdays import *
cal = Calendar.load('files/cal/ANBIMA.cal') # path in my file system - should be changed
cal.offset('2016-11-03', -1)
cal.offset('2016-11-02', -1)

The first call to offset gives me the expected result:
Out[25]: datetime.date(2016, 11, 1)

The second one, however, does not:
Out[26]: datetime.date(2016, 10, 31)

I was expecting to get the exact same result (i.e. '2016-11-1').

Calendar's start and end dates

The start and end dates should be passed in the constructor, instead of trying to guess.
The rule is use the first and last dates of the given calendar spec or use whatever is passed.

Add `now` argument to getdate

getdate has the following default call.

getdate(self, expr, year, month=None)

Instead of using year and month arguments it should use a now argument to specify where it is in time.

getdate(self, expr, now=datetime.today())

Fail to get bizday

My code:

from bizdays import Calendar
holidays = ['2017-01-01','2017-04-2017','2017-05-2017','2017-09-07','2017-10-12','2017-11-11','2017-11-15','2017-12-25']
cal = Calendar(holidays=holidays, weekdays=['Sunday', 'Saturday'])
cal.isbizday('2017-11-01')

The output:
Traceback (most recent call last):
File "dias_uteis.py", line 10, in
cal = Calendar(holidays=holidays, weekdays=['Sunday', 'Saturday'])
File "/usr/local/lib/python2.7/dist-packages/bizdays.py", line 285, in init
self._holidays = [Date(d) for d in holidays]
File "/usr/local/lib/python2.7/dist-packages/bizdays.py", line 245, in init
d = datetime.strptime(d, format).date()
File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 17
*Test on cloud9 ide.

Creation of an empty calendar

The empty calendar should be useful in situations like those where someone wants to compute business days considering only weekends (or any other day) as non-working days, so that a list of holidays couldn't be provided.
In situations like that start and end dates must be provided in order to define calendar's range.

Helper functions to load a list of holidays

Develop helper functions to load list of dates in any format stored in text files.

load_dates(file, formats=(format1, format2))

format_n_ should be specified with regular expressions.

2023-02-22 was a B3 holiday

2023-02-22 was included as holiday in B3.cal file by mistake.

For instance, there were deals for DI futures and the official calendar explicitly says it only had different negotiation time.

getdate unable to handle '1st bizday before 2nd fri'

In bizdays 1.0.2, in the getdate function, I'm unable to pass in '1st bizday before 2nd fri'

contractyear=2002
contractmonth=5
newltd=cal.getdate("1st bizday before 2nd fri",contractyear,contractmonth)

This shows:

  File ".../bizdays.py", line 888, in getdate
    dt = self._index.getdate(expr, year, month)
  File ".../bizdays.py", line 313, in getdate
    raise ValueError('Invalid reference day:', tok[4])
ValueError: ('Invalid reference day:', 'fri')

(Incidentally, the usage case for this is for futures market expiries - this one in particular is the 90 Day Bank Accepted Bills contract listed on the Australian Securities Exchange (formerly Sydney Futures Exchange).

Of course, I can do this is a nested fashion, as below, but it'd be nicer for the parser to handle this.

newltd=cal.offset(cal.getdate("2nd fri",contractyear,contractmonth),-1)

Weekdays consistency

I create a Calendar using

>>> cal = Calendar(holidays, ['Sunday', 'Saturday'], name='Brazil')

and use getdate with

>>> cal.getdate('last wed', 2002, 5)

I should use short weekdays declaring Calendars

Add timezone option

Add an option to create dates with given timezone.

set_option('timezone', timezone.utc)

Use context manager to change options locally

A project might set a specific option to bizdays.
But in another part, it needs to change this option.
This should be done locally with context managers.

from bizdays import set_option, Calendar, option

set_option("mode", "pandas")
cal = Calendar.load("B3")

with bizdays.option("mode", "datetime"):
    cal.bizdays(date1, date2)

Create a calendar register

A calendar register for applications which have many calendar.
Instead of creating many calendar instances a register should work as a repository for calendar that could be called by its name.

offset method

A offset method should be implemented.

cal.offset('2012-01-03', 6)
# returns the the date offset by 6, considering only the business days
# or
cal.offset('2012-01-03', -6)
# offset backward

it can be done with adjust_next and adjust_previous

Verification Error

Hi!
I got an unexpected error while trying to check a date using isbizday method.

>>> from bizdays import Calendar, load_holidays
>>> cal = Calendar.load("ANBIMA")
>>> cal.isbizday("2023-04-21")
False
>>> cal.isbizday('2023-04-20')
True

However, seeing the "ANBIMA.cal" file, we clearly visualize that 21 April's is a Holiday, but the bizday lib counts it as 20 April.

~ % python --version
Python 3.8.5
~ % uname -a
Darwin 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64 x86_64

Upgrade to numpy 2.x

I noticed when I installed bizdays it triggered a numpy downgrade:

 - numpy==2.0.0
 + numpy==1.26.4

The current pyproject.toml specifies numpy 1.x. It might be better to remove this entirely and then just pull in the numpy version corresponding to pandas 2.x:

numpy = ">=1.26.0"

adding market calendar to getdate

How do you load a calendar from another package in the python version? In R I use bizdays all the time, its amazing. In that example, let's say I wanted to find the "last bizday" in a sequence and reference the Rmetrics/NYSE calendar:
bizdays::getdate("last bizday",as.Date(timeSequence(from = as.Date('2020-01-01'), to = as.Date('2022-12-31'), by = "1 month")),"Rmetrics/NYSE")

In the python version, if i loaded for example:
import pandas_market_calendars as mcal
cal = mcal.get_calendar('NYSE')
holidays = cal.holidays()
How can i pass these holidays to bizdays for getdate?

Thanks!!

Provide the directory to specify where to find the calendar spec

Where to find the calendar spec?
The directory should be provided, or the path should be passed together with the calendar spec name, but the calendar's name wouldn't have the path.

cal = Calendar('/path/to/calendar/spec/Test')
cal.name
# Test

or specify the directory explicitly

cal = Calendar('Test', repository='/path/to/calendar/spec/')

or lastly a singleton to hold the settings.

bizdays.calendar_settings(repository='/path/to/calendar/spec/')

Wrong values

bizdays version 1.0.11

'bizdays' is returning wrong values.

Reproducible example (September 2022 has 21 business days with Anbima calendar):

bizdays("2022-09-01", "2022-09-30", "Brazil/ANBIMA")
20

bizdays("2022-09-01", "2022-10-01", "Brazil/ANBIMA")
20

However, that's correct:

length(bizseq(dmy("01/09/2022"), dmy("01/10/2022"), "Brazil/ANBIMA"))
21

bizdays doesn't handle correctly non-business days in edges

bizdays doesn't handle correctly non-business days in edges.

The following call

bizdays("2013-01-01", "2014-01-01")

has two non-business days as arguments.
In this situation, the index counts one more bizday, which is incorrect.

The example below must return 0 since the arguments represent two consecutive dates non-business days.

bizdays("2013-06-22", "2013-06-23")

This specific case has to be handled separately but is similar to the first example.

Support Python 3.12

Currently the maximum supported Python version is 3.11; it would be great to be able to use this in 3.12 as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.