支持websocket的python 2.7和3的poloniex api包装器
poloniexapi的Python项目详细描述
灵感来源于this由“oipminer”编写的包装器
I (s4w3d0ff) am not affiliated with, nor paid by Poloniex. If you wish to contribute to the repository please read CONTRIBUTING.md. All and any help is appreciated.
功能:
- [X]python 2.7和3.5+
- [X]PYPI
- [X]特拉维斯
- [X]WebSocket API支持
- [X]最小依赖量
- [X]内部检查以减少外部API错误
- [X]防止超过通话限制的速率限制器
- [X]在连接问题期间重试失败的API调用
安装:
pip install --upgrade poloniexapi
用法:
有关详细信息,请参见wiki或help(poloniex)
。
所有api调用都是通过poloniex.Poloniex
的实例完成的。您可以使用以下实例:
# import this packagefrompolonieximportPoloniex# make an instance of poloniex.Poloniexpolo=Poloniex()# show the tickerprint(polo('returnTicker'))
使用instances__call__
方法(如上所示),您可以将命令字符串作为第一个参数传递以进行api调用。poloniex.Poloniex
类还为每个命令提供了帮助“清理”commands参数的“helper”方法。例如,Poloniex.returnChartData('USDT_BTC', period=777)
将提升PoloniexError("777 invalid candle period")
。
# using a 'helper' methodprint(polo.returnChartData(currencyPair='BTC_LTC',period=900))# bypassing 'helper'print(polo(command='returnChartData',args={'currencyPair':'BTC_LTC','period':900}))
几乎每个api命令都可以这样调用。此包装器还检查传递给command
参数的命令是否是发送给poloniex的有效命令,这有助于减少由于输入错误而导致的api错误。
专用命令:
要使用私有api命令,首先需要api密钥和密钥(由poloniex提供)。当创建poloniex.Poloniex
的实例时,您可以像这样将api密钥和机密传递给对象:
importpoloniexpolo=poloniex.Poloniex(key='your-Api-Key-Here-xxxx',secret='yourSecretKeyHere123456789')# or this workspolo.key='your-Api-Key-Here-xxxx'polo.secret='yourSecretKeyHere123456789'# get your balancesbalance=polo.returnBalances()print("I have %s ETH!"%balance['ETH'])# or use '__call__'balance=polo('returnBalances')print("I have %s BTC!"%balance['BTC'])
交易历史:
poloniex有两个同名的api命令returnTradeHistory
。要解决此问题而不拆分命令或指定“public”或“private”,我们使用助手方法Poloniex.marketTradeHist
用于公共交易历史,使用Poloniex.returnTradeHistory
用于私人交易。如果试图使用Poloniex.__call__
绕过helper方法,它将调用private命令。
public交易记录:
print(polo.marketTradeHist('BTC_ETH'))
private交易记录:
print(polo.returnTradeHistory('BTC_ETH'))
您也不能使用'helper'方法,也不能使用只有returnMarketHist
和__call__
的poloniex.PoloniexBase
来进行rest api调用。
WebSocket用法:
要连接到WebSocket API,请使用PoloniexSocketed
类,如下所示:
importpolonieximportloggingfromtimeimportsleep# helps show what is going onlogging.basicConfig()poloniex.logger.setLevel(logging.DEBUG)defon_volume(data):print(data)# make instancesock=poloniex.PoloniexSocketed()# start the websocket thread and subscribe to '24hvolume' setting the callback to 'on_volume'sock.startws(subscribe={'24hvolume':on_volume})# give the socket some time to initsleep(5)# use the channel name str or id int to subscribe/unsubscribesock.subscribe(chan='ticker',callback=print)sleep(1)# unsub from ticker using id (str name can be use as well)sock.unsubscribe(1002)sleep(4)# stop websocketsock.stopws()
INFO:poloniex:Websocket thread started
DEBUG:poloniex:Subscribed to 24hvolume
[1010]
DEBUG:poloniex:Subscribed to ticker
[241, '86.59997298', '86.68262835', '85.69590501', '0.01882321', '22205.56419338', '258.30264061', 0, '87.31843098', '82.81638725']
...
...
[254, '5.89427014', '6.14542299', '5.92000026', '-0.03420118', '9978.11197201', '1649.83975863', 0, '6.19642428', '5.74631502']
DEBUG:poloniex:Unsubscribed to ticker
[1010]
[1010]
[1010]
['2019-06-07 04:16', 2331, {'BTC': '2182.115', 'ETH': '490.635', 'XMR': '368.983', 'USDT': '7751402.061', 'USDC': '5273463.730'}]
DEBUG:poloniex:Websocket Closed
INFO:poloniex:Websocket thread stopped/joined
在创建PoloniexSocketed
实例时,还可以使用subscribe
和start
参数订阅并启动websocket线程:
sock=poloniex.PoloniexSocketed(subscribe={'24hvolume':print},start=True)
有关如何使用websocket推送api的更多示例可以在here中找到。