Technical Trading Bot – Strategy 1, MACD & RSI

As part of the first strategy, I will be making use of the RSI. The RSI short for Relative strength index is an indicator used in technical analysis, the value of the indicator will range from 0 to 100. Values over 70 signal that the instrument is being overbought and values below 30 signal that the instrument is being oversold. So, when in an overbought situation I will look to sell and in an oversold situation I will look to buy.

To compliment as well as confirm the above I will also make use of the MACD. The MACD short for Moving average convergence divergence is another indicator used in technical analysis. Using the MACD I will be looking out for crosses occurring between the MACD and signal line to confirm my entry and exit points.

The RSI indicator looks at price changes in relation to recent highs and lows, whilst the MACD indicator looks at the relationship between two exponential moving averages. They are commonly used together by several analysts and seemed to have stood the test of time in the world of technical analysis.

To achieve the above I will be creating a script in Python that will retrieve price data via Binance, once the data has been received, I will manipulate it further to derive to the values of the indicators above using the Python BTALIB library.

Find the full source code below. If you are requiring a Binance API and secret key, click here to create an account with them.

import pandas as pd
import btalib
from datetime import datetime
from binance.client import Client

api_key = 'BINANCE API KEY'
api_secret = 'BINANCE SECRET KEY'
client = Client(api_key,api_secret)

def Main(Instrument):
    #Here we define our data from date, in this case I want hourly data starting from yesterday
    Data = client.get_historical_klines(Instrument, '1h', '12 November, 2021')
    #Define the data for frame
    bnb_df = pd.DataFrame(Data, columns=['timestamp', 'Open', 'High', 'Low', 'Close','Volume', 'Ctime', 'Quotes', 'Trades', 'Taker base','Taker quote', 'Ignore'])
    #Set the index
    bnb_df.set_index('timestamp', inplace=True)
    #Convert the timestamp which is in MS to an appropriate timestamp
    bnb_df.index = pd.to_datetime(bnb_df.index, unit='ms')
    #Prepping the OHLCV data for validation
    ohlcv_columns = ['Open', 'High', 'Low', 'Close','Volume']
    #Validating the data types for the OHLCV data columns
    bnb_df[ohlcv_columns] = bnb_df[ohlcv_columns].astype('float')
    #Generating the MACD using btalib
    MACD = btalib.macd(bnb_df, pfast=12, pslow=26, psignal=9)
    #Generating the RSI using btalib
    RSI = btalib.rsi(bnb_df, period=12)
    #Merging the MACD and RSI to our data frame
    bnb_df = bnb_df.join([MACD.df,RSI.df])
    #Writing the data frame to a CSV file

Main("BNBGBP") #For this example we are using BNB TO GBP although any pair would be available to you

Leave a Reply