-
Notifications
You must be signed in to change notification settings - Fork 1
/
eod_simulation_demo.py
169 lines (147 loc) · 5.71 KB
/
eod_simulation_demo.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pandas as pd
from scripts.fetch_data import grab_data
from scripts.util import create_vanilla_option, create_underlying, combine_portfolios
from scripts.simulation import run_simulation
from scripts.portfolio import Portfolio
from collections import OrderedDict
# plotly imports
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools
# api authentication for plotly.
import plotly
plotly.tools.set_credentials_file(
username='imran.ahmad', api_key='2HnnK4oSHSi6rWRVACVt')
plotly.tools.set_config_file(world_readable=False,
sharing='private')
def plot_output(log):
# get unique values of each paramter.
allvids = log.vol_id.unique()
# tmp = log[log.vol_id == allvids[0]]
tmp = log.drop_duplicates('value_date')
# tmp.to_csv('tmp_log.csv', index=False)
nets = tmp
dailypnl = nets.eod_pnl_net.values
cupnl_gross = nets.cu_pnl_gross.values
cupnl_net = nets.cu_pnl_net.values
gpnl = nets.cu_gamma_pnl.values
vpnl = nets.cu_vega_pnl.values
dates = nets.value_date
# delta_rolled = nets.delta_rolled
# dd_pct = nets.drawdown_pct.values
# drd = nets.drawdown.values
# colors = ['rgb(55, 83, 109)' for x in delta_rolled if (not x) else
# 'rgba(204,204,204)']
log.columns = ['future price' if x == 'px_settle' else x for x in log.columns]
ftprices = {}
for x in allvids:
ftprices[x] = {}
tmp2 = log[log.vol_id == x]
ftprices[x]['values'] = tmp2['future price'].values
ftprices[x]['dates'] = pd.to_datetime(tmp2.value_date.values)
data = []
xvals = dates
bar = go.Bar(
x=xvals,
y=dailypnl,
# text=['DD Pct: ' + str(round(x * 100, 2)) for x in dd_pct],
name='daily pnl'
)
# configuring plotly data.
gross_pnl = go.Scatter(
x=xvals,
y=cupnl_gross,
name='gross cu. pnl'
)
net_pnl = go.Scatter(
x=xvals,
y=cupnl_net,
name='net cu. pnl'
)
gamma_pnl = go.Scatter(
x=xvals,
y=gpnl,
name='cu. gamma pnl'
)
vega_pnl = go.Scatter(
x=xvals,
y=vpnl,
name='cu. vega pnl'
)
data = [bar, gross_pnl, net_pnl, gamma_pnl, vega_pnl]
# get the net greeks for each product
for x in ftprices:
trace = go.Scatter(
x=ftprices[x]['dates'],
y=ftprices[x]['values'],
name=x,
yaxis='y2'
)
data.append(trace)
# configuring plotly layout
layout = go.Layout(
title='Cu. Gross/Net/Gamma/Vega PnLs, and Future Price.',
yaxis=dict(
title='PnL Value'
),
yaxis2=dict(
title='Future Price',
titlefont=dict(
color='rgb(148, 103, 189)'
),
tickfont=dict(
color='rgb(148, 103, 189)'
),
overlaying='y',
side='right'
)
)
fig = go.Figure(data=data, layout=layout)
py.plot(fig)
if __name__ == "__main__":
for year in [2015]:
# initializing variables
start_date = str(year) + '-05-25'
end_date = str(year) + '-08-15'
pdt = ['W']
vid1 = pdt[0] + " " + 'U' + str(year % 2010) + '.' + 'U' + str(year % 2010)
vid2 = pdt[0] + " " + 'Z' + str(year % 2010) + '.' + 'Z' + str(year % 2010)
uid1 = "U" + str(year % 2010)
uid2 = "Z" + str(year % 2010)
#########################################
# creating datasets
vdf, pdf, edf = grab_data(pdt, start_date, end_date)
# create portfolio
u_option = create_vanilla_option(vdf, pdf, vid1, "call", True, strike='atm',
vol=0.31, greek='vega', greekval='100000')
z_option = create_vanilla_option(vdf, pdf, vid2, "call", False, strike='atm',
vol=0.26, greek='vega', greekval='100000')
# specify the hedging parameters
gen_hedges = OrderedDict({'delta': [['static', 0, 1]]})
# hedge_dict_1 = OrderedDict({'gamma': [['bound', (3800, 4200), 1, 'straddle',
# 'strike', 'atm', 'uid']]})
# hedge_dict_2 = OrderedDict({'gamma': [['bound', (2000, 2400), 1, 'straddle',
# 'strike', 'atm', 'uid']]})
pf = Portfolio(gen_hedges, name='test')
# pf1.add_security([u_option], 'OTC')
# pf2 = Portfolio(hedge_dict_2, name='test')
# pf2.add_security([z_option], 'OTC')
# pf = combine_portfolios([pf1, pf2], hedges=gen_hedges, refresh=True, name='full')
pf.add_security([z_option], 'OTC')
# zeroing deltas
dic = pf.get_net_greeks()
hedge_fts = []
for pdt in dic:
for mth in dic[pdt]:
# get the delta value.
delta = round(dic[pdt][mth][0])
shorted = True if delta > 0 else False
delta = abs(delta)
ft, ftprice = create_underlying(pdt, mth, pdf, shorted=shorted, lots=delta)
hedge_fts.append(ft)
if hedge_fts:
pf.add_security(hedge_fts, 'hedge')
print(pf)
# results = run_simulation(vdf, pdf, pf, flat_vols=True, plot_results=False)
results = run_simulation(vdf, pdf, pf, flat_vols=True, plot_results=False)
# plot_output(results[0])