Overbought to Oversold: The Crucial Addition!Here's why most algorithms fail. They don't take into account the context you needed it to.
Here's why it's hard to correct that: you need something OUTSIDE of the raw number-crunching that you're doing in order to understand what's happening. If you're very good at that, you're already a profitable trader to begin with! In that case, it can be discouraging to move to a different platform where, at least to begin with, you don't see yourself making any move into profitable territory any time soon.
In this example, I used a raw "overbought to oversold" strategy that looks at RSI crossing from overbought/oversold to fair value to time exits and entries. The problem is, results vary widely. Sometimes, it is right 95% of the time for hundreds and hundreds of bars. Other times, it stops you out thirty times in a row. Whenever you see this type of erratic behavior, you know something outside of the stochastic nature of asset prices is playing its part.
This is why you need to understand what you're doing; what you're coding, and why the results are what they are. What kind of indicator is RSI? When does it work, and when does it fail? When you realize RSI is an oscillating momentum indicator, it's not hard to figure out it does well when a stock is ranging, and does poorly if it's strongly trending. RSI can be seen hitting overbought a dozen times during a strong bull run without the price action showing more than a few small pull-backs along the way. Makes sense: it is essentially built to analyze what it thinks of price action if you'd assume that the price will stay within the same range, or is given time to consolidate after having broken out of a previous range. When you understand this, the answer to our erratic algorithm becomes clear.
We need a trend indicator that stays flat enough when a stock is showing certain types of ranging behavior, but steep enough when it rallies for long enough or ranges so wildly during an erratic time that RSI won't measure it correctly. If we try to do this with a simple moving average, the problem you'll encounter is that it's either too reactive or too sluggish. You want to have some indication on the slope of the moving average, which indicates trend rather than momentum. The difference in slopes in the near term could be read as a form of "MACD", which is obviously momentum, but if you take it day-over-day, it more or less indicates the general trend direction.
Now, we can see on the chart below how we're not trading when RSI is misrepresenting the "overbought" and "oversold" states because it failed to take into account price action and trend, while trading very accurately when the stock is ranging. That's a home run algorithm waiting to enter the next stages: adding optional functionalities, optimization, and a battery of backtests.
Whether you want to build an algo, or trade manually, simply understanding how indicators can complement one another and how you can quantify your pre-existing intuition for "ranging" and "trending" without using complex variables and indicators beyond simple differences of moving averages can be hugely beneficial to your development as a trader.
Hope you learned something and happy trading!