https://www.flickr.com/photos/hyku/2038668839/in/photolist-p17PV-RMYXgt-479HdK-GzTV1-X4zyAN-fJTaK8-xAova-5231GR-7PVLV2-4yyB97-7bBQKy-q59Qjw-47dLVE-3S8V6x-oNgdBE-pPdor-fz5tqV-9g6265-5vkHZ-h8Qpe-dyMqw-6Zt9gp-egQGCp-bUNiA4-6raRwD-3szRS2-jGaC8-bCEZnm-7wB6mF-7xM1gi-8pH75W-dnYUpc-7xM2Zi-9Qck6U-cYnDrd-54DSyh-6oXscC-MEYE6-RuknVF-adYVEm-9S6K4M-QcMBXW-RiA3zt-RukiA8-7wB5X8-7wEUPm-8U79pB-MEYEV-7wB6Re-7ffUX7

Trading with Poloniex API in Python

Poloniex is a cryptocurrency exchange, you can trade ~80 cryptocurrencies against Bitcoin and a few others against Ethereum. I chose to trade on Poloniex because it supports a lot of currencies and the liquidity is usually very good, we can easily implement an algorithmic trading strategy on this exchange.

The most traded currencies are:
– Bitcoin (BTC)
– Ethereum (ETH)
– Monero (XMR)
– Tether (USDT)

The Setup

Fortunately for us, Poloniex provides an API to get market data, to get balances for each currency and to send buy/sell orders to the market. You can find a documentation here.

I found a Python wrapper for their API on GitHub, this one is super easy to use.
You can install the package like this:

pip install https://github.com/s4w3d0ff/python-poloniex/archive/v0.3.5.zip

Once it’s installed, you need to insert the appropriate import in your code:

from poloniex import Poloniex

Now you need to get an API key in order to be able to retrieve your account balances and to send orders to the market. If you just want to get market data you can skip that part.
Go to https://poloniex.com/apiKeys , click on Create new key, now you have the API key and you may need to get some email validation to see the secret key (which you also need). Check the options you want, if you want to trade via the API, just select the appropriate check box, same for withdrawals.

Using the API

In your code, you need to set up the connection so that you can get authenticated. You can just use the commented line if you only want to access the public API:

apiKey = "API_KEY"
secret = "SECRET_KEY"
polo = Poloniex(apiKey, secret)
# polo = Poloniex()

If you want to get market data for a ticker:

market_data = polo.returnTicker()['BTC_ETH']
bid = market_data["highestBid"]
ask = market_data["lowestAsk"]
volume = market_data["baseVolume"]

Now to send an order, it’s pretty simple:

pair = "BTC_ETH"
price = 0.1
order = polo.buy("BTC_ETH", price, 1)
order = polo.sell("BTC_ETH", price, 1)

You’ll get an order object in JSON, resultingtrades is an array of trades generated by the order, the order can be filled straight away with multiple trades:

{‘orderNumber’: ‘0000000’, ‘resultingTrades’: []}

To manage your risks, you’ll need to retrieve your balances:

balance = polo.returnBalances()
print("ETH="+str(balance ["ETH"]))

With this basic API you can code any algorithmic strategy in Python for Poloniex, you can try to predict the value of a cryptocurrency using our previous tutorials for example.