Automatic Trendline & Pattern Detection - The Legend of Hanuman

Automatic Trendline & Pattern Detection


February 16, 2025

Here’s a bit more about the trendlines I revealed in the previous blogpost and why they’re still in beta testing.

For a long time I’ve wanted to add automatic trendline detection to SwingTradeBot.  It seemed like a pretty daunting technical task so the idea had remained on my back burner.  The two (related) things that make auto-trendlines tricky are:

  • Trendlines can be subjective.  Given the same chart, two people may not draw the same lines.  
  • How far back do you look? There can be very different & valid trendlines on different time frames.  
  • For an uptrend within the period of consideration (emphasis is mine), draw a line from the lowest low, up and to the highest minor low point preceding the highest high so that the line does not pass through prices in between the two low points. Extend the line upwards past the highest high point…
  • For a downtrend within the period of consideration, draw a line from the highest high point to the lowest minor high point preceding the lowest low so that the line does not pass through prices in between the two points
  • Vic goes on to say that: “While this method is quite simple, it is extremely consistent and very accurate.  The slope of the trendline is a close approximation to the slope you would get by doing a linear regression analysis on the price data over the same time period.  Unlike other methods, it prevents you from drawing a trendline to suit your purposes — it prevents you from imposing your wish onto the trendline.
As for time frames, I think looking at FinViz’s auto-trendlines illustrates that point nicely.  Here’s what they had for AMBA at the time I wrote this blog post:
AD 4nXd0uefL3NrJB2A2XWrtQiOiY5xJQ9NlQQOF8 FiVCjjLWCHSF9sXAONWBESnVCHlVR5vSnwA6y1aGbeUkHS1UukOQAyaCynTSBl7tBJ5QBycgyiOYHYDV83hsyD5UcItHUa3JoA?key=Rv93nZfncemwjJTRRMcCjdVw

Those lines are looking back about 7 months and form an upward sloping wedge pattern.

Let’s contrast that with what SwingTradeBot is currently drawing for AMBA:

AD 4nXcykQBCRJL7 BD5MZgQbHjkvok8Lbb1 A7Mm0zvUjRnYFC1akJp5p9GITUKOFImQue93L7OqU RyCLuEUhwlfUb833r sg100R4dUp k44 k80TpV 4RkO6OQrDzerszlHRg4Sy?key=Rv93nZfncemwjJTRRMcCjdVw

The trendlines on the two charts are telling very different stories.  SwingTradeBot is looking at a much shorter timeframe in which the upper & lower trendlines are forming a symmetrical triangle (coil) pattern.

Initially I thought I’d offer trendlines on multiple timeframes – say 50 & 20 days –  but I think that would be a little messy & possibly confusing. Plus I’m not sure how actionable those longer term trend lines are.  So for now I’ve decided to look back 20 trading days.  

The other thing to note is that SwingTrade Bot’s trendlines aren’t cutting through or excluding nearly as many prices as FinViz’s lines.  I still have some algorithm tweaking to do to improve the lines on certain charts.  For example, sometimes SwingTradeBot produces a trendline like this:

At this point I have not attempted to make my algorithm strictly follow Trader Vic’s guidelines.  That’s because it’s not as easy as it sounds to convert his guidelines into convention computer code.  (I am starting to learn some machine learning & AI, so perhaps I will find a way to exactly match the lines that would adhere to Vic’s guidelines.). Currently my algorithm actually starts with a linear regression line and then does some adjustments.  It’s working pretty well in many cases but as you saw above there are still some edge cases.  

Once I’ve got the trendline drawing algorithm nailed down those lines will allow the addition of signals (scans) like:
  • Bounced off a trendline (support or resistance)
  • Broke a trendline

One interesting thing with breaking trendlines is that when it happens it’s actually a breach of the previous day’s trendline.  If you look at the chart intraday you’ll see the breakout / breakdown clearly.  But once the market’s closed and the system draws the new line, you won’t see yesterday’s broken trendline.  Here’s an example from Google’s chart.  It broke down through a trendline (and a trend channel) last week:

AD 4nXekOZ27ab3itsmzqbC6x7ukCd7YfvfGUqgvPXHqDQ7Si 9cR1dJxYjr

That’s something I’m still thinking about how to deal with.  Perhaps I can have a toggle to add yesterday’s trendlines to the chart.  Or automatically add a freshly broken trendline to the chart.

Once I’m happy with the lines I’ll be able to program SwingTradeBot to detect certain patterns, such as
As with the trendlines, these will have intraday alerting as well as end-of-day signals. 
I hope to start rolling the trendline signals and scans by the end of February.  The patterns will roll out in the following weeks.  In conjunction with doing so I’m also evaluating some new charting software options for the “Quick Charts” (as I called them in the previous blog post) — the TradingView widget on the individual stock pages will stay as.  I’d like to get something that gives me more control over what’s plotted and give users of the site some level of interactivity with the charts – like the ability to zoom in & out as well as having an info overlay (tooltips) showing prices as you move your cursor over the charts.
If you have any thoughts about the trendlines & patterns please leave a comment. 

P.S.  After all the above is done I’m thinking of rolling out some more pattern detection for:

  • Double Bottoms (forming & confirmed)
  • Double Tops (forming & confirmed)
  • Unfilled Gaps (alert once filled and/or close to getting filled)

Trendlines
Patterns
Technical Analysis
site features


Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment