Building a Basic Crypto Trading Bot in Python

Lakshmi Ajay
Geek Culture
Published in
6 min readFeb 9, 2022

Applying technical analysis to build a trading bot in 3 simple steps with ~125 lines of code

Source: Unsplash

Cryptocurrency is a digital asset or a digital form of money that is built on the blockchain technology. Similar to stock exchanges there are crypto exchanges through which the cryptocurrency can be traded.

In this article we will build a simple bot framework that will automate the cryptocurrency trading by using some of the popularly used technical indicators like relative strength index (RSI) and MACD.

Though this article focuses on cryptocurrency, similar approach can be applied on other trading instruments (stocks, forex etc) as well. Couple of reasons for using crypto in this article, the cryptocurrency market operates 24/7 and hence the bot can be kept running. Also, the cryptocurrencies are normally very volatile, which makes it an ideal candidate to try out the trading algorithms.

DISCLAIMER: The goal of this article is to share some of the learnings on building a basic bot framework to perform algorithmic trading. This article is a programming tutorial only and is not intended to be any kind of investment advice.

BUILDING A TRADING BOT — 3 STEP PROCESS

STEP 1: Connect to an exchange to fetch the live data

STEP 2: Apply a trading algorithm on the data

STEP 3: Execute the trade

Flow diagram for the trading bot (Image by Author)

There are two instances where we need to interact with the exchange. Firstly, to fetch the data and run the trading algorithm on it. Secondly, to place the buy/sell orders to trade the assets.

In this article there are two different packages used to interact with the exchanges. The ccxt package is used to fetch the data from the exchange. The trading algorithm is run on this data and when a trade order has to be placed I have used the APIs provided by an exchange with which I have a trading account (WazirX).

In case you have an account in any of the other exchanges that provide public APIs to fetch the data and also to place orders, then the same can be used.

STEP 1: Fetch the live data

CCXT (CryptoCurrency eXchange Trading Library) is a library to connect and trade with cryptocurrency exchanges and payment processing services worldwide.

We will be using this library and the Binance exchange to fetch the price data of the cryptos. Any other exchange or api can also be used for this purpose.

Refer here to view the latest manual on ccxt which has the details on the interfaces that it provides.

STEP 2: Apply the trading strategy

Here we will apply a very basic trading strategy. Please note that the goal of this article is only to get a know-how on creating a trading bot and the focus is not on building a strong trading algorithm.

The strategy applied here uses two technical indicators, namely the RSI and the MACD indicators. To know more about these indicators, refer to this article: Basics of Technical Analysis

Trading Strategy

BUY → MACD crossover with BUY signal & RSI in oversold region

SELL → MACD crossover with SELL signal & RSI in overbought region

WAIT → Otherwise

Illustration of the trading strategy (Image by Author)

Trade can be executed based on the signal received from the trading algorithm. A sequential BUY-SELL loop is implemented. Hence once a BUY order is placed, it will not place another BUY order until the SELL order is executed.

STEP 3: Execute the trade

Here are a few pre-requisites in order to execute a trade programmatically:

  1. The trading exchange needs to provide the APIs to place the order.
  2. We need to hold an account with such a trading exchange.
  3. We need to create an API key and secret to access the trading account.
  4. There should be sufficient funds in the trading account to execute the orders.

As an example I have used WazirX, which is one of the crypto trading exchanges. They provide APIs to perform the trade and since I have an account in WazirX, I am using this account for demo purpose.

Any crypto exchange provider who provides the APIs can be used, the overall flow will remain the same, only the APIs might differ slightly.

Here is the documentation reference to the APIs provided: WazirX API reference

Wrapping it together

Putting together all the steps explained above gives us a trading bot. Here we have used Bitcoin against the USDT as an example.

That’s it, with less than 125 lines of code, we are ready with a trading bot and good to go!!

Please refer to the git repo for the complete code reference.

Profit or Loss?

Given that we have not set any minimum profit and the buy/sell decision is based only on the indicators, this is a very basic approach to trade. The profit or loss will depend on how the particular crypto is trading on that day.

Here is a sample output after running the bot for 24 hours on bitcoin traded against the USDT. Investment amount was set to $10 and 1-minute candles were used in this test.

Details of the trades executed in the 24-hour test run
Sample summary of running the bot for 24 hours on Bitcoin (Image by Author)

Given the simplicity of the trading algorithm’s logic, the results may not be very profitable. In many of the actual trading bots, thousands of such low profit trades are done to gain a substantial cumulative profit.

Constraints

The trading strategy shown here is a very naive algorithm to generate the buy and sell signals. It can be further improvised by:

  • Adding more technical indicators
  • Adding candlestick patterns to take better decisions
  • Along with the indicators, set a minimum profit% to place a sell order and also set a stop-loss
  • Currently the bot will be in one of the two states only — buy or sell. Can change it to place multiple orders to average on the profit/loss.

Code Reference

Refer to the following github repo to access the complete source code — Simple Trading Bot

Conclusion

This framework will help you get started in building your own trading bot. Feel free to explore and improvise on it to make the algorithm stronger.

Given the other external factors beyond the scope of technical analysis that impact a stock/crypto, the results from this app may not always end in positive figures.

Please use this article only for learning & experimenting. If bots can make us billionaires, there would be many such billionaires by now :-)

--

--