# 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 ago for 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 functionmy_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`