Obtaining EMA20 Rubber Band Theory Values Using Web Scraping and Pandas (Part 1)

Sze Zhong LIM
Data And Beyond
Published in
4 min readAug 27, 2023

One of the criteria I use to plan my Take Profit (TP) for stocks is to use the EMA20 and check how far away the price is away from the EMA20 indicator. The theory is also know as The Rubber Band Theory which can be found here.

In short, the theory is that if the price is too high / low above the EMA20, the gap between the price of the EMA20 will reduce.

We can use this knowledge to complement our TP and entry strategies. For instance, if our partial TP is higher than the historical highest % Gap between the EMA20 and the price, it is very unlikely we will hit our TP target.

We will use a mock example to better elaborate. Assuming for the past 2 years, the biggest gap TSLA has had is a price which is 25% above it’s EMA20. If TSLA is currently 23% higher than its EMA20, would you still enter, assuming the target TP price is 28% higher than its current EMA20? No we definitely won’t as it is statistically improbable.

If TSLA is currently only 5% higher than its EMA20, and all the other factors favor the trade (very important), we can know from the Rubber Band Theory that it still has areas of growth which are not against the trading plan.

I use the Rubber Band Theory not as a strategy to go into a trade, but more as a strategy to prevent myself from going into a statistically improbable trading plan, which I define as a risky trade.

TSLA YTD Chart (2023 Jan till Aug) with EMA20 line indicator

We can see from the TSLA chart above, if we roughly draw it out, that:

  1. The biggest difference between the EMA20 and price ABOVE, is approximately 28%
  2. The next 2 biggest difference between the EMA20 and price ABOVE, is approximately 17–19%
  3. The biggest difference between the EMA20 and price BELOW, is approximately -25%
  4. The next 4 biggest difference between the EMA20 and price BELOW, is approximately 12–14%

The above are just estimates, and to accurately get the statistics, we should use programming software.

For my strategy, I decided to use the median of the 80th Percentile or 0.8 Quantile of the difference as I felt it would be a good guideline. Which means that I will compute all the highs above the EMA20, and extract the 80th percentile data. I will then use the median of this new dataset as my benchmark.

Using my code, we can see that the Median of the 80th Percentile for both the Above EMA and Below EMA correlate has a strong relevance with what we inferred to on the charts we drew with.

I included the other parameters within so that it could be used if the trade is a tricky one worth risking for.

Using GOOGL as a trading example. If I were to go in, I have a pre-determined stop loss and a standard TP at RR Ratio of 1:1.5. I would like to counter check my strategy with the Rubber Band Theory.

Excel Sheet for Standard Position Sizing

Using a standard position sizing template, I obtained the EMA RBT 0.8% from my code, and used it as input to check whether my TP makes sense. Based on my code, the EMA20 Rubber Band Theory value i will use is 8.59%. We have the EMA20 value which was 128.4, and we add 8.59%, to get $139.42.

We can see based that based on my TP ratio settings, it is very very close to TP3. As such, the strategy is a go for me and I will proceed with this strategy. In the case that the EMA20 RBT value is far below my TP, I will either revise the RR Ratio to get more realistic TP levels, or reconsider the entry to this trade.

In Part 2, I will be posting the code and explanation on how to use web scraping and pandas to obtain these values instead of manually checking them out every single time for every different ticker.

--

--