How to Make a Low-Frequency Quantitative Crypto Trading Bot

Mohak Malhotra
Coinmonks
7 min readAug 21, 2021

--

Source: Quora

Note: This article does not aim to provide any professional financial/investment advice. It is for educational purposes only.

I’ve had extensive experience creating algo trading bots for the spot and futures crypto market using my own strategies.

In most cases, creating a profitable trading bot is a complex and lengthy process of quantitative and mathematical analysis, finding chart patterns, and then accurately translating the trading strategy into an application.

In this article, I will show you one of the easiest ways to create your own trading bot using Freqtrade. Freqtrade (freqtrade.io )is an open-source trading software made in Python.

Quantitative trading actually does two things:

1. build a mathematical model (people)

2. according to the mathematical model, buy or sell at the right time (computer)

Before developing a bot, you first need a strategy(mathematical model). Creating a good strategy can be tricky. I use various tools to design my strategies, Neural Network Algorithms, linear/non-linear regression, Data Mining, and Fisher Linear Discriminant to name a few.

An illustration of the Fisher Linear Discriminant (Source: SemanticsScholar.org)

However, in this tutorial, I will make a very basic trader with a template strategy since I want to make this tutorial beginner-friendly.

Before we start

This approach has various limitations.

  • One major limitation is that you cannot develop an options and futures trading bot. The options market is huge and the leverage provided by various exchanges such as Binance and Deribit makes it quite a lucrative arena to play in.
  • You cannot open short positions with Freqtrade
  • if your strategy requires extremely high-frequency trading where latency is key, I do not recommend using Freqtrade

With that said, let’s begin this tutorial!

Step 1: Install Freqtrade using Docker

For those who don’t know, Docker is an open-source platform for building, deploying, and managing containerized applications. You can read the docs here: https://docs.docker.com/

In your target directory:

Once in ft_userdata:

Step 2: Bot configuration

Now that we’ve set up docker and the base repo, now we will enter the basic configuration for our bot. I’m going to go with a templated approach, however, these values would depend on your strategy.

Also, for this tutorial, we will stick to Binance exchange since it is one of the more popular ones.

Step 3: Implementing the Strategy

Now let's look at our basic config. Navigate to the config.js file as shown below:

In this config file, add the following under where it says dry_run: true

This lets us test and experiment in sandbox mode with a dry run wallet.

The main file we want to look at is in the “strategies” folder, sample_strategy.py.

The strategy workflow is quite simple. Based on the timeframe you provide in the configuration, you get OHLC dataframes which are then used as inputs for whichever indicators you’re using.

An example of BTCUSD 24hr dataframe

In our case, we configured a 15min timeframe, so that means we will get a new dataframe every 15 min from Binance.

Below I have explained the core components of a strategy config in Freqtrade:

  • minimal_roi: this specifies when to sell a position (after ‘x’ minutes have elapsed), based on a specified ROI for that corresponding time
  • stoploss: this is an overriding attribute that sells your position when you incur a specific loss (in percent). Ideally, for a risk-averse investor, this shouldn’t be less than -0.10 (-10%)
  • order_types: This object defines the types of orders you want to allow
  • def informative_pairs(self): Define additional, informative pair/interval combinations to be cached from the exchange. These pair/interval combinations are non-tradeable unless they are part of the whitelist as well.
  • def populate_indicators(self, dataframe: DataFrame, metadata: dict): This is where you instantiate your indicators. Some popular indicators are RSI, Bollinger Bands, MACD etc. You can create your own indicators, use 3rd party libraries or use the libraries Freqtrade provides by default (Talib and qtypylib)
Bollinger bands example (Source: Investopedia)
  • def populate_buy_trend(self, dataframe: DataFrame, metadata: dict): This is where you specify the buying indicators. This method is called every time you get a new timeframe. The buying trend/indicator is created based on your prescribed indicator metrics. An example is shown below. This function returns a dataframe. This dataframe has a “buy” column which has the value of 1 when the buy trend is set. This will trigger a buy.
  • def populate_sell_trend(self, dataframe: DataFrame, metadata: dict): This is where you specify the selling trend. When the condition specified here is met, a sell is executed. An example is shown below:

Step 4: Run in Sandbox mode

To test the bot in sandbox, we need to spin it up as a daemon in Docker so it is continuously running. Before doing so, make sure the strategy name is correct. To verify check the command section in docker-compose.yml.

Now run the following command in your terminal.

To check the logs,

When you check the logs, you should see the bot state as ‘RUNNING’ like shown below:

Step 5: Download historical OHLCV datasets

In your config file, specify the pairs you want to download data for in the “pair_whitelist” object. I will download data for these pairs:

Now in a new terminal window, enter the following command which will download 15min candle data for your specified pairs on which we can then run a backtest.

Step 6: Backtesting

To now run backtesting on our downloaded data, use the following command:

Our backtest shows the following results for each of our pairs:

Backtesting results

We made a total profit of 2.95% (29.15 BTC) in one month! 😂

Now obviously, this is the templated strategy and will probably perform poorly in live mode. But, as I said before, the process of creating a well-performing strategy is a very complex one and beyond the scope of this tutorial.

Closing remarks

I only scratched the surface with the capabilities of Freqtrade. You can further plot your results, do further enhancements through the REST API, and you can also use HyperOpt which uses machine learning to further find optimal parameters for your strategy, a process called hyperparameter optimization.

Source: Akira AI

Freqtrade is a great learning tool but it has its drawbacks. I personally don’t use Freqtrade or any other trading software for my algo traders because most of the time the need is for a high-frequency trader where efficiency is key.

If you’d like to build your own high-frequency algo trader or if you have any questions, feel free to reach out to me!

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--