forked from abides-sim/abides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marketreplay.py
145 lines (123 loc) · 5.94 KB
/
marketreplay.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
import argparse
import numpy as np
import pandas as pd
import sys
import datetime as dt
from Kernel import Kernel
from util import util
from util.order import LimitOrder
from agent.ExchangeAgent import ExchangeAgent
from agent.examples.MarketReplayAgent import MarketReplayAgent
########################################################################################################################
############################################### 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='Name of the stock/symbol')
parser.add_argument('-d',
'--date',
required=True,
help='Historical date')
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')
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
simulation_start_time = dt.datetime.now()
print("Simulation Start Time: {}".format(simulation_start_time))
print("Configuration seed: {}".format(seed))
print("Log Directory: {}".format(log_dir))
########################################################################################################################
############################################### AGENTS CONFIG ##########################################################
# Historical date to simulate.
historical_date = args.date
historical_date_pd = pd.to_datetime(historical_date)
symbol = args.ticker
print("Symbol: {}".format(symbol))
print("Date: {}\n".format(historical_date))
agent_count, agents, agent_types = 0, [], []
# 1) Exchange Agent
mkt_open = historical_date_pd + pd.to_timedelta('09:00:00')
mkt_close = historical_date_pd + pd.to_timedelta('16:00:00')
print("Market Open : {}".format(mkt_open))
print("Market Close: {}".format(mkt_close))
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=10,
book_freq='all',
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))])
agent_types.extend("ExchangeAgent")
agent_count += 1
# 2) Market Replay Agent
file_name = f'DOW30/{symbol}/{symbol}.{historical_date}'
orders_file_path = f'/efs/data/{file_name}'
agents.extend([MarketReplayAgent(id=1,
name="MARKET_REPLAY_AGENT",
type='MarketReplayAgent',
symbol=symbol,
log_orders=False,
date=historical_date_pd,
start_time=mkt_open,
end_time=mkt_close,
orders_file_path=orders_file_path,
processed_orders_folder_path='/efs/data/marketreplay/',
starting_cash=0,
random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32,
dtype='uint64')))])
agent_types.extend("MarketReplayAgent")
agent_count += 1
########################################################################################################################
########################################### 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
kernelStopTime = historical_date_pd + pd.to_timedelta('17:00: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=None,
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))