Q-Fin: A Python library for mathematical finance.

Related tags

Finance Q-Fin
Overview

Q-Fin

A Python library for mathematical finance.

Installation

https://pypi.org/project/QFin/

pip install qfin

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

Standard model for implementing 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]

Stochastic Variance Process

Stochastic volatility model based on Heston's paper (1993).

from qfin.simulations import StochasticVarianceModel
# 100 - initial underlying asset price
# 0 - underlying asset drift (mu)
# .01 - risk free rate of interest
# .05 - continuous dividend
# 2 - rate in which variance reverts to the implied long run variance
# .25 - implied long run variance as time tends to infinity
# -.7 - correlation of motion generated
# .3 - Variance's volatility
# 1/52 - time steps (dt)
# 1 - time to maturity (annum)
svm = StochasticVarianceModel(100, 0, .01, .05, 2, .25, -.7, .3, .09, 1/52, 1)
print(svm.simulated_path)
[98.21311553503577, 100.4491317019877, 89.78475515902066, 89.0169762497475, 90.70468848525869, 86.00821802256675, 80.74984494892573, 89.05033807013137, 88.51410029337134, 78.69736798230346, 81.90948751054125, 83.02502248913251, 83.46375102829755, 85.39018282900138, 78.97401642238059, 78.93505221741903, 81.33268688455111, 85.12156706038515, 79.6351983987908, 84.2375291273571, 82.80206517176038, 89.63659376223292, 89.22438477640516, 89.13899271995662, 94.60123239511816, 91.200165507022, 96.0578905115345, 87.45399399599378, 97.908745925816, 97.93068975065052, 103.32091104292813, 110.58066464778392, 105.21520242908348, 99.4655106985056, 106.74882010453683, 112.0058519886151, 110.20930861932342, 105.11835510815085, 113.59852610881678, 107.13315204738092, 108.36549026977205, 113.49809943785571, 122.67910031073885, 137.70966794451425, 146.13877267735612, 132.9973784430374, 129.75750117504984, 128.7467891695649, 127.13115959080305, 130.47967713110302, 129.84273088908265, 129.6411527208744]

Simulation Pricing

Exotic Options

Simulation pricing for exotic options is available under the assumptions associated with the respective stochastic processes. Geometric Brownian motion is the base underlying stochastic process used in each Monte Carlo simulation. However, should additional parameters be provided, the appropriate stochastic process will be used to generate each sample path.

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)
# These additional parameters will generate a Monte Carlo price based on a stochastic volatility process
# 2 - rate in which variance reverts to the implied long run variance
# .25 - implied long run variance as time tends to infinity
# -.5 - correlation of motion generated
# .02 - continuous dividend
# .3 - Variance's volatility
put_option = MonteCarloPut(100, 1000, .01, 100, 0, .3, 1/52, 1, 2, .25, -.5, .02, .3)
print(call_option.price)
print(put_option.price)
12.73812121792851
23.195814963576286

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

Barrier Options

from qfin.simulations import MonteCarloBarrierCall
from qfin.simulations import MonteCarloBarrierPut
# 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)
# True/False - Barrier is Up or Down
# True/False - Barrier is In or Out
barrier_call = MonteCarloBarrierCall(100, 1000, 150, .01, 100, 0, .3, 1/52, 1, up=True, out=True)
barrier_put = MonteCarloBarrierCall(100, 1000, 95, .01, 100, 0, .3, 1/52, 1, up=False, out=False)
print(binary_call.price)
print(binary_put.price)
4.895841997908933
5.565856754630819

Asian Options

from qfin.simulations import MonteCarloAsianCall
from qfin.simulations import MonteCarloAsianPut
# 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)
asian_call = MonteCarloAsianCall(100, 1000, .01, 100, 0, .3, 1/52, 1)
asian_put = MonteCarloAsianPut(100, 1000, .01, 100, 0, .3, 1/52, 1)
print(asian_call.price)
print(asian_put.price)
6.688201154529573
7.123274528125894

Extendible Options

from qfin.simulations import MonteCarloExtendibleCall
from qfin.simulations import MontecarloExtendiblePut
# 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)
# .5 - extension if out of the money at expiration
extendible_call = MonteCarloExtendibleCall(100, 1000, .01, 100, 0, .3, 1/52, 1, .5)
extendible_put = MonteCarloExtendiblePut(100, 1000, .01, 100, 0, .3, 1/52, 1, .5)
print(extendible_call.price)
print(extendible_put.price)
13.60274931789973
13.20330578685724

Futures Pricing

