API: Add factory for calendars

This commit is contained in:
dmichalowicz
2017-04-18 13:54:51 -04:00
parent cd91d518bb
commit 2f33ddb023
5 changed files with 78 additions and 16 deletions
+37 -10
View File
@@ -54,16 +54,18 @@ from zipline.data.us_equity_pricing import (
BcolzDailyBarWriter,
)
from zipline.errors import (
OrderDuringInitialize,
RegisterTradingControlPostInit,
TradingControlViolation,
AccountControlViolation,
SymbolNotFound,
UnsupportedDatetimeFormat,
CannotOrderDelistedAsset,
OrderDuringInitialize,
OrderInBeforeTradingStart,
RegisterTradingControlPostInit,
ScheduleFunctionInvalidCalendar,
SetCancelPolicyPostInit,
SymbolNotFound,
TradingControlViolation,
UnsupportedCancelPolicy,
OrderInBeforeTradingStart)
UnsupportedDatetimeFormat,
)
from zipline.api import (
order,
order_value,
@@ -437,22 +439,23 @@ def handle_data(context, data):
# run a simulation on the CME cal, and schedule a function
# using the NYSE cal
algotext = """
from zipline.api import schedule_function, get_datetime, time_rules, date_rules
from zipline.utils.calendars import get_calendar
from zipline.api import (
schedule_function, get_datetime, time_rules, date_rules, calendars,
)
def initialize(context):
schedule_function(
func=log_nyse_open,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(),
calendar=get_calendar("NYSE")
calendar=calendars.US_EQUITIES,
)
schedule_function(
func=log_nyse_close,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(),
calendar=get_calendar("NYSE")
calendar=calendars.US_EQUITIES,
)
context.nyse_opens = []
@@ -488,6 +491,30 @@ def log_nyse_close(context, data):
session_close = nyse.open_and_close_for_session(session_label)[1]
self.assertEqual(session_close - timedelta(minutes=1), minute)
# Test that passing an invalid calendar parameter raises an error.
erroring_algotext = dedent(
"""
from zipline.api import schedule_function
from zipline.utils.calendars import get_calendar
def initialize(context):
schedule_function(func=my_func, calendar=get_calendar('NYSE'))
def my_func(context, data):
pass
"""
)
algo = TradingAlgorithm(
script=erroring_algotext,
sim_params=self.sim_params,
env=self.env,
trading_calendar=get_calendar('CME'),
)
with self.assertRaises(ScheduleFunctionInvalidCalendar):
algo.run(self.data_portal)
def test_schedule_function(self):
us_eastern = pytz.timezone('US/Eastern')