shlomiku / zipline-trader

Zipline Trader, a Pythonic Algorithmic Trading Library with broker integration
https://github.com/shlomikushchi/zipline-trader
Apache License 2.0
314 stars 71 forks source link

KeyError: Timestamp after version 1.5.0 #172

Closed Sutyke closed 3 years ago

Sutyke commented 3 years ago

When running back_test example, with this commit up to current commit I get error below.

When I run the same example above with version 1.5.0 there is no problem.

NOTE: After version 1.5.0, there had to be introduced bug related to loading data bundle as timestamp in the error change when bundle ingestion date change. And it is usually one day after bundle start date.

Error msg: KeyError: Timestamp('2012-11-30 05:00:00+0000', tz='UTC')

KeyError                                  Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1354251600000000000

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~/OneDrive/02_Research/examples/zipline_trader/backtesting.py in 
     96                   before_trading_start=before_trading_start,
     97 #                   analyze=analyze,
---> 98                   data_frequency='daily'
     99                   )
    100 fig, axes = plt.subplots(1, 1, figsize=(16, 5), sharex=True)

~/zipline-trader/zipline/utils/run_algo.py in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, teardown, data_frequency, bundle, bundle_timestamp, trading_calendar, metrics_set, benchmark_returns, default_extension, extensions, strict_extensions, environ, blotter, broker, performance_callback, stop_execution_callback, execution_id, state_filename, realtime_bar_target)
    487         performance_callback=performance_callback,
    488         stop_execution_callback=stop_execution_callback,
--> 489         execution_id=execution_id
    490     )
    491 

~/zipline-trader/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, benchmark_spec, broker, state_filename, realtime_bar_target, performance_callback, stop_execution_callback, teardown, execution_id)
    205         equity_minute_reader=bundle_data.equity_minute_bar_reader,
    206         equity_daily_reader=bundle_data.equity_daily_bar_reader,
--> 207         adjustment_reader=bundle_data.adjustment_reader,
    208     )
    209 

~/zipline-trader/zipline/data/data_portal.py in __init__(self, asset_finder, trading_calendar, first_trading_day, equity_daily_reader, equity_minute_reader, future_daily_reader, future_minute_reader, adjustment_reader, last_available_session, last_available_minute, minute_history_prefetch_length, daily_history_prefetch_length)
    289                 self._first_trading_day
    290             )
--> 291             if self._first_trading_day is not None else (None, None)
    292         )
    293 

~/anaconda3/envs/zt151/lib/python3.6/site-packages/trading_calendars/trading_calendar.py in open_and_close_for_session(self, session_label)
    842         """
    843         return (
--> 844             self.session_open(session_label),
    845             self.session_close(session_label)
    846         )

~/anaconda3/envs/zt151/lib/python3.6/site-packages/trading_calendars/trading_calendar.py in session_open(self, session_label)
    869         return self.schedule.at[
    870             session_label,
--> 871             'market_open'
    872         ].tz_localize(UTC)
    873 

~/anaconda3/envs/zt151/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1867 
   1868         key = self._convert_key(key)
-> 1869         return self.obj._get_value(*key, takeable=self._takeable)
   1870 
   1871     def __setitem__(self, key, value):

~/anaconda3/envs/zt151/lib/python3.6/site-packages/pandas/core/frame.py in _get_value(self, index, col, takeable)
   1983 
   1984         try:
-> 1985             return engine.get_value(series._values, index)
   1986         except (TypeError, ValueError):
   1987 

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()

KeyError: Timestamp('2012-11-30 05:00:00+0000', tz='UTC')
Sutyke commented 3 years ago

v1.6.0 solved the problem.