August 5


Finding The Optimal Period

By Curtis White

August 5, 2019

System: optimization

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.

Test 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:

RSI(2) Simple vs RSI(2..14) 50/50

@ES Daily 1/1/2004 - 1/29/2017

RSI(2) Simple

RSI (2..14) 50/50

Net Profit



Max DD:



Profit Factor:



Avg Profit Per Trade



Number  of Trades



Win %



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.

Tightening The Threshold 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.

RSI(2..14) 30/70 vs RSI(2..14) 20/80

@ES Daily 1/1/2004 - 1/29/2017

RSI (2..14) 30/70

RSI (2..14) 20/80

Net Profit



Max DD:



Profit Factor:



Avg Profit Per Trade



Number  of Trades



Win %



Final Analysis & Summary 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 White

About the author

Hi, I'm Curtis and I'm passionate about markets and trading.

My mission is to apply machine learning to build better trading systems and to become a better discretionary trader by applying quantitative and machine learning techniques to solve my greatest trading problems. I develop trading systems and graybox trading solutions using Tradestation's Easylanguage, Python, C#, and Ninjatrader.

  • 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?


  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Learn To Code & Build Strategies
    Using EasyLanguage.