简体   繁体   English

Python 将字典作为参数传递

[英]Python pass a dictionary as an argument

I'm trying to pass a dictionary opt_chain 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
}

to the function api get_options_chain到函数 api get_options_chain

But I get the following error但我收到以下错误

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

Here is the code that I run (excerpt):这是我运行的代码(摘录):

 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)

This is Python v3.6.9这是 Python v3.6.9

Any help is greatly appreciated,任何帮助是极大的赞赏,

Thank you谢谢

It looks like your code is perfect here, but the way you're calling the get_options_chain function of the TDClient library is incorrect.看起来您的代码在这里很完美,但是您调用 TDClient 库的get_options_chain函数的方式不正确。

Let's walk through this... The error you're receiving here is TypeError: get_options_chain() got an unexpected keyword argument 'args_dictionary' .让我们来看看这个……您在这里收到的错误是TypeError: get_options_chain() got an unexpected keyword argument 'args_dictionary' This indicates that the function is not expecting a parameter called args_dictionary which you are setting in your parameters here (args_dictionary = opt_chain) .这表明该函数不期望您在此处的参数中设置名为args_dictionary的参数(args_dictionary = opt_chain)

After looking at the library, I believe the fix is just passing in the dictionary without defining it as an argument, like so:查看库后,我相信修复只是传入字典而不将其定义为参数,如下所示:

option_chains = td_client.get_options_chain(opt_chain)

If you look at the source code for the function , it does not require you to strictly define the argument name, and only takes in one argument anyways.如果您查看function源代码,它不需要您严格定义参数名称,并且无论如何只接受一个参数。

In your code, option_chains = td_client.get_options_chain(args_dictionary = opt_chain) here args_dictionary was not expected in the API call, it should be 'option_chain'在您的代码中,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) option_chains = td_client.get_options_chain(option_chain=opt_chain)

The issue is, td_client.get_options_chain method is not taking an argument called args_dictionary .问题是, td_client.get_options_chain方法没有采用名为args_dictionary的参数。 It is expecting an argument called option_chain .它期待一个名为option_chain的参数。 You can either pass opt_chain as keyword argument like td_client.get_options_chain(option_chain = opt_chain) or you can directly pass opt_chain like td_client.get_options_chain(opt_chain) since it is the first argument td_client.get_options_chain is expecting.您可以将opt_chain作为关键字参数传递,如td_client.get_options_chain(option_chain = opt_chain) ,也可以直接传递opt_chaintd_client.get_options_chain(opt_chain)因为它是td_client.get_options_chain期望的第一个参数。

    option_chains = td_client.get_options_chain(option_chain = opt_chain)

OR要么

    option_chains = td_client.get_options_chain(opt_chain)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM