-
Notifications
You must be signed in to change notification settings - Fork 0
/
American Option.py
77 lines (51 loc) · 2.19 KB
/
American Option.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from dylan.payoff import VanillaPayoff, call_payoff, put_payoff
from dylan.engine import BinomialPricingEngine, EuropeanBinomialPricer
from dylan.marketdata import MarketData
from dylan.option import Option
def EurpeanBinomialPricer(pricing_engine, option, data):
"""
The binomial option pricing model for an American option.
Args:
pricing_engine (PricingEngine): a pricing method via the PricingEngine interface
option (Payoff): an option payoff via the Payoff interface
data (MarketData): a market data variable via the MarketData interface
"""
expiry = option.expiry
strike = option.strike
(spot, rate, volatility, dividend) = data.get_data()
steps = pricing_engine.steps
nodes = steps + 1
h = expiry / steps
u = np.exp((rate * h) + volatility * np.sqrt(h))
d = np.exp((rate * h) - volatility * np.sqrt(h))
pu = (np.exp(rate * h) - d) / (u - d)
pd = 1 - pu
disc = np.exp(-rate * expiry)
spotT = 0.0
payoffT = 0.0
for i in range(nodes):
spotT = spot * (u ** (steps - i)) * (d ** (i))
payoffT += option.payoff(spotT) * binom.pmf(steps - i, steps, pu)
price = disc * payoffT
return price
def AmericanBinomialPricer(pricing_engine, option, data):
expiry = option.expiry
strike = option.strike
(spot, rate, volatility, dividend) = data.get_data()
steps = pricing_engine.steps
nodes = steps + 1
h = expiry/steps
u = np.exp((rate * h) + volatility * np.sqrt(h))
d = np.exp((rate* h) - volatility * np.sqrt(h))
pu = (np.exp(rate * h) - d)/ (u - d)
pd = 1 - pu
disc = np.exp(-rate * expiry)
V = [[0.0 for k in range(i + 1)] for j in range (steps + 1)]
for i in range(nodes):
V[i][k] = max(spot * (u ** (steps - i)) * (d**(i)) - strike, 0.0)
for i in range(steps - 1, -1, -1):
for k in range(i + 1):
V1 = (disc * V[i+1][k+1] + pd * V[i+1][k])
V2 = max(spot - strike, 0)
V[i][k] = max(V1,V2)
return V[0][0]