# Creating the Correlation-Adjusted Reversal Indicator

The idea for the correlation-adjusted reversal indicator is to detect average extremes where the correlation between returns and prices is high enough to justify a possible market inflection. The steps required to calculate the indicator are as follows:

**Calculate the difference between the current price and the price 1 period ago.****Calculate the difference between the current price and the price 2 periods ago.****Calculate the difference between the current price and the price 3 periods ago.****Calculate the difference between the current price and the price 4 periods ago.****Calculate the difference between the current price and the price 5 periods ago.****Calculate the difference between the current price and the price 6 periods ago.****Calculate the average of all the calculations above for each row.****Calculate the 10-period correlation between the price and the average from the previous step.****Select a threshold such as 0.75. Then, loop around the data with a condition that if the correlation is greater than 0.75, keep the current average, otherwise, input zero as the average.**

The full code of the indicator can be found as below, considering an OHLC data array.

def correlation_adjusted_reversal_indicator(Data, lookback, close, where, threshold = 0.75):

# Adding a few columns

Data = adder(Data, 8)

# Average of current close minus the previous period

for i in range(len(Data)):

Data[i, where] = Data[i, close] - Data[i - 1, close]# Average of current close minus n then

for i in range(len(Data)):

Data[i, where + 1] = Data[i, close] - Data[i - 2, close]# Average of current close minus the close 2 periods ago

for i in range(len(Data)):

Data[i, where + 2] = Data[i, close] - Data[i - 3, close]# Average of current close minus the close 3 periods ago

for i in range(len(Data)):

Data[i, where + 3] = Data[i, close] - Data[i - 4, close]# Average of current close minus close 4 periods agofor i in range(len(Data)):

Data[i, where + 4] = Data[i, close] - Data[i - 5, close]# Average of current close minus close 5 periods ago

for i in range(len(Data)):

Data[i, where + 5] = Data[i, close] - Data[i - 6, close]# Calculating the average mean-reversion

Data[:, where + 6] = (Data[:, where] + Data[:, where + 1] + Data[:, where + 2] + Data[:, where + 3] + Data[:, where + 4] + Data[:, where + 5]) / 6

# Cleaning

Data = deleter(Data, where, 6)

# Adjusting for correlation

Data = rolling_correlation(Data, close, where, lookback, where + 1)

for i in range(len(Data)):

if Data[i, where + 1] > threshold:

Data[i, where] = Data[i, where]

elif Data[i, where + 1] < threshold:

Data[i, where] = 0

# Cleaning

Data = deleter(Data, where + 1, 1)

return Data# Calling the function

my_data = correlation_adjusted_reversal_indicator(my_data, 10, 3, 4)

If you are also interested by more technical indicators and strategies, then my book might interest you:

# Using the Indicator

As with any proper research method, the aim is to test the indicator and to be able to see for ourselves whether it is worth having as an add-on to our pre-existing trading framework. The conditions for the back-test are as follows:

**Long (Buy) whenever the CARI reaches -0.002 with the two previous readings above -0.002.****Short (Sell) whenever the CARI reaches 0.002 with the two previous readings below 0.002.**

`def signal(Data, what, buy, sell):`

Data = adder(Data, 10)

Data = rounding(Data, 5)

for i in range(len(Data)):

if Data[i, what] < lower_barrier and Data[i - 1, what] > lower_barrier and Data[i - 2, what] > lower_barrier :

Data[i, buy] = 1

if Data[i, what] > upper_barrier and Data[i - 1, what] < upper_barrier and Data[i - 2, what] < upper_barrier :

Data[i, sell] = -1

return Data

If you want to see more articles relating to trading strategies, consider subscribing to my DAILY Newsletter (A ** Free **Plan is Available) via the below link. It features

**my Medium articles, more trading strategies, coding lessons related to research and analysis, also, subscribers get a free PDF copy of my first book**. You can expect

**5–7**articles per week with your paid subscription and

**1–2**articles per week with the free plan. This would help me continue sharing my research. Thank you!

Remember to always do your back-tests. You should always believe that other people are **wrong**. My indicators and style of trading may work for me but maybe not for you.

I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.

48 views