Python – Python And Trading
Trading crypto-currencies on Poloniex
Poloniex is a crypto-currency exchange, you can trade
80 currencies 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 lightly implement an algorithmic trading strategy on this exchange.
The most traded currencies are:
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 the Poloniex API on GitHub, this one is super effortless to use.
You can install the package like this:
Once it’s installed, you need to insert the adequate import in your code:
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 fresh 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 adequate check box, same for withdrawals.
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:
If you want to get market data for a ticker:
Now to send an order, it’s pretty elementary:
You’ll get an order object in JSON, resultingtrades is an array of trades generated by the order, the order can be packed straight away with numerous trades:
To manage your risks, you’ll need to retrieve your balances:
With this basic API you can code any algorithmic strategy in Python for Poloniex, you can attempt to predict the value of a crypto-currency using our previous tutorials for example.
Geyser market data from Quandl
In the previous articles, we loaded market data from CSV files, the drawback is that we’d need to redownload the CSV file every day to get latest data. Why not get them directly from the source ? Quandl is a website aggregating market data from various sources: Yahoo Finance, CBOE, LIFFE among others.
Fortunately for us, Quandl has an API in Python which let you access its data. You’d need very first to get your private API key here, here is a basic code snippet:
The quandl.get() method comebacks a Pandas data framework with the dates in the index and open/high/low/close data, this depends on the data source, you may get more information like volume etc.
So now you can directly work with that data framework, you can merge it with other data, apply some calculations and use it as an input in a machine learning algorithm. The main advantage is that you’ll always get the latest data, no need to redownload a file.