You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As noted #227 (and demonstrated by the original version of the code in Clenow's book), at some time in the past zipline.run_algorithm accepted tz aware dates for start and end but then that stopped working. As I recently commented there, it appears to me the probably came Zipline upgrading to exchange_calendar 4.x.
This issue is about how zipline.run_algorithm can't work correctly if a tz naive date is used for end (and therefore start either) because it isn't possible to use zipline.api.symbol as it was intended (i.e. date sensitive symbol lookups).
Note that using self.sim_params.end_session for the default lookup date defeats the very purpose of date-sensitive symbol lookup (which is how I got here because the S&P 500 portfolio has symbols that don't denote the asset at the end of the session and the lookup needs to be done at the simulation's today).
I think this default for _lookup_date should change to be self.get_datetime().
That will always fail if set_symbol_lookup_date was called because as_of_date will be tz-aware but start and end are tz-naive. And of course if set_symbol_lookup_date is not called then it will be sim_params.end_session which is now (unlike before the change to run_algorithm) tz-naive. And as noted above, not calling set_symbol_lookup_date means as_of_date will be the very end of the simulation which makes a date-sensitive symbol lookup unhelpful.
What did you expect to happen?
zipline.api.symbol returns sid for a symbol as of the current (self.get_datetime()) of the simulation.
Calling zipline.api.set_symbol_lookup_date should work with zipline.api.symbol so that the lookup is as of that date.
What happened instead?
By default zipline.api.symbol returns the sid, if any, of a symbol as of the end of the simulation, regardless of the current simulation time it is called at.
Calling zipline.api.set_symbol_lookup_date causes zipline.api.symbol to fail with a "Can't compare tz naive with tz aware" error.
Here is how you can reproduce this issue on your machine:
Reproduction Steps
For the problem with using self.sim_params.end_session as the default value for zipline.api.symbol:
Set up run_algorithm simulation that starts before September 2020 (mine happened to be 2020-08-03) and ends after that (mine happened to be 2024-08-30).
Call zipline.api.symbol on "CTL" (CenturyLink an S&P 500 constituent that changed symbol to LUMN).
For the problem that you can't work around that by changing the as of date:
Set up run_algorithm
Schedule a function that calls zipline.api.set_symbol_lookup_date then zipline.api.symbol.
What steps have you taken to resolve this already?
A lot of digging and hacking.
Anything else?
I can prepare a PR if there is agreement on the solution. I propose two changes:
Change the default for zipline.api.symbol to be self.get_datetime() (instead of self.sim_params.end_session). AFAIK self.get_datetime() is always tz-aware but I don't know for sure.
Change zipline.run_algorithm back to using tz-aware start and end so that the comparisons done by zipline.api.symbol work correctly (actually the requirement isn't so much about the args but that the values passed to SimulationParameters need to be tz-aware).
Sincerely,
Jim White
The text was updated successfully, but these errors were encountered:
Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Environment
A generous hand selected subset of packages
Now that you know a little about me, let me tell you about the issue I am
having:
Description of Issue
I'm trying to make Clenow's S&P 500 momentum portfolio example work to demonstrate using my WIP Polygon.io Zipline data bundle and to make it easy-as-possible to get up and running with Zipline Reloaded on Colab. This is the rather large complete notebook: https://github.com/fovi-llc/trading_evolved/blob/main/Chapter%2012%20-%20Momentum/S%26P_500_Momentum_Model_using_Polygon.ipynb
As noted #227 (and demonstrated by the original version of the code in Clenow's book), at some time in the past
zipline.run_algorithm
accepted tz aware dates for start and end but then that stopped working. As I recently commented there, it appears to me the probably came Zipline upgrading to exchange_calendar 4.x.This issue is about how
zipline.run_algorithm
can't work correctly if a tz naive date is used forend
(and thereforestart
either) because it isn't possible to usezipline.api.symbol
as it was intended (i.e. date sensitive symbol lookups).As you can see at
zipline-reloaded/src/zipline/algorithm.py
Line 1582 in e945ae1
zipline.api.set_symbol_lookup_date
method always setsself._symbol_lookup_date
to be UTC.zipline.api.symbol
atzipline-reloaded/src/zipline/algorithm.py
Line 1037 in e945ae1
Note that using
self.sim_params.end_session
for the default lookup date defeats the very purpose of date-sensitive symbol lookup (which is how I got here because the S&P 500 portfolio has symbols that don't denote the asset at the end of the session and the lookup needs to be done at the simulation's today).I think this default for
_lookup_date
should change to beself.get_datetime()
.lookup_symbol
get us herezipline-reloaded/src/zipline/assets/assets.py
Line 723 in f5abd98
zipline-reloaded/src/zipline/assets/assets.py
Line 815 in f5abd98
That will always fail if
set_symbol_lookup_date
was called becauseas_of_date
will be tz-aware butstart
andend
are tz-naive. And of course ifset_symbol_lookup_date
is not called then it will besim_params.end_session
which is now (unlike before the change torun_algorithm
) tz-naive. And as noted above, not callingset_symbol_lookup_date
meansas_of_date
will be the very end of the simulation which makes a date-sensitive symbol lookup unhelpful.zipline.api.symbol
returnssid
for a symbol as of the current (self.get_datetime()
) of the simulation.Calling
zipline.api.set_symbol_lookup_date
should work withzipline.api.symbol
so that the lookup is as of that date.By default
zipline.api.symbol
returns thesid
, if any, of a symbol as of the end of the simulation, regardless of the current simulation time it is called at.Calling
zipline.api.set_symbol_lookup_date
causeszipline.api.symbol
to fail with a "Can't compare tz naive with tz aware" error.Here is how you can reproduce this issue on your machine:
Reproduction Steps
For the problem with using
self.sim_params.end_session
as the default value forzipline.api.symbol
:run_algorithm
simulation that starts before September 2020 (mine happened to be 2020-08-03) and ends after that (mine happened to be 2024-08-30).zipline.api.symbol
on "CTL" (CenturyLink an S&P 500 constituent that changed symbol to LUMN).For the problem that you can't work around that by changing the as of date:
run_algorithm
zipline.api.set_symbol_lookup_date
thenzipline.api.symbol
.What steps have you taken to resolve this already?
A lot of digging and hacking.
Anything else?
I can prepare a PR if there is agreement on the solution. I propose two changes:
Change the default for
zipline.api.symbol
to beself.get_datetime()
(instead ofself.sim_params.end_session
). AFAIKself.get_datetime()
is always tz-aware but I don't know for sure.Change
zipline.run_algorithm
back to using tz-awarestart
andend
so that the comparisons done byzipline.api.symbol
work correctly (actually the requirement isn't so much about the args but that the values passed toSimulationParameters
need to be tz-aware).Sincerely,
Jim White
The text was updated successfully, but these errors were encountered: