Start Freqtrade As Service On OpenHAB

This how to helps to configure Freqtrade as service on an OpenHAB system. The how to most likely also works on any Debian Linux on Raspberry.

Start Freqtrade As Service On OpenHAB

Start Freqtrade As Service On OpenHAB

Index:

Background

This how to bases on an OpenHAB openhabian installation that is similar to any Debian Linux installation. For the OpenHAB openhabian setup please consult the Setup A New OpenHAB how to. 

Furthermore, in this configuration we use a Raspberry Pi 4 8GB.

Finally, to Install Freqtrade On OpenHAB (most likely this works on any Debian and other Linux too) how to. This can be found here.

How to Start Freqtrade As Service On OpenHAB

Once you have successfully installed Freqtrade On OpenHAB or your Linux system, then most likely you like to run this as a service and thereby headless. To get Freqtrade running as a service, a couple of steps need to be undertaken, but I would consider them easy.

First of all, create a service. To do so you type the following into your command line:

sudo vi /etc/systemd/system/default.target.wants/freqtrade.service 

This will create a file in your system service (systemctl) the necessary file that is required to run Freqtrade as a service. Following this, you include into this file the following content (whilst considering the below bullet points explaining the content):

[Unit]
Description=Freqtrade Daemon
After=network.target
[Service]
# Set WorkingDirectory and ExecStart to your file paths accordingly
# NOTE: %h will be resolved to /home/
WorkingDirectory=/home/openhabian/freqtrade
ExecStart=/home/openhabian/freqtrade/.env/bin/freqtrade trade --strategy AmazingStrategy2022
Restart=on-failure
[Install]
WantedBy=default.target

Content of the freqtrade.service file:

  • WorkingDirectory = The directory in which you have installed your freqtrade
  • ExecStart = the path to the freqtrade bin file. trade says freqtrade should start trading and the –strategy tells the strategy to be used for trading. In this case AmazingStrategy2022

Once you created and updated based on your environment the file successfully, you save it and close the editor. Next step is to make it executable by entering:

sudo chmod +x /lib/systemd/system/freqtrade.service

Now we tell systemctl to enable the service by typing:

sudo systemctl enable freqtrade.service

We are nearly there! Lets start the service!! This you do by enter:

sudo systemctl start freqtrade.service

Anytime you can check whether the service is running as you expect it by simply fire the following command:

sudo systemctl status freqtrade.service

The result, if the service runs well, should look like something like this:

openhabian@OpenHAB:/etc/systemd/user $ sudo systemctl status freqtrade.service
● freqtrade.service - Freqtrade Daemon
Loaded: loaded (/lib/systemd/system/freqtrade.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-09-04 20:03:48 CEST; 7s ago
Main PID: 25601 (freqtrade)
Tasks: 5 (limit: 4915)
CPU: 7.004s
CGroup: /system.slice/freqtrade.service
└─25601 /home/openhabian/freqtrade/.env/bin/python3.9 /home/openhabian/freqtrade/.env/bin/freqtrade trade --strategy AmazingStrategy2022
Sep 04 20:03:48 OpenHAB systemd[1]: Started Freqtrade Daemon.

Additional Information

If the above does not work for you, then most likely one of the Paths for your Freqtrade installation is different. What you can do to fix it is to try to find exactly where your files and directories are located. Compare these with what you have written in the freqtrade.service file.

Alternatively, and if you run an openhabian, than you maybe like to deinstall your Freqtrade (please ensure you backup all relevant configuration files) and install it from new leveraging the Freqtrade On OpenHAB  How To.

Further information on configurations for Freqtrade and systemctl can be found here: