Beginner’s Guide to Algo Trading Part II: Generate and Visualize buy/sell signals with Relative Strength Index, Bollinger Bands and Money Flow Index in Python

Sarp Nalcin
5 min readApr 27, 2022

--

Photo by Walkator on Unsplash

This is Part II of the “Beginner’s Guide to Algo Trading” where we build the system to generate buy/sell signals of the Trading Strategy in Part I and show results with visuals. We will use Relative Strength Index (RSI) as momentum indicator, Bollinger Bands (BB) as volatility indicator and Money Flow Index (MFI) as volume indicator

If you still have not read Part I, I highly suggest you to check it out below. For setting up your Python environment, extracting the dataset and the code flow it will be very useful

TL;DR

We use RSI, BB, MFI to generate trading signals for a long/short Trading Strategy for BTC/USDT and ETH/USDT pairs, visualize them on price chart and perform backtest to see the result with metrics such as cumulative return and win ratio

Check out my Github page below to jump in the code:

The structure of this article is as follows: We will generate buy/sell signals with each Technical Indicator individually for BTC/USDT & ETH/USDT and analyze how frequent the signals are. Then, we will combine them all (RSI, BB, MFI), analyze the changes in signal frequency and plot the buy/sell signals on a graph

Relative Strength Index (RSI)

RSI is one of the most widely used Technical Indicators. It measures velocity and the magnitude of price movements. RSI ranges from 0 to 100 where above 70 means overbought and below 30 is oversold. Most common look-back period for RSI is 14. For more details, you can visit the link:

https://www.tradingview.com/support/solutions/43000502338-relative-strength-index-rsi/

RSI — Signal Generation (look-back period = 14)

If RSI is smaller than 30, then it is a BUY signal. If RSI is greater than 70 then it is a SELL signal. You can detailed explanation of the code in comments

RSI — Analysis of Results

When we run the code, we see that RSI outputs signal for BTC/USDT at every 32.9 hours and for ETH/USDT at every 30.7 hours. Depending on the length of your dataset, you may get different signal frequency. Also, because the look-back period is 14, RSI does not generate signals for the first 14 records (It is not the case that your code does not work but it is how it is supposed to be)

Bollinger Bands (BB)

Bollinger Bands is a price volatility indicator developed by John Bollinger in 1980s. It measures the standard deviation of price around Simple Moving Average. Check the link below for more details of Bollinger Bands: https://www.tradingview.com/support/solutions/43000501840-bollinger-bands-bb/

BB — Signal Generation (look-back period = 20, std. dev. = 2)

If price goes below Lower Bollinger Band, then it is a BUY signal. If price goes above Upper Bollinger Band, then it is a SELL signal. In other words, when the asset is oversold, it is a sign to BUY and when it is overbought it is a sign to SELL

BB — Analysis of Results

BB outputs signal for BTC/USDT at every 33.5 hours and for ETH/USDT at every 32.3 hours. Compared to RSI, BB is more sensitive to changes in both BTC/USDT and ETH/USDT but not drastically. If you prefer less sensitivity, you can always increase the standard deviation in BB

Money Flow Index (MFI)

One of the most famous volume indicators that uses price and volume together. It counts the number of times that there are positive money flow and negative money flow over the specified period. For more detail, check the link: https://www.tradingview.com/support/solutions/43000502348-money-flow-mfi/

MFI — Signal Generation (look-back period = 20)

If MFI is smaller than 30, then it is a BUY signal. If MFI is greater than 70 then it is a SELL signal

MFI — Analysis of Results

MFI outputs signals for BTC/USDT at every 17.9 hours and for ETH/USDT at every 17.0 hours. Among the three of the indicators, MFI is the most sensitive one

Trading Strategy: RSI, BB and MFI combined

Now, we will combine RSI, BB and MFI to generate signals which is the foundation of our Trading Strategy. We will expect to get signal from each indicator at the same time to generate a final buy/sell signal. Because our Trading Strategy is a long/short strategy, buy orders in a row and sell orders in a row are not allowed (To get to know the strategies that allows this, you can check “Pyramid Trading”). In the code below, you should use ‘Trade_Signal’ column to get buy/sell signals (‘Trade_Signal_Raw’ column is NOT for this Trading Strategy but used as a step)

RSI, BB and MFI combined — Analysis of Results

The plot below shows that there are 5buy (green triangles) and 4sell (red triangles) signals generated for BTC. This means that our Trading Strategy’s signal frequency is around 1 month (Again, this depends on your dataset)

The code to generate the BTC/USDT graph above can be found here:

For ETH/USDT, the signal frequency of Trading Strategy is lower. There is only 4 buy and 4 sell signals. The plot of ETH and the code to generate are as follows:

To find out the performance of our Trading Strategy for BTC and ETH, we need to do a Backtest. You can check out Part III of this guide below to learn how to do the Backtest in Python and see the performance results:

Disclaimer: Statements are for educational purposes only. Nothing contained herein is intended to be or to be constructed as financial advice.

--

--