The Relative Strength Index (RSI) is one of the world's most popular technical indicators. Most trading platforms offer the RSI and various strategies connected to it.
Adding the RSI to your trading bot allows your autotrading bot to integrate this powerful indicator into your trading strategies.
In this episode, I’m going to show you how to do it.
As part of my development environment setup story, I showed you the easiest way to setup TA Lib. Now we’re going to start building an indicator library for you to use in your trading bots.
In your trading bot dev environment, create a file called indicators.py
This little file is a bit of a game changer for trading bot development. Moving forward, every time you want to add a new indicator to your trading bot, you’ll be able to drop it here. Before long, you’ll be adding 3, 4, or more indicators to your trading bot. You might even add some custom ones!
This is the exact same approach we use to build trading bots for our customers at TradeOxy.
The Pandas Python Library has entered legend status in the Python Programming Language. It’s an incredibly powerful-yet-simple library to use, with a plug in to interface with just about any system you’ve ever imagined.
If you’re thinking, “Oh no, I bet this is really hard to install🥲” then cheer up. Panda’s is super simple to install and use.
To do so, head to your requirements.txt
and add the following line:
pandas
That’s it.
If you’ve been following my blog series, your requirements.txt should now look like this:
Quickly run the command pip install -r requirements.txt
to update your development environment.
Done.
Let’s add some routing to the indicator library. This will make it easy for you to keep adding indicators, while leveraging some of the features of Python to make the function dynamic.
Add this code to your indicators.py
file.
There’s a few pretty cool things happening here:
By the way, if you’d like to learn more about the RSI and why we might want to define those values, check out this article.
For the more advanced programmers out there, you’ll also notice that I’ve typed the inputs to the function and clearly defined a dictionary return. This will drastically simplify your development life moving forward!
Now we need to add the RSI calculation to the indicator library. There’s a few things we need to consider to do this:
The default values I’ll be using for the RSI are:
You can of course change them to whatever you’d like!
Add this code to the bottom of your indicators.py
Nice work. You’ve just added the RSI!
Let’s update your app.py
file so you can see the RSI in action on the stock(s) of your choice.
Start by adding a new import to the top of your file: import indicators
This tells your main program to use the indicators library we’ve just created in the trading bot.
Next, update your auto_run_trading_bot
function so that it looks like this:
Now, if you hit ▶️ you should see something similar to the following:
Your values will be different than mine as you are doing this at a different time to me.
It’s here we can see the true power of a trading bot at work. Using the variables for symbols
and timeframe
from previous episodes, you can analyze huge swathes of the market all at once.
For instance, keen on looking at Apple, Google, and Meta? Change the following line and rerun:
symbols = ["AAPL", "GOOGL", "META"]
What if you want to change the timeframe? Change the following line and rerun:
timeframe = "1hour"
Boom.