finance - Financial Risk Calculations. Optimized for ease of use through class construction and operator overload
The purpose of this project is to deliver ease of use python code for financial risk calculations. This code is not unconsious reproduction of textbook material.
It’s about developing abstract data types as objects to ease financial calculations and code development.
At this point the code is by no means optimized for speed.
Financial and mathematical concepts are developed on the PythonHacks homepage.
Time is generic like a period such as eg 1 month and non-generic like a specific date. In part both types are implemented with a heavy use of operator overload.
This means that questions like: How many days are there between a date 2009-12-27 and 3 months ahead can be calculated like:
>>> from finance import bankdate >>> t1 = bankdate('2009-12-27') >>> print t1 + '3m' 2010-03-27 >>> print t1 + '3m' - t1 90
Further a vector-like structure handling future payments - a dateflow - is implemented as a class.
Through method overload it is easy to build even very complex cashflows (= dateflow)
Generators of standard dateflows is also a part of the package.
Before any calculations on a dateflow can be made dates has to be converted into times. For this the class datetotime is created.
Finally simpel calculations like present value and different sorts of duration can be made though the class timeflow
Just run setup.py install command. Or in windows use the windows installer.
Visit my homepage to see more on how to use and the research behind the code. It’s a blog like place on finance, math and scientific computing.
There were still some problems with ultimo dates which now should be solved. Thank you to Johan Uys for bringing it to my attention.
Problems with generating ultimo dates has been solved. Thank you to Ankush Sahai for bringing it to my attention.
Code has been rewritten to isolate strickt mathematical strucktures like e.g. DecimalVector in separate packages. There have been slight modifications to yieldcurves.
Now discount curves based on benchmark zero bonds where the rates are continous forward rates. It is possible to get standard yieldcalculations done like:
Instantiate:>>> import finance >>> ns = finance.yieldcurves.NelsonSiegel(0.061, -0.01, -0.0241, 0.275)
See the settings:>>> ns Nelson Siegel (level=0.061, slope=-0.01, curvature=-0.0241, scale=0.275)
Get the discountfactors at times 1, 2, 5, 10:>>> times = [1, 2, 5, 10] >>> ns(times) DecimalVector([0.9517121708497056177816078083, 0.9072377300179418172521412527, 0.7844132592062346545344544940, 0.6008958407659500402742872859])
Get the zero coupon rate at time 5 and 7>>> r5, r7 = ns.zero_coupon_rate([5, 7]) >>> r5, r7 (Decimal('0.049762403554685553400657196'), Decimal('0.050625188777310061599365592'))
Get the forward rate between time 5 and 7>>> f5_7 = ns.discrete_forward_rate(5, 7) >>> f5_7 Decimal('0.052785255470657667493924028')
As shown above yieldcurves are made using the DecimalVector concept. Especially all outputs will be Decimal or DecimalVector.
For now there are 3 different yield curve types:
This way the finance package covers a large part of yieldcurves in use. Since it is easy to add more yieldcurves due to the design more will come.
Yieldcurves are of course integrated into the timeflow. So now it is possible to do most fixed income calculations.
A tutorial on fixed income calculations in the finance package is on its way.
Risk calculations based on linearily decomposable discountcurves is postponed intil later.
The planned development so far is: