Package: poloniexapi
Inspired bythiswrapper written by 'oipminer'I (s4w3d0ff) am not affiliated with, nor paid byPoloniex. If you wish to contribute to the repository please readCONTRIBUTING.md. All and any help is appreciated.Features:Python 2.7 and 3.5+PypiTravisWebsocket api supportMinimal amount of dependenciesInternal checks to reduce external api errorsRate limiter to keep from going over call limitsRetries failed api calls during connection issuesInstall:pip install --upgrade poloniexapiUsage:See thewikiorhelp(poloniex)for more.All api calls are done through an instance ofpoloniex.Poloniex. You can use the instance as follows:# import this packagefrompolonieximportPoloniex# make an instance of poloniex.Poloniexpolo=Poloniex()# show the tickerprint(polo('returnTicker'))Using the instances__call__method (shown above) you can pass the command string as the first argument to make an api call. Thepoloniex.Poloniexclass also has 'helper' methods for each command that will help 'sanitize' the commands arguments. For example,Poloniex.returnChartData('USDT_BTC', period=777)will raisePoloniexError("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}))Almost every api command can be called this way. This wrapper also checks that the command you pass to thecommandarg 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 ofpoloniex.Poloniexyou can pass your api key and secret to the object like so: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%sETH!"%balance['ETH'])# or use '__call__'balance=polo('returnBalances')print("I have%sBTC!"%balance['BTC'])Trade History:Poloniex has two api commands with the same namereturnTradeHistory. To work around this without splitting up the commands or having to specify 'public' or 'private' we use the helper methodPoloniex.marketTradeHistfor public trade history andPoloniex.returnTradeHistoryfor private trades. If you try to bypass the helper method usingPoloniex.__call__, it will call the private command.Publictrade history:print(polo.marketTradeHist('BTC_ETH'))Privatetrade history:print(polo.returnTradeHistory('BTC_ETH'))You can also not use the 'helper' methods at all and usepoloniex.PoloniexBasewhich only hasreturnMarketHistand__call__to make rest api calls.Websocket Usage:To connect to the websocket api use thePoloniexSocketedclass like so: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/joinedYou can also subscribe and start the websocket thread when creating an instance ofPoloniexSocketedby using thesubscribeandstartargs:sock=poloniex.PoloniexSocketed(subscribe={'24hvolume':print},start=True)More examples of how to use websocket push API can be foundhere.