You might also like...
Zipline, a Pythonic Algorithmic Trading Library
Zipline, a Pythonic Algorithmic Trading Library

Zipline is a Pythonic algorithmic trading library. It is an event-driven system for backtesting. Zipline is currently used in production as the backte

Technical Analysis Library using Pandas and Numpy
Technical Analysis Library using Pandas and Numpy

Technical Analysis Library in Python It is a Technical Analysis library useful to do feature engineering from financial time series datasets (Open, Cl

A python wrapper for Alpha Vantage API for financial data.
A python wrapper for Alpha Vantage API for financial data.

alpha_vantage Python module to get stock data/cryptocurrencies from the Alpha Vantage API Alpha Vantage delivers a free API for real time financial da

Portfolio and risk analytics in Python
Portfolio and risk analytics in Python

pyfolio pyfolio is a Python library for performance and risk analysis of financial portfolios developed by Quantopian Inc. It works well with the Zipl

Python sync/async framework for Interactive Brokers API

Introduction The goal of the IB-insync library is to make working with the Trader Workstation API from Interactive Brokers as easy as possible. The ma

bt - flexible backtesting for Python

bt - Flexible Backtesting for Python bt is currently in alpha stage - if you find a bug, please submit an issue. Read the docs here: http://pmorissett

ARCH models in Python
ARCH models in Python

arch Autoregressive Conditional Heteroskedasticity (ARCH) and other tools for financial econometrics, written in Python (with Cython and/or Numba used

:mag_right: :chart_with_upwards_trend: :snake: :moneybag:  Backtest trading strategies in Python.
:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.

Backtesting.py Backtest trading strategies with Python. Project website Documentation the project if you use it. Installation $ pip install backtestin

Este repositorio es creado con el fin de brindar soporte a las personas que están en el proceso de aprendizaje MISIONTIC 2022 en la universidad de Antioquia
Este repositorio es creado con el fin de brindar soporte a las personas que están en el proceso de aprendizaje MISIONTIC 2022 en la universidad de Antioquia

Este repositorio es creado con el fin de brindar soporte a las personas que están en el proceso de aprendizaje MISIONTIC 2022 en la universidad de Antioquia. Hecho por los estudiantes para los estudiantes y todos aquellos que se quieran involucran en el proceso.

High-performance TensorFlow library for quantitative finance.

TF Quant Finance: TensorFlow based Quant Finance Library Table of contents Introduction Installation TensorFlow training Development roadmap Examples

Pandas Machine Learning and Quant Finance Library Collection
Pandas Machine Learning and Quant Finance Library Collection

Pandas Machine Learning and Quant Finance Library Collection

Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It can use GPUs and perform efficient symbolic differentiation.

============================================================================================================ `MILA will stop developing Theano https:

Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It can use GPUs and perform efficient symbolic differentiation.

============================================================================================================ `MILA will stop developing Theano https:

Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It can use GPUs and perform efficient symbolic differentiation.

============================================================================================================ `MILA will stop developing Theano https:

A Python library created to assist programmers with complex mathematical functions
A Python library created to assist programmers with complex mathematical functions

libmaths was created not only as a learning experience for me, but as a way to make mathematical models in seconds for Python users using mat

A Python library created to assist programmers with complex mathematical functions
A Python library created to assist programmers with complex mathematical functions

libmaths libmaths was created not only as a learning experience for me, but as a way to make mathematical models in seconds for Python users using mat

Official Python wrapper for the Quantel Finance API
Official Python wrapper for the Quantel Finance API

Quantel is a powerful financial data and insights API. It provides easy access to world-class financial information. Quantel goes beyond just financial statements, giving users valuable information like insider transactions, major shareholder transactions, share ownership, peers, and so much more.

personal finance tracker, written in python 3 and using the wxPython GUI toolkit.
personal finance tracker, written in python 3 and using the wxPython GUI toolkit.

personal finance tracker, written in python 3 and using the wxPython GUI toolkit.

Backtest 1000s of minute-by-minute trading algorithms for training AI with automated pricing data from: IEX, Tradier and FinViz. Datasets and trading performance automatically published to S3 for building AI training datasets for teaching DNNs how to trade. Runs on Kubernetes and docker-compose. >150 million trading history rows generated from +5000 algorithms. Heads up: Yahoo's Finance API was disabled on 2019-01-03 https://developer.yahoo.com/yql/
Comments
  • Problem with the Geometric Brownian Motion Code from YT video

    Problem with the Geometric Brownian Motion Code from YT video

    I get the following error:


    TypeError Traceback (most recent call last) Cell In [19], line 1 ----> 1 sample_paths = geometric_Brownian_motion (S_0, mu, sigma, T, dt, n)

    Cell In [17], line 13, in geometric_Brownian_motion(S_0, mu, sigma, T, dt, n) 10 prices.append( prices[-1]np.exp((mu - .5(sigma2))dt + sigmanp.random.normal(0, np.sqrt(dt)))) 11 time += dt ---> 13 if T (time) > 0: 14 prices.append(prices[-1]np.exp((mu - .5(sigma2))(T-time) + sigmanp.random.normal(0, np.sqrt(T-time)))) 16 paths.append(prices)

    TypeError: 'int' object is not callable

    under the following code: sample_paths = geometric_Brownian_motion (S_0, mu, sigma, T, dt, n)

    opened by DJRKM 2
  • Negative Put Prices

    Negative Put Prices

    The library produces negative prices for put options having small delta. Calls don't have this behaviour (price tends to zero, as expected).

    i.e compare:

    BlackScholesCall(4600, 0.19, 4400, float(5)/365, 0.01).price # 201.4
    BlackScholesPut(4600, 0.19, 4400, float(5)/365, 0.01).price # -190.6
    

    or see (where price and delta columns are derived from q-fin BlackScholesPut)

    image

    I'm wondering what I should make of this -- whether there's an error with the library, or a quirk of BS model (in which case I can take a floor of zero of put price value).

    opened by micycle1 2
  • Backtesting code error - no 'modules.isolation_model' found

    Backtesting code error - no 'modules.isolation_model' found

    I'm enjoying your Medium article and have implemented the code but its giving errors.

    Are these suppsed to be part of one huge class or is there a function not included? I've expanded upon your code in my repo here for reference since I didnt see it in yours. Thanks for writing these articles they are good reference material.

    box/PycharmProjects/Financial_Backtrader/backtesting_b.py", line 3, in <module>
        from models.isolation_model import IsolationModel
    ModuleNotFoundError: No module named 'models'
    

    https://github.com/datatalking/Financial_Backtrader/issues/1

    opened by datatalking 1
Owner
Roman Paolucci
Quantitative Finance, Mathematics, and Computer Science
Roman Paolucci
personal finance tracker, written in python 3 and using the wxPython GUI toolkit.

personal finance tracker, written in python 3 and using the wxPython GUI toolkit.

wenbin wu 23 Oct 30, 2022
Yahoo! Finance market data downloader (+faster Pandas Datareader)

Yahoo! Finance market data downloader Ever since Yahoo! finance decommissioned their historical data API, many programs that relied on it to stop work

Ran Aroussi 8.4k Jan 1, 2023
'Personal Finance' is a project where people can manage and track their expenses

Personal Finance by Abhiram Rishi Pratitpati 'Personal Finance' is a project where people can manage and track their expenses. It is hard to keep trac

Abhiram Rishi Prattipati 1 Dec 21, 2021
ffn - a financial function library for Python

ffn - Financial Functions for Python Alpha release - please let me know if you find any bugs! If you are looking for a full backtesting framework, ple

Philippe Morissette 1.4k Jan 1, 2023
An Algorithmic Trading Library for Crypto-Assets in Python

Service Master Develop CI Badge Catalyst is an algorithmic trading library for crypto-assets written in Python. It allows trading strategies to be eas

Enigma 2.4k Jan 5, 2023
Python library for backtesting trading strategies & analyzing financial markets (formerly pythalesians)

finmarketpy (formerly pythalesians) finmarketpy is a Python based library that enables you to analyze market data and also to backtest trading strateg

Cuemacro 3k Dec 30, 2022
Python Backtesting library for trading strategies

backtrader Yahoo API Note: [2018-11-16] After some testing it would seem that data downloads can be again relied upon over the web interface (or API v

DRo 9.8k Dec 30, 2022
Python Algorithmic Trading Library

PyAlgoTrade PyAlgoTrade is an event driven algorithmic trading Python library. Although the initial focus was on backtesting, paper trading is now pos

Gabriel Becedillas 3.9k Jan 1, 2023
Beibo is a Python library that uses several AI prediction models to predict stocks returns over a defined period of time.

Beibo is a Python library that uses several AI prediction models to predict stocks returns over a defined period of time.

Santosh 54 Dec 10, 2022
Indicator divergence library for python

Indicator divergence library This module aims to help to find bullish/bearish divergences (regular or hidden) between two indicators using argrelextre

null 8 Dec 13, 2022