Skip to content

Commit

Permalink
Simulation Pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmichaelpaolucci committed Apr 21, 2021
1 parent fff9d65 commit 947d583
Show file tree
Hide file tree
Showing 20 changed files with 460 additions and 2 deletions.
127 changes: 127 additions & 0 deletions QFin.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
Metadata-Version: 2.1
Name: QFin
Version: 0.0.11
Summary: A Python package for mathematical finance.
Home-page: UNKNOWN
Author: Roman Paolucci
Author-email: <[email protected]>
License: MIT
Description: # Q-Fin
A mathematical finance Python library

# Time Value of Money

# Bond Pricing

# Option Pricing

### <a href="https://medium.com/swlh/deriving-the-black-scholes-model-5e518c65d0bc"> Black-Scholes Pricing</a>
Theoretical options pricing for non-dividend paying stocks is available via the BlackScholesCall and BlackScholesPut classes.

```Python
# 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)
```

```Python
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.
```Python
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.
```Python
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.
```Python
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.
```Python
print('Call theta: ', euro_call.theta)
print('Put theta: ', euro_put.theta)
```
```
Call theta: -6.35319039407325
Put theta: -5.363140560324083
```

# Simulation Pricing

### <a href="https://medium.com/swlh/python-for-pricing-exotics-3a2bfab5ff66"> Exotic Options </a>
Simulation pricing for exotic options is available under the assumptions associated with Geometric Brownian motion.

#### Binary Options
```Python
# 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)
```

```Python
print(binary_call.price)
print(binary_put.price)
```

```
22.42462873441866
27.869902820039087
```
# Futures Pricing

Keywords: python,finance
Platform: UNKNOWN
Classifier: Development Status :: 1 - Planning
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: Unix
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Description-Content-Type: text/markdown
13 changes: 13 additions & 0 deletions QFin.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
README.md
setup.py
QFin/__init__.py
QFin/bonds.py
QFin/futures.py
QFin/options.py
QFin/simulations.py
QFin/tvm.py
QFin.egg-info/PKG-INFO
QFin.egg-info/SOURCES.txt
QFin.egg-info/dependency_links.txt
QFin.egg-info/requires.txt
QFin.egg-info/top_level.txt
1 change: 1 addition & 0 deletions QFin.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions QFin.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scipy
numpy
1 change: 1 addition & 0 deletions QFin.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QFin
4 changes: 2 additions & 2 deletions __init__.py → QFin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tvm.*
import bonds.*
import options.*
import exotics.*
import
import simulations.*
import futures.*
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions simulations.py → QFin/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ def simulate_price(self, strike, payout, n, r, S, mu, sigma, dt, T):

def __init__(self, strike, payout, n, r, S, mu, sigma, dt, T):
self.price = self.simulate_price(strike, payout, n, r, S, mu, sigma, dt, T)


class MonteCarloBarrierCall:

def __init__(self):
pass


class MonteCarloBarrierPut:

def __init__(self):
pass
File renamed without changes.
5 changes: 5 additions & 0 deletions build/lib/QFin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import tvm.*
import bonds.*
import options.*
import simulations.*
import futures.*
Empty file added build/lib/QFin/bonds.py
Empty file.
Empty file added build/lib/QFin/futures.py
Empty file.
152 changes: 152 additions & 0 deletions build/lib/QFin/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import math
from scipy.stats import norm


class BlackScholesCall:

def call_delta(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.cdf(x1)
return z1

def call_gamma(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.cdf(x1)
z2 = z1/(asset_price*asset_volatility*math.sqrt(time_to_expiration))
return z2

def call_vega(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.pdf(x1)
z2 = asset_price*z1*math.sqrt(time_to_expiration)
return z2

def call_theta(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
n1 = -((asset_price*asset_volatility*norm.pdf(x1))/(2*math.sqrt(time_to_expiration)))
n2 = -(risk_free_rate*strike_price*math.exp(-risk_free_rate*time_to_expiration)*norm.cdf((x1 - (asset_volatility*math.sqrt(time_to_expiration)))))
return (n1 + n2)

def call_price(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.cdf(x1)
z1 = z1*asset_price
x2 = math.log(asset_price/(strike_price)) - .5*(asset_volatility*asset_volatility)*time_to_expiration
x2 = x2/(asset_volatility*(time_to_expiration**.5))
z2 = norm.cdf(x2)
z2 = b*strike_price*z2
return z1 - z2

def __init__(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
self.asset_price = asset_price
self.asset_volatility = asset_volatility
self.strike_price = strike_price
self.time_to_expiration = time_to_expiration
self.risk_free_rate = risk_free_rate
self.price = self.call_price(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.delta = self.call_delta(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.gamma = self.call_gamma(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.vega = self.call_vega(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.theta = self.call_theta(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)


class BlackScholesPut:

def put_delta(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.cdf(x1)
return z1 - 1

def put_gamma(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.cdf(x1)
z2 = z1/(asset_price*asset_volatility*math.sqrt(time_to_expiration))
return z2

def put_vega(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.pdf(x1)
z2 = asset_price*z1*math.sqrt(time_to_expiration)
return z2

def put_theta(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
x1 = math.log(asset_price/(strike_price)) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
n1 = -((asset_price*asset_volatility*norm.pdf(x1))/(2*math.sqrt(time_to_expiration)))
n2 = (risk_free_rate*strike_price*math.exp(-risk_free_rate*time_to_expiration)*norm.cdf(-(x1 - (asset_volatility*math.sqrt(time_to_expiration)))))
return (n1 + n2)

def put_price(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
b = math.exp(-risk_free_rate*time_to_expiration)
x1 = math.log((asset_price)/strike_price) + .5*(asset_volatility*asset_volatility)*time_to_expiration
x1 = x1/(asset_volatility*(time_to_expiration**.5))
z1 = norm.cdf(x1)
z1 = b*strike_price*z1
x2 = math.log((asset_price)/strike_price) - .5*(asset_volatility*asset_volatility)*time_to_expiration
x2 = x2/(asset_volatility*(time_to_expiration**.5))
z2 = norm.cdf(x2)
z2 = asset_price*z2
return z1 - z2

def __init__(
self, asset_price, asset_volatility, strike_price,
time_to_expiration, risk_free_rate
):
self.asset_price = asset_price
self.asset_volatility = asset_volatility
self.strike_price = strike_price
self.time_to_expiration = time_to_expiration
self.risk_free_rate = risk_free_rate
self.price = self.put_price(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.delta = self.put_delta(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.gamma = self.put_gamma(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.vega = self.put_vega(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
self.theta = self.put_theta(asset_price, asset_volatility, strike_price, time_to_expiration, risk_free_rate)
Loading

0 comments on commit 947d583

Please sign in to comment.