forked from quantopian/zipline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_clock.py
180 lines (144 loc) · 5.58 KB
/
test_clock.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
from datetime import time
from unittest import TestCase
import pandas as pd
from zipline.gens.sim_engine import (
MinuteSimulationClock,
SESSION_START,
BEFORE_TRADING_START_BAR,
BAR,
SESSION_END
)
from zipline.utils.calendars import get_calendar
from zipline.utils.calendars.trading_calendar import days_at_time
class TestClock(TestCase):
@classmethod
def setUpClass(cls):
cls.nyse_calendar = get_calendar("NYSE")
# july 15 is friday, so there are 3 sessions in this range (15, 18, 19)
cls.sessions = cls.nyse_calendar.sessions_in_range(
pd.Timestamp("2016-07-15"),
pd.Timestamp("2016-07-19")
)
trading_o_and_c = cls.nyse_calendar.schedule.ix[cls.sessions]
cls.opens = trading_o_and_c['market_open']
cls.closes = trading_o_and_c['market_close']
def test_bts_before_session(self):
clock = MinuteSimulationClock(
self.sessions,
self.opens,
self.closes,
days_at_time(self.sessions, time(6, 17), "US/Eastern"),
False
)
all_events = list(clock)
def _check_session_bts_first(session_label, events, bts_dt):
minutes = self.nyse_calendar.minutes_for_session(session_label)
self.assertEqual(393, len(events))
self.assertEqual(events[0], (session_label, SESSION_START))
self.assertEqual(events[1], (bts_dt, BEFORE_TRADING_START_BAR))
for i in range(2, 392):
self.assertEqual(events[i], (minutes[i - 2], BAR))
self.assertEqual(events[392], (minutes[-1], SESSION_END))
_check_session_bts_first(
self.sessions[0],
all_events[0:393],
pd.Timestamp("2016-07-15 6:17", tz='US/Eastern')
)
_check_session_bts_first(
self.sessions[1],
all_events[393:786],
pd.Timestamp("2016-07-18 6:17", tz='US/Eastern')
)
_check_session_bts_first(
self.sessions[2],
all_events[786:],
pd.Timestamp("2016-07-19 6:17", tz='US/Eastern')
)
def test_bts_during_session(self):
self.verify_bts_during_session(
time(11, 45), [
pd.Timestamp("2016-07-15 11:45", tz='US/Eastern'),
pd.Timestamp("2016-07-18 11:45", tz='US/Eastern'),
pd.Timestamp("2016-07-19 11:45", tz='US/Eastern')
],
135
)
def test_bts_on_first_minute(self):
self.verify_bts_during_session(
time(9, 30), [
pd.Timestamp("2016-07-15 9:30", tz='US/Eastern'),
pd.Timestamp("2016-07-18 9:30", tz='US/Eastern'),
pd.Timestamp("2016-07-19 9:30", tz='US/Eastern')
],
1
)
def test_bts_on_last_minute(self):
self.verify_bts_during_session(
time(16, 00), [
pd.Timestamp("2016-07-15 16:00", tz='US/Eastern'),
pd.Timestamp("2016-07-18 16:00", tz='US/Eastern'),
pd.Timestamp("2016-07-19 16:00", tz='US/Eastern')
],
390
)
def verify_bts_during_session(self, bts_time, bts_session_times, bts_idx):
def _check_session_bts_during(session_label, events, bts_dt):
minutes = self.nyse_calendar.minutes_for_session(session_label)
self.assertEqual(393, len(events))
self.assertEqual(events[0], (session_label, SESSION_START))
for i in range(1, bts_idx):
self.assertEqual(events[i], (minutes[i - 1], BAR))
self.assertEqual(
events[bts_idx],
(bts_dt, BEFORE_TRADING_START_BAR)
)
for i in range(bts_idx + 1, 391):
self.assertEqual(events[i], (minutes[i - 2], BAR))
self.assertEqual(events[392], (minutes[-1], SESSION_END))
clock = MinuteSimulationClock(
self.sessions,
self.opens,
self.closes,
days_at_time(self.sessions, bts_time, "US/Eastern"),
False
)
all_events = list(clock)
_check_session_bts_during(
self.sessions[0],
all_events[0:393],
bts_session_times[0]
)
_check_session_bts_during(
self.sessions[1],
all_events[393:786],
bts_session_times[1]
)
_check_session_bts_during(
self.sessions[2],
all_events[786:],
bts_session_times[2]
)
def test_bts_after_session(self):
clock = MinuteSimulationClock(
self.sessions,
self.opens,
self.closes,
days_at_time(self.sessions, time(19, 5), "US/Eastern"),
False
)
all_events = list(clock)
# since 19:05 Eastern is after the NYSE is closed, we don't emit
# BEFORE_TRADING_START. therefore, each day has SESSION_START,
# 390 BARs, and then SESSION_END
def _check_session_bts_after(session_label, events):
minutes = self.nyse_calendar.minutes_for_session(session_label)
self.assertEqual(392, len(events))
self.assertEqual(events[0], (session_label, SESSION_START))
for i in range(1, 391):
self.assertEqual(events[i], (minutes[i - 1], BAR))
self.assertEqual(events[-1], (minutes[389], SESSION_END))
for i in range(0, 2):
_check_session_bts_after(
self.sessions[i],
all_events[(i * 392): ((i + 1) * 392)]
)