Getting JSONDecodeError: Expecting value: line 1 c

2020-07-24 04:07发布

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

3条回答
老娘就宠你
2楼-- · 2020-07-24 04:14

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

查看更多
男人必须洒脱
3楼-- · 2020-07-24 04:26

step 3 should be:

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

end_session = pd.Timestamp(end_session).tz_convert('UTC')
查看更多
闹够了就滚
4楼-- · 2020-07-24 04:35

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

查看更多
登录 后发表回答