Ally Invest API包装器
pyall的Python项目详细描述
pyally
的python3包装器Ally Bank的投资平台非常适合那些看重成熟网络/移动界面和低经纪费的小型投资者。我制作这个包装器是为了让我可以更容易地将平台与python集成,并减少对我的帐户进行人工监督的需要。
在设置了api密钥之后,pyally可以从一个简单的python请求中提供基本的ally代理事务函数。
支持的功能
- 股票买入/卖出/卖空/买入补仓订单
- 查询账户交易记录
- 代表账户持有量
- 查询账户持有量
- 支持的订单:
- 市场
- 停止
- 极限
- 停止限制
- 止损
- 乐器报价
- 期权交易
要求
- 请求OATHLIB
- matplotlib
安装
pip3 install pyally
下载包后,建议设置环境变量以存储ally api凭据。
登录Ally Invest,转到“特定帐户”页,单击“工具”->;“API”
将API令牌应用程序作为个人应用程序填写
在环境变量中输入api标记和机密
在~/.bashrc
中插入以下内容:
exportALLY_CONSUMER_KEY=XXXXXXXXXXXXXXXXXXXXXXXX exportALLY_CONSUMER_SECRET=XXXXXXXXXXXXXXXXXXXXX exportALLY_OATH_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXX exportALLY_OATH_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXX
文档
初始化
importallya=ally.Ally()
账户功能
交易历史记录
# Get transaction history in JSON dictionary form# - Optionally specify account, if not in set in# environment# - Optionally specify type of transaction, must be # "all", "bookkeeping", or "trade"# [default "all"]# - Optionally specify time window for transactions, must be in # "all", "today", "current_week", "current_month", "last_month"# [default "all"]trans_history=a.account_history(account=12345678,type="all",range="current_week")
经常账户持有量
# Get current holdings for an account in JSON dict format# Uses default account if not specified# Optionally dump json to filecurrent_holdings=a.get_holdings(account=12345678,outfile="./holdings.json")
持股饼图
# Create pie graph of asset allocations for account, using matplotib# Dumps to file ./graph.png by default# Specify account optionally# - specify regen=True to prevent outputting cached graph [default False]pie_file=a.holdings_chart(account=12345678,graph_file="./my_graph_file.png",regen=True)
实时报价
# Get quote:# Go to# https://www.ally.com/api/invest/documentation/market-ext-quotes-get-post/# to see available fields options# [defaults to None]quote=a.get_quote(symbols="SPY,ALLY",fields="ask,bid,vol")
仪器
权益
Equity("SPY")# Perfectly equivalent statementsInstrument('spy')# Perfectly equivalent statements
选项
Call(instrument=Equity("spy"),# Underlyingmaturity_date="2019-09-30",# Expiration datestrike=290# Strike)Put(instrument=Instrument("ALLY"),# Underlyingmaturity_date="2019-10-18",# Expiration datestrike=300# Strike)
订单
# Get current holdings for an account in JSON dict format# Uses default account if not specified# Optionally dump json to filecurrent_holdings=a.get_holdings(account=12345678,outfile="./holdings.json")
持股饼图
# Create pie graph of asset allocations for account, using matplotib# Dumps to file ./graph.png by default# Specify account optionally# - specify regen=True to prevent outputting cached graph [default False]pie_file=a.holdings_chart(account=12345678,graph_file="./my_graph_file.png",regen=True)
实时报价
# Get quote:# Go to# https://www.ally.com/api/invest/documentation/market-ext-quotes-get-post/# to see available fields options# [defaults to None]quote=a.get_quote(symbols="SPY,ALLY",fields="ask,bid,vol")
仪器
权益
Equity("SPY")# Perfectly equivalent statementsInstrument('spy')# Perfectly equivalent statements
选项
Call(instrument=Equity("spy"),# Underlyingmaturity_date="2019-09-30",# Expiration datestrike=290# Strike)Put(instrument=Instrument("ALLY"),# Underlyingmaturity_date="2019-10-18",# Expiration datestrike=300# Strike)
订单
Order( timespan, type, price, instrument, quantity)
market_buy=ally.order.Order(# Good for day ordertimespan=ally.order.Timespan('day'),# Buy order (to_open is True by defaul)type=ally.order.Buy(),# Market orderprice=ally.order.Market(),# Stock, symbol Finstrument=ally.instrument.Equity('f'),# 1 sharequantity=ally.order.Quantity(1))
时间信息(时间跨度)
#### TimespansTimespan('day')Timespan('gtc')Timespan('marketonclose')
类型
Buy()# Buy to open (default)Buy(to_open=False)# Buy to coverSell()# Sell short (default)Sell(to_open=False)# Sell to close
定价
Market()# Give me anythingLimit(69)# Execute at least as favorably as $69.00Stop(4.20)# Stop order at $4.20StopLimit(Stop(10),# Stop at $10.00Limit(9.50)# No worse than $9.50)StopLoss(isBuy=False,# Interpret stop as less than current pricepct=True,# Treat 'stop' as pctstop=5.0# Stop at 5% from highest achieved after order placed)StopLoss(isBuy=True,# Interpret stop as less than current pricepct=False,# Treat 'stop' as pctstop=5.0# Stop at $5.00 above lowest price)
数量
Quantity(15)# In shares or lots, for an option
提交订单
exec_status=a.submit_order(# specify order created, see aboveorder=,# Can dry-run using preview=True, defaults to True# Must specify preview=False to actually executepreview=True,# Like always, if not specified in environment, use a specific accountaccount=12345678)
作者
贡献
如果你喜欢这个项目或者认为它可以改进,请联系我。我尽力用高质量的代码编写,但有时说起来容易做起来难。任何对细节感兴趣的人都欢迎参与。