From c2159d429bc0bfca0d2f2b0dad4c7a716f62e942 Mon Sep 17 00:00:00 2001 From: Stewart Douglas Date: Thu, 3 Sep 2015 16:17:50 -0400 Subject: [PATCH] ENH: Allow user to set the symbol lookup date Previously symbols were resolved to sids based on the end of simulation date. This commit allows the user to specify the date for which resolution will take place using a new set_symbol_lookup_date() API method. If the user does not use this method the lookup date will default back to the simulation end date. --- zipline/algorithm.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 0403c1ef..52086100 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -225,6 +225,10 @@ class TradingAlgorithm(object): # Set the dt initally to the period start by forcing it to change self.on_dt_changed(self.sim_params.period_start) + # The symbol lookup date specifies the date to use when resolving + # symbols to sids, and can be set using set_symbol_lookup_date() + self._symbol_lookup_date = None + self.portfolio_needs_update = True self.account_needs_update = True self.performance_needs_update = True @@ -693,9 +697,14 @@ class TradingAlgorithm(object): Default symbol lookup for any source that directly maps the symbol to the Asset (e.g. yahoo finance). """ + # If the user has not set the symbol lookup date, + # use the period_end as the date for sybmol->sid resolution. + _lookup_date = self._symbol_lookup_date if self._symbol_lookup_date is not None \ + else self.sim_params.period_end + return self.asset_finder.lookup_symbol_resolve_multiple( symbol_str, - as_of_date=self.sim_params.period_end + as_of_date=_lookup_date ) @api_method @@ -986,6 +995,15 @@ class TradingAlgorithm(object): raise OverrideCommissionPostInit() self.commission = commission + @api_method + def set_symbol_lookup_date(self, dt): + """ + Set the date for which symbols will be resolved to their sids + (symbols may map to different firms or underlying assets at + different times) + """ + self._symbol_lookup_date = pd.Timestamp(dt, tz='UTC') + def set_sources(self, sources): assert isinstance(sources, list) self.sources = sources