4

I want to create a reproducible example where the traded series and the benchmark are manually provided. This would make the life of people that are approaching to zipline incredibly easier. In fact, given the recent shut down of Yahoo!Finance API, even introductory examples with zipline are not going to work anymore since an HTTP error will be returned when trying to import the ^GSPC benchmark from Yahoo behind the scenes. As a consequence, nowadays there is not a single code snippet from the official tutorial that works AFAIK.

import pytz
from pandas_datareader import DataReader
from collections import OrderedDict
from zipline.algorithm import TradingAlgorithm
from zipline.api import order, record, symbol, set_benchmark
# Import data from yahoo
data = OrderedDict()
start_date = '01/01/2014'
end_date = '01/01/2017'
data['AAPL'] = DataReader('AAPL',
                          data_source='google',
                          start=start_date,
                          end=end_date)
data['SPY'] = DataReader('SPY',
                         data_source='google',
                         start=start_date,
                         end=end_date)
# panel.minor_axis is ['Open', 'High', 'Low', 'Close', 'Volume'].
panel = pd.Panel(data)
panel.major_axis = panel.major_axis.tz_localize(pytz.utc)

def initialize(context):
    set_benchmark(data['SPY'])

def handle_data(context, data):
    order(data['AAPL'], 10)
    record(AAPL=data.current(data['AAPL'], 'Close'))

algo_obj = TradingAlgorithm(initialize=initialize,
                            handle_data=handle_data,
                            capital_base=100000)
perf_manual = algo_obj.run(panel)

Returns: HTTPError: HTTP Error 404: Not Found

Question: how to make the strategy to work using AAPL as traded asset and SPY as benchmark? Constraint: AAPL and SPY must be manually provided as in the example.

MLguy
  • 1,776
  • 3
  • 15
  • 28
  • 1
    Zipline has switched to use Google Finance instead, but it's not in the latest release. You can reinstall from master https://github.com/quantopian/zipline – user748455 Jun 21 '17 at 12:06
  • Zipline's benchmark now defaults to SPY, the Google ticker for the S&P 500. – user748455 Jun 21 '17 at 12:14

2 Answers2

3

Disclaimer: I'm a maintainer of Zipline.

You can use the csvdir bundle to ingest csv files (tutorial here) and then make a call to set_benchmark() in your initialize() function. I'm also working a branch that allows zipline algorithms to run without a benchmark so even if you're not able to get benchmark data, your algorithm shouldn't crash.

freddiev4
  • 2,501
  • 2
  • 26
  • 46
  • 1
    Any update on this? I am facing similar issues and I could do away without benchmarks. Thanks. – zubinmehta Sep 21 '19 at 04:16
  • @freddiev4 after setting up like above, the index is still a tradable component. How do we specify so that index should not be a tradable component? – Punith Raj Oct 23 '21 at 13:32
-1

Replace zipline in your requirements.txt with this:

git+https://github.com/quantopian/zipline

Then run pip install -r requirements.txt

Lionel
  • 3,188
  • 5
  • 27
  • 40