【问题标题】:Python pass a dictionary as an argumentPython将字典作为参数传递
【发布时间】:2021-09-27 15:28:16
【问题描述】:

我正在尝试将字典 opt_chain 作为参数传递

opt_chain = {
  'symbol': 'AAPL',
  'contractType': 'CALL',
  'optionType': 'S',
  'fromDate': '2021-07-18',
  'afterDate': '2021-07-19',
  'strikeCount': 4,
  'includeQuotes': True,
  'range': 'ITM',
  'strategy': 'ANALYTICAL',
  'volatility': 29.0
}

到函数apiget_options_chain

但我收到以下错误

    option_chains = td_client.get_options_chain(args_dictionary = opt_chain)
TypeError: get_options_chain() got an unexpected keyword argument 'args_dictionary'

这是我运行的代码(摘录):

opt_chain = {
  'symbol': 'AAPL',
  'contractType': 'CALL',
  'optionType': 'S',
  'fromDate': '2021-07-18',
  'afterDate': '2021-07-19',
  'strikeCount': 4,
  'includeQuotes': True,
  'range': 'ITM',
  'strategy': 'ANALYTICAL',
  'volatility': 29.0
}

option_chains = td_client.get_options_chain(args_dictionary = opt_chain)
pprint.pprint(option_chains)

这是 Python v3.6.9

非常感谢任何帮助,

谢谢

【问题讨论】:

    标签: python-3.x dictionary arguments


    【解决方案1】:

    您的代码在这里看起来很完美,但是您调用 TDClient 库的get_options_chain 函数的方式不正确。

    让我们来看看这个... 您在这里收到的错误是 TypeError: get_options_chain() got an unexpected keyword argument 'args_dictionary'。这表明该函数不需要一个名为 args_dictionary 的参数,您在此处的参数中设置该参数 (args_dictionary = opt_chain)。

    查看库后,我相信修复只是传入字典而不将其定义为参数,如下所示:

    option_chains = td_client.get_options_chain(opt_chain)
    

    如果您查看source code for the function,它不需要您严格定义参数名称,并且无论如何只接受一个参数。

    【讨论】:

      【解决方案2】:

      在您的代码中, option_chains = td_client.get_options_chain(args_dictionary = opt_chain) 这里的 args_dictionary 不是 API 调用中预期的,它应该是 'option_chain'

      option_chains = td_client.get_options_chain(option_chain=opt_chain)

      【讨论】:

        【解决方案3】:

        问题是,td_client.get_options_chain 方法没有采用名为 args_dictionary 的参数。它期待一个名为option_chain 的参数。您可以将opt_chain 作为关键字参数传递,如td_client.get_options_chain(option_chain = opt_chain),也可以直接传递opt_chain,如td_client.get_options_chain(opt_chain),因为它是td_client.get_options_chain 所期望的第一个参数。

            option_chains = td_client.get_options_chain(option_chain = opt_chain)
        

        或

            option_chains = td_client.get_options_chain(opt_chain)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-26
          • 2021-12-14
          • 2019-11-18
          • 2011-08-01
          • 2019-09-14
          • 2016-10-10
          • 2017-03-21
          相关资源
          最近更新 更多