quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.75k stars 4.74k forks source link

set_benchmark not working with minute mode #1827

Open ikfdarba opened 7 years ago

ikfdarba commented 7 years ago

Zipline 1.0.2

Hello,

i am trying to set a different benchmark in a zipline minute mode simulation but am not being successful:

I am running:

rom zipline.api import order, record, symbol, set_benchmark, order_target_percent

def initialize(context):
    set_benchmark(symbol('AAPL'))
    context.tradeIn=True

def handle_data(context, data):
    if context.tradeIn:
        order_target_percent(symbol('AAPL'), 1)
        context.tradeIn=False

I am able to successfully change the benchmark if i run:

zipline run -f buyLB.py --start 2016-8-18 --end 2016-8-22 -o buyLB_out.pickle

however if i run:

zipline run -f buyLB.py --start 2016-8-18 --end 2016-8-22 -o buyLB_out.pickle --data-frequency minute

i am getting

KeyError: 0

what am i doing wrong?

ernestoeperez88 commented 7 years ago

Hi @ikfdarba. Could you please share with us the full error/stack trace?

Are you otherwise able to run backtests in minute mode (by commenting out set_benchmark(symbol('AAPL')))? Note that the default data bundles do not include minute frequency data. You would need to provide your own data, and create a custom bundle with it.

lukevs commented 6 years ago

Also experiencing this. Imported a custom bundle from CSV with minute data.

Here's the stack trace:

Traceback (most recent call last):
  File "/projects/spy.py", line 27, in <module>
    end=pd.Timestamp('05-2013', tz='UTC'))
  File "/zipline/zipline/utils/run_algo.py", line 371, in run_algorithm
    environ=environ,
  File "/zipline/zipline/utils/run_algo.py", line 186, in _run
    overwrite_sim_params=False,
  File "/zipline/zipline/algorithm.py", line 721, in run
    for perf in self.get_generator():
  File "/zipline/zipline/algorithm.py", line 608, in get_generator
    return self._create_generator(self.sim_params)
  File "/zipline/zipline/algorithm.py", line 587, in _create_generator
    self._create_benchmark_source(),
  File "/zipline/zipline/algorithm.py", line 559, in _create_benchmark_source
    benchmark_returns=benchmark_returns,
  File "/zipline/zipline/sources/benchmark_source.py", line 49, in __init__
    self.data_portal
  File "/zipline/zipline/sources/benchmark_source.py", line 173, in _initialize_precalculated_series
    ffill=True
  File "/zipline/zipline/data/data_portal.py", line 910, in get_history_window
    "close", data_frequency)
  File "/zipline/zipline/data/data_portal.py", line 757, in _get_history_daily_window
    assets, days_for_window, end_dt, field_to_use, data_frequency
  File "/zipline/zipline/data/data_portal.py", line 780, in _get_history_daily_window_data
    extra_slot=False
  File "/zipline/zipline/data/data_portal.py", line 1063, in _get_daily_window_data
    extra_slot)
  File "/zipline/zipline/data/history_loader.py", line 550, in history
    is_perspective_after)
  File "/zipline/zipline/data/history_loader.py", line 432, in _ensure_sliding_windows
    array = self._array(prefetch_dts, needed_assets, field)
  File "/zipline/zipline/data/history_loader.py", line 574, in _array
    assets,
  File "/zipline/zipline/data/dispatch_bar_reader.py", line 120, in load_raw_arrays
    for t in asset_types if sid_groups[t]}
  File "/zipline/zipline/data/dispatch_bar_reader.py", line 120, in <dictcomp>
    for t in asset_types if sid_groups[t]}
  File "/zipline/zipline/data/us_equity_pricing.py", line 629, in load_raw_arrays
    assets,
  File "/zipline/zipline/data/us_equity_pricing.py", line 619, in _compute_slices
    assets,
  File "zipline/data/_equities.pyx", line 44, in zipline.data._equities._compute_row_slices
  File "zipline/data/_equities.pyx", line 103, in zipline.data._equities._compute_row_slices
KeyError: 0

Here's the example algorithm:

import pandas as pd
from zipline import run_algorithm, api

def initialize(context):
    """called once at the start of the algorithm"""

    security = api.symbol('SPY')
    api.set_benchmark(security)

def handle_data(context, data):
    print('Date: {}'.format(context.get_datetime()))

    security = api.symbol('SPY')
    api.order_target_percent(security, 1.0)

if __name__ == '__main__':
    perf = run_algorithm(
        initialize=initialize,
        capital_base=2500000,
        bundle='custom-csv-bundle',
        handle_data=handle_data,
        data_frequency='minute',
        start=pd.Timestamp('02-2013', tz='UTC'),
        end=pd.Timestamp('05-2013', tz='UTC'))

    perf.to_pickle('result.pickle')

Running inside a container from the included Dockerfile with docker exec -it zipline python /projects/spy.py

Commenting out the set_benchmark does silence the error