MAINT: Coerce user input to Timestamps, catching errors

This commit is contained in:
Stewart Douglas
2015-09-04 11:18:43 -04:00
parent c2159d429b
commit ad31e1ff6e
2 changed files with 20 additions and 1 deletions
+12 -1
View File
@@ -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)
+8
View File
@@ -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.")