How to Make Your Own Cryptocurrency Trading Bot
The heart of the cryptocurrency market is always beating. The price action is continuous and volatile. Rest days are absent for the cryptocurrency trader. This is why many people have looked to automate their trading. An automated algorithmic trading bot never rests.
Algorithmic trading is nothing new. Trading bots have been used for decades, buying and selling on the global stock & commodity exchanges. Sophisticated participants in those markets have made much profit with strategies from market-making, arbitrage, high frequency trading and everything between and beyond.
We can use the knowledge, techniques and technologies from the automated trading systems created for the traditional markets and use it to create an automated trading bot for cryptocurrency exchanges.
So here is the guide about how to build your own cryptocurrency / Bitcoin trading bot.
Table of Contents
This guide will go into detail about how to setup your own cryptocurrency trading bot. This means that it is quite long. If you haven’t got time to read it all now, here is a 8 step quick start, bullet-pointed version of the process of setting up a trading bot. If you have any questions about any of the steps then decide to invest your time where your money is and read the rest of the guide.
Step 1: Decide whether you want to use open source software or proprietary software.
– Using Open Source Software will require some knowledge of coding
– Proprietary software will always remain a ‘black box’
Step 2: Installing and Configuring the Software for your trading bot
The proprietary cryptocurrency trading bots all come with documentation on their respective websites.
If you have decided that you want full adaptability and control over your bot then you will need to download the open-source software on your computer.
Step 3: Setting up the accounts at the cryptocurrency exchanges that are suitable for automated trading
Sign up to an exchange. There are a lot of ‘Automated-trading’ friendly exchanges. I will be using BitMex in this guide.
Step 4: Set Up the API keys – the way that your trading bot and the exchange ‘talk’ to each other
The API keys from each exchange you plan to trade on will need to be acquired and saved in a file that is accessible to your cryptocurrency trading bot.
Step 5: Set up a Websocket Stream for your data
REST API calls are used to access price data ‘snap shots’ and to place/cancel orders on the cryptocurrency exchange. However, for a live-feed of market data your trading bot will need to subscribe to the exchange’s Websocket.
Step 6: Write the code for your Bot
There is no point re-inventing the wheel. Most problems have already been solved, so it’s a matter of putting the pieces together. Many open-source solutions and wrappers are available on GitHub and BitMex themselves have a trading bot template suggested on their website.
Step 7: Research and choose your indicators and strategies
Study the indicators that you want to feed to your cryptocurrency trading bot. Use these indicators to produce a strategy for your automated trading system to follow. This is a process that you will constantly want to review, up-date and develop to maintain the profitability of your cryptocurrency trading bot.
Step 8: How to keep the crypto trading bot running even while your computer is turned off
Set up a Docker account and host your automated trading bot on a cloud computing service like AWS, this way your automated trading program is constantly running.
Algorithmic trading and trading bots are not new. They have been used for decades, buying and selling on the global stock & commodity exchanges. Anything from market-making bots, arbitrage bots, high frequency trading bots and everything between and beyond. Traditional markets have highly sophisticated participants, their trading bots exploit the best minds and technology that money can buy.
The benefits of algorithmic trading
– Instant and accurate order placement
– Timing of trade can be set and exact
– Reducing risk of errors over manually placing trades
– Strategies can be easily backtested (run the bot over historical data)
– Trades executed at best available price
– Transaction cost savings
– Better timed trades when cross-market conditions are met
A cryptocurrency bot can automate trading algorithms, implementing a vast array of strategies including using technical analysis, cross-referencing live data from correlated markets/Twitter sentiment, market making based on micro-market structure or strategies lead by machine learning and AI. The possibilities are endless, though in this guide we are focussing on the basic steps to get your crypto bot up and running.
So you have decided you want to try your hand and make a crypto trading bot yourself? Great.
What You Need To Download:
You will need Python to follow this guide. Python has become the de-facto lingua franca of finance in recent years. Automated trading systems can be written in any language but you can’t go too wrong with Python. If you do not already have Python install, I would recommend to get an Anaconda distribution as it will come ready with the extra commonly used packages.
The PyCharm IDE is a very user-friendly application where you can run and edit your Python files (and other file types). PyCharm can create a virtual environment for us and we can also use the command line within PyCharm.
There are many cryptocurrency exchanges. In this guide I have used the BitMex exchange.
Market Maker Template
This template has all the code you need to begin an automated trading operation on BitMex. It’s just a case of adding your API details and running the program and away you go.
After you have your trading bot running on your own computer, you may wish to transfer it to a managed server in the cloud to ensure 24/7 up-time for your bot. For that process you will need to sign up to the following services:
Microsoft Azure is a cloud computing solution that will allow you to keep your bot trading 24/7. By hosting the code for the bot on Microsoft’s Azure server’s, the bot can stay engaged in the market even when your home PC is turned off and you are asleep.
Docker is like a container for which to put your bot in. By putting the bot inside a Docker container, we will be able to transfer the bot from your home PC to a cloud based server (Microsoft Azure) easier.
A guide on how to get your bot hosted on Azure is worth its own guide. I will look to make that in the very near future.
Open up your BitMex account. In this guide we are using the test-net version of BitMex (paper trading) https://testnet.bitmex.com.
Once inside, click on ‘Account’ in the menu bar (1) and then API Keys (2) in the side bar. You will then need to give your API keys a name and scroll the ‘Key Permissions’ to ORDER. Then select ‘Create API Key’. Now save these two numbers for we will need them later.
Now open up PyCharm and select ‘Create New Project’. In this example I called mine ‘bitmex-testbot’.
PyCharm should have now set you up in a virtual environment with the Project ‘bitmex-testbot’ visible top left. Now open the Terminal within PyCharm.
To install the BitMex Market Maker template bot, you need to type in:
pip install bitmex-market-maker
The full documentation can be found at https://github.com/BitMEX/sample-market-maker.
After the Market Maker is installed, run the following command in the Terminal:
You should now have a file called ‘settings.py’, you’ll need to open this file and put your API keys saved from earlier between the inverted commas shown here:
Note: You may have had an issue with the last step, as I did. I found the following fix worked for me https://github.com/BitMEX/sample-market-maker/issues/180.
If you have your BitMex account open in a browser, you might see orders being automatically submitted to the exchange.
If everything has gone to plan, you should be seeing a screen like the one below.
What is Market Making?
Market making is an ‘execution strategy’, or a ‘sell-side method’. The market maker’s aim is to capture the spread between the bid and the ask price, to both buy and sell, receiving the difference between the buys and the sells as the profit.
A market maker provides liquidity to a market, which is why exchanges like market makers on their exchange and reward them, usually with lower trading fees as structured in the maker/taker fee policy. BitMEX actually pays a 0.025% rebate to market makers when they take the other side of a market order.
Generally, a market maker will provide liquidity to both the buy and the sell side of a market. The market maker is the counterparty to directional traders that wish to buy or sell at the latest offered prices.
The market maker must refresh their bids and offers as prices move and as market takers take liquify and put the market maker in a directional-risk position.
A market maker strategy is exactly what the Foreign Exchange shops at the airport do. They offer an exchange (a trade) between multiple currencies and their quoted rates. The spread is normally quite large (about %10 is not uncommon). If they do not charge a commission, the spread is where they are taking their profit.
The Foreign Exchange shop is providing the liquidity to the customer who is able to walk over and trade a currency with them as and when the customer pleases and at the amount the customer pleases (within reason).
The market maker is there to accept the trade and make a profit on the difference that they will be able to sell at in a different trade or in a different location.
As the Foreign Exchange shops at the airport, market makers control how much inventory in available for purchase in the marketplace. Also like the Foreign Exchange shops, there is more than one market maker present, so customers can go elsewhere if one particular shop is not offering any inventory.
Market maker’s goal: To profit from the bid/ask spread.
Ideally, a market maker will be flat in the market (ie. no directional position) at any given moment.
However, it’s not possible to be flat at every moment and the market can move (or gap significantly) against you whilst you are holding inventory. In a sense, non-arbitraged market making is a bit like “picking up pennies in front of a steamroller”. Non-arbitraged market makers and making small amounts on each trade but if they are holding a position and the market moves against them then then losses will most likely exceed many multiples of winning trades.
Inventory management is critical for risk management.
The BitMEX sample trading bot is a very simple trading system that will more than likely lose money in live trading. However the frame work given in the code is a good starting place for making your own strategy.
Is This Process Everything There is to Making a Cryptocurrency Trading Bot?
Well there you go, that is the guide to getting a cryptocurrency trading bot off of the ground. Is it everything you need to know to make a successful (profitable) bot? Most probably, no.
You will want to experiment with different strategies, backtesting and refining the execution of your trading bot.
By following all the steps in this guide it is possible to go from almost zero knowledge, to having a fully functioning automated trading system up and running.
Of course, the strategy is a boiler-plate strategy and is therefore not a premium one. The next fun challenge is to:
- Find a trading strategy that has Alpha
- Edit your trading bot’s code to execute this new strategy
- Host bot in the cloud to let it trade 24/7
We will look into this in a guide coming soon….