Creating a Variable RSI for Dynamic Trading. A Study in Python.
By: Sofien Kaabar
Tags: Machine Learning, Python, Data Science, Technology, Trading

This is a way to gradually weigh the RSI lookback periods. Note that you can select whichever periods you want and optimize them according to your preferences. The default parameters on the Dynamic RSI (according to me) are the ones above.

Let us now see the full function that gives out this indicator before we proceed to the back-testing step. Note that you must use it on an OHLC array with multiple extra columns to be populated by the function.

def dynamic_rsi(Data, momentum_lookback, corr_lookback, what, where):



for i in range(len(Data)):

Data[i, where] = Data[i, what] / Data[i - momentum_lookback, what] * 100 Data = rolling_correlation(Data, what, where, corr_lookback, where + 1)



for i in range(len(Data)):



if Data[i, where + 1]>= -1.00 and Data[i, where + 1]<= 0.10:

Data[i, where + 1] = 14

if Data[i, where + 1] > 0.10 and Data[i, where + 1]<= 0.20:

Data[i, where + 1] = 10

if Data[i, where + 1] > 0.20 and Data[i, where + 1]<= 0.30:

Data[i, where + 1] = 9

if Data[i, where + 1] > 0.30 and Data[i, where + 1]<= 0.40:

Data[i, where + 1] = 8

if Data[i, where + 1] > 0.40 and Data[i, where + 1]<= 0.50:

Data[i, where + 1] = 7

if Data[i, where + 1] > 0.50 and Data[i, where + 1]<= 0.60:

Data[i, where + 1] = 6

if Data[i, where + 1] > 0.60 and Data[i, where + 1]<= 0.70:

Data[i, where + 1] = 5

if Data[i, where + 1] > 0.70 and Data[i, where + 1]<= 0.80:

Data[i, where + 1] = 4

if Data[i, where + 1] > 0.80 and Data[i, where + 1]<= 0.90:

Data[i, where + 1] = 3

if Data[i, where + 1] > 0.90 and Data[i, where + 1]<= 1.00:

Data[i, where + 1] = 2



Data = rsi(Data, 14, 3, 0)

Data = rsi(Data, 10, 3, 0)

Data = rsi(Data, 9, 3, 0)

Data = rsi(Data, 8, 3, 0)

Data = rsi(Data, 7, 3, 0)

Data = rsi(Data, 6, 3, 0)

Data = rsi(Data, 5, 3, 0)

Data = rsi(Data, 4, 3, 0)

Data = rsi(Data, 3, 3, 0)

Data = rsi(Data, 2, 3, 0) for i in range(len(Data)):



if Data[i, where + 1] == 14:

Data[i, where + 12] = Data[i, where + 2]

if Data[i, where + 1] == 10:

Data[i, where + 12] = Data[i, where + 3]

if Data[i, where + 1] == 9:

Data[i, where + 12] = Data[i, where + 4]

if Data[i, where + 1] == 8:

Data[i, where + 12] = Data[i, where + 5]

if Data[i, where + 1] == 7:

Data[i, where + 12] = Data[i, where + 6]

if Data[i, where + 1] == 6:

Data[i, where + 12] = Data[i, where + 7]

if Data[i, where + 1] == 5:

Data[i, where + 12] = Data[i, where + 8]

if Data[i, where + 1] == 4:

Data[i, where + 12] = Data[i, where + 9]

if Data[i, where + 1] == 3:

Data[i, where + 12] = Data[i, where + 10]

if Data[i, where + 1] == 2:

Data[i, where + 12] = Data[i, where + 11] return Data

Now, how do the results of the Dynamic RSI compare to the results of the regular RSI? After all, we need a benchmark or some form of comparison to properly judge our strategy. I will make an exception in this back-test regarding the risk management processes I employ, as I will rather respect the optimal risk-reward ratio of 2.00. This means that I will place my stops at 1x the 50-period ATR and my targets at the 2x 50-period ATR. Another way of saying that I will be risking half of what I expect in each trade.