diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 52086100..39389b30 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -41,6 +41,7 @@ from zipline.errors import ( UnsupportedCommissionModel, UnsupportedOrderParameters, UnsupportedSlippageModel, + UnsupportedDatetimeFormat, ) from zipline.finance.trading import TradingEnvironment from zipline.finance.blotter import Blotter @@ -746,6 +747,12 @@ class TradingAlgorithm(object): RootSymbolNotFound If a future chain could not be found for the given root symbol. """ + if as_of_date: + try: + as_of_date = pd.Timestamp(as_of_date, tz='UTC') + except ValueError: + raise UnsupportedDatetimeFormat(input=as_of_date, + method='future_chain') return FutureChain( asset_finder=self.asset_finder, get_datetime=self.get_datetime, @@ -1002,7 +1009,11 @@ class TradingAlgorithm(object): (symbols may map to different firms or underlying assets at different times) """ - self._symbol_lookup_date = pd.Timestamp(dt, tz='UTC') + try: + self._symbol_lookup_date = pd.Timestamp(dt, tz='UTC') + except ValueError: + raise UnsupportedDatetimeFormat(input=dt, + method='set_symbol_lookup_date') def set_sources(self, sources): assert isinstance(sources, list) diff --git a/zipline/errors.py b/zipline/errors.py index c1a04831..3e9a9d60 100644 --- a/zipline/errors.py +++ b/zipline/errors.py @@ -404,3 +404,11 @@ class NoFurtherDataError(ZiplineError): # This accepts an arbitrary message string because it's used in more places # that can be usefully templated. msg = '{msg}' + + +class UnsupportedDatetimeFormat(ZiplineError): + """ + Raised when an unsupported datetime is passed to an API method. + """ + msg = ("The input '{input}' passed to '{method}' is not " + "coercible to a pandas.Timestamp object.")