Python 将字典作为参数传递

Python pass a dictionary as an argument

我正在尝试传递字典 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
}

函数 api get_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

非常感谢任何帮助,

谢谢

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

让我们来看看这个... 您在此处收到的错误是 TypeError: get_options_chain() got an unexpected keyword argument 'args_dictionary'。这表明该函数不期望您在此处 (args_dictionary = opt_chain).

中设置的名为 args_dictionary 的参数

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

option_chains = td_client.get_options_chain(opt_chain)

如果你看一下source code for the function,它不要求你严格定义参数名称,反正只接受一个参数。

在你的代码中, 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)

问题是,td_client.get_options_chain 方法没有采用名为 args_dictionary 的参数。它期待一个名为 option_chain 的参数。您可以像 td_client.get_options_chain(option_chain = opt_chain) 一样将 opt_chain 作为关键字参数传递,也可以像 td_client.get_options_chain(opt_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)