mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-16 11:18:05 +08:00
Re-implemented the Calendar API.
Instead of having separate ExchangeCalendar and TradingSchedule objects, we now just have TradingCalendar. The TradingCalendar keeps track of each session (defined as a contiguous set of minutes between an open and a close). It's also responsible for handling the grouping logic of any given minute to its containing session, or the next/previous session if it's not a market minute for the given calendar.
This commit is contained in:
@@ -111,9 +111,9 @@ class BundleCoreTestCase(WithInstanceTmpDir, ZiplineTestCase):
|
||||
def test_ingest(self):
|
||||
start = pd.Timestamp('2014-01-06', tz='utc')
|
||||
end = pd.Timestamp('2014-01-10', tz='utc')
|
||||
trading_days = get_calendar('NYSE').all_trading_days
|
||||
trading_days = get_calendar('NYSE').all_sessions
|
||||
calendar = trading_days[trading_days.slice_indexer(start, end)]
|
||||
minutes = get_calendar('NYSE').trading_minutes_for_days_in_range(
|
||||
minutes = get_calendar('NYSE').minutes_for_sessions_in_range(
|
||||
calendar[0], calendar[-1]
|
||||
)
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ class YahooBundleTestCase(WithResponses, ZiplineTestCase):
|
||||
columns = 'open', 'high', 'low', 'close', 'volume'
|
||||
asset_start = pd.Timestamp('2014-01-02', tz='utc')
|
||||
asset_end = pd.Timestamp('2014-12-31', tz='utc')
|
||||
trading_days = get_calendar('NYSE').all_trading_days
|
||||
trading_days = get_calendar('NYSE').all_sessions
|
||||
calendar = trading_days[
|
||||
(trading_days >= asset_start) &
|
||||
(trading_days <= asset_end)
|
||||
|
||||
@@ -45,7 +45,7 @@ from zipline.data.minute_bars import (
|
||||
|
||||
from zipline.testing.fixtures import (
|
||||
WithInstanceTmpDir,
|
||||
WithTradingSchedule,
|
||||
WithTradingCalendar,
|
||||
ZiplineTestCase,
|
||||
)
|
||||
|
||||
@@ -56,17 +56,20 @@ TEST_CALENDAR_START = Timestamp('2014-06-02', tz='UTC')
|
||||
TEST_CALENDAR_STOP = Timestamp('2015-12-31', tz='UTC')
|
||||
|
||||
|
||||
class BcolzMinuteBarTestCase(WithTradingSchedule, WithInstanceTmpDir,
|
||||
class BcolzMinuteBarTestCase(WithTradingCalendar, WithInstanceTmpDir,
|
||||
ZiplineTestCase):
|
||||
|
||||
@classmethod
|
||||
def init_class_fixtures(cls):
|
||||
super(BcolzMinuteBarTestCase, cls).init_class_fixtures()
|
||||
trading_days = cls.trading_schedule.trading_sessions(
|
||||
TEST_CALENDAR_START, TEST_CALENDAR_STOP
|
||||
)
|
||||
cls.market_opens = trading_days.market_open
|
||||
cls.market_closes = trading_days.market_close
|
||||
|
||||
cal = cls.trading_calendar.schedule.loc[
|
||||
TEST_CALENDAR_START:TEST_CALENDAR_STOP
|
||||
]
|
||||
|
||||
cls.market_opens = cal.market_open
|
||||
cls.market_closes = cal.market_close
|
||||
|
||||
cls.test_calendar_start = cls.market_opens.index[0]
|
||||
cls.test_calendar_stop = cls.market_opens.index[-1]
|
||||
|
||||
@@ -798,9 +801,9 @@ class BcolzMinuteBarTestCase(WithTradingSchedule, WithInstanceTmpDir,
|
||||
data = {sids[0]: data_1, sids[1]: data_2}
|
||||
|
||||
start_minute_loc = \
|
||||
self.trading_schedule.all_execution_minutes.get_loc(minutes[0])
|
||||
self.trading_calendar.all_minutes.get_loc(minutes[0])
|
||||
minute_locs = [
|
||||
self.trading_schedule.all_execution_minutes.get_loc(minute)
|
||||
self.trading_calendar.all_minutes.get_loc(minute)
|
||||
- start_minute_loc
|
||||
for minute in minutes
|
||||
]
|
||||
@@ -822,9 +825,11 @@ class BcolzMinuteBarTestCase(WithTradingSchedule, WithInstanceTmpDir,
|
||||
'close': arange(1, 781),
|
||||
'volume': arange(1, 781)
|
||||
}
|
||||
dts = array(self.trading_schedule.execution_minutes_for_days_in_range(
|
||||
start_day, end_day
|
||||
dts = array(self.trading_calendar.minutes_for_sessions_in_range(
|
||||
self.trading_calendar.minute_to_session_label(start_day),
|
||||
self.trading_calendar.minute_to_session_label(end_day)
|
||||
))
|
||||
|
||||
self.writer.write_cols(sid, dts, cols)
|
||||
|
||||
self.assertEqual(
|
||||
@@ -866,9 +871,13 @@ class BcolzMinuteBarTestCase(WithTradingSchedule, WithInstanceTmpDir,
|
||||
'close': arange(1, 601),
|
||||
'volume': arange(1, 601)
|
||||
}
|
||||
dts = array(self.trading_schedule.execution_minutes_for_days_in_range(
|
||||
start_day, end_day
|
||||
))
|
||||
dts = array(
|
||||
self.trading_calendar.minutes_for_sessions_in_range(
|
||||
self.trading_calendar.minute_to_session_label(start_day),
|
||||
self.trading_calendar.minute_to_session_label(end_day)
|
||||
)
|
||||
)
|
||||
|
||||
self.writer.write_cols(sid, dts, cols)
|
||||
|
||||
self.assertEqual(
|
||||
|
||||
@@ -46,7 +46,6 @@ from zipline.testing.fixtures import (
|
||||
WithBcolzEquityDailyBarReader,
|
||||
ZiplineTestCase,
|
||||
)
|
||||
from zipline.utils.calendars import get_calendar
|
||||
|
||||
TEST_CALENDAR_START = Timestamp('2015-06-01', tz='UTC')
|
||||
TEST_CALENDAR_STOP = Timestamp('2015-06-30', tz='UTC')
|
||||
@@ -97,16 +96,17 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
@classmethod
|
||||
def init_class_fixtures(cls):
|
||||
super(BcolzDailyBarTestCase, cls).init_class_fixtures()
|
||||
cls.trading_days = get_calendar('NYSE').trading_days(
|
||||
TEST_CALENDAR_START, TEST_CALENDAR_STOP
|
||||
).index
|
||||
cls.sessions = cls.trading_calendar.sessions_in_range(
|
||||
cls.trading_calendar.minute_to_session_label(TEST_CALENDAR_START),
|
||||
cls.trading_calendar.minute_to_session_label(TEST_CALENDAR_STOP)
|
||||
)
|
||||
|
||||
@property
|
||||
def assets(self):
|
||||
return EQUITY_INFO.index
|
||||
|
||||
def trading_days_between(self, start, end):
|
||||
return self.trading_days[self.trading_days.slice_indexer(start, end)]
|
||||
return self.sessions[self.sessions.slice_indexer(start, end)]
|
||||
|
||||
def asset_start(self, asset_id):
|
||||
return asset_start(EQUITY_INFO, asset_id)
|
||||
@@ -181,14 +181,14 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
expected_calendar_offset,
|
||||
)
|
||||
assert_index_equal(
|
||||
self.trading_days,
|
||||
self.sessions,
|
||||
DatetimeIndex(result.attrs['calendar'], tz='UTC'),
|
||||
)
|
||||
|
||||
def test_read_first_trading_day(self):
|
||||
self.assertEqual(
|
||||
self.bcolz_equity_daily_bar_reader.first_trading_day,
|
||||
self.trading_days[0],
|
||||
self.sessions[0],
|
||||
)
|
||||
|
||||
def _check_read_results(self, columns, assets, start_date, end_date):
|
||||
@@ -234,7 +234,7 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
columns,
|
||||
self.assets,
|
||||
start_date=self.asset_start(asset),
|
||||
end_date=self.trading_days[-1],
|
||||
end_date=self.sessions[-1],
|
||||
)
|
||||
|
||||
def test_start_on_asset_end(self):
|
||||
@@ -248,7 +248,7 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
columns,
|
||||
self.assets,
|
||||
start_date=self.asset_end(asset),
|
||||
end_date=self.trading_days[-1],
|
||||
end_date=self.sessions[-1],
|
||||
)
|
||||
|
||||
def test_end_on_asset_start(self):
|
||||
@@ -261,7 +261,7 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
self._check_read_results(
|
||||
columns,
|
||||
self.assets,
|
||||
start_date=self.trading_days[0],
|
||||
start_date=self.sessions[0],
|
||||
end_date=self.asset_start(asset),
|
||||
)
|
||||
|
||||
@@ -275,7 +275,7 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
self._check_read_results(
|
||||
columns,
|
||||
self.assets,
|
||||
start_date=self.trading_days[0],
|
||||
start_date=self.sessions[0],
|
||||
end_date=self.asset_end(asset),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user