用于tdameritrade api的python库
tdlink的Python项目详细描述
t链接
用于tdameritrade api的python库
所有日期和时间都以UTC为单位。
认证
注意:在使用此库之前,请确保从tdameritrade获取代码或刷新令牌。
按照以下步骤获取代码:
- 转到tdameritrade开发者:https://developer.tdameritrade.com/
- 登录并转到“我的应用程序”
- 创建应用程序。使用http://localhost作为回调URL。
- 转到此URL:https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=http://localhost&client_id=[YourAppName]@AMER.OAUTHAP
- 输入您的TD凭证并给予预许可
- 登录后,您将被重定向到此表单的url https://localhost/?code=[code]
- 复制[代码]部分。将其作为参数添加到tdameritrade类。
- TDAmeritrade类现在应该可以工作了!
注意:您获得的代码仅在有限的时间内有效。如果过期,请再次执行步骤以获取新代码。
安装
来自PYPI
pip install tdlink
源代码
pip install --upgrade git+git://github.com/ysriram1/tdlink
示例
连接到API
fromtdlinkimportTDlinktd=TDlink(app_key='TRADING123',redirect_uri='http://localhost',code=code,# follow instructions to get the codereturn_raw_response=False# will return formatted df if False, else returns HTTP response)
获取刷新和访问令牌
td.refresh_token# returns refresh tokentd.access_token# returns the access token
注意:您可以保存刷新令牌以创建未来会话,而不是使用代码。
从api获取数据
当前报价
```Python td.获取当前报价(symbol='qq', to_return=['askprice','bidprice','totalvolume']) ```注意:这也用于选项的价格。
历史数据
td.get_historical_prices(symbol='QQQ',period_type='day',# 'day'period=2,# day: 1, 2, 3, 4, 5, 10* month: 1*, 2, 3, 6 year: 1*, 2, 3, 5, 10, 15, 20 ytd: 1*frequency_type='minute',# day: minute* month: daily, weekly* year: daily, weekly, monthly* ytd: daily, weekly*frequency=5,# minute: 1*, 5, 10, 15, 30 daily: 1* weekly: 1* monthly: 1*start_date='06/03/2019',# mm/dd/yyyystart_time=None,# hh:mm:ssend_date='06/04/2019',end_time=None,extended_hours=True,return_df=True# if False, returns a dictionary instead)
选项链
td.get_options_chain(symbol='QQQ',# symbol of underlying securitystrike=None,# mention a specific strikefrom_date='01/01/2020',# only expirations after this date (mm/dd/yyyy)from_time=None,to_date='01/30/2020',# only expirations before this dateto_time=None,expiry_month='ALL',# option expiry month ALL or JANkind='OTM',# ITM, NTM, OTM, SAK (Strikes Above Market), ALL etc.include_quotes=False,# if FALSE only returns the option names (use get_current_quote() to get value)contract_type='CALL',# CALL, PUTstrikes=5,# number of strikes above or below at-the-money pricestrategy='SINGLE',return_df=True# if False, returns a dictionary instead)
指数的上扬者
td.get_movers_for_index(symbol='DJI',# has to be an index DJI, SPX.X etcdirection=None,# 'up' or 'down'. if None, returns bothchange=None# 'value' or 'percent'. if None, defaults to percentage)
支持
td.get_options_chain(symbol='QQQ',# symbol of underlying securitystrike=None,# mention a specific strikefrom_date='01/01/2020',# only expirations after this date (mm/dd/yyyy)from_time=None,to_date='01/30/2020',# only expirations before this dateto_time=None,expiry_month='ALL',# option expiry month ALL or JANkind='OTM',# ITM, NTM, OTM, SAK (Strikes Above Market), ALL etc.include_quotes=False,# if FALSE only returns the option names (use get_current_quote() to get value)contract_type='CALL',# CALL, PUTstrikes=5,# number of strikes above or below at-the-money pricestrategy='SINGLE',return_df=True# if False, returns a dictionary instead)
指数的上扬者
td.get_movers_for_index(symbol='DJI',# has to be an index DJI, SPX.X etcdirection=None,# 'up' or 'down'. if None, returns bothchange=None# 'value' or 'percent'. if None, defaults to percentage)
支持
如果您有任何问题、建议或改进意见,请发电子邮件给我ysriram@umich.edu。