QuantRocket

Zipline futures KeyError: 0


#1

Hi,

I’m getting the following error when I try to run a zipline futures backtest. It occurs with GC but works with CL for example.

    quantrocket_zipline_1|HTML(value='')
    quantrocket_zipline_1|AttributeError: 'NoneType' object has no attribute 'next'
    quantrocket_zipline_1|Exception ignored in: 'zipline.assets.continuous_futures.OrderedContracts.contract_before_auto_close'
    quantrocket_zipline_1|AttributeError: 'NoneType' object has no attribute 'next'
    quantrocket_zipline_1|Traceback (most recent call last):
    quantrocket_zipline_1|  File "sym://qrocket_app_py", line 386, in post
    quantrocket_houston_1|172.21.0.9 - - [13/May/2020:03:28:22 +0000] "POST /zipline/backtests/futures_trend.py?data_frequency=daily&bundle=futures2-1d&start=2018-01-01+08%3A15%3A12%2B00%3A00&end=2020-05-11+08%3A15%3A12%2B00%3A00 HTTP/1.1" 500 32 "-" "-"
    quantrocket_zipline_1|  File "sym://qrocket_qrzipline_algorithm_py", line 133, in run_algo
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/algorithm.py", line 638, in run
    quantrocket_zipline_1|    for perf in self.get_generator():
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/gens/tradesimulation.py", line 205, in transform
    quantrocket_zipline_1|    for capital_change_packet in every_bar(dt):
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/gens/tradesimulation.py", line 133, in every_bar
    quantrocket_zipline_1|    handle_data(algo, current_data, dt_to_use)
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/utils/events.py", line 216, in handle_data
    quantrocket_zipline_1|    dt,
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/utils/events.py", line 235, in handle_data
    quantrocket_zipline_1|    self.callback(context, data)
    quantrocket_zipline_1|  File "/codeload/zipline/futures_trend.py", line 202, in daily_trade
    quantrocket_zipline_1|    bar_count=250,
    quantrocket_zipline_1|  File "zipline/_protocol.pyx", line 121, in zipline._protocol.check_parameters.__call__.assert_keywords_and_call
    quantrocket_zipline_1|  File "zipline/_protocol.pyx", line 709, in zipline._protocol.BarData.history
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/data_portal.py", line 967, in get_history_window
    quantrocket_zipline_1|    field, data_frequency)
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/data_portal.py", line 806, in _get_history_daily_window
    quantrocket_zipline_1|    assets, days_for_window, end_dt, field_to_use, data_frequency
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/data_portal.py", line 829, in _get_history_daily_window_data
    quantrocket_zipline_1|    extra_slot=False
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/data_portal.py", line 1117, in _get_daily_window_data
    quantrocket_zipline_1|    extra_slot)
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/history_loader.py", line 549, in history
    quantrocket_zipline_1|    is_perspective_after)
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/history_loader.py", line 431, in _ensure_sliding_windows
    quantrocket_zipline_1|    array = self._array(prefetch_dts, needed_assets, field)
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/history_loader.py", line 573, in _array
    quantrocket_zipline_1|    assets,
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/dispatch_bar_reader.py", line 120, in load_raw_arrays
    quantrocket_zipline_1|    for t in asset_types if sid_groups[t]}
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/dispatch_bar_reader.py", line 120, in <dictcomp>
    quantrocket_zipline_1|    for t in asset_types if sid_groups[t]}
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/data/continuous_future_reader.py", line 39, in load_raw_arrays
    quantrocket_zipline_1|    asset.offset
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/assets/roll_finder.py", line 91, in get_rolls
    quantrocket_zipline_1|    front = self._get_active_contract_at_offset(root_symbol, end, 0)
    quantrocket_zipline_1|  File "/opt/conda/lib/python3.6/site-packages/zipline/assets/roll_finder.py", line 41, in _get_active_contract_at_offset
    quantrocket_zipline_1|    back = oc.contract_at_offset(front, 1, dt.value)
    quantrocket_zipline_1|  File "zipline/assets/continuous_futures.pyx", line 395, in zipline.assets.continuous_futures.OrderedContracts.contract_at_offset
    quantrocket_zipline_1|  File "zipline/assets/continuous_futures.pyx", line 401, in zipline.assets.continuous_futures.OrderedContracts.contract_at_offset
    quantrocket_zipline_1|KeyError: 0
    quantrocket_zipline_1|
    quantrocket_zipline_1|[pid: 303|app: 0|req: 1/1] 172.21.0.9 () {34 vars in 670 bytes} [Wed May 13 03:28:20 2020] POST /zipline/backtests/futures_trend.py?data_frequency=daily&bundle=futures2-1d&start=2018-01-01+08%3A15%3A12%2B00%3A00&end=2020-05-11+08%3A15%3A12%2B00%3A00 => generated 32 bytes in 1776 msecs (HTTP/1.1 500) 2 headers in 90 bytes (1 switches on core 0)

Any help appreciated,

Thanks


#2

My guess based on the traceback is that Zipline is looking for the front month contract as of the simulation date and is not finding it. Maybe you need to shorten the date range?