February 16, 2025
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.”
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:
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.
- 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:
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.
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