On this page
There is a particular kind of retail trader who can draw a trendline through any two random points and call it valid. The chart accommodates them — there are infinite pairs of swing points and infinite lines through them. This is the deep problem with trendlines: they look objective and they're not.
But trendlines that follow a few hard rules do contain real information. They quantify the slope of a move and give you a clean break signal that's measurable in code. Here's the discipline.
What a trendline actually is
A trendline is a straight line connecting two or more swing extremes — swing lows for an uptrend support, swing highs for a downtrend resistance. The line projects forward and price is expected to respect it (bounce off, or break decisively through).
Critical: the points must be swings, not arbitrary candles. A swing low is a bar whose low is strictly lower than the N bars on either side. Without that constraint, you can draw a "trendline" through any two bars and call it a setup.
The two-touch / three-touch rule
A trendline becomes drawable at two touches. It becomes confirmed at three. Below three touches, you have a guess. At three or more, you have a level that other traders are also watching, which is what gives it any predictive power at all.
Slope matters more than touches
A 45-degree trendline is sustainable. A 75-degree trendline is a moonshot — it can stay valid for a while, but the slope itself is information: the trend is consuming buying pressure unsustainably fast. Mathematically, the slope of an uptrend trendline tells you the implied price change per bar. Compare to ATR to see if it's physically realistic.
# A trendline through (t1, low1) and (t2, low2) has slope:
slope_per_bar = (low2 - low1) / (t2 - t1)
# Compare to current volatility:
ratio = slope_per_bar / atr(14)
# ratio < 0.3 → mild, sustainable trend
# ratio 0.3-0.6 → strong, watch for exhaustion
# ratio > 0.6 → parabolic, expect a break within 5-15 barsThe "broken trendline" myth
Retail wisdom: "sell when the uptrend trendline breaks." Practical reality: most trendline breaks are noise. A single bar piercing the line on a 4h chart is statistically indistinguishable from a fakeout. You need confirmation — either a candle close below the line, or a touch-and-reject of the broken line as new resistance (the so-called "throwback" or "polarity flip").
The cleaner signal is structural: when the trend itself breaks (a lower high or lower low forms — see the Dow definition in the trend post), the trendline becomes irrelevant. The line was just one visual representation; the structure is the underlying truth.
Encoding a trendline in code
If you want trendlines as a signal, you need an algorithm that doesn't suffer from confirmation bias. The classic approach: at every bar, find the two most recent swing lows. Compute the line through them. Extend it forward. Test whether the current bar's low touched or pierced the line. If yes, that's a potential entry (bounce) or exit (break) signal — depending on which side and how it closed.
strategy:
name: trendline_bounce_long
indicators:
- { id: swing, kind: SwingDetector, lookback: 5 }
- { id: trendline, kind: AutoTrendline, anchor: lows, min_touches: 2 }
- { id: rsi, kind: RSI, period: 14 }
rules:
entry:
type: AND
children:
- { type: lt, left: bar.low, right: trendline.value }
- { type: gt, left: bar.close, right: trendline.value } # touch + bounce
- { type: lt, left: rsi, right: 50 } # not overbought
exit:
type: OR
children:
- { type: lt, left: bar.close, right: trendline.value } # close below line
- { type: gt, left: rsi, right: 70 }
risk:
stop_loss: { kind: percent_below_trendline, mult: 0.5 }Common mistakes
- Connecting wicks vs bodies. Pick one and stay consistent. Bodies are the consensus close; wicks are the extreme. For most strategies, connecting closing prices (bodies) produces more robust lines.
- Curve-fitting after the fact. If you only "see" the trendline after the trend has already played out, your forward edge from it is zero. Draw it from the live left edge of the chart and only act on it going right.
- Mistaking a horizontal range boundary for a trendline. Horizontal support and resistance are different beasts — see the dedicated post.
- Treating the broken trendline as a binary signal. Markets are noisy; require a candle close, not a wick.
Next steps
Trendlines are most useful when you already understand what a trend actually is. For horizontal levels (the other side of the same coin), see the support and resistance post.
Try it on your own data
Every concept above is implemented in the platform. Backtest, walk-forward, paper-trade, then promote to live — same rule set, all stages.