支持websocket的python 2.7和3的poloniex api包装器

poloniexapi的Python项目详细描述


pythonlicencereleaserelease build
mastermaster builddevdev build

灵感来源于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

用法:

有关详细信息,请参见wikihelp(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实例时,还可以使用subscribestart参数订阅并启动websocket线程:

sock=poloniex.PoloniexSocketed(subscribe={'24hvolume':print},start=True)

有关如何使用websocket推送api的更多示例可以在here中找到。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java集合属性值   java字符串==运算符是否比较引用?   java是否存在过太多的ListView或适配器?   json获取java中类路径中下载的文件   我可以用java代码解决数据库并发问题吗?   在多个线程中使用forEach()或使用forEach()和lambdas进行java集合迭代   java输出JFrame中的整个循环   java禁用高度详细的日志记录   java在没有特定属性的对象中访问模型的值   java Smack xmpp建立连接   处理过时域对象引起的并发问题的java策略(Grails/GORM/Hibernate)   java从ObservableList中提取元素   使用图像进行java相似图像搜索   java ListView和图像:我快疯了   在Java中,如何从毫秒时间戳中提取一天的周期?   java我需要这样的设计,但我面临两个问题   java如何获取JGoodies FormLayout中的单元格大小   Spring引导生成的java War文件未部署到Weblogic 12c