forked from abides-sim/abides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution_iabs_plots.py
294 lines (258 loc) · 12.3 KB
/
execution_iabs_plots.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import argparse
import numpy as np
import pandas as pd
import sys
import datetime as dt
from dateutil.parser import parse
from agent.ExchangeAgent import ExchangeAgent
from agent.NoiseAgent import NoiseAgent
from agent.ValueAgent import ValueAgent
from agent.market_makers.MarketMakerAgent import MarketMakerAgent
from agent.examples.MomentumAgent import MomentumAgent
from agent.execution.TWAPExecutionAgent import TWAPExecutionAgent
from agent.execution.VWAPExecutionAgent import VWAPExecutionAgent
from agent.execution.POVExecutionAgent import POVExecutionAgent
from Kernel import Kernel
from util import util
from util.order import LimitOrder
from util.oracle.ExternalFileOracle import ExternalFileOracle
########################################################################################################################
############################################### GENERAL CONFIG #########################################################
parser = argparse.ArgumentParser(description='Detailed options for market replay config.')
parser.add_argument('-c',
'--config',
required=True,
help='Name of config file to execute')
parser.add_argument('-t',
'--ticker',
required=True,
help='Ticker (symbol) to use for simulation')
parser.add_argument('-d', '--historical-date',
required=True,
type=parse,
help='historical date being simulated in format YYYYMMDD.')
parser.add_argument('-f',
'--fundamental-file-path',
required=True,
help="Path to external fundamental file.")
parser.add_argument('-e',
'--execution_agents',
action='store_true',
help='Flag to add the execution agents')
parser.add_argument('-p',
'--pov',
type=float,
default=0.1,
help='Participation of Volume level')
parser.add_argument('-s',
'--seed',
type=int,
default=None,
help='numpy.random.seed() for simulation')
parser.add_argument('-l',
'--log_dir',
default=None,
help='Log directory name (default: unix timestamp at program start)')
parser.add_argument('-v',
'--verbose',
action='store_true',
help='Maximum verbosity!')
parser.add_argument('--config_help',
action='store_true',
help='Print argument options for this config file')
parser.add_argument('--wide-book', action='store_true',
help='Store orderbook in `wide` format')
args, remaining_args = parser.parse_known_args()
if args.config_help:
parser.print_help()
sys.exit()
seed = args.seed # Random seed specification on the command line.
if not seed: seed = int(pd.Timestamp.now().timestamp() * 1000000) % (2 ** 32 - 1)
np.random.seed(seed)
util.silent_mode = not args.verbose
LimitOrder.silent_mode = not args.verbose
simulation_start_time = dt.datetime.now()
print("Simulation Start Time: {}".format(simulation_start_time))
print("Configuration seed: {}".format(seed))
######################## Agents Config #########################################################################
# Historical date to simulate.
historical_date_pd = pd.to_datetime(args.historical_date)
mkt_open = historical_date_pd + pd.to_timedelta('10:45:00')
mkt_close = historical_date_pd + pd.to_timedelta('11:45:00')
agent_count, agents, agent_types = 0, [], []
# Hyperparameters
symbol = args.ticker
starting_cash = 10000000 # Cash in this simulator is always in CENTS.
# Oracle
symbols = {
symbol : {
'fundamental_file_path': args.fundamental_file_path,
'random_state': np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64'))
}
}
oracle = ExternalFileOracle(symbols)
r_bar = util.get_value_from_timestamp(oracle.fundamentals[symbol], mkt_open)
sigma_n = r_bar / 10
kappa = 1.67e-15
lambda_a = 7e-13
# 1) Exchange Agent
agents.extend([ExchangeAgent(id=0,
name="EXCHANGE_AGENT",
type="ExchangeAgent",
mkt_open=mkt_open,
mkt_close=mkt_close,
symbols=[symbol],
log_orders=True,
pipeline_delay=0,
computation_delay=0,
stream_history=sys.maxsize,
book_freq=0,
wide_book=args.wide_book,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))])
agent_types.extend("ExchangeAgent")
agent_count += 1
# 2) Noise Agents
num_noise = 5000
agents.extend([NoiseAgent(id=j,
name="NOISE_AGENT_{}".format(j),
type="NoiseAgent",
symbol=symbol,
starting_cash=starting_cash,
wakeup_time=util.get_wake_time(mkt_open, mkt_close),
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64')))
for j in range(agent_count, agent_count + num_noise)])
agent_count += num_noise
agent_types.extend(['NoiseAgent'])
# 3) Value Agents
num_value = 100
agents.extend([ValueAgent(id=j,
name="VALUE_AGENT_{}".format(j),
type="ValueAgent",
symbol=symbol,
starting_cash=starting_cash,
sigma_n=sigma_n,
r_bar=r_bar,
kappa=kappa,
lambda_a=lambda_a,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64')))
for j in range(agent_count, agent_count + num_value)])
agent_count += num_value
agent_types.extend(['ValueAgent'])
# 4) Market Maker Agent
num_mm_agents = 1
agents.extend([MarketMakerAgent(id=j,
name="MARKET_MAKER_AGENT_{}".format(j),
type='MarketMakerAgent',
symbol=symbol,
starting_cash=starting_cash,
min_size=200,
max_size=201,
wake_up_freq="1S",
log_orders=False,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
for j in range(agent_count, agent_count + num_mm_agents)])
agent_count += num_mm_agents
agent_types.extend('MarketMakerAgent')
# 5) Momentum Agents
num_momentum_agents = 25
agents.extend([MomentumAgent(id=j,
name="MOMENTUM_AGENT_{}".format(j),
type="MomentumAgent",
symbol=symbol,
starting_cash=starting_cash,
min_size=1,
max_size=10,
log_orders=False,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
for j in range(agent_count, agent_count + num_momentum_agents)])
agent_count += num_momentum_agents
agent_types.extend("MomentumAgent")
# 6) Execution Agent Config
trade = True if args.execution_agents else False
#### Participation of Volume Agent parameters
pov_agent_start_time = '11:00:00'
pov_agent_end_time = '11:30:00'
pov_proportion_of_volume = args.pov
pov_quantity = 12e5
pov_frequency = '1min'
pov_direction = "BUY"
pov_agent = POVExecutionAgent(id=agent_count,
name='POV_EXECUTION_AGENT',
type='ExecutionAgent',
symbol=symbol,
starting_cash=starting_cash,
start_time=historical_date_pd+pd.to_timedelta(pov_agent_start_time),
end_time=historical_date_pd+pd.to_timedelta(pov_agent_end_time),
freq=pov_frequency,
lookback_period=pov_frequency,
pov=pov_proportion_of_volume,
direction=pov_direction,
quantity=pov_quantity,
trade=trade,
log_orders=True,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
execution_agents = [pov_agent]
"""
twap_agent = TWAPExecutionAgent(id=agent_count,
name='TWAP_EXECUTION_AGENT',
type='ExecutionAgent',
symbol=symbol,
starting_cash=0,
start_time=historical_date_pd + pd.to_timedelta('11:00:00'),
end_time=historical_date_pd + pd.to_timedelta('13:00:00'),
freq=60,
direction='BUY',
quantity=12e3,
trade=trade,
log_orders=True,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
execution_agents = [twap_agent]
"""
"""
vwap_agent = VWAPExecutionAgent(id=agent_count,
name='VWAP_EXECUTION_AGENT',
type='ExecutionAgent',
symbol=symbol,
starting_cash=0,
start_time=historical_date_pd + pd.to_timedelta('10:00:00'),
end_time=historical_date_pd + pd.to_timedelta('12:00:00'),
freq=60,
direction='BUY',
quantity=12e3,
volume_profile_path=None,
trade=trade,
log_orders=True,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
execution_agents = [vwap_agent]
"""
agents.extend(execution_agents)
agent_types.extend("ExecutionAgent")
agent_count += 1
print("Number of Agents: {}".format(agent_count))
########################################################################################################################
########################################### KERNEL AND OTHER CONFIG ####################################################
kernel = Kernel("Market Replay Kernel",
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64')))
kernelStartTime = historical_date_pd + pd.to_timedelta('10:44:00')
kernelStopTime = historical_date_pd + pd.to_timedelta('11:46:00')
defaultComputationDelay = 0
latency = np.zeros((agent_count, agent_count))
noise = [0.0]
kernel.runner(agents=agents,
startTime=kernelStartTime,
stopTime=kernelStopTime,
agentLatency=latency,
latencyNoise=noise,
defaultComputationDelay=defaultComputationDelay,
defaultLatency=0,
oracle=oracle,
log_dir=args.log_dir)
simulation_end_time = dt.datetime.now()
print("Simulation End Time: {}".format(simulation_end_time))
print("Time taken to run simulation: {}".format(simulation_end_time - simulation_start_time))