avatarDanny Groves

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3428

Abstract

period RSI — this will be generated using the <a href="https://hexdocs.pm/talib/TAlib.Indicators.RSI.html">TAlib</a> Python package.</li><li>Support trend lines — using an algorithm I developed</li></ol><p id="a0eb">The idea is that we are going to find support lines over a fixed window length (I used 22 weeks) at the close of each week; these lines are determined for both the RSI and the price action. Once the support lines are found, we can determine the gradient value for each to compare for a divergence in the slopes. The rules are then simple:</p><ul><li>If the RSI gradient > 0 <b>and </b>the price action gradient < 0, then we are in a bullish divergence phase.</li><li>Else, ignore!</li></ul><h2 id="8501">Wait, why are you using 22 weeks!?</h2><p id="af87">To be totally transparent, this is what visually looked best over the 2001 and 2008 bear markets — with the code you can try a number of window lengths to see if you find out anything else interesting!</p><p id="8f2d">Typically, shorter windows introduced more false signals; longer windows missed some major market movements entirely.</p><p id="68ab">Besides, 22 weeks is roughly half a year, which seems a reasonable length to measure larger market changes.</p><h2 id="b82e">Please can you summarise how the trend line algorithm works?</h2><p id="b579">Sure! Basically, to draw a trend line algorithmically, we find the gradient and intercept parameters for the equation of a straight line:</p><figure id="de5b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*jzx8bfYsUDKtM3DT"><figcaption></figcaption></figure><p id="6d0f">For support lines, we want to find the line that cuts through the minimum point over that window, and minimises the distance between the line and each other point. Resistance lines are the opposite where it cuts through the maximum.</p><p id="9af7">The article linked below describes this in a lot more detail!</p><div id="a06e" class="link-block"> <a href="https://wire.insiderfinance.io/algorithmically-drawing-stock-market-trend-lines-ecb5ef6d112a"> <div> <div> <h2>Algorithmically Drawing Stock Market Trend Lines</h2> <div><h3>Considering both simple and sophisticated techniques in Python</h3></div> <div><p>wire.insiderfinance.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*WjmIJ2sc24Z7evjI)"></div> </div> </div> </a> </div><h1 id="b567">The 2001 Dot-Com Bubble</h1><p id="7585">The next most notable bear market in history is surely the 2001 Dot-Com bubble; so how did the code perform here?</p><p id="c42a">Well… not entirely great, but I’ll let you make up your own mind:</p><figure id="1062"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vOuBWuKa6KyhU_CyVBGn2w.png"><figcaption>The bearish divergence calculations over the 2001 Dot-Com bubble</figcaption></figure><p id="5b82">It does indeed give a signal that the bear market is coming to an end, but weeks before any uptrend starts. Likewise, the “false-signals” do not time any counter trend rallies well.</p><p id="ddd1">Perhaps a little adjustment of the codes hyperparameters will give a better result, but you’re then at risk of overfitting to get the result we desire.</p><h1 id="fe

Options

c2">The 2022 Bear Market</h1><p id="4e06">How about the recent market condition?</p><figure id="19be"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ncVRB50Xux6kkNeqtu9UVQ.png"><figcaption>The bullish divergence code applied to the 2022 bear market</figcaption></figure><p id="1f9d">If the method is to be believed then we are indeed coming out of the bear market. The optimist in me wants to believe this is true, the pessimist believes that it is almost too easy to tune these parameters to get a nice result, and therefore find a pattern that really isn’t there.</p><p id="f22b"><b>Note!</b> The 2020 flash bear market was also included here, the method completely mistimed the bottom; however, this is likely because of the large window size I used. A shorter window is likely required for such quick drops.</p><p id="74e6">⚠️<b>Please do not take this as advice on timing the market. This post is for educational purposes only and you should do your own testing/validation before believing anything on the internet! ⚠️</b></p><h1 id="705f">Wrapping Up</h1><p id="eb04">Overall, I quite like the principles of this method and the results are somewhat interesting. <b>However</b>, it is incredibly easy to overfit these observations based on the fact we have, well, two main examples. A more in-depth study could be to tune these parameters to catch the end of countertrend rallies in bull markets, or perhaps to flip the situation and consider market tops.</p><p id="049b">Personally, I won’t be using this indicator as a measure of re-entering the market fully. I think I’ll take Mark Minervini’s advice and let the stock setups guide me!</p><p id="63f4">Thank you for reading, I hope you enjoyed the article! Please feel free to connect with me on <a href="https://www.linkedin.com/in/dannygrovesn7/">LinkedIn</a> — I’d love to hear from you! Also feel free to follow/DM me on <a href="https://twitter.com/DrDanobi">twitter</a>.</p><p id="f6d9">If you are thinking getting a medium account, then please consider supporting me and thousands of other writers by <a href="https://medium.com/@dannygrovesn7/membership">signing up for a membership</a>. For full disclosure, signing up through this link grants me a portion of your membership fee, at no additional cost to you (a win-win for sure!).</p><p id="cb4c">Or if you’d like another way to support my content creation, then you could</p><figure id="2f5a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9vg3-OY14aZN1UpKwIxxZg.png"><figcaption></figcaption></figure><p id="d5bd">Because I work a full-time job, go to the gym, trade, and write on Medium, I use a lot of coffee!</p><h1 id="b78f">The Full Code</h1> <figure id="806f"> <div> <div>

            <iframe class="gist-iframe" src="/gist/GrovesD2/5f02c6e5102c0ce071d0a5f79b4cc242.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="ca99">Subscribe to DDIntel <a href="https://ddintel.datadriveninvestor.com/">Here</a>.</p><p id="97d6">Visit our website here: <a href="https://www.datadriveninvestor.com/">https://www.datadriveninvestor.com</a></p><p id="09c0">Join our network here: <a href="https://datadriveninvestor.com/collaborate">https://datadriveninvestor.com/collaborate</a></p></article></body>

Can We Use Simple Technical Analysis Time the Bottom of the Bear Market?

Using the RSI, trend lines and Python to investigate when the market bottom will occur

Photo by Mark Basarab on Unsplash

As a swing-style trader, the bear market of 2022 has not been too kind; I’m sure many longer term traders/investors are also sharing that sentiment. How do we know when it’s all going to end, and when it’s safe to move from mainly cash back to positions in the market?

My suspicion is that no one can really time this perfectly, after all, the saying “time in the market beats timing the market” does hold some weight. However, there are examples such as this video by the Financial Wisdom channel who suggest it may all be over soon.

The strategy laid out in this video is based on technical analysis, using:

The principle idea is that if we have increasing RSI but a decreasing price action, then we are in a “bullish divergence” phase — i.e. market momentum is strengthening and price could soon move in the direction of the indicator.

Using Python, I was able to implement a simple bit of code to perform such analysis, and produce visualisations to investigate this myself; as an example, here is the outcome from the end of the 2008 financial crisis:

The bullish divergence analysis applied to the end of the 2008 financial crisis. The orange highlighted areas are regions where the code detected a bullish divergence, and therefore, a potential end to the bear market.

The key areas are those highlighted in orange, these are where the Python code detected bullish divergence. It gave a pretty strong band towards the market bottom, and besides one false signal, the highlighted regions give a good prediction of a countertrend rally.

Throughout this article I will describe:

  • How the code detects the bullish divergence.
  • How the method performs on the 2001 Dot-Com bubble.
  • Importantly, what this method says about the current market.

Please see the end of the article for the full code 🙂

The Technical Analysis Approach

Personally, I found the examples laid out by the financial wisdom channel a little too discretionary for my taste, which is precisely why I looked to an implementation with code. As described in the introduction, the approach requires two key ingredients:

  1. The 14 period RSI — this will be generated using the TAlib Python package.
  2. Support trend lines — using an algorithm I developed

The idea is that we are going to find support lines over a fixed window length (I used 22 weeks) at the close of each week; these lines are determined for both the RSI and the price action. Once the support lines are found, we can determine the gradient value for each to compare for a divergence in the slopes. The rules are then simple:

  • If the RSI gradient > 0 and the price action gradient < 0, then we are in a bullish divergence phase.
  • Else, ignore!

Wait, why are you using 22 weeks!?

To be totally transparent, this is what visually looked best over the 2001 and 2008 bear markets — with the code you can try a number of window lengths to see if you find out anything else interesting!

Typically, shorter windows introduced more false signals; longer windows missed some major market movements entirely.

Besides, 22 weeks is roughly half a year, which seems a reasonable length to measure larger market changes.

Please can you summarise how the trend line algorithm works?

Sure! Basically, to draw a trend line algorithmically, we find the gradient and intercept parameters for the equation of a straight line:

For support lines, we want to find the line that cuts through the minimum point over that window, and minimises the distance between the line and each other point. Resistance lines are the opposite where it cuts through the maximum.

The article linked below describes this in a lot more detail!

The 2001 Dot-Com Bubble

The next most notable bear market in history is surely the 2001 Dot-Com bubble; so how did the code perform here?

Well… not entirely great, but I’ll let you make up your own mind:

The bearish divergence calculations over the 2001 Dot-Com bubble

It does indeed give a signal that the bear market is coming to an end, but weeks before any uptrend starts. Likewise, the “false-signals” do not time any counter trend rallies well.

Perhaps a little adjustment of the codes hyperparameters will give a better result, but you’re then at risk of overfitting to get the result we desire.

The 2022 Bear Market

How about the recent market condition?

The bullish divergence code applied to the 2022 bear market

If the method is to be believed then we are indeed coming out of the bear market. The optimist in me wants to believe this is true, the pessimist believes that it is almost too easy to tune these parameters to get a nice result, and therefore find a pattern that really isn’t there.

Note! The 2020 flash bear market was also included here, the method completely mistimed the bottom; however, this is likely because of the large window size I used. A shorter window is likely required for such quick drops.

⚠️Please do not take this as advice on timing the market. This post is for educational purposes only and you should do your own testing/validation before believing anything on the internet! ⚠️

Wrapping Up

Overall, I quite like the principles of this method and the results are somewhat interesting. However, it is incredibly easy to overfit these observations based on the fact we have, well, two main examples. A more in-depth study could be to tune these parameters to catch the end of countertrend rallies in bull markets, or perhaps to flip the situation and consider market tops.

Personally, I won’t be using this indicator as a measure of re-entering the market fully. I think I’ll take Mark Minervini’s advice and let the stock setups guide me!

Thank you for reading, I hope you enjoyed the article! Please feel free to connect with me on LinkedIn — I’d love to hear from you! Also feel free to follow/DM me on twitter.

If you are thinking getting a medium account, then please consider supporting me and thousands of other writers by signing up for a membership. For full disclosure, signing up through this link grants me a portion of your membership fee, at no additional cost to you (a win-win for sure!).

Or if you’d like another way to support my content creation, then you could

Because I work a full-time job, go to the gym, trade, and write on Medium, I use a lot of coffee!

The Full Code

Subscribe to DDIntel Here.

Visit our website here: https://www.datadriveninvestor.com

Join our network here: https://datadriveninvestor.com/collaborate

Finance
Money
Trading
Investing
Python
Recommended from ReadMedium