-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.pyf("math";["erf" "sqrt"]);.pyf("numpy";["log" "sqrt" "exp"]) | ||
|
||
:" normal cdf " | ||
ncdf::{(1+erf(x%sqrt(2)))%2} | ||
|
||
:" Option delta computed via Black-Scholes " | ||
optiondelta::{[s k r t iv];s::x@0;k::x@1;r::x@2;t::x@3;iv::x@4;ncdf((log(s%k)+(r+0.5*iv^2)*t)%(iv*sqrt(t)))} | ||
|
||
:" Option price computed via Black-Scholes " | ||
d::optiondelta([[100] [100] [0.05] [1] [0.2]]) | ||
.d("expecting: 0.6368306511756191 computed: ");.p(d) | ||
|
||
d1fn::{(log(s%k)+(r+0.5*iv^2)*t)%(iv*sqrt(t))} | ||
d2fn::{d1-iv*sqrt(t)} | ||
pricefn::{(s*ncdf(d1))-(k*exp((-r)*t)*ncdf(d2))} | ||
optionprice::{[s k r t iv d1 d2];s::x@0;k::x@1;r::x@2;t::x@3;iv::x@4;d1::d1fn();d2::d2fn();pricefn()} | ||
|
||
d::optionprice([100 100 0.05 1 0.2]) | ||
.d("expecting: 10.4506 computed: ");.p(d) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import numpy as np | ||
import math | ||
#from scipy.stats import norm | ||
|
||
|
||
def _get_option_delta(s, k, r, t, sigma): | ||
d1 = (np.log(s / k) + (r + 0.5 * sigma ** 2) * t) / (sigma * np.sqrt(t)) | ||
delta = norm.cdf(d1) | ||
return delta | ||
|
||
def _get_option_price(s, k, r, t, sigma): | ||
d1 = (np.log(s / k) + (r + 0.5 * sigma ** 2) * t) / (sigma * np.sqrt(t)) | ||
d2 = d1 - sigma * np.sqrt(t) | ||
option_price = s * norm.cdf(d1) - k * np.exp(-r * t) * norm.cdf(d2) | ||
return option_price | ||
|
||
def norm_cdf(x, mu=0, sigma=1): | ||
#return (1 + np.erf((x - mu) / (sigma * np.sqrt(2)))) / 2 | ||
return (1 + math.erf(x / math.sqrt(2))) / 2 | ||
|
||
def get_option_delta(s, k, r, t, sigma): | ||
d1 = (np.log(s / k) + (r + 0.5 * sigma ** 2) * t) / (sigma * np.sqrt(t)) | ||
#delta = norm.cdf(d1) | ||
delta = norm_cdf(d1) | ||
return delta | ||
|
||
def get_option_price(s, k, r, t, sigma): | ||
d1 = (np.log(s / k) + (r + 0.5 * sigma ** 2) * t) / (sigma * np.sqrt(t)) | ||
d2 = d1 - sigma * np.sqrt(t) | ||
#option_price = s * norm.cdf(d1) - k * np.exp(-r * t) * norm.cdf(d2) | ||
option_price = s * norm_cdf(d1) - k * np.exp(-r * t) * norm_cdf(d2) | ||
return option_price | ||
|
||
|
||
def test_get_option_delta(): | ||
assert np.isclose(get_option_delta(100, 100, 0.05, 1, 0.2), 0.6368306511756191, rtol=1e-5) | ||
|
||
def test_get_option_price(): | ||
assert np.isclose(get_option_price(100, 100, 0.05, 1, 0.2), 10.4506, rtol=1e-4) | ||
|
||
test_get_option_delta() | ||
test_get_option_price() | ||
|
||
from py_vollib.black_scholes import black_scholes | ||
from py_vollib.black_scholes.greeks.analytical import delta | ||
|
||
|
||
def test_get_option_delta(): | ||
s = 100 | ||
k = 100 | ||
r = 0.05 | ||
t = 1 | ||
sigma = 0.2 | ||
expected_delta = delta('c', s, k, t, r, sigma) | ||
assert np.isclose(get_option_delta(s, k, r, t, sigma), expected_delta, rtol=1e-5) | ||
|
||
def test_get_option_price(): | ||
s = 100 | ||
k = 100 | ||
r = 0.05 | ||
t = 1 | ||
sigma = 0.2 | ||
expected_price = black_scholes('c', s, k, t, r, sigma) | ||
assert np.isclose(get_option_price(s, k, r, t, sigma), expected_price, rtol=1e-4) | ||
|
||
|
||
test_get_option_delta() | ||
test_get_option_price() | ||
|