【问题标题】:run_algorithm() fails because it cannot fetch data from the internet source (403 Forbidden)run_algorithm() 失败,因为它无法从 Internet 源获取数据(403 Forbidden)
【发布时间】:2020-01-08 21:58:34
【问题描述】:

我尝试关注ziplineQuickstart

from zipline.api import order_target, record, symbol

def initialize(context):
    context.i = 0
    context.asset = symbol('AAPL')


def handle_data(context, data):
    # Skip first 300 days to get full windows
    context.i += 1
    if context.i < 300:
        return

    # Compute averages
    # data.history() has to be called with the same params
    # from above and returns a pandas dataframe.
    short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
    long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()

    # Trading logic
    if short_mavg > long_mavg:
        # order_target orders as many shares as needed to
        # achieve the desired number of shares.
        order_target(context.asset, 100)
    elif short_mavg < long_mavg:
        order_target(context.asset, 0)

    # Save values for later inspection
    record(AAPL=data.current(context.asset, 'price'),
           short_mavg=short_mavg,
           long_mavg=long_mavg)

但它失败了:

$ zipline run -f dual_moving_average.py --start 2014-1-1 --end 2018-1-1 -o dma.pickle
[2020-01-06 20:31:38.548002] INFO: Loader: Cache at /home/jupyter/.zipline/data/SPY_benchmark.csv does not have data from 2014-01-02 00:00:00+00:00 to 2017-12-29 00:00:00+00:00.

[2020-01-06 20:31:38.548265] INFO: Loader: Downloading benchmark data for 'SPY' from 2013-12-31 00:00:00+00:00 to 2017-12-29 00:00:00+00:00
Traceback (most recent call last):
  File "/home/jupyter/env/bin/zipline", line 8, in <module>
    sys.exit(main())
  File "/home/jupyter/env/lib/python3.5/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/jupyter/env/lib/python3.5/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/jupyter/env/lib/python3.5/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/jupyter/env/lib/python3.5/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/jupyter/env/lib/python3.5/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/__main__.py", line 107, in _
    return f(*args, **kwargs)
  File "/home/jupyter/env/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/__main__.py", line 276, in run
    blotter=blotter,
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/utils/run_algo.py", line 159, in _run
    trading_days=trading_calendar.schedule[start:end].index,
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/finance/trading.py", line 103, in __init__
    self.bm_symbol,
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/data/loader.py", line 149, in load_market_data
    environ,
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/data/loader.py", line 216, in ensure_benchmark_data
    data = get_benchmark_returns(symbol)
  File "/home/jupyter/env/lib/python3.5/site-packages/zipline/data/benchmarks.py", line 35, in get_benchmark_returns
    data = r.json()
  File "/home/jupyter/env/lib/python3.5/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我认为这是因为它试图从api.iextrading.com/1.0/stock/... 获取一些数据,但此端点当前返回403 Forbidden 并危及zipline,即:

requests.get('https://api.iextrading.com/1.0/stock/{}/chart/5y'.format('AAPL'))

我可能可以从其他互联网资源中提取相同的数据...zipline 期望的格式是什么?

【问题讨论】:

    标签: python python-3.x zipline


    【解决方案1】:

    这个问题是由于 IEX 交易 api 的变化,现在需要一个 API 密钥(帐户)来查询它。我希望将来可以在文档中对其进行更正和澄清,并且从外部 API 检索数据会更加容易。

    目前,您有两种选择:

    1。更改基准代码:

    /home/jupyter/env/lib/python3.5/site-packages/zipline/data/benchmarks.py
    

    改变get_benchmark_returns的定义

    ...
    import os 
    ...
    def get_benchmark_returns(symbol):
         The data is provided by IEX (https://iextrading.com/), and we can
         get up to 5 years worth of data.
         """
        IEX_TOKEN = os.environ.setdefault("IEX_PUBLIC_TOKEN", "YOUR_API_KEY") 
        r = requests.get('https://cloud.iexapis.com/stable/stock/{}/chart/5y?token={}'.format(symbol, IEX_TOKEN))
    
        data = r.json()
    
        df = pd.DataFrame(data)
        ...
    

    如果你想修改 IEX_PUBLIC_TOKEN,你可以像 Quandl 一样在外面设置这个环境变量。

    也就是说,基准数据的管理方式可以改进:

    • 它应该类似于默认的数据摄取(zipline 摄取...)。
    • 它应该保留旧数据并只检索丢失的数据。您会看到,系统每天都会再次从 IEX 下载所有基准数据,而不是 .SPX 的新每日数据。

    2。通过将基准设置为零来删除基准(请检查here

    您的交易算法的基准比较很重要,因为它对于评估算法的性能很有用,因此我不建议将其设置为零。

    另请注意,quandl 的默认捆绑包不再受支持(数据仅限于 2018 年 4 月)。

    如果您遇到任何问题,请告诉我,

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多