Getting JSONDecodeError: Expecting value: line 1 column 1 (char 0) with Python + Zipline + Docker + Jupyter

强颜欢笑 提交于 2020-08-03 06:00:54

问题


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:

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


回答1:


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




回答2:


step 3 should be:

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

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



回答3:


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



来源:https://stackoverflow.com/questions/56957791/getting-jsondecodeerror-expecting-value-line-1-column-1-char-0-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!