-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.py
187 lines (141 loc) · 5.22 KB
/
simulator.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
from datetime import *
from datafeed import DataFeed
from broker import Broker
from trader import Trader
from strategy import Strategy
from msgq import MsgQ
# ------------------
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# ------------------
import logging
class Simulator(object):
'''
simulation manager
'''
def __init__(self, instrument, strategies, start_date, end_date, opening_bal,time_stamp=None):
'''
constructs message queues
initialises brokers and traders
'''
self.instrument = instrument
self.start_date = start_date
self.end_date = end_date
self.opening_bal = opening_bal
self.datafeed = DataFeed(instrument)
self.orderQ = MsgQ()
self.receiptQ = MsgQ()
self.term_req_Q = MsgQ()
self.term_notice_Q = MsgQ()
self.broker = Broker(self.datafeed, self.orderQ, self.receiptQ, self.term_req_Q, self.term_notice_Q)
self.traders = []
for strategy in strategies:
trader = Trader(self.datafeed, self.broker, self.opening_bal, self.instrument, strategy, self.start_date, self.end_date)
self.traders.append(trader)
self.time_stamp = time_stamp
def run(self):
'''
simulate event series
'''
current_date = date(self.start_date.year, self.start_date.month, self.start_date.day)
length = self.end_date - self.start_date
d_total = length.days
display_int = d_total / 10
while (current_date <= self.end_date):
# PROCESS TRADING DAYS
if (self.datafeed.date_is_trading_day(current_date) == True):
self.broker.open_manage_and_close(current_date)
# book keeping
for trader in self.traders:
trader.ac.tally_individual_open_positions(current_date)
trader.ac.record_net_end_of_day_pos(current_date)
trader.ac.record_end_of_day_balances(current_date)
for trader in self.traders:
trader.execute_strategy(current_date)
#self.broker.log_closed_positions()
self.broker.log_all_positions(current_date)
# IGNORE NON-TRADING DAYS
else:
pass
current_date = current_date + timedelta(days=1)
elapsed = (self.end_date - current_date)
d_elapsed = elapsed.days
progress = (float(d_total) - float(d_elapsed)) / float(d_total) * 100.0
if (d_elapsed % display_int == 0):
print('%i/100' % int(progress))
self.traders[0].strategy.log_self()
def plot(self):
'''
analyse & report on simulation path and outcome
'''
d = date(self.start_date.year, self.start_date.month, self.start_date.day)
dates = []
prices = []
cash_bal = []
margin_bal = []
net_booked_position = []
net_open_position = []
daily_high = []
daily_low = []
mavg_band_ceiling = []
mavg_band_floor = []
trader = self.broker.traders[0]
ac = trader.ac
df = self.datafeed
pMin = None
pMax = None
while (d <= self.end_date):
# TRADING DAYS
if (self.datafeed.date_is_trading_day(d) == True):
dates.append(d)
mavg_top = df.n_day_moving_avg(None, d, 'high', Strategy.n)
mavg_bottom = df.n_day_moving_avg(None, d, 'low', Strategy.n)
mavg_band_ceiling.append(mavg_top)
mavg_band_floor.append(mavg_bottom)
pinfo = df.get_price_info(None, d)
prices.append(pinfo['close'])
daily_high.append(pinfo['high'])
daily_low.append(pinfo['low'])
s = str(d) + ',' + str(mavg_band_ceiling[len(mavg_band_ceiling) - 1]) + ',' + str(mavg_band_floor[len(mavg_band_floor) - 1]) + ',' + str(pinfo['close'])
logging.info(s)
cash_bal.append(ac.d_cash_bal[d])
margin_bal.append(ac.d_margin_bal[d])
net_booked_position.append(ac.d_net_booked_position[d])
net_open_position.append(ac.net_open_position[d])
if (pMin == None):
pMin = pinfo['low']
pMax = pinfo['high']
else:
if pinfo['low'] < pMin:
pMin = pinfo['low']
if pinfo['high'] > pMax:
pMax = pinfo['high']
# NON-TRADING DAYS
else:
pass
d = d + timedelta(days=1)
aDate = np.array(dates)
aPrice = np.array(prices)
fig = plt.figure(figsize=(20, 20))
ax = fig.add_subplot(111)
#ax.plot(aDate, aPrice, color='blue')
for series in [mavg_band_ceiling, mavg_band_floor]:
y = np.array(series)
t = np.array(dates)
ax.plot(t, y, color='red')
for series in [daily_high, daily_low]:
y = np.array(series)
t = np.array(dates)
ax.plot(t, y, color='blue')
plt.ylim([float(pMin), float(pMax)])
for series in [net_booked_position]:
y = np.array(series)
t = np.array(dates)
ax2 = ax.twinx()
ax2.plot(t, y, color='green')
ax.grid(False)
fig.autofmt_xdate(rotation=90)
fname = 'plot/plot_' + self.time_stamp
fig.savefig(fname)