## Learn To STOP Curve Fitting!

Download this free guide on how to stop curve fitting. Following these four simple steps can improve your trading dramatically!

- You are here:
- Home »
- Blog »
- Strategy Development
- » Finding The Optimal Period

Today’s topic is on finding the optimal periodicity for the RSI indicator and the techniques should apply to other sorts of indicators as well. The good news is that it won’t require any rocket science. John Ehlers has written a lot about this topic, and his articles spurred my interest in it, which frankly I had never considered. I might should note now that I have not reviewed the actual methodology that Ehlers used in any depth but my impression was that the methods were very advanced. However, I realized I could tackle this problem using just basic statistics.

Before we start analyzing specific solutions, I want to take a moment to describe what we are actually attempting to do. The question that we are trying to answer is how do we find the optimal length to use when calculating an indicator. A poor length value may result in the RSI spending too much time in either the overbought or oversold states, or failing to reach the proper levels. Obviously, the optimal lookback will change as market conditions change and therefore our methods should be adaptive and responsive to the market.

The goal is to find a metric to rank how well behaved the RSI is. A well behaved RSI is one that spends about an equal amount of time in overbought and oversold levels. During a trending market, if the RSI gets stuck into overbought conditions then that would be an example of a poorly behaved RSI. The solution thus is simply to measure the amount of time that the RSI spends in overbought and oversold conditions. My original inspiration came from the idea of a histogram. We simply add 1 for every bar that the RSI is above our threshold and subtract 1 for every bar that the RSI is below our threshold. The absolute value of the resulting summation is our error metric, which we seek to minimize.

**Key Concept:** Sum the amount of time that the RSI spends above 50 and below 50, and take the difference. The absolute value of the difference is our error metric, which we seek to minimize.

Next, we will simply compute our error metric for every RSI length in our search space, and select the RSI with the minimum error. In this example, I searched from RSI(2) to RSI(14). There is one more consideration which is determining our lookback. Too short of a lookback will lack statistical relevance while too long of a lookback will defeat the benefits of adaptation. Some manual trial and error were used before I settled on a lookback of 90 days.

We conducted the test using the S&P 500 Emini and have selected the long only RSI(2) with standard 30/70 overbought/sold thresholds as our baseline. The results are below:

While the results are not too different, we do see our profit factor and average profit per trade has improved. The net profit is slightly reduced and the drawdown is slightly higher but essentially similar.

We can get better results by making our threshold more restrictive. Instead of using the median line, we can use the amount of time that our indicator spends in actual overbought and oversold levels. This makes some sense as the overbought/sold conditions are what’s really important. We use the same technique but now we try to balance the amount of time we spend below 30 and above 70. We see significantly better results. The results improve even more when we try to balance the extreme overbought/oversold conditions using 20/80 as our thresholds: notice that we do not change our actual buy/sell conditions.

Both the 30/70 and 20/80 optimization thresholds show significant improvement in the profit per trade over the baseline, fewer trades, and higher profit factor. This suggests that our RSI optimizations are improving trade quality significantly and suggests that our indicators are better synchronized to the more dominant cyclical structure of the market. While the net profits are not quite as strong, the strong increase in average profit per trade is especially appealing.

It appears that we can find significant cyclical structure in the markets using simple statistical techniques. However, we did have to use some trial and error in determining the lookback to calculate our statistics over, and that may be one area for improvement.

–Curtis White from Beyond Back Testing

Curtis is a market enthusiast and programmer who enjoys predicting markets, developing trading systems, and tape reading. He offers a blog at BeyondBacktesting.com where he shares his original ideas for developing and enhancing trading systems. He is a creative,synthesis thinker who likes to combine ideas and techniques from a broad palette. One of his core goals is the creation of graybox systems, software, and decision support tools for enhancing the capabilities of high performance discretionary traders.

**Session expired**

Please log in again. The login page will open in a new window. After logging in you can close it and return to this page.

Thanks for sharing this. I was wondering if you could clarify something. What are the buy/sell thresholds you used on the adaptive RSI approach? Your base case is a two-period RSI with longs taken below 30 and exits above 70. I don’t believe you explicitly said what they are. If you did, I apologize for missing it. Thanks.

The buy/sell thresholds are exactly the same. The adaptive approach dynamically changes the period (lookback) from 2 to 14 based 90 day lookback. To understand what the algo is doing, imagine adjusting your RSI lookback every day from 2 to 14 with the goal of finding the one that is most well-balanced, i.e. one that isn’t stuck in overbought or oversold conditions.

I have worked with RSI quite a bit, never thought to record the results. My idea would be to work with the highest win percentage and lowest equity drawdown. I suggest that changing the period beyond 14 might give some nice results also.

Thanks for the insights.

Great analysis. I did something very similar for tracking the “RSI Trend” over a lookback of 55 periods. The analysis gave a result of 1 for instances where the RSI was over 69, and 1 when under 31. An RSI calculation was performed:

N1 = 2… 14

N2 = 55

r = rsi(src, N1)

// Normalize extreme levels

OB = r > 69 ? 1 : 0

OS = r < 31 ? 1 : 0

// Average gain, average loss

avgU = (avgU[1] * (N2 – 1) + OB) / N2

avgD = (avgD[1] * (N2 – 1) + OS) / N2

// RSI calculation of the trend

rTrend = (100 – 100 / (1 + avgU / avgD)) / 100

The result shows when the normal RSI of period N2 is spending most of its time near extreme levels. This helped, for example, filter out short trades when the "RSI Trend" was above say, 70. When the RSI Trend is above 70, the vast majority of the time when the normal RSI breached oversold (< 30), there was an immediate snap back up.

Great work, I will research this further on intraday testing in Trading View.

Hi Curtis,

Thanks for an interesting article. What would easy language code look like to sum the amount of time that the RSI spends above 80 and below 20, and take the difference?

I was curious to know which period (2…14) in the first table produced the $82,812? Ditto for both columns (30/70 and 20/80) in the second table for the Emini?

Cheers

Bard