Skip to content

Commit 8287b5e

Browse files
committed
Added back Equity derivative unit tests
1 parent 82d5888 commit 8287b5e

38 files changed

Lines changed: 2022 additions & 950 deletions

financepy/models/black.py

Lines changed: 282 additions & 342 deletions
Large diffs are not rendered by default.

financepy/models/black_shifted.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class BlackShifted:
2020

2121
####################################################################################
2222

23-
def __init__(self, volatility: float, shift: float, implementation: int = 0) -> None:
23+
def __init__(
24+
self, volatility: float, shift: float, implementation: int = 0
25+
) -> None:
2426
"""Create FinModel black using parameters."""
2527
self.volatility = volatility
2628
self.shift = shift
27-
self.implementation = 0
29+
self.implementation = implementation
2830
self.num_steps = 0
2931
self.seed = 0
3032
self.param1 = 0
@@ -44,13 +46,38 @@ def value(
4446
measure following a change of measure. The sign of the shift is the
4547
same as Matlab."""
4648

49+
if call_or_put not in (
50+
OptionTypes.EUROPEAN_CALL,
51+
OptionTypes.EUROPEAN_PUT,
52+
):
53+
raise ValueError("Option type must be EUROPEAN_CALL or EUROPEAN_PUT")
54+
55+
if time_to_expiry < 0.0:
56+
raise ValueError("time_to_expiry must be non-negative")
57+
58+
if df < 0.0:
59+
raise ValueError("df must be non-negative")
60+
61+
if forward_rate + self.shift <= 0.0:
62+
raise ValueError("forward_rate + shift must be positive")
63+
64+
if strike_rate + self.shift <= 0.0:
65+
raise ValueError("strike_rate + shift must be positive")
66+
4767
s = self.shift
4868
f = forward_rate
4969
t = time_to_expiry
5070
k = strike_rate
5171
sqrt_t = np.sqrt(t)
5272
vol = self.volatility
5373

74+
if t == 0.0 or vol == 0.0:
75+
if call_or_put == OptionTypes.EUROPEAN_CALL:
76+
return df * max(f - k, 0.0)
77+
78+
if call_or_put == OptionTypes.EUROPEAN_PUT:
79+
return df * max(k - f, 0.0)
80+
5481
d1 = np.log((f + s) / (k + s)) + vol * vol * t / 2
5582
d1 = d1 / (vol * sqrt_t)
5683
d2 = d1 - vol * sqrt_t

0 commit comments

Comments
 (0)