-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path003.snowball.py
73 lines (60 loc) · 2.34 KB
/
003.snowball.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
import datetime as dt
import pandas as pd
import numpy as np
from dateutil.relativedelta import relativedelta
from dal import *
spot = 1.00
vol = 0.15
rate = 0.0
div = 0.0
ko = 1.00
ki = 0.88
coupon = 0.069
today = dt.date(2023, 3, 1)
maturity = dt.date(2025, 3, 1)
event_dates = []
event_dates = ["KI", "KO", "STRIKE", "COUPON"] + [Date_(today.year, today.month, today.day)]
start = event_dates[4]
EvaluationDate_Set(start)
events = [f"{ki:.6f}", f"{ko:.6f}", f"{spot:.6f}", f"{coupon:.6f}", "alive = 1 is_ki = 0"]
n_paths = 2 ** 20
use_bb = False
rsg = "sobol"
freq = "1M"
event_dates.append(f"START: 2023-06-01 END: 2025-02-01 FREQ: {freq}")
events.append(
f"""
if spot() < KI:0.001 then is_ki = 1 end
if spot() > KO:0.001 then call pays alive * COUPON * DCF(ACT365F, {start}, PeriodEnd) alive = 0 end
""")
event_dates.append(Date_(maturity.year, maturity.month, maturity.day))
events.append(
f"""
if spot() < KI:0.001 then is_ki = 1 end
if spot() > KO:0.001 then call pays alive * COUPON * DCF(ACT365F, {start}, {event_dates[-1]}) alive = 0 end
call pays alive * is_ki * (spot() - STRIKE) + alive * (1.000000 - is_ki) * COUPON * DCF(ACT365F, {start}, {event_dates[-1]})
"""
)
print("------ Model Parameters ------")
print(f"rate : {rate* 100:.2f}%")
print(f"div : {div * 100:.2f}%")
print(f"vol : {vol * 100:.2f}%\n")
print("------ Product Description ------")
print(f"NPV date : {event_dates[4]}")
print(f"Maturity : {event_dates[-1]}")
print(f"knock in : {ki:.2f}")
print(f"knock out : {ko:.2f}")
print(f"# of obs : {(len(event_dates) - 4) * 12 if freq == '1M' else (len(event_dates) - 4) * 51}")
print(f"# of paths: {n_paths}\n")
product = Product_New(event_dates, events)
model = BSModelData_New(spot, vol, rate, div)
print("------ Product Evaluation ------")
now = dt.datetime.now()
res = MonteCarlo_Value(product, model, n_paths, rsg, use_bb, False)
all_res = {"Non-AAD": [res["PV"], np.nan, np.nan, np.nan, np.nan, (dt.datetime.now() - now).total_seconds() * 1000]}
now = dt.datetime.now()
res = MonteCarlo_Value(product, model, n_paths, rsg, use_bb, True)
all_res["AAD"] = [res["PV"], res["d_spot"], res["d_vol"], res["d_rate"], res["d_div"], (dt.datetime.now() - now).total_seconds() * 1000]
df = pd.DataFrame.from_dict(all_res)
df.index = ["NPV", "delta", "vega", "dP/dR", "dP/dDiv", "Elapsed (ms)"]
print(df.T.to_markdown())