mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-14 12:10:24 +08:00
TST: Adds TradingSchedule test fixture
This commit is contained in:
@@ -38,7 +38,6 @@ from zipline.testing.fixtures import (
|
||||
WithSimParams,
|
||||
ZiplineTestCase,
|
||||
)
|
||||
from zipline.utils.calendars import default_nyse_schedule
|
||||
|
||||
|
||||
class SlippageTestCase(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
@@ -92,9 +91,10 @@ class SlippageTestCase(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
start=normalize_date(self.minutes[0]),
|
||||
end=normalize_date(self.minutes[-1])
|
||||
)
|
||||
with tmp_bcolz_minute_bar_reader(self.env, days, assets) as reader:
|
||||
with tmp_bcolz_minute_bar_reader(self.trading_schedule, days, assets) \
|
||||
as reader:
|
||||
data_portal = DataPortal(
|
||||
self.env, default_nyse_schedule,
|
||||
self.env, self.trading_schedule,
|
||||
first_trading_day=reader.first_trading_day,
|
||||
equity_minute_reader=reader,
|
||||
)
|
||||
@@ -481,14 +481,11 @@ class SlippageTestCase(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
start=normalize_date(self.minutes[0]),
|
||||
end=normalize_date(self.minutes[-1])
|
||||
)
|
||||
with tmp_bcolz_minute_bar_reader(self.env, days, assets) as reader:
|
||||
with tmp_bcolz_minute_bar_reader(self.trading_schedule, days, assets) \
|
||||
as reader:
|
||||
data_portal = DataPortal(
|
||||
<<<<<<< HEAD
|
||||
self.env,
|
||||
self.env, self.trading_schedule,
|
||||
first_trading_day=reader.first_trading_day,
|
||||
=======
|
||||
self.env, default_nyse_schedule,
|
||||
>>>>>>> ENH: Adds ExchangeCalendar, TradingSchedule, and implementations
|
||||
equity_minute_reader=reader,
|
||||
)
|
||||
|
||||
|
||||
@@ -931,7 +931,7 @@ class SyntheticBcolzTestCase(WithAdjustmentReader,
|
||||
cls.equity_info = ret = make_rotating_equity_info(
|
||||
num_assets=6,
|
||||
first_start=cls.first_asset_start,
|
||||
frequency=cls.TRADING_ENV_TRADING_CALENDAR.trading_day,
|
||||
frequency=cls.trading_schedule.day,
|
||||
periods_between_starts=4,
|
||||
asset_lifetime=8,
|
||||
)
|
||||
@@ -992,9 +992,9 @@ class SyntheticBcolzTestCase(WithAdjustmentReader,
|
||||
window_length = 5
|
||||
asset_ids = self.all_asset_ids
|
||||
dates = date_range(
|
||||
self.first_asset_start + self.env.trading_day,
|
||||
self.first_asset_start + self.trading_schedule.day,
|
||||
self.last_asset_end,
|
||||
freq=self.env.trading_day,
|
||||
freq=self.trading_schedule.day,
|
||||
)
|
||||
dates_to_test = dates[window_length:]
|
||||
|
||||
@@ -1014,7 +1014,7 @@ class SyntheticBcolzTestCase(WithAdjustmentReader,
|
||||
# **previous** day's data.
|
||||
expected_raw = rolling_mean(
|
||||
expected_bar_values_2d(
|
||||
dates - self.env.trading_day,
|
||||
dates - self.trading_schedule.day,
|
||||
self.equity_info,
|
||||
'close',
|
||||
),
|
||||
@@ -1046,9 +1046,9 @@ class SyntheticBcolzTestCase(WithAdjustmentReader,
|
||||
window_length = 5
|
||||
asset_ids = self.all_asset_ids
|
||||
dates = date_range(
|
||||
self.first_asset_start + self.env.trading_day,
|
||||
self.first_asset_start + self.trading_schedule.day,
|
||||
self.last_asset_end,
|
||||
freq=self.env.trading_day,
|
||||
freq=self.trading_schedule.day,
|
||||
)
|
||||
dates_to_test = dates[window_length:]
|
||||
|
||||
|
||||
+18
-15
@@ -751,7 +751,7 @@ class TestTransformAlgorithm(WithLogger,
|
||||
[100, 100, 100, 300],
|
||||
timedelta(days=1),
|
||||
cls.sim_params,
|
||||
cls.env
|
||||
cls.trading_schedule,
|
||||
) for sid in cls.sids
|
||||
},
|
||||
index=cls.sim_params.trading_days,
|
||||
@@ -1023,10 +1023,11 @@ class TestBeforeTradingStart(WithDataPortal,
|
||||
|
||||
@classmethod
|
||||
def make_minute_bar_data(cls):
|
||||
asset_minutes = cls.env.minutes_for_days_in_range(
|
||||
cls.data_start,
|
||||
cls.END_DATE,
|
||||
)
|
||||
asset_minutes = \
|
||||
cls.trading_schedule.execution_minutes_for_days_in_range(
|
||||
cls.data_start,
|
||||
cls.END_DATE,
|
||||
)
|
||||
minutes_count = len(asset_minutes)
|
||||
minutes_arr = np.arange(minutes_count) + 1
|
||||
split_data = pd.DataFrame(
|
||||
@@ -1042,13 +1043,13 @@ class TestBeforeTradingStart(WithDataPortal,
|
||||
split_data.iloc[780:] = split_data.iloc[780:] / 2.0
|
||||
for sid in (1, 8554):
|
||||
yield sid, create_minute_df_for_asset(
|
||||
cls.env,
|
||||
cls.trading_schedule,
|
||||
cls.data_start,
|
||||
cls.sim_params.period_end,
|
||||
)
|
||||
|
||||
yield 2, create_minute_df_for_asset(
|
||||
cls.env,
|
||||
cls.trading_schedule,
|
||||
cls.data_start,
|
||||
cls.sim_params.period_end,
|
||||
50,
|
||||
@@ -1069,7 +1070,7 @@ class TestBeforeTradingStart(WithDataPortal,
|
||||
def make_daily_bar_data(cls):
|
||||
for sid in cls.ASSET_FINDER_EQUITY_SIDS:
|
||||
yield sid, create_daily_df_for_asset(
|
||||
cls.env,
|
||||
cls.trading_schedule,
|
||||
cls.data_start,
|
||||
cls.sim_params.period_end,
|
||||
)
|
||||
@@ -1397,7 +1398,8 @@ class TestAlgoScript(WithLogger,
|
||||
|
||||
@classmethod
|
||||
def make_daily_bar_data(cls):
|
||||
days = len(cls.env.days_in_range(cls.START_DATE, cls.END_DATE))
|
||||
days = len(cls.trading_schedule.execution_days_in_range(cls.START_DATE,
|
||||
cls.END_DATE))
|
||||
return trades_by_sid_to_dfs(
|
||||
{
|
||||
0: factory.create_trade_history(
|
||||
@@ -1406,14 +1408,14 @@ class TestAlgoScript(WithLogger,
|
||||
[100] * days,
|
||||
timedelta(days=1),
|
||||
cls.sim_params,
|
||||
cls.env),
|
||||
cls.trading_schedule),
|
||||
3: factory.create_trade_history(
|
||||
3,
|
||||
[10.0] * days,
|
||||
[100] * days,
|
||||
timedelta(days=1),
|
||||
cls.sim_params,
|
||||
cls.env)
|
||||
cls.trading_schedule)
|
||||
},
|
||||
index=cls.sim_params.trading_days,
|
||||
)
|
||||
@@ -3169,10 +3171,11 @@ class TestOrderCancelation(WithDataPortal,
|
||||
|
||||
@classmethod
|
||||
def make_minute_bar_data(cls):
|
||||
asset_minutes = cls.env.minutes_for_days_in_range(
|
||||
cls.sim_params.period_start,
|
||||
cls.sim_params.period_end,
|
||||
)
|
||||
asset_minutes = \
|
||||
cls.trading_schedule.execution_minutes_for_days_in_range(
|
||||
cls.sim_params.period_start,
|
||||
cls.sim_params.period_end,
|
||||
)
|
||||
|
||||
minutes_count = len(asset_minutes)
|
||||
minutes_arr = np.arange(1, 1 + minutes_count)
|
||||
|
||||
@@ -111,21 +111,21 @@ class TestMinuteBarData(WithBarDataChecks,
|
||||
# illiquid_split_asset trades every 10 minutes
|
||||
for sid in (1, cls.SPLIT_ASSET_SID):
|
||||
yield sid, create_minute_df_for_asset(
|
||||
cls.env,
|
||||
cls.trading_schedule,
|
||||
cls.bcolz_minute_bar_days[0],
|
||||
cls.bcolz_minute_bar_days[-1],
|
||||
)
|
||||
|
||||
for sid in (2, cls.ILLIQUID_SPLIT_ASSET_SID):
|
||||
yield sid, create_minute_df_for_asset(
|
||||
cls.env,
|
||||
cls.trading_schedule,
|
||||
cls.bcolz_minute_bar_days[0],
|
||||
cls.bcolz_minute_bar_days[-1],
|
||||
10,
|
||||
)
|
||||
|
||||
yield cls.HILARIOUSLY_ILLIQUID_ASSET_SID, create_minute_df_for_asset(
|
||||
cls.env,
|
||||
cls.trading_schedule,
|
||||
cls.bcolz_minute_bar_days[0],
|
||||
cls.bcolz_minute_bar_days[-1],
|
||||
50,
|
||||
|
||||
@@ -147,13 +147,13 @@ class TestBenchmark(WithDataPortal, WithSimParams, ZiplineTestCase):
|
||||
)
|
||||
|
||||
tmp_reader = tmp_bcolz_minute_bar_reader(
|
||||
self.env,
|
||||
self.env.trading_days,
|
||||
self.trading_schedule,
|
||||
self.trading_schedule.all_execution_days,
|
||||
create_minute_bar_data(minutes, [2]),
|
||||
)
|
||||
with tmp_reader as reader:
|
||||
data_portal = DataPortal(
|
||||
self.env,
|
||||
self.env, self.trading_schedule,
|
||||
first_trading_day=reader.first_trading_day,
|
||||
equity_minute_reader=reader,
|
||||
equity_daily_reader=self.bcolz_daily_bar_reader,
|
||||
@@ -163,7 +163,7 @@ class TestBenchmark(WithDataPortal, WithSimParams, ZiplineTestCase):
|
||||
source = BenchmarkSource(
|
||||
2,
|
||||
self.env,
|
||||
default_nyse_schedule,
|
||||
self.trading_schedule,
|
||||
self.sim_params.trading_days,
|
||||
data_portal
|
||||
)
|
||||
|
||||
@@ -394,44 +394,6 @@ class TradingEnvironmentTestCase(WithLogger,
|
||||
"""
|
||||
Tests for date management utilities in zipline.finance.trading.
|
||||
"""
|
||||
@timed(DEFAULT_TIMEOUT)
|
||||
def test_is_trading_day(self):
|
||||
# holidays taken from: http://www.nyse.com/press/1191407641943.html
|
||||
new_years = datetime(2008, 1, 1, tzinfo=pytz.utc)
|
||||
mlk_day = datetime(2008, 1, 21, tzinfo=pytz.utc)
|
||||
presidents = datetime(2008, 2, 18, tzinfo=pytz.utc)
|
||||
good_friday = datetime(2008, 3, 21, tzinfo=pytz.utc)
|
||||
memorial_day = datetime(2008, 5, 26, tzinfo=pytz.utc)
|
||||
july_4th = datetime(2008, 7, 4, tzinfo=pytz.utc)
|
||||
labor_day = datetime(2008, 9, 1, tzinfo=pytz.utc)
|
||||
tgiving = datetime(2008, 11, 27, tzinfo=pytz.utc)
|
||||
christmas = datetime(2008, 5, 25, tzinfo=pytz.utc)
|
||||
a_saturday = datetime(2008, 8, 2, tzinfo=pytz.utc)
|
||||
a_sunday = datetime(2008, 10, 12, tzinfo=pytz.utc)
|
||||
holidays = [
|
||||
new_years,
|
||||
mlk_day,
|
||||
presidents,
|
||||
good_friday,
|
||||
memorial_day,
|
||||
july_4th,
|
||||
labor_day,
|
||||
tgiving,
|
||||
christmas,
|
||||
a_saturday,
|
||||
a_sunday
|
||||
]
|
||||
|
||||
for holiday in holidays:
|
||||
self.assertTrue(not self.cal.is_open_on_day(holiday))
|
||||
|
||||
first_trading_day = datetime(2008, 1, 2, tzinfo=pytz.utc)
|
||||
last_trading_day = datetime(2008, 12, 31, tzinfo=pytz.utc)
|
||||
workdays = [first_trading_day, last_trading_day]
|
||||
|
||||
for workday in workdays:
|
||||
self.assertTrue(self.cal.is_open_on_day(workday))
|
||||
|
||||
def test_simulation_parameters(self):
|
||||
sp = SimulationParameters(
|
||||
period_start=datetime(2008, 1, 1, tzinfo=pytz.utc),
|
||||
@@ -477,85 +439,3 @@ class TradingEnvironmentTestCase(WithLogger,
|
||||
self.assertEquals(num_expected_trading_days, params.days_in_period)
|
||||
np.testing.assert_array_equal(expected_trading_days,
|
||||
params.trading_days.tolist())
|
||||
|
||||
@timed(DEFAULT_TIMEOUT)
|
||||
def test_minute_window(self):
|
||||
|
||||
# January 2008
|
||||
# Su Mo Tu We Th Fr Sa
|
||||
# 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
|
||||
|
||||
us_east = pytz.timezone('US/Eastern')
|
||||
utc = pytz.utc
|
||||
|
||||
# 10:01 AM Eastern on January 7th..
|
||||
start = us_east.localize(datetime(2008, 1, 7, 10, 1))
|
||||
utc_start = pd.Timestamp(start.astimezone(utc))
|
||||
|
||||
# Get the next 10 minutes
|
||||
minutes = self.cal.trading_minute_window(
|
||||
utc_start, 10,
|
||||
)
|
||||
self.assertEqual(len(minutes), 10)
|
||||
for i in range(10):
|
||||
self.assertEqual(minutes[i], utc_start + timedelta(minutes=i))
|
||||
|
||||
# Get the previous 10 minutes.
|
||||
minutes = self.cal.trading_minute_window(
|
||||
utc_start, 10, step=-1,
|
||||
)
|
||||
self.assertEqual(len(minutes), 10)
|
||||
for i in range(10):
|
||||
self.assertEqual(minutes[i], utc_start + timedelta(minutes=-i))
|
||||
|
||||
# Get the next 900 minutes, including utc_start, rolling over into the
|
||||
# next two days.
|
||||
# Should include:
|
||||
# Today: 10:01 AM -> 4:00 PM (360 minutes)
|
||||
# Tomorrow: 9:31 AM -> 4:00 PM (390 minutes, 750 total)
|
||||
# Last Day: 9:31 AM -> 12:00 PM (150 minutes, 900 total)
|
||||
minutes = self.cal.trading_minute_window(
|
||||
start, 900,
|
||||
)
|
||||
today = self.cal.trading_minutes_for_day(utc_start)[30:]
|
||||
tomorrow = self.cal.trading_minutes_for_day(
|
||||
start + timedelta(days=1)
|
||||
)
|
||||
last_day = self.cal.trading_minutes_for_day(
|
||||
start + timedelta(days=2))[:150]
|
||||
|
||||
self.assertEqual(len(minutes), 900)
|
||||
self.assertEqual(minutes[0], utc_start)
|
||||
self.assertTrue(all(today == minutes[:360]))
|
||||
self.assertTrue(all(tomorrow == minutes[360:750]))
|
||||
self.assertTrue(all(last_day == minutes[750:]))
|
||||
|
||||
# Get the previous 801 minutes, including utc_start, rolling over into
|
||||
# Friday the 4th and Thursday the 3rd.
|
||||
# Should include:
|
||||
# Today: 10:01 AM -> 9:31 AM (31 minutes)
|
||||
# Friday: 4:00 PM -> 9:31 AM (390 minutes, 421 total)
|
||||
# Thursday: 4:00 PM -> 9:41 AM (380 minutes, 801 total)
|
||||
minutes = self.cal.trading_minute_window(
|
||||
start, 801, step=-1,
|
||||
)
|
||||
|
||||
today = self.cal.trading_minutes_for_day(utc_start)[30::-1]
|
||||
# minus an extra two days from each of these to account for the two
|
||||
# weekend days we skipped
|
||||
friday = self.cal.trading_minutes_for_day(
|
||||
start + timedelta(days=-3),
|
||||
)[::-1]
|
||||
thursday = self.cal.trading_minutes_for_day(
|
||||
start + timedelta(days=-4),
|
||||
)[:9:-1]
|
||||
|
||||
self.assertEqual(len(minutes), 801)
|
||||
self.assertEqual(minutes[0], utc_start)
|
||||
self.assertTrue(all(today == minutes[:31]))
|
||||
self.assertTrue(all(friday == minutes[31:421]))
|
||||
self.assertTrue(all(thursday == minutes[421:]))
|
||||
|
||||
@@ -1136,7 +1136,7 @@ class MinuteEquityHistoryTestCase(WithHistory, ZiplineTestCase):
|
||||
def test_history_window_before_first_trading_day(self):
|
||||
# trading_start is 2/3/2014
|
||||
# get a history window that starts before that, and ends after that
|
||||
self.data_portal._first_trading_day = self.TRADING_START_DT
|
||||
self.data_portal.set_first_trading_day(self.TRADING_START_DT)
|
||||
first_day_minutes = default_nyse_schedule.execution_minutes_for_day(
|
||||
self.TRADING_START_DT
|
||||
)
|
||||
@@ -1645,6 +1645,7 @@ class DailyEquityHistoryTestCase(WithHistory, ZiplineTestCase):
|
||||
# trading_start is 2/3/2014
|
||||
# get a history window that starts before that, and ends after that
|
||||
|
||||
self.data_portal.set_first_trading_day(self.TRADING_START_DT)
|
||||
second_day = default_nyse_schedule.next_execution_day(
|
||||
self.TRADING_START_DT
|
||||
)
|
||||
|
||||
@@ -395,7 +395,7 @@ class TestStatelessRules(RuleTestCase):
|
||||
sim_start = pd.Timestamp('01-06-2014', tz='UTC') + \
|
||||
timedelta(days=start_offset)
|
||||
|
||||
jan_minutes = self.env.minutes_for_days_in_range(
|
||||
jan_minutes = self.nyse_cal.trading_minutes_for_days_in_range(
|
||||
datetime.date(year=2014, month=1, day=6) +
|
||||
timedelta(days=start_offset),
|
||||
datetime.date(year=2014, month=1, day=31)
|
||||
@@ -426,9 +426,8 @@ class TestStatelessRules(RuleTestCase):
|
||||
trigger_dates = \
|
||||
[x - timedelta(days=rule_offset) for x in trigger_dates]
|
||||
|
||||
should_trigger = partial(
|
||||
rule(rule_offset).should_trigger, env=self.env
|
||||
)
|
||||
rule.cal = self.nyse_cal
|
||||
should_trigger = rule(rule_offset).should_trigger
|
||||
|
||||
# If offset is 4, there is not enough trading days in the short week,
|
||||
# and so it should not trigger
|
||||
@@ -439,9 +438,9 @@ class TestStatelessRules(RuleTestCase):
|
||||
trigger_dates = [x for x in trigger_dates if x >= sim_start]
|
||||
|
||||
# Get all the minutes on the trigger dates
|
||||
trigger_dts = self.env.market_minutes_for_day(trigger_dates[0])
|
||||
trigger_dts = self.nyse_cal.trading_minutes_for_day(trigger_dates[0])
|
||||
for dt in trigger_dates[1:]:
|
||||
trigger_dts += self.env.market_minutes_for_day(dt)
|
||||
trigger_dts += self.nyse_cal.trading_minutes_for_day(dt)
|
||||
|
||||
expected_n_triggered = len(trigger_dts)
|
||||
trigger_dts = iter(trigger_dts)
|
||||
@@ -460,11 +459,14 @@ class TestStatelessRules(RuleTestCase):
|
||||
NDaysBeforeLastTradingDayOfWeek(4)
|
||||
time_rule = AfterOpen(minutes=60)
|
||||
|
||||
week_rule.cal = self.nyse_cal
|
||||
time_rule.cal = self.nyse_cal
|
||||
|
||||
composed_rule = week_rule & time_rule
|
||||
|
||||
should_trigger = partial(composed_rule.should_trigger, env=self.env)
|
||||
should_trigger = composed_rule.should_trigger
|
||||
|
||||
week_minutes = self.env.minutes_for_days_in_range(
|
||||
week_minutes = self.nyse_cal.trading_minutes_for_days_in_range(
|
||||
datetime.date(year=2014, month=1, day=6),
|
||||
datetime.date(year=2014, month=1, day=10)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user