2

I installed Zipline and Jupyter using Docker: https://github.com/quantopian/zipline/blob/master/Dockerfile

I am now trying to run the following Zipline code under Jupyter

%%zipline --bundle quantopian-quantl --start 2008-1-1 --end 2012-1-1 -o strat.pickle

from zipline.api import symbol, order, record

def initialize(context):
    pass

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

The error message I am getting is:

**JSONDecodeError: Expecting value: line 1 column 1 (char 0)**

Here is the picture of the error: enter image description here

Again, this takes place when I try to run the program.

What could the problem be? Any help, hints or advice is ~greatly~ appreciated!

TIA

Addendum: I have also tried this as well: https://docs.google.com/document/d/1mvZO_JDirbJNXJfM0bTS9uMipHE5cfSGFj0sUpJIcsw/edit?usp=sharing

Casey Harrils
  • 2,793
  • 12
  • 52
  • 93
  • What would be interesting is the line in **your** code that causes the problem. Anyway, this error means that you try to decode an empty json, often cause by a non existing file (or more exactly by a current working directory that is different from what the user/developper expects). – Serge Ballesta Jul 09 '19 at 17:34
  • @SergeBallesta - thanks for the response. I used Docker for the Zipline (see description above) does this have anything to do with it? Thanks! – Casey Harrils Jul 09 '19 at 17:46
  • I am sorry but I do not use Docker nor Zipline. The error message is a hint for an empty file but I have no idea of the cause. – Serge Ballesta Jul 09 '19 at 17:53
  • "Followed the information here and got the problem solved: https://github.com/quantopian/zipline/issues/2480" was posted as a link only answer by the OP. – Trenton McKinney Nov 01 '19 at 17:05

3 Answers3

12

I know this question is sort of solved, but I tried what they are offering on the github issues and it did not help me, so I desided to show how I fixed my problem. Maybe it will help you.

The problem is in the benchmark.py file (and several others) of zipline where it tries to get data from iex and fails because their function changed.

I will show you what I did in order to get the sample code running:

(I assume you already have zipline installed and runnig ther apple buying sample code)

1.benchmark.py: look into your zipline folder in your computer (what you have downloaded or pip/conda installed). Open benchmark.py (find it first) and edit it, change the whole code there to this:

import numpy as np
import pandas as pd
import pandas_datareader.data as pd_reader
def get_benchmark_returns(symbol, first_date, last_date):
    data = pd_reader.DataReader(
        symbol,
        'yahoo',
        first_date,
        last_date
    )

    data = data['Close']

    data[pd.Timestamp('2008-12-15')] = np.nan
    data[pd.Timestamp('2009-08-11')] = np.nan
    data[pd.Timestamp('2012-02-02')] = np.nan

    data = data.fillna(method='ffill')

    return data.sort_index().tz_localize('UTC').pct_change(1).iloc[1:]

this code was taken from the answer of shlomikushchi github page about the issue. Here shlomikushchi switched the data source from iex to pandas, yahoo.

2.Next, open the file: loaders.py , also somewhere in zipline:

there is a row there in which they call the function: (look for this in the code)

data = get_benchmark_returns(symbol

change it to :

 data = get_benchmark_returns(symbol,first_date, last_date)

3.open trading.py , also somewhere in the zipline folder, after this line:

class SimulationParameters(object):
def __init__(self, start_session, end_session,
             trading_calendar,
             capital_base=DEFAULT_CAPITAL_BASE,
             emission_rate='daily',
             data_frequency='daily',
             arena='backtest'):

enter those lines:

start_session = pd.Timestamp(start_session).tz_localize(tz='US/Central')
    end_session = pd.Timestamp(end_session).tz_localize(tz='US/Central')

now it should work when you run the code in here:

https://www.zipline.io/beginner-tutorial.html

David Johns
  • 1,201
  • 5
  • 15
  • 34
0

step 3 should be:

start_session = pd.Timestamp(start_session).tz_convert('UTC')

end_session = pd.Timestamp(end_session).tz_convert('UTC')
Piyush Patel
  • 1,646
  • 1
  • 14
  • 26
0

Another solution -- mentioned on the Github issue -- is to sign-up for a free API token from IEX, and insert it into your zipline module's benchmark.py file. Change the request.get line to this:

r= requests.get( "https://cloud.iexapis.com/stable/stock/{}/chart/5y?chartCloseOnly=True&token={}".format(symbol, IEX_API_KEY) )

Needs to be a string, so surround your key with " " quotation marks

benmercerdev
  • 392
  • 4
  • 11