Photo by Hitesh Choudhary on Unsplash

Beginner’s Guide to Algo Trading Part I: Build Trading Strategy with Technical Indicators in Python

Sarp Nalcin

--

Building Trading Strategy may require rigorous Math/Statistics, Coding and Trading experience. If you are new to Algorithmic Trading -in short Algo Trading- don’t be scared. This guide is intended for you to build your Trading Strategy on Python and prepare you to execute trades based on algorithms

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:

Skills that are good to have…

Before starting, I like to provide you a list of skills that are good to have to get the best out of this guide. If you don’t feel comfortable with one/many, I encourage you to keep reading since you will be provided codes, explanations and examples throughout:

1. Basic mathematics

2. Basic coding experience (Does not need to be Python)

3. Basic trading knowhow: Buy low, Sell high (with some signals)

4. So much enthusiasm!

Overview of steps to build a Trading Strategy

1.Find the right market to trade

Although many folks think a winning Trading Strategy will correctly give out the time to buy/sell in any market, this is totally a misconception in reality. It is highly advised to first understand the market dynamics and then build a strategy. In this guide, we dive into the cryptocurrency market and choose the 2 most well-known pairs: Bitcoin (BTC/USDT) and Ether(ETH/USDT)

Photo by Markus Spiske on Unsplash

2.Analyze Technical Indicators and Implement Trading Strategy

We need tools to identify signals. In our Trading Strategy, these tools are Technical Indicators (A quick overview: Technical Indicators -in short indicators- are mathematical formulae exploiting mostly price & volume data which enable investors/traders to examine price movement and make predictions. Don’t forget that it is not healthy to see one indicator as sole Trading Strategy but try treating it as one condition in your overall strategy)

If you do some research on Technical Indicators, you will see there are plenty of them exploiting various methods. In a quick and dirty way, Technical Indicators can be grouped in 3 categories:

i- Trend Following & Momentum Indicators: Measure the rate of change in price, identify direction and strength of the change. We will exploit RSI (Relative Strength Index)

ii- Volatility Indicators: Show the dispersion of price. We will be looking at BB (Bollinger Bands)

iii- Volume Indicators: Account for buying and selling pressure in the market. We will be using MFI (Money Flow Index)

Trading Strategy: We expect to receive buy/sell signals from all indicators aforementioned (RSI and BB and MFI) at the same time. With this approach, we incorporate both the momentum and the volatility of price as well as the volume change. Before combining all three indicators, it is good to analyze each one’s buy/sell signal generation frequency and period

3.Backtest

To understand the performance of indicators, we need to do backtests. Backtest will help us find out the performance of indicators on individual basis and also the performance of the overall Trading Strategy

Photo by Stephen Dawson on Unsplash

Setting up Python Environment & Data Extraction

If you have Python installed on your device, you can jump straightforward into the codes below. If not, then you need to download Anaconda Navigator and install. On Anaconda Navigator, I recommend writing codes on Jupyter Notebook which is both practical to use while having user-friendly interface

Let’s start with importing some of the must-have libraries of Python. Pandas and Numpy are key libraries for data analysis. Matplotlib is a quite easy to use visualization library. We also need Datetime library to transform machine-readable time into human-readable format

Additionally, we need two more libraries. These are ccxt and pandas_ta which is used for extracting price & volume data from exchanges and applying indicators respectively. If these libraries are not installed to your Anaconda, it is required to first install them by pip install and then perform the import. Since these libraries are already installed on my computer, I do import only

As we are done with importing libraries, we can now move on the data extraction phase. Here, we use Binance to get the price & volume data but you are free to use any exchange you like. To get market related information such as maker/taker fee, min/max limit, price precision etc. you can run the code below and markets will reload

Below is the most juicy part of the data extraction phase. We use fetchOHLCV function in Binance to extract data (fetchOHLCV gets open, high, low, close prices and volume respectively indexed by machine-readable time). To achieve that, we should specify the following:

  1. Starting point of the period with since which is the beginning of 2019
  2. Currency pair with symbol which is BTC/USDT and ETH/USDT
  3. Frequency of the data with timeframe which is 4h

Then, we loop over until we reach out the most recent datapoint that exchange provides (If you run fetchOHLCV function without looping, you will end up with only 500 data points limited by exchange. That’s why we need a loop in the code). This process of looping is known as Pagination. Finally, to see the output properly, we convert it into DataFrame format

One remark: I deliberately put the same code in two different cells by only changing the symbol to show you separately for each currency pair. You can combine them into one cell and even create a function that takes since, symbol and timeframe as inputs and outputs data

If you run BTC_data or ETH_data, you will see that time format doesn’t make much sense. Thus, we transform it into human-readable format as follows

Lastly, to easily distinguish between columns, we give names. Note that you can get SettingwithCopyWarning error which is normal

Now, we are finished with data extraction phase. If you also see the following or similar depending on the beginning time and timeframe used, this means that you are on the right track!

With price & volume data extracted, we can now proceed with using technical indicators to build our algorithmic Trading Strategy in Part II of this guide:

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

--

--