[Tutorial] Create your own trading bot strategy with Freqtrade

Introduction

In our last article, we saw how to create our first trading bot with Freqtrade, but his results were not satisfactory. We will now discover how to build our own customized strategy.

Prerequisite

  • A full installation of Freqtrade, see our article.
  • Basics of technical analysis.
  • Basics in the python computer language.

Creation of a new strategy on Freqtrade

The strategies usable by Freqtrade are stored in the freqtrade/user_data/strategies folder and are python (.py) files.

To start, move to a terminal in the /freqtrade folder and activate its virtual environment with this command :

source .env/bin/activate;

Then create a new strategy called MyStrategy.

freqtrade new-strategy --strategy MyStrategy

After executing this command, a MyStrategy.py file was created in /freqtrade/user_data/strategies. It is this file that we will customize throughout this article. The final file is available at the end of the article.

Configuration of the strategy on Freqtrade

Configuring strategy settings

Open the MyStrategy.py file with a text editor. In the class, we will specify parameters that our strategy will have to respect.

We start by defining the minimal_roi, ROI meaning return on investment, which allows us to define to sell our assets at any moment of the strategy if a percentage of profit is reached independently from the rest of the strategy. For this tutorial, I have chosen a minimal_roi that at any moment of the strategy will engage a sale if the profits exceed 50%. Furthermore, if the trade has been open for more than 40 minutes, then a sale will be issued if the profits exceed 20%.

minimal_roi = {
  "40": 0.2,
  "0": 0.5
}

We are also going to set up a stop loss, to avoid having too big losses.

stoploss = -0.15

With this stop loss, if one of my trades registers a loss of more than 15%, a sell order will be issued.

We will then choose a unit of time for our bot this way (here, 5 minutes).

timeframe = '5m'
Addition of technical indicators to the strategy

To add technical indicators to the strategy, we will modify the populate_indicators function. In this tutorial, I will use the Relative Strength Index, a technical indicator that we have already discussed many times.

dataframe['rsi'] = ta.RSI(dataframe)

After adding the indicator (you can of course add more), I end this function with a dataframe return.

return dataframe
Addition of conditions of buy/sell

To add a buy condition, we need to modify the function named populate_buy_trend and then add our condition inside as well as a return of the dataframe like this.

dataframe.loc[
(
(dataframe['rsi'] < 30)
),
'buy'] = 1

return dataframe

In this example, I decide that a buy order is sent if the ROI is less than 30.

It works the same way for the sell condition. I decide that a sell order is issued when the ROI is greater than 70.

dataframe.loc[
(
(dataframe['rsi'] > 70)
),
'sell'] = 1

return dataframe

There you go! All that remains is to test our strategy. If you don’t know how to do it, I redirect you to our tutorial on how to use Freqtrade, where we presented how it works in general. This strategy is not intended to be implemented with your funds. It is very simple, and its main interest is to show you how to build your own strategy with Freqtrade. You now have all the cards in hand to create and customize your trading bots with Freqtrade! The next step is to increase the results of your strategy with an incredible freqtrade feature, Hyperopt. Happy trading!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.