A trading bot which automatically decides when and how to trade cryptocurrencies, doesn’t it sounds nice? We just check it here and than to optimize our settings. Without looking on stock prices all day long. Basically that is possible with a trading bot, of course it’s not that simple and it’s not free money but if you spend some time on it to setup a good fitting strategy, it can be very lucrative. Freqtrade is an application that enables exactly that. It’s free to use and open source, it is well documented and has an active user base behind. In the following we start by installing Freqtrade as a Docker container, linking it to our previous created Traefik reverse proxy server and also setting up a dashboard as well as a Telegram bot.
Docker Container Initialization
Inside our shell we start by creating a folder where we will store all related bot files.
Now we download the docker-compose file from the official freqtrade repository
curl https://raw.githubusercontent.com/freqtrade/freqtrade/stable/docker-compose.yml -o docker-compose.yml
With it we can pull the freqtrade image
sudo docker-compose pull
To create the user directory structure we run
sudo docker-compose run --rm freqtrade create-userdir --userdir user_data
A new folder user_data is now available but we are still missing a configuration file, so let’s run the command
sudo docker-compose run --rm freqtrade new-config --config user_data/config.json
A bunch of setup question will be asked, depending if the bot should be trading live or as paper trades the steps will be described below.
For paper trading, so with a defined imaginary starting capital and trades on the live price, we answer the questions like this:
? 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 -1 for unlimite d open trades): 3 ? Time Override in configuration. ? Please insert your desired timeframe (e.g. 5m): 5m ? Please insert your display Currency (for reporting): USD ? 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, oth erwise best left untouched) 0.0.0.0 ? Insert api-server username freqtrader ? Insert api-server password api 2022-02-14 10:09:45,928 - freqtrade.commands.build_config_commands - INFO - Writing config to `user_data/config.json`. 2022-02-14 10:09:45,930 - freqtrade.commands.build_config_commands - INFO - Please make sure to check the configuration contents and adjust settings to your needs.
The stake currency is set to USDT, so all calculations will be based on that. The desired timeframe is on 5 minutes, so our strategy will be using 5 minute tick charts to make it’s decisions. And the exchange of choice is Binance. We have activated the user interface (API server) and a Telegram bot. We will have a closer look on them later. Be aware that the user interface password is just picked as an example, it’s saver to choose a strong one here. All configurations can be changed later on at the config.json file.
If we want to let our bot trade with our actual own money, we just have to change the first question and answer it with no instead of yes. We will be asked for a key and secret of the exchange. They are provided directly at the exchange. At Binance for example, we need to login into our Binance account and navigate to API-Security at the profile section.
The remaining conifg questions can be answered as like on the dry-run setup.
To properly link our Freqtrade container to Traefik a few changes on the docker-compose.yml file have to be made. If Traefik isn’t set up yet, this article describes the procedure.
--- version: '3' networks: web: external: true services: freqtrade: image: freqtradeorg/freqtrade:develop restart: unless-stopped volumes: - "./user_data:/freqtrade/user_data" networks: - web deploy: mode: replicated replicas: 1 placement: constraints: - node.role == manager labels: - traefik.http.routers.freqtrade.rule=Host(`freqtrade.your_domain.com`) - traefik.http.routers.freqtrade.tls=true - traefik.http.services.freqtrade.loadbalancer.server.port=8080 command: > trade --logfile /freqtrade/user_data/logs/freqtrade.log --db-url sqlite:////freqtrade/user_data/tradesv3_freqtrade.sqlite --config /freqtrade/user_data/config.json --strategy SampleStrategy
A network web with the communication outwards is mentioned. We already created that one on the Traefik setup. The most important part here is the labels section. Here we define the final URL were we can access the bot. With the tls label we say that the application has to be encrypted. And the port we are using for the application is on 8080.
We can now restart the Freqtrade container and check it by navigation to our defined URL if it is working fine.
sudo docker-compose up -d
If all steps were successfully and our bot is still running, we should be able to access the user interface by calling the defined URL, freqtrade.you_domain.com. On the top right corner is a button called Login, we have to click on it. Three parameters have to inserted
- Bot Name: We can choose whatever we want, it’s just an internal name.
- Username: The username defined in our config.json file, in that example it was Freqtrade.
- Password: We also defined it on the setup and it is stored in config.json, here it was api.
After a while the indicator light on the top bar will change from red to green and our interface is up and running.
Freqtrade provides a solution where we can even access our bot through Telegram. We can start/stop the bot and print out some statistics. First we are opening our config file inside the folder user_data.
On the bottom of the file we see a section called telegram. First of all it has to be enabled with the value true. Than two other parameters are necessary, a token and chat_id. Starting with the token, we go to Telegram and searching for a user called BotFather and adding him to our contacts. Afterwards BotFather will be displayed in our contacts and we need to click on him to see the chat history. Next we simply sending him the command /newbot. We will be asked to give the bot a name, a username and he answers with a token id. We copy it and insert it in the section token.
For the chat_id we need to add another contact at Telegram, called @get_id_bot. We are adding him again and starting a conversation with him. We receive right away a chat_id which we can again copy and paste it in our config.json file.
Let’s save the file changes and restarting the bot with:
sudo docker-compose up -d
We receive a status message that our bot is starting and running in Telegram.