forked from FoozhanA/ADRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation_runner.py
333 lines (287 loc) · 13.5 KB
/
simulation_runner.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import argparse
import numpy as np
import pandas as pd
import sys
import datetime as dt
from dateutil.parser import parse
from Kernel import Kernel
from util import util
from util.order import LimitOrder
from util.oracle.SparseMeanRevertingOracle import SparseMeanRevertingOracle
from agent.ExchangeAgent import ExchangeAgent
from agent.NoiseAgent import NoiseAgent
from agent.ValueAgent import ValueAgent
from agent.market_makers.AdaptiveMarketMakerAgent import AdaptiveMarketMakerAgent
from agent.examples.MomentumAgent import MomentumAgent
from agent.examples.ExampleExperimentalAgent import ExampleExperimentalAgentTemplate, ExampleExperimentalAgent
from model.LatencyModel import LatencyModel
########################################################################################################################
############################################### GENERAL CONFIG #########################################################
parser = argparse.ArgumentParser(description='Detailed options for RMSC03 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('--start-time',
default='09:30:00',
type=parse,
help='Starting time of simulation.'
)
parser.add_argument('--end-time',
default='10:30:00',
type=parse,
help='Ending time of simulation.'
)
parser.add_argument('-l',
'--log_dir',
default=None,
help='Log directory name (default: unix timestamp at program start)')
parser.add_argument('-s',
'--seed',
type=int,
default=None,
help='numpy.random.seed() for simulation')
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('--fund-vol',
type=float,
default=1e-8,
help='Volatility of fundamental time series.'
)
parser.add_argument('-e',
'--experimental-agent',
action='store_true',
help='Switch to allow presence of ExampleExperimentalAgent in market')
parser.add_argument('--ea-short-window',
type=pd.to_timedelta,
default='1s',
help='Length of short window for use in experimental agent mean-reversion strategy.'
)
parser.add_argument('--ea-long-window',
type=pd.to_timedelta,
default='30s',
help='Length of long window for use in experimental agent mean-reversion strategy.'
)
args, remaining_args = parser.parse_known_args()
# if args.config_help:
# parser.print_help()
# sys.exit()
log_dir = args.log_dir # Requested log directory.
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
exchange_log_orders = True
log_orders = None
book_freq = 0
simulation_start_time = dt.datetime.now()
print("Simulation Start Time: {}".format(simulation_start_time))
print("Configuration seed: {}\n".format(seed))
########################################################################################################################
############################################### AGENTS CONFIG ##########################################################
# Historical date to simulate.
historical_date = pd.to_datetime(args.historical_date)
mkt_open = historical_date + pd.to_timedelta(args.start_time.strftime('%H:%M:%S'))
mkt_close = historical_date + pd.to_timedelta(args.end_time.strftime('%H:%M:%S'))
agent_count, agents, agent_types = 0, [], []
# Hyperparameters
symbol = args.ticker
starting_cash = 10000000 # Cash in this simulator is always in CENTS.
r_bar = 1e5
sigma_n = r_bar / 10
kappa = 1.67e-15
lambda_a = 7e-11
# Oracle
symbols = {symbol: {'r_bar': r_bar,
'kappa': 1.67e-16,
'sigma_s': 0,
'fund_vol': args.fund_vol,
'megashock_lambda_a': 2.77778e-18,
'megashock_mean': 1e3,
'megashock_var': 5e4,
'random_state': np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64'))}}
oracle = SparseMeanRevertingOracle(mkt_open, mkt_close, symbols)
# 1) Exchange Agent
# How many orders in the past to store for transacted volume computation
# stream_history_length = int(pd.to_timedelta(args.mm_wake_up_freq).total_seconds() * 100)
stream_history_length = 25000
agents.extend([ExchangeAgent(id=0,
name="EXCHANGE_AGENT",
type="ExchangeAgent",
mkt_open=mkt_open,
mkt_close=mkt_close,
symbols=[symbol],
log_orders=exchange_log_orders,
pipeline_delay=0,
computation_delay=0,
stream_history=stream_history_length,
book_freq=book_freq,
wide_book=True,
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
noise_mkt_open = historical_date + pd.to_timedelta("09:00:00") # These times needed for distribution of arrival times
# of Noise Agents
noise_mkt_close = historical_date + pd.to_timedelta("16:00:00")
agents.extend([NoiseAgent(id=j,
name="NoiseAgent {}".format(j),
type="NoiseAgent",
symbol=symbol,
starting_cash=starting_cash,
wakeup_time=util.get_wake_time(noise_mkt_open, noise_mkt_close),
log_orders=log_orders,
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,
log_orders=log_orders,
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 Agents
"""
window_size == Spread of market maker (in ticks) around the mid price
pov == Percentage of transacted volume seen in previous `mm_wake_up_freq` that
the market maker places at each level
num_ticks == Number of levels to place orders in around the spread
wake_up_freq == How often the market maker wakes up
"""
# each elem of mm_params is tuple (window_size, pov, num_ticks, wake_up_freq, min_order_size)
mm_params = [('adaptive', 0.025, 10, '10S', 1),
('adaptive', 0.025, 10, '10S', 1)
]
num_mm_agents = len(mm_params)
mm_cancel_limit_delay = 50 # 50 nanoseconds
agents.extend([AdaptiveMarketMakerAgent(id=j,
name="ADAPTIVE_POV_MARKET_MAKER_AGENT_{}".format(j),
type='AdaptivePOVMarketMakerAgent',
symbol=symbol,
starting_cash=starting_cash,
pov=mm_params[idx][1],
min_order_size=mm_params[idx][4],
window_size=mm_params[idx][0],
num_ticks=mm_params[idx][2],
wake_up_freq=mm_params[idx][3],
cancel_limit_delay=mm_cancel_limit_delay,
skew_beta=0,
level_spacing=5,
spread_alpha=0.75,
backstop_quantity=50000,
log_orders=log_orders,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
for idx, j in enumerate(range(agent_count, agent_count + num_mm_agents))])
agent_count += num_mm_agents
agent_types.extend('POVMarketMakerAgent')
# 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,
wake_up_freq='20s',
log_orders=log_orders,
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) Experimental Agent
#### Example Experimental Agent parameters
if args.experimental_agent:
experimental_agent = ExampleExperimentalAgent(
id=agent_count,
name='EXAMPLE_EXPERIMENTAL_AGENT',
type='ExampleExperimentalAgent',
symbol=symbol,
starting_cash=starting_cash,
levels=5,
subscription_freq=1e9,
wake_freq='10s',
order_size=100,
short_window=args.ea_short_window,
long_window=args.ea_long_window,
log_orders=True,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64'))
)
else:
experimental_agent = ExampleExperimentalAgentTemplate(
id=agent_count,
name='EXAMPLE_EXPERIMENTAL_AGENT',
type='ExampleExperimentalAgent',
symbol=symbol,
starting_cash=starting_cash,
levels=5,
subscription_freq=1e9,
log_orders=True,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64'))
)
experimental_agents = [experimental_agent]
agents.extend(experimental_agents)
agent_types.extend("ExperimentalAgent")
agent_count += 1
########################################################################################################################
########################################### KERNEL AND OTHER CONFIG ####################################################
kernel = Kernel("RMSC03 Kernel", random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))
kernelStartTime = historical_date
kernelStopTime = mkt_close + pd.to_timedelta('00:01:00')
defaultComputationDelay = 50 # 50 nanoseconds
# LATENCY
latency_rstate = np.random.RandomState(seed=np.random.randint(low=0, high=2**32))
pairwise = (agent_count, agent_count)
# All agents sit on line from Seattle to NYC
nyc_to_seattle_meters = 3866660
pairwise_distances = util.generate_uniform_random_pairwise_dist_on_line(0.0, nyc_to_seattle_meters, agent_count,
random_state=latency_rstate)
pairwise_latencies = util.meters_to_light_ns(pairwise_distances)
model_args = {
'connected': True,
'min_latency': pairwise_latencies
}
latency_model = LatencyModel(latency_model='deterministic',
random_state=latency_rstate,
kwargs=model_args
)
# KERNEL
kernel.runner(agents=agents,
startTime=kernelStartTime,
stopTime=kernelStopTime,
agentLatencyModel=latency_model,
defaultComputationDelay=defaultComputationDelay,
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))