mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-03 13:26:01 +08:00
ENH: Adds ExchangeCalendar, TradingSchedule, and implementations
Conflicts: tests/data/test_minute_bars.py tests/data/test_us_equity_pricing.py tests/finance/test_slippage.py tests/pipeline/test_engine.py tests/pipeline/test_us_equity_pricing_loader.py tests/serialization_cases.py tests/test_algorithm.py tests/test_assets.py tests/test_bar_data.py tests/test_benchmark.py tests/test_exception_handling.py tests/test_fetcher.py tests/test_finance.py tests/test_history.py tests/test_perf_tracking.py tests/test_security_list.py tests/utils/test_events.py zipline/algorithm.py zipline/data/data_portal.py zipline/data/us_equity_loader.py zipline/errors.py zipline/finance/trading.py zipline/testing/core.py zipline/utils/events.py
This commit is contained in:
+35
-24
@@ -166,7 +166,7 @@ from zipline.utils.control_flow import nullctx
|
||||
import zipline.utils.events
|
||||
from zipline.utils.events import date_rules, time_rules, Always
|
||||
import zipline.utils.factory as factory
|
||||
from zipline.utils.tradingcalendar import trading_day, trading_days
|
||||
from zipline.utils.calendars import default_nyse_schedule
|
||||
|
||||
# Because test cases appear to reuse some resources.
|
||||
|
||||
@@ -826,7 +826,7 @@ def before_trading_start(context, data):
|
||||
self.sim_params.data_frequency = 'daily'
|
||||
|
||||
sim_params = factory.create_simulation_parameters(
|
||||
num_days=4, env=self.env, data_frequency='daily')
|
||||
num_days=4, data_frequency='daily')
|
||||
|
||||
algo = TestRegisterTransformAlgorithm(
|
||||
sim_params=sim_params,
|
||||
@@ -835,7 +835,7 @@ def before_trading_start(context, data):
|
||||
self.assertEqual(algo.sim_params.data_frequency, 'daily')
|
||||
|
||||
sim_params = factory.create_simulation_parameters(
|
||||
num_days=4, env=self.env, data_frequency='minute')
|
||||
num_days=4, data_frequency='minute')
|
||||
|
||||
algo = TestRegisterTransformAlgorithm(
|
||||
sim_params=sim_params,
|
||||
@@ -953,7 +953,7 @@ def before_trading_start(context, data):
|
||||
period_end=period_end,
|
||||
capital_base=float("1.0e5"),
|
||||
data_frequency='minute',
|
||||
env=env
|
||||
trading_schedule=default_nyse_schedule,
|
||||
)
|
||||
|
||||
data_portal = create_data_portal(
|
||||
@@ -961,6 +961,7 @@ def before_trading_start(context, data):
|
||||
tempdir,
|
||||
sim_params,
|
||||
equities.index,
|
||||
default_nyse_schedule,
|
||||
)
|
||||
algo = algo_class(sim_params=sim_params, env=env)
|
||||
algo.run(data_portal)
|
||||
@@ -1551,9 +1552,10 @@ def handle_data(context, data):
|
||||
env=self.env,
|
||||
)
|
||||
trades = factory.create_daily_trade_source(
|
||||
[0], self.sim_params, self.env)
|
||||
[0], self.sim_params, self.env, default_nyse_schedule)
|
||||
data_portal = create_data_portal_from_trade_history(
|
||||
self.env, tempdir, self.sim_params, {0: trades})
|
||||
self.env, default_nyse_schedule, tempdir, self.sim_params,
|
||||
{0: trades})
|
||||
results = test_algo.run(data_portal)
|
||||
|
||||
all_txns = [
|
||||
@@ -1640,7 +1642,7 @@ def handle_data(context, data):
|
||||
params = SimulationParameters(
|
||||
period_start=pd.Timestamp("2007-01-03", tz='UTC'),
|
||||
period_end=pd.Timestamp("2007-01-05", tz='UTC'),
|
||||
env=self.env
|
||||
trading_schedule=default_nyse_schedule,
|
||||
)
|
||||
|
||||
# order method shouldn't blow up
|
||||
@@ -2719,7 +2721,6 @@ class TestTradingControls(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
sim_params = factory.create_simulation_parameters(
|
||||
start=start,
|
||||
num_days=4,
|
||||
env=env,
|
||||
data_frequency='minute',
|
||||
)
|
||||
|
||||
@@ -2727,7 +2728,8 @@ class TestTradingControls(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
env,
|
||||
tempdir,
|
||||
sim_params,
|
||||
[1]
|
||||
[1],
|
||||
default_nyse_schedule,
|
||||
)
|
||||
|
||||
def handle_data(algo, data):
|
||||
@@ -2848,7 +2850,8 @@ class TestTradingControls(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
env,
|
||||
tempdir,
|
||||
self.sim_params,
|
||||
[0]
|
||||
[0],
|
||||
default_nyse_schedule,
|
||||
)
|
||||
algo.run(data_portal)
|
||||
|
||||
@@ -2862,7 +2865,8 @@ class TestTradingControls(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
env,
|
||||
tempdir,
|
||||
self.sim_params,
|
||||
[0]
|
||||
[0],
|
||||
default_nyse_schedule,
|
||||
)
|
||||
algo = SetAssetDateBoundsAlgorithm(
|
||||
sim_params=self.sim_params,
|
||||
@@ -2881,7 +2885,8 @@ class TestTradingControls(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
env,
|
||||
tempdir,
|
||||
self.sim_params,
|
||||
[0]
|
||||
[0],
|
||||
default_nyse_schedule,
|
||||
)
|
||||
algo = SetAssetDateBoundsAlgorithm(
|
||||
sim_params=self.sim_params,
|
||||
@@ -2907,7 +2912,7 @@ class TestAccountControls(WithDataPortal, WithSimParams, ZiplineTestCase):
|
||||
[100, 100, 100, 300],
|
||||
timedelta(days=1),
|
||||
cls.sim_params,
|
||||
cls.env,
|
||||
default_nyse_schedule,
|
||||
),
|
||||
},
|
||||
index=cls.sim_params.trading_days,
|
||||
@@ -3054,7 +3059,7 @@ class TestFutureFlip(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
[1e9, 1e9, 1e9],
|
||||
timedelta(days=1),
|
||||
cls.sim_params,
|
||||
cls.env
|
||||
default_nyse_schedule,
|
||||
),
|
||||
},
|
||||
index=cls.sim_params.trading_days,
|
||||
@@ -3064,7 +3069,7 @@ class TestFutureFlip(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
def test_flip_algo(self):
|
||||
metadata = {1: {'symbol': 'TEST',
|
||||
'start_date': self.sim_params.trading_days[0],
|
||||
'end_date': self.env.next_trading_day(
|
||||
'end_date': default_nyse_schedule.next_execution_day(
|
||||
self.sim_params.trading_days[-1]),
|
||||
'multiplier': 5}}
|
||||
|
||||
@@ -3206,7 +3211,7 @@ class TestOrderCancelation(WithDataPortal,
|
||||
sim_params=SimulationParameters(
|
||||
period_start=self.sim_params.period_start,
|
||||
period_end=self.sim_params.period_end,
|
||||
env=self.env,
|
||||
trading_schedule=self.env,
|
||||
data_frequency=data_frequency,
|
||||
emission_rate='minute' if minute_emission else 'daily'
|
||||
)
|
||||
@@ -3419,8 +3424,12 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase):
|
||||
sids = asset_info.index
|
||||
|
||||
env = self.enter_instance_context(tmp_trading_env(equities=asset_info))
|
||||
market_opens = env.open_and_closes.market_open.loc[self.test_days]
|
||||
market_closes = env.open_and_closes.market_close.loc[self.test_days]
|
||||
market_opens = default_nyse_schedule.schedule.market_open.loc[
|
||||
self.test_days
|
||||
]
|
||||
market_closes = default_nyse_schedule.schedule.market_close.loc[
|
||||
self.test_days
|
||||
]
|
||||
|
||||
if frequency == 'daily':
|
||||
dates = self.test_days
|
||||
@@ -3441,12 +3450,12 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase):
|
||||
)
|
||||
reader = BcolzDailyBarReader(path)
|
||||
data_portal = DataPortal(
|
||||
env,
|
||||
env, default_nyse_schedule,
|
||||
first_trading_day=reader.first_trading_day,
|
||||
equity_daily_reader=reader,
|
||||
)
|
||||
elif frequency == 'minute':
|
||||
dates = env.minutes_for_days_in_range(
|
||||
dates = default_nyse_schedule.execution_minutes_for_days_in_range(
|
||||
self.test_days[0],
|
||||
self.test_days[-1],
|
||||
)
|
||||
@@ -3471,7 +3480,7 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase):
|
||||
)
|
||||
reader = BcolzMinuteBarReader(self.tmpdir.path)
|
||||
data_portal = DataPortal(
|
||||
env,
|
||||
env, default_nyse_schedule,
|
||||
first_trading_day=reader.first_trading_day,
|
||||
equity_minute_reader=reader,
|
||||
)
|
||||
@@ -3485,7 +3494,6 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase):
|
||||
end=self.test_days[-1],
|
||||
data_frequency=frequency,
|
||||
emission_rate=frequency,
|
||||
env=env,
|
||||
capital_base=capital_base,
|
||||
)
|
||||
|
||||
@@ -3498,7 +3506,7 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase):
|
||||
else:
|
||||
final_prices = {
|
||||
asset.sid: trade_data_by_sid[asset.sid].loc[
|
||||
env.get_open_and_close(asset.end_date)[1]
|
||||
default_nyse_schedule.start_and_end(asset.end_date)[1]
|
||||
].close
|
||||
for asset in assets
|
||||
}
|
||||
@@ -3852,6 +3860,9 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase):
|
||||
expected_cash.extend([after_second_auto_close] * (390 + 390))
|
||||
expected_position_counts.extend([1] * (390 + 390))
|
||||
|
||||
# Check list lengths first to avoid expensive comparison
|
||||
self.assertEqual(len(algo.cash), len(expected_cash))
|
||||
# TODO find more efficient way to compare these lists
|
||||
self.assertEqual(algo.cash, expected_cash)
|
||||
self.assertEqual(
|
||||
list(output['ending_cash']),
|
||||
@@ -3987,7 +3998,7 @@ class TestOrderAfterDelist(WithTradingEnvironment, ZiplineTestCase):
|
||||
sim_params=SimulationParameters(
|
||||
period_start=pd.Timestamp("2016-01-06", tz='UTC'),
|
||||
period_end=pd.Timestamp("2016-01-07", tz='UTC'),
|
||||
env=self.env,
|
||||
trading_schedule=default_nyse_schedule,
|
||||
data_frequency="minute"
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user