The next step is to create two columns to contain the values of when volatility increases or decreases from the last period. Thus, we can define the following conditions:

**If the current Standard Deviation is greater than the previous Standard Deviation, then, input in a new column the current value of the Standard Deviation. Otherwise, input zero. Let us call this column the Positive Change.****If the current Standard Deviation is lower than the previous Standard Deviation, then, input in a new column the current value of the Standard Deviation. Otherwise, input zero. Let us call this column the Negative Change.**

Now that we have two columns where one contains the positive changes in volatility and the other contains the negative changes in volatility, we can proceed by coding them in this way:

for i in range(len(my_ohlc_array)):if my_ohlc_array[i, std_col] > my_ohlc_array[i - 1, std_col]:

my_ohlc_array[i, positive_ch] = my_ohlc_array[i, std_col]

else:

my_ohlc_array[i, positive_ch] = 0for i in range(len(my_ohlc_array)):

if my_ohlc_array[i, std_col] < my_ohlc_array[i - 1, std_col]:

my_ohlc_array[i, negative_ch] = my_ohlc_array[i, std_col]

else:

my_ohlc_array[i, negative_ch] = 0# The variable std_col refers to the column where the Standard Deviation is stored

# The variable positive_ch refers to the Positive Change column that will be populated

# The variable negative_ch refers to the Negative Change column that will be populated

Next, we can calculate a simple moving average of the Positive Change and the Negative Change columns. We can also compute a smoothed moving average instead if we want to be exactly in line with the RSI formula, but I like to keep it simple from time to time. Here is how to do so:

def ma(Data, lookback, what, where):for i in range(len(Data)):

try:

Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())except IndexError:

my_ohlc_array = ma(my_ohlc_array, lookback, positive_ch, up_ma)

pass

return Data

my_ohlc_array= ma(my_ohlc_array, lookback, negative_ch, down_ma)# The variable up_ma refers to where we want to store the averaged values of the positive change while the down_ma refers to where we want to store the averaged values of the negative change

All the above work can be combined into one function that defines the Relative Volatility Index: