quantopian / zipline

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

Getting benckmark data via IEX API does not work anymore #2480

Closed MarikoKujo closed 4 years ago

MarikoKujo commented 5 years ago

Zipline uses IEX API to get benchmark data in benchmarks.py:

def get_benchmark_returns(symbol):
    """
    Get a Series of benchmark returns from IEX associated with `symbol`.
    Default is `SPY`.

    Parameters
    ----------
    symbol : str
        Benchmark symbol for which we're getting the returns.

    The data is provided by IEX (https://iextrading.com/), and we can
    get up to 5 years worth of data.
    """
    r = requests.get(
        'https://api.iextrading.com/1.0/stock/{}/chart/5y'.format(symbol)
    )
    data = r.json()

    df = pd.DataFrame(data)

    df.index = pd.DatetimeIndex(df['date'])
    df = df['close']

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

However, according to the IEX FAQ page, the chart api was already removed on June 15, 2019. Currently, using this api to try to download any stock data such as SPY will return nothing but an HTTP 403 error. The functions of deprecated APIs are now transferred to their new API, IEX Cloud, which requires a unique token per user in any request. Any idea how to fix this issue in the long run?

Sigmares commented 4 years ago

The triple quote is a block comment, meaning that everything between will be disabled. """ anything between the triple quotes will be interpreted as a comment, not executed as code. print("This will not be printed") """ print("This will be printed")

ah I see, got it. I've managed to set up zipline with the help of your book and the following the trend forum. After hours of looking through GitHub, Following the Trend forum and your book trading evolved, I've finally managed to run the 'First Zipline Backtest' code.

It ran the codes somewhat smooth and produced the graphs. However, now I have this notification below the kernel. (In a red box)

"C:\Users\name\Anaconda3\envs\zip35\lib\site-packages\empyrical\stats.py:704: RuntimeWarning: invalid value encountered in true_divide out=out, C:\Users\name\Anaconda3\envs\zip35\lib\site-packages\empyrical\stats.py:790: RuntimeWarning: invalid value encountered in true_divide np.divide(average_annual_return, annualized_downside_risk, out=out)"

Is this a big problem? and will it cause me further problems down the line?

If anyone could help me understand why it's happening and if there's a solution for it, I'd be greatful.

Thank you.

Hi @k-s30011, did you find a solution? I have exactly the same issue.

marlowequart commented 4 years ago

I am also not able to get this working following the suggestions above. It seems like it is timing out trying to divide, possibly because the benchmark is just a bunch of zeros? I am just trying to run a basic file.

I am getting this same error: RuntimeWarning: invalid value encountered in true_divide np.divide(average_annual_return, annualized_downside_risk, out=out). Does anyone know where this divide is being called from? I don't see it in benchmarks.py or loader.py

I am wondering if it might be due to the version of numpy and other packages I am using. Using numpy version 1.14.6, python 3.5.6

marlowequart commented 4 years ago

@Sigmares

Hi @k-s30011, did you find a solution? I have exactly the same issue.

I have been able to narrow down this issue to zipline trying to calculate the sharpe ratio and sorting ratio. I commented out the divide in the stats.py file and set the sharp and sorting ratio to a fixed value and it caused this error to go away. I believe the problem is that when we implemented the fix above for the API issue, the benchmark data that is loaded is zeros and this is causing the divide function issues. I don't think this is a very good solution, but I cannot find where in the zippline algorithm it is calling the stats functions so this will have to do for now.

Does anyone know where in the zipline package the functions for sharpe and sorting are called? it seems like it would be better to comment those out in zipline rather than the stats module.

PatrickTunni commented 4 years ago

@k-s30011 @marlowequart Looks like I'm late to the game, and a newbie but I've gotten as far as repairing the benchmarks and loader files, got my first backtest to work, albeit with the divide by zero error.

Have you been successful with this onward in Andreas book? I am getting the idea that passing zero's into the code is causing it to throw those errors.

Thanks for the feedback.

cemal95 commented 4 years ago

@PatrickTunni You are better off using your own data, for example if you have your own SPY data you can use it for the benchmark returns. Andreas's is book is pretty nice in terms of programming for beginners and is probably top 5 for algo trading for beginners. Also quantopian lectures slides are really usefull and a good place to start.

If you dont have SPY data, I have data from 1990 to 2020 of data, check my github