当前位置: 首页 > 软件库 > Web3 > 开源货币/比特币 >

python-poloniex

Poloniex API wrapper for Python 2.7 & 3
授权协议 GPL-2.0 License
开发语言 Python
所属分类 Web3、 开源货币/比特币
软件类型 开源软件
地区 不详
投 递 者 高海阳
操作系统 跨平台
开源组织
适用人群 未知
 软件概览


Inspired by this wrapper written by '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.

Features:

  • Python 2.7 and 3+
  • Pypi
  • Travis
  • Websocket api support
  • Minimal amount of dependencies
  • Internal checks to reduce external api errors
  • Rate limiter to keep from going over call limits
  • Retries failed api calls during connection issues

Install:

pip install --upgrade poloniexapi

Usage:

See the wiki or help(poloniex) for more.

All api calls are done through an instance of poloniex.Poloniex. You can use the instance as follows:

# import this package
from poloniex import Poloniex

# make an instance of poloniex.Poloniex
polo = Poloniex()

# show the ticker
print(polo('returnTicker'))

Using the instances __call__ method (shown above) you can pass the command string as the first argument to make an api call. The poloniex.Poloniex class also has 'helper' methods for each command that will help 'sanitize' the commands arguments. For example, Poloniex.returnChartData('USDT_BTC', period=777) will raise PoloniexError("777 invalid candle period").

# using a 'helper' method
print(polo.returnChartData(currencyPair='BTC_LTC', period=900))
# bypassing 'helper'
print(polo(command='returnChartData', args={'currencyPair': 'BTC_LTC',
                                            'period': 900}))

Almost every api command can be called this way. This wrapper also checks that the command you pass to the command arg is a valid command to send to poloniex, this helps reduce api errors due to typos.

Private Commands:

To use the private api commands you first need an api key and secret (supplied by poloniex). When creating the instance of poloniex.Poloniex you can pass your api key and secret to the object like so:

import poloniex
polo = poloniex.Poloniex(key='your-Api-Key-Here-xxxx', secret='yourSecretKeyHere123456789')

# or this works
polo.key = 'your-Api-Key-Here-xxxx'
polo.secret = 'yourSecretKeyHere123456789'

# get your balances
balance = polo.returnBalances()
print("I have %s ETH!" % balance['ETH'])

# or use '__call__'
balance = polo('returnBalances')
print("I have %s BTC!" % balance['BTC'])

Trade History:

Poloniex has two api commands with the same name returnTradeHistory. To work around this without splitting up the commands or having to specify 'public' or 'private' we use the helper method Poloniex.marketTradeHist for public trade history and Poloniex.returnTradeHistory for private trades. If you try to bypass the helper method using Poloniex.__call__, it will call the private command.

Public trade history:

print(polo.marketTradeHist('BTC_ETH'))

Private trade history:

print(polo.returnTradeHistory('BTC_ETH'))

You can also not use the 'helper' methods at all and use poloniex.PoloniexBase which only has returnMarketHist and __call__ to make rest api calls.

Websocket Usage:

To connect to the websocket api use the PoloniexSocketed class like so:

import poloniex
import logging
from time import sleep

# helps show what is going on
logging.basicConfig()
poloniex.logger.setLevel(logging.DEBUG)

def on_volume(data):
    print(data)
# make instance
sock = 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 init
sleep(5)
# use the channel name str or id int to subscribe/unsubscribe
sock.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 websocket
sock.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

You can also subscribe and start the websocket thread when creating an instance of PoloniexSocketed by using the subscribe and start args:

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

More examples of how to use websocket push API can be found here.

  • 我对Python比较陌生,使用的是3.6.1版 我希望我可以用HMAC-SHA512为poloniex trading api编写python3.6.1post请求的代码,但是得到了这个错误。 我得到的答案大多来自python2,这不是我想要的。请帮忙。 我的python shell日志首先:{'command': 'returnBalances', 'nonce': 1492523610} co

  • Examples PHP wrapper by compcentral: http://pastebin.com/iuezwGRZ Python wrapper by oipminer: http://pastebin.com/fbkheaRb Node.js example of how to connect to the push API (requires autobahn): http:/

  • I now get the following error: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in () 1 for ticker in tickers: ----> 2 CryptoDat

 相关资料
  • 问题内容: 本来我想问这个问题,但是后来我发现它已经被想到了…… 在谷歌搜索中,我发现了扩展configparser的示例。以下适用于Python 3: 但不支持Python 2: 然后,我读了一些关于Python New Class vs. Old Class样式的信息(例如,在这里。现在我很想知道,我可以这样做: 但是,我不应该叫init吗?这在Python 2中是否等效: 问题答案: (不带

  • 主要内容:Python 3.x print()函数代替了print语句,Python 3.x 默认使用 UTF-8 编码,Python 3.x 除法运算,Python 3.x 异常,Python 3.x 八进制字面量表示,Python 3.x 不等于运算符,Python 3.x 输入差异,Python 3.x 数据类型Python 版本分为两大流派,一个是 Python 2.x 版本,另外一个是 Python 3.x 版本,Python 官方同时提供了对这两个版本的支持和维护。 2020 年 1

  • 我一直在尝试在我的Windows 64位笔记本电脑上安装Python包,因为我想使用的另一个包需要它。这个包还需要Python3.6,因此在我的计算机上,我有Python2.7和3.6,并使用和来区分两者。在执行时,一切都会正常安装,但在使用时,我会出现以下错误: 我尝试通过如下方式克隆存储库来解决此问题: 然后给出以下错误 然后,我通过在 简单地让库成为64位的库 但这再次给出了一个错误: 在这

  • 问题内容: 在Python 2中,一个常见的(旧的,遗留的)习惯用法是使用如下形式来连接长度不均匀的迭代器: 在Python 2中,它进行了扩展,以便 最长的 迭代器为返回列表的长度,如果一个比另一个短,则用填充。 在Python 3中,这是不同的。首先,您不能在位置1用作可调用对象的参数: 好的-我可以这样解决: 但是现在,我有一个不同的问题:返回 最短的 迭代器长度-不再用填充。 当我将Pyt

  • 问题内容: 我最近开始学习python3。 在 python 2 中,可以使用函数来分配列表元素。 如使用功能时在 python 3 中一样 为什么会这样呢? python为什么要进行此更改? 是恩赐还是祸根? 问题答案: Python 3 在很多地方使用了 迭代器 ,而 python 2 使用了 列表 。文档给出了详细的解释,包括对的更改。 优点是,如果您使用大范围的迭代器或映射, Python

  • In the following examples, input and output are distinguished by the presence or absence of prompts (">>> " and "... "): to repeat the example, you must type everything after the prompt, when the prom

  • Python还能够创建3d图表。 它涉及将子图添加到现有的二维图并将投影参数指定为3d。 绘制3D图 3dPlot由mpl_toolkits.mplot3d绘制,以将子图添加到现有的2d图。 from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt chart = plt.figure() chart3d = c

  • 这部分文档特别要求使用 Werkzeug 和 WSGI 的环境为 Python 3。 警告 Werkzeug 的 Python 3 支持目前只是实验性的。所以有问题欢迎反馈以帮助我们来 改善它。 WSGI 环境 Python 3 的 WSGI 环境和 Python 2 有一点不同。如果你使用高级的 API,Werkzeug 会帮你隐藏这些区别的大部分。Python 2 和 Pyhton 3 最主要