A Python package for mathematical finance.
Project description
Q-Fin
A mathematical finance Python library
Installation
https://pypi.org/project/QFin/
pip install qfin
Time Value of Money
Bond Pricing
Option Pricing
Black-Scholes Pricing
Theoretical options pricing for non-dividend paying stocks is available via the BlackScholesCall and BlackScholesPut classes.
from qfin.options import BlackScholesCall
from qfin.options import BlackScholesPut
# 100 - initial underlying asset price
# .3 - asset underlying volatility
# 100 - option strike price
# 1 - time to maturity (annum)
# .01 - risk free rate of interest
euro_call = BlackScholesCall(100, .3, 100, 1, .01)
euro_put = BlackScholesPut(100, .3, 100, 1, .01)
print('Call price: ', euro_call.price)
print('Put price: ', euro_put.price)
Call price: 12.361726191532611
Put price: 11.366709566449416
Option Greeks
First-order and some second-order partial derivatives of the Black-Scholes pricing model are available.
Delta
First-order partial derivative with respect to the underlying asset price.
print('Call delta: ', euro_call.delta)
print('Put delta: ', euro_put.delta)
Call delta: 0.5596176923702425
Put delta: -0.4403823076297575
Gamma
Second-order partial derivative with respect to the underlying asset price.
print('Call gamma: ', euro_call.gamma)
print('Put gamma: ', euro_put.gamma)
Call gamma: 0.018653923079008084
Put gamma: 0.018653923079008084
Vega
First-order partial derivative with respect to the underlying asset volatility.
print('Call vega: ', euro_call.vega)
print('Put vega: ', euro_put.vega)
Call vega: 39.447933090788894
Put vega: 39.447933090788894
Theta
First-order partial derivative with respect to the time to maturity.
print('Call theta: ', euro_call.theta)
print('Put theta: ', euro_put.theta)
Call theta: -6.35319039407325
Put theta: -5.363140560324083
Stochastic Processes
Simulating asset paths is available using common stochastic processes.
Geometric Brownian motion
from qfin.simulations import GeometricBrownianMotion
# 100 - initial underlying asset price
# 0 - underlying asset drift (mu)
# .3 - underlying asset volatility
# 1/52 - time steps (dt)
# 1 - time to maturity (annum)
gbm = GeometricBrownianMotion(100, 0, .3, 1/52, 1)
print(gbm.simulated_path)
[107.0025048205179, 104.82320056538235, 102.53591127422398, 100.20213816642244, 102.04283245358256, 97.75115579923988, 95.19613943526382, 96.9876745495834, 97.46055174410736, 103.93032659279226, 107.36331603194304, 108.95104494118915, 112.42823319947456, 109.06981862825943, 109.10124426285238, 114.71465058375804, 120.00234814086286, 116.91730159923688, 118.67452601825876, 117.89233466917202, 118.93541257993591, 124.36106523035058, 121.26088015675688, 120.53641952983601, 113.73881043255554, 114.91724168548876, 112.94192281337791, 113.55773877160591, 107.49491796151044, 108.0715118831013, 113.01893111071472, 110.39204535739405, 108.63917240906524, 105.8520395233433, 116.2907247951675, 114.07340779267213, 111.06821275009212, 109.65530380775077, 105.78971667172465, 97.75385009989282, 97.84501925249452, 101.90695475825825, 106.0493833583297, 105.48266575656817, 106.62375752876223, 112.39829297429974, 111.22855058562658, 109.89796974828265, 112.78068777325248, 117.80550869036715, 118.4680557054793, 114.33258212280838]
Simulation Pricing
Exotic Options
Simulation pricing for exotic options is available under the assumptions associated with Geometric Brownian motion.
Vanilla Options
from qfin.simulations import MonteCarloCall
from qfin.simulations import MonteCarloPut
# 100 - strike price
# 1000 - number of simulated price paths
# .01 - risk free rate of interest
# 100 - initial underlying asset price
# 0 - underlying asset drift (mu)
# .3 - underlying asset volatility
# 1/52 - time steps (dt)
# 1 - time to maturity (annum)
call_option = MonteCarloCall(100, 1000, .01, 100, 0, .3, 1/52, 1)
put_option = MonteCarloCall(100, 1000, .01, 100, 0, .3, 1/52, 1)
print(call_option.price)
print(put_option.price)
12.73812121792851
12.304109267761028
Binary Options
from qfin.simulations import MonteCarloBinaryCall
from qfin.simulations import MonteCarloBinaryPut
# 100 - strike price
# 50 - binary option payout
# 1000 - number of simulated price paths
# .01 - risk free rate of interest
# 100 - initial underlying asset price
# 0 - underlying asset drift (mu)
# .3 - underlying asset volatility
# 1/52 - time steps (dt)
# 1 - time to maturity (annum)
binary_call = MonteCarloBinaryCall(100, 50, 1000, .01, 100, 0, .3, 1/52, 1)
binary_put = MonteCarloBinaryPut(100, 50, 1000, .01, 100, 0, .3, 1/52, 1)
print(binary_call.price)
print(binary_put.price)
22.42462873441866
27.869902820039087
Futures Pricing
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file QFin-0.0.16.tar.gz.
File metadata
- Download URL: QFin-0.0.16.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b61dc3d509e7d3f2b06d9223605784a8684b3f2e7e3012029ac3cec7229185e
|
|
| MD5 |
57a9518283be6efc3592c4b839e93906
|
|
| BLAKE2b-256 |
279c8be315012f30af7fafd38bdb5ce48c2ff73b508b712df8c478faa3b6e4f2
|
File details
Details for the file QFin-0.0.16-py3-none-any.whl.
File metadata
- Download URL: QFin-0.0.16-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33e84ccc80c4bcab0c8117f0cdbf5b00f8b098afde66e2017428a805875e4ec6
|
|
| MD5 |
f6e3c5962aff1f497004db1d775668c9
|
|
| BLAKE2b-256 |
16e7afd2d8ea7b66e9a872221b344164ecd31a34dffd1757e7d8912eebc9d59e
|