【问题标题】:Risk index out of range风险指数超出范围
【发布时间】:2019-03-30 06:28:25
【问题描述】:

我看到很多帖子处理相同的问题,但没有找到我的具体问题的答案。

我正在运行两个循环:-loop 1 获取一堆不同的股票名称并将它们添加到 yahoo Finance API URL 以获取它们的期权数据。因为每只股票都有多种期权,我正在运行循环 2,它在范围内循环 i(len(来自互联网的期权价格))以访问每只股票的每个期权的每个价格。 整个事情对大约 15 只股票运行良好,然后停止并显示错误消息“列表索引超出范围”

有人知道我做错了什么吗?提前致谢。

代码:

stock_list = ['TREE', 'TSLA', ...]

y = len(stock_list)
while True:
    for x in range(0,y):
        link =("https://query2.finance.yahoo.com/v7/finance/options/" + stock_list[x])
        try:
            optionchain = requests.get(link).json()
        except:
            optionchain = 0

        L = len(optionchain['optionChain']['result'][0]['options'][0]['calls'])
        while True:
            for i in range (L+1):
                try:
                    arbitrage =  optionchain['optionChain']['result'][0]['options'][0]['calls'][i]['lastPrice'] - (optionchain['optionChain']['result'][0]['options'][0]['calls'][i]['strike'] + optionchain['optionChain']['result'][0]['options'][0]['calls'][i]['ask'])
                except:
                    arbitrage = 0
                if arbitrage > 0:
                    print(stock_list[x])
                    print('pay: ')
                    print(100*optionchain['optionChain']['result'][0]['options'][0]['calls'][i]['ask'])
                    print('for a risk free profit of: ')
                    print(100*arbitrage)
                    print('info:')
                    print(optionchain['optionChain']['result'][0]['options'][0]['calls'][i])
                    print(' ')
                else:
                    print(stock_list[x], i, ' No arbitrage')
            break

一段时间后,shell返回:

LOXO 13  No arbitrage
LOXO 14  No arbitrage
Traceback (most recent call last):
  File "/Users/owner/Desktop/arbitrage option.py", line 18, in <module>
    L = len(optionchain['optionChain']['result'][0]['options'][0]['calls'])
IndexError: list index out of range

【问题讨论】:

    标签: python list indexing range out


    【解决方案1】:

    这不是一个完整的答案,但这里有一些解决问题的建议。

    注意错误出现在这一行:

     L = len(optionchain['optionChain']['result'][0]['options'][0]['calls'])
    

    这为您提供了关于哪个列表和哪个索引可能是问题的线索。

    现在,我看到该行中有两个地方可以访问列表:

    L = len(optionchain['optionChain']['result'][0]['options'][0]['calls'])
                                                 ^             ^
                                                 |             |
                                                 Here          Here
    

    所以,其中之一肯定是问题所在。但是是哪一个?

    要找出答案,请尝试将长线分成两条较短的线:

    part_1 = optionchain['optionChain']['result'][0]
    L = len(part_1['options'][0]['calls'])
    

    现在,当您运行它时,您将获得一个更具体的错误行号。

    例如,假设错误发生在第一行 (part_1 =)。

    为什么会发生错误?

    了解更多信息的一种方法是在访问列表之前打印出列表:

    list_1 = optionchain['optionChain']['result']
    print('list_1 = ', list_1)
    part_1 = list_1[0]
    L = len(part_1['options'][0]['calls'])
    

    现在,如果您看到 list_1 = [],这将为您提供有关错误发生原因的线索。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2011-01-17
      • 2015-01-09
      相关资源
      最近更新 更多