GithubHelp home page GithubHelp logo

domokane / financepy Goto Github PK

View Code? Open in Web Editor NEW
2.0K 68.0 307.0 72.91 MB

A Python Finance Library that focuses on the pricing and risk-management of Financial Derivatives, including fixed-income, equity, FX and credit derivatives.

License: GNU General Public License v3.0

Python 19.82% Jupyter Notebook 80.18% Batchfile 0.01%
risk pricing risk-management asset-allocation finance valuation python derivatives-pricing numba bonds

financepy's People

Contributors

abhi-g80 avatar ameyshrivastava avatar cclauss avatar dependabot[bot] avatar dominichong avatar domokane avatar fergalok avatar gpelleri avatar idorrington92 avatar jtamas18 avatar mattyuan98 avatar mayank1897 avatar nashquant avatar piterbarg avatar poornagurram avatar rkcah avatar ryanrussell avatar saeedamen avatar sagayev avatar shunmaruko avatar vfermat 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  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  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

financepy's Issues

Implied Volatility for FX Options

The current FX implied volatility calculator uses Newton Raphson with an analytical first derivative (vega).

This has numerical issues for short-dated ITM options.

Needs to be fixed by converting ITM option to OTM using put call parity and then solving for that option with new implied price.

solveforsmilestrike

Once I have a FxVolSurface object calibrated I am trying to retrieve the Strike corresponding to a set deltaTarget and ExpiryDate. This would be used to price a fxVanillaOption with specified delta as input instead of specified strike.

#Issue 1
This function doesn't seem to exist in the class functions with only 2 args as parameters (self, deltaTarget as float,ExpiryDate as FinDate).

#Issue 2 (alternative using solveForSmileStrike)
solveForSmileStrike seem to do the job but only for specific Expiry dates. However in FinFXVolSurfacePlus the function solveForSmileStrike takes into parameters:
OptionTypeValue
deltaTarget
tenorIndex
initialValue

This currently returns an error probably because the args (strikes = args[9] and gaps = args[10]) are not specified when calling deltaFit

FinDate constructor

Request to overload the FinDate constructor to accept date string and formatting string

Numba specialist to Examine how to JIT Optimisers

Scipy optimisers are used in a number of locations. However it is not been possible to Numba the objection functions.
I have rewritten the optimisers in FinSolver but there are problems using njit
The aim here is to

  1. Numba the optimisers
  2. Numba the objective function
  3. Amend all functions that use Scipy to use FinSolver to use the Numba versions

Overall aim is to make these functions as fast as possible.

Failed Tests

runAllTests.py raises error while testing the master. only two test cases passed. Need to add functionality to check other tests instead of halting if one test fails too.

Testing module TestFinModelRatesBDT (3 out of 90)
Traceback (most recent call last):
  File "tests/runAllTests.py", line 26, in <module>
    moduleName = __import__(moduleTextName)
  File "/home/runner/work/FinancePy/FinancePy/tests/TestFinModelRatesBDT.py", line 284, in <module>
    test_BDTExampleTwo()
  File "/home/runner/work/FinancePy/FinancePy/tests/TestFinModelRatesBDT.py", line 160, in test_BDTExampleTwo
    price = bond.cleanPriceFromDiscountCurve(settlementDate, curve)
  File "/opt/hostedtoolcache/Python/3.8.5/x64/lib/python3.8/site-packages/financepy-0.184-py3.8.egg/financepy/products/bonds/FinBond.py", line 320, in cleanPriceFromDiscountCurve
    self.calcAccruedInterest(settlementDate)
  File "/opt/hostedtoolcache/Python/3.8.5/x64/lib/python3.8/site-packages/financepy-0.184-py3.8.egg/financepy/products/bonds/FinBond.py", line 443, in calcAccruedInterest
    (accFactor, num, _) = dc.yearFrac(self._pcd,
  File "/opt/hostedtoolcache/Python/3.8.5/x64/lib/python3.8/site-packages/financepy-0.184-py3.8.egg/financepy/finutils/FinDayCount.py", line 213, in yearFrac
    freq = FinFrequency(frequencyType)
  File "/opt/hostedtoolcache/Python/3.8.5/x64/lib/python3.8/site-packages/financepy-0.184-py3.8.egg/financepy/finutils/FinFrequency.py", line 30, in FinFrequency
    if frequencyType in FinFrequencyTypes:
  File "/opt/hostedtoolcache/Python/3.8.5/x64/lib/python3.8/enum.py", line 315, in __contains__
    raise TypeError(
TypeError: unsupported operand type(s) for 'in': 'int' and 'EnumMeta'

Normal SABR calibration

It would be very helpful to have a normal vol (bachelier) SABR pricing model. I know the bachelier model exists as an alternative to black's, but the SABR formulation is a bit different.

Inflation Linked Bonds

Add inflation linked bond type and associated real and inflation linked yield measures. Test against BBG.

Perpetual Bond

Add perpetual bond type and associated yield and risk measures.

FinEquity...Options dividends input

Looking at Equity products. FinEquityVanillaOption methods for example take as argument for the dividend as a float. Would be nice to have the dividendCurve object passed as argument directly similar to the discountCurve instead of passing dividendCurve.df(self._expiryDate)

Few common payoffs missing for FX

Great library!

A few useful payoffs could be added in FX:

  • FinFXOneTouchOption (adaptation of FinEquityOneTouchOption)
  • FinDoubleOneTouchOption & FinDoubleDigitalOption (adaptation of "single" barrier payoffs)
  • FinFXDualOneTouchOption & FinFXDualDigitalOption (would require an additional implied correlation parameter)

All five payoffs are quite commonly used by FX market practitioners as considered "Light exotic".

G10 Calendars

@domokane , awesome work with this package! Great to see everything written in Python.

One feature request - it would be great to get calendars added for the rest of the G10 markets. Could use QL calendars as a reference.

Thanks!

Curve Building OIS and IBOR

You'll need to offer curve calibration using outright OIS & IBOR tenor swaps, OIS meeting date swaps, basis swaps of either IBOR/IBOR, IBOR/OIS or OIS/OIS, OIS and IBOR futures, IBOR FRAs (I include ESTR and SOFR in the OIS category). You should also allow for multiple overnight rates per currency (SOFR+FF & ESTR+EONIA).

The most interesting case to cover is probably AUD where you need full multi-curve support as the 3m and 6m curves are co-dependent on each other.

FinEquityVolSurface

Hi,

Would it be possible to extend the FinEquityVolCurve class into a Surface:
User Inputs (for S&P 500 on 11 Jan 2021)

  • value Date (ex: FindDate(2021,1,11) )
  • Array of Expires (ex: [FindDate(2021,2,11), FindDate(2021,3,11), FindDate(2021,4,11), FindDate(2021,7,11), FindDate(2021,10,11), FindDate(2022,1,11), FindDate(2023,1,11)])
  • Array of Strikes (ex: [3037 3418 3608 3703 3798 3893 3988 4178 4557])
  • Array of Vols (ex: [42.94 31.30 25.88 22.94 19.72 16.90 15.31 17.54 25.67,
    37.01 28.25 24.19 21.93 19.57 17.45 15.89 15.34 21.15,
    34.68 27.38 23.82 21.85 19.83 17.98 16.52 15.31 18.94,
    31.41 26.25 23.51 22.05 20.61 19.25 18.03 16.01 15.90,
    29.91 25.58 23.21 22.01 20.83 19.70 18.62 16.63 14.94,
    29.26 25.24 23.03 21.91 20.81 19.73 18.69 16.76 14.63,
    27.59 24.33 22.72 21.93 21.17 20.43 19.71 18.36 16.26] )

This would return a VolSurface object. The volatility method would also be extended to take as input a strike and expiry date.

Implement numba'd conjugate gradient minimiser

The conjugate gradient descent algorithm in scipy is slow and prevents the calling function from being jitted by numba. Idea is to find a python version of the CG minimiser that can be jitted to give faster minimisations.

Implied Volatility for Equity Vanilla Options - Numerical issue

The current Equity implied volatility calculator uses Newton Raphson with an analytical first derivative (vega).

This has numerical issues for short-dated ITM options.

Needs to be fixed by converting ITM option to OTM using put call parity and then solving for that option with new implied price.

BBG Testing Examples

If anyone has access to Bloomberg analytics that I can compare to then please send examples.
Especially with regards to

  • Bonds - bullet, floating rate notes, perpetuals, step-ups, callable, puttable
  • Convertibles - all sorts
  • Options on bonds
  • FRAs
  • Interest rate futures
  • Swaps
  • Cross currency swaps
  • Swap curves construction
  • OIS
  • Caps, floors, swaptions
  • Other exotics
    etc...

FinDate Optimisation

FinDate has been implemented with only some Python datetime dependency in a few functions.

However the issue of processing speed is becoming more important.

I would be interested to know which of the following approaches is superior in speed terms.

  1. Make FinDate (which now includes date and time) a thin layer over datetime i.e. all functions should call datetime functions rather than do processing themselves.

or

  1. Make FinDate independent of datetime and do all of the logic in the FinDate class. In this case consider if FinDate should be sped up using numba ? Functions like addDays and addMonths might benefit.

I imagine two implementations of FinDate which can be implemented and compared. Only core functions of date generation and differencing and rolling foward in date steps should be compared as most other functions build on these.

Vectorisation of Calculations

Some functions take a date as an input. However I would like to be able to pass in a vector/list of dates and/or a vector of enums and get a corresponding vector of values.

This is shown in notebook

https://github.com/domokane/FinancePy/blob/master/notebooks/products/equity/EQUITY_VANILLA_EUROPEAN_STYLE_OPTION_VECTORISATION.ipynb

Consider f(x,y,z). Behaviour that would be good

  1. If x is an N-element vector and y, z not then f(x,y,z) is an N-element vector for each value of x
  2. If x is an N-element vector and y is an N element vector but z is not then f(x,y,z) is an N-element vector for each value of x,y
  3. If x, y, z are all an N-element vector then f(x,y,z) is an N-element vector for each value of x,y

A generic approach would be best that is easy to modify and relies on a central function that takes as argument the function to be called.

Swaption Cube Object

Create an object to store volatility by start tenor, swap tenor and strike
Also does interpolation.

Add an Equity Index Option product type

Add a product type for an option on an equity index called FinEquityIndexOption

This is similar to an Equity vanilla option but customised to index options.

The option can be European or American exercise.

The valuation can take in the futures price rather than the spot price.

It also needs an implied volatility calculation.

Most of this can be copied from FinEquityVanillaOption.

As we have index futures contracts, pricing uses Black's model rather than Black-Scholes.

You will need to add a test module and a notebook showing how this product works.

Reference

https://globaljournals.org/GJMBR_Volume12/10-Pricing-of-Index-Options-Using-Blacks-Model.pdf

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.