Create a freqtrade bot development environment

Freqtrade is a crypto-currency algorithmic trading software developed in Python 3.7+ and supported on Windows, macOS and GNU/Linux.

This software is for educational purposes only. Do not risk money which you are afraid to lose. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR TRADING RESULTS.

Always start by running a trading bot in dry-run and do not engage money before you understand how it works and what profit/loss you should expect.

We strongly recommend you to have basic coding skills and Python knowledge. Do not hesitate to read the source code and understand the mechanisms of this bot, algorithms and techniques implemented in it.

The purpose of this article is mainly to create a development environment for freqtrade and not to publish the interface on the Internet. Therefore I will disable SELinux because the files are installed locally and the access management with SELinux will be catastrophic. In a future article, we will see how to install globally freqtrade and to use a reverse proxy to access its interface.

Information and requirements

These elements are to be taken into consideration to follow this article:

Update the system

sudo dnf -y update

Disable SELinux

If you are wondering why, read the introduction above.

sudo sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
sudo touch /.autorelabel
sudo reboot

Install Python 3.9 and tools

sudo dnf -y install python39 python39-pip tar gcc make git

Download and compile TA-Lib

TA-Lib is widely used by trading software developers requiring to perform technical analysis of financial market data.

curl -LOsSf http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib
./configure
make
sudo make install

Load the dynamic library

Freqtrade uses the dynamic library libta_lib.so.0 when a strategy uses the TA library. This library is installed in /usr/local/lib/libta_lib.so.0. So you have to inform the loader and dynamic linker (ld.so) where this library is located and reload it.

echo "/usr/local/lib" | sudo tee -a /etc/ld.so.conf
sudo ldconfig

Install freqtrade

When cloning the repository, the default working branch has the name develop. This branch contains all last features (can be considered as relatively stable, thanks to automated tests). The stable branch contains the code of the last release (done usually once per month on an approximately one week old snapshot of the develop branch to prevent packaging bugs, so potentially it’s more stable).

Clone the repository

git clone https://github.com/freqtrade/freqtrade.git ~/freqtrade
cd ~/freqtrade

Launch the install script

This script will install required dependencies and create the Python virtual environment for us. As shown below, answer yes via y to all questions.

./setup.sh --install
...

----------------------------
Please use 'freqtrade new-config -c config.json' to generate a new configuration file.
----------------------------
----------------------------
Run the bot !
----------------------------
You can now use the bot by executing 'source .env/bin/activate; freqtrade <subcommand>'.
You can see the list of available bot sub-commands by executing 'source .env/bin/activate; freqtrade --help'.
You verify that freqtrade is installed successfully by running 'source .env/bin/activate; freqtrade --version'.

Configure the bot

Enable the Python virtual environment

Enabling the virtual environment allows you to have an isolated environment from your local installation. This allows, among other things, to avoid rotting your system. Once the virtual environment is activated, you will see this: (env).

source .env/bin/activate
(.env) command -v python
/home/adrien/freqtrade/.env/bin/python

As you can see, Python executable is located at ~/freqtrade/.env/bin/python

Create the configuration file

The creation of the configuration file is done in an interactive way: freqtrade asks you questions and you answer them. The answers given below are given as an example but correspond to a basic configuration. Tune the answers according to your needs. In addition, the description of the different questions can be found here.

(.env) freqtrade new-config --config user_data/config.json
? Do you want to enable Dry-run (simulated trades)? Yes
? Please insert your stake currency: USDT
? Please insert your stake amount (Number or 'unlimited'): 100
? Please insert max_open_trades (Integer or 'unlimited'): 3
? Tim Have the strategy define timeframe.
? Please insert your display Currency (for reporting): EUR
? Select exchange binance
? Do you want to enable Telegram? No
? Do you want to enable the Rest API (includes FreqUI)? Yes
? Insert Api server Listen Address (0.0.0.0 for docker, otherwise best left untouched) 0.0.0.0
? Insert api-server username freqtrader
? Insert api-server password KiCsyDPLH8Gpa!?kfo4!B$ie9BoecLtADfDQ5E&Y
2022-03-07 22:57:28,821 - freqtrade.commands.build_config_commands - INFO - Writing config to `user_data/config.json`.
2022-03-07 22:57:28,821 - freqtrade.commands.build_config_commands - INFO - Please make sure to check the configuration contents and adjust settings to your needs.

Install the freqtrade UI

Freqtrade UI is a frontend for freqtrade. Through this interface, you can see the performance of your bot, your balance, force the sale of an asset…

(.env) freqtrade install-ui
2022-03-07 22:58:12,634 - freqtrade.commands.deploy_commands - INFO - Removing UI directory content.
2022-03-07 22:58:12,635 - freqtrade.commands.deploy_commands - INFO - Downloading https://github.com/freqtrade/frequi/releases/download/0.2.6/freqUI.zip

Once the configuration file is created and the UI installed, you can exit the virtual environment.

(.env) deactivate

Setup the systemd service

Copy the service

sudo cp freqtrade.service /etc/systemd/system/

Edit the service

We have just deposited the service in /etc/systemd/system/freqtrade.service, edit it to look something like this.

[Unit]
Description=Freqtrade Daemon
After=network.target

[Service]
WorkingDirectory=/home/adrien/freqtrade
ExecStart=/home/adrien/freqtrade/.env/bin/freqtrade trade --logfile user_data/logs/freqtrade.log --db-url sqlite:///user_data/tradesv3.sqlite --config user_data/config.json --strategy InformativeSample
Restart=on-failure

[Install]
WantedBy=default.target

Reload the systemd daemon

sudo systemctl daemon-reload

Start the bot

Before launching our bot, we need to have a strategy. There are many examples and people sharing them on the Internet. In the freqtrade service file we wrote --strategy InformativeSample, this means that freqtrade will look for a strategy called InformativeSample. This strategy is provided as an example and is not especially efficient.

Retrieve the strategy

curl -Lo ~/freqtrade/user_data/strategies/InformativeSample.py -sSf https://raw.githubusercontent.com/freqtrade/freqtrade-strategies/master/user_data/strategies/InformativeSample.py

Enable and start freqtrade service

sudo systemctl enable --now freqtrade

Modify authorized ports

sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

You can now access freqtrade via its IP address (or domain name if, like me, you have mapped it in the /etc/hosts file). Remember that the listening port of the internal Python HTTP server is 8080.

Configure Telegram

It’s possible to configure Telegram to receive notification on bot’s actions. You just have to create a bot as I did in this article, and that’s almost done!

Let’s suppose that you have the following token.

1390824186:AAE-a336pYNwMqH41PjxJR-UP0xk_stWtWU

And the following chat id.

939838712

You have to insert the previous information inside ~/freqtrade/user_data/config.json. The section should look like this.

"telegram": {
   "enabled": true,
   "token": "1390824186:AAE-a336pYNwMqH41PjxJR-UP0xk_stWtWU",
   "chat_id": "939838712"
}

As soon as your bot is starting, buying or selling assets, you will receive this information on Telegram. But you can also ask it for things like daily performance, profit, your balance…

This article was the first of a serie related to freqtrade. There are a lot of concepts to understand, I recommend you to read the official documentation to learn more.