Stockprices = np.where((stockprices 70),0,new) Stockprices = np.where((stockprices > 0) ,stockprices,0) Stockprices = stockprices - stockprices.shift(1) #calculate the movement on the price compared to the previous day closing price Stockprices = np.log(stockprices / stockprices.shift(1) ) #calculate the return of the day and add as new column #reverse dates in the index to have more recent days at the end
In the next section, we will merge the DataFrame and the Series and apply our strategy. After running the script, we will end up having a Pandas DataFrame with all stock information and a RSI series containing the RSI indicator for each of the days. The RSI variable will contain a Pandas series with the RSI indicator calculated for each of the days.īelow is the first part of the code. We can calculate RS by dividing these two variables.įinally, we are able to calculate the RSI applying below formula. Now that we have calculated the last 14 days average gains and the 14 days average loss. Similar approach to calculate the down variable. Where for the up column, we only have a value if the price movement of the day was positive (if not we will have a 0). Note that the moving average is only calculated using the last 14 days. We will use that period length to calculate the 14 days period gain moving average and store in a variable named up. Similarly, we will have a column named down where we will get the price movement of the day only if the price of the stock goes down for that day.Īs suggested by the creator of the RSI indicator, we will have a 14 day period length. The amount will be the daily price movement in absolute terms. In addition, this DataFrame will also contain a column named up where we will get an amount only if the price of the stock goes up. The aim is to get a Pandas DataFrame containing the closing price, return and movement for each of the days. Alternatively, you can use another data source to get the stock prices for each of the days.īelow you can find the first part of the code.
You can get one for free with up to 250 API requests a month. N ote that you need to sign up to financialmodelingprep in order to get your private API key. I will use financialmodelingprep to get the stock prices. To do that, we need first to request from an API the last 5 years historical prices of Apple. We will start by calculating the RSI indicator for each of the 5 historical years. Then, based on the RSI indicator and the stock closing prices of the day, we will define if we go long or if we do not hold any position on that stock for each of the days. To do this, we will calculate the RSI indicator using the 14 days moving average (To know more on moving averages in Python have a look at my previous post). We will use the last 5 years of Apple stock prices.Īs already mentioned before, we will enter a long position if the stock crosses the level 30 RSI indicator from below. Our momentum strategy to backtest will be quite easy to build. Welles Wilder, we will use a length of 14 periods to calculate the indicator.īacktesting RSI Momentum Strategies using Python Since I am not a big fan of going short, we will keep our strategy to only enter long positions.Īs it was suggested by the RSI developer, J.
That is, if the RSI on the stock crosses the level 70 from above, we should enter into a short position. On the other side, when the RSI indicator on a stock goes above 70, the stock is considered to be overbought. At that point, the stock is seen as oversold.
To set up a trading strategy following RSI, it is common to open a long position (buy the stock) if the RSI indicator goes above the level 30 from below. The Relative Strength Index (RSI) is a technical indicator that measures the speed and change of price movements. This indicator is the one that we will use in order to define our backtesting strategy with Python. One of this type of indicators is the Relative Strength Index (RSI). Momentum indicators can help us locate the entry and exit points to follow momentum strategies. For example, if the market is or starts going up, we will bet that the trend will continue for a while and we will try to make our trading strategy based on this. What is a Momentum Strategy?Ī momentum strategy basically bets on the continuation of an existing market trend. In addition, the content of this blog may not be free of errors. Note that the content in is only for education purposes, and therefore, it should not be used for trading or investing decisions. This strategy will be exactly the opposite to the mean reversion strategy that we backtested in my previous post using Python.
During this article, we are going to learn how to backtest RSI momentum strategies using Python.