The auto trendline indicator draws one straight line through recent closes. It uses a least-squares fit, so the line settles where the total error runs smallest. A single Lookback input decides how many bars feed that fit. Traders who want an objective slope instead of a hand-drawn line can lean on it.
This build ships as auto-trendline-indicator-indicator-forexmt4systems.ex4 for MT4 and auto-trendline-indicator-indicator-forexmt4systems.ex5 for MT5; the screenshots below come from EURUSD H1.
| Platforms | MT4 (.ex4) + MT5 (.ex5) |
|---|---|
| Chart window | Main price chart |
| Plots | 1: Trendline |
| Alerts | popup, push notification, email, sound |
| Inputs | 7 |
What the Auto TrendLine Indicator draws on the chart
The build declares a single plot, named Trendline. It paints in dodger blue at two pixels wide, straight across the candles. The line covers the most recent Lookback bars and stops there. Older bars stay empty, so nothing clutters the left side of the chart.
No channel and no bands come with it. Slope carries the message on its own: a rising line means the closes in the window drift up, while a flat one means they balance out. The whole line moves every time a bar closes, because the fit runs again from scratch.
- Trendline: line, dodger blue, width 2

How the Auto TrendLine Indicator calculates its values
Collecting the window
The code takes the last Lookback closes. It numbers them from the newest bar backwards, then adds up the bar numbers, the closes, the squares and the cross products. Four running sums are all the fit needs. If the chart holds too few bars, the routine exits before it draws anything.
Solving the fit
Two formulas finish the job. Slope comes from the cross-product sum weighed against the sum of squares. The intercept then follows from the average close and that slope. Fitted values go onto every bar inside the window, and the rest of the buffer stays empty.
How to read the signals
Treat the slope as a bias reading. A rising Trendline says recent closes drift higher, and a falling one says the opposite. The steeper the angle, the stronger that drift ran over the window.
Distance matters as much as direction. Price far above the line has stretched away from its own average path, which often precedes a snap back. A cross of the line marks a change in the short-term balance. Many traders use the line as a moving support or resistance reference.
Alert channels in this build: popup, push notification, email and sound.
EnableAlerts gates the alert block. EnablePopup, EnablePushNotify, EnableEmail and EnableSound pick between popup, mobile push, email and sound.
Every Auto TrendLine Indicator input
Core settings
| Setting | What it does | Default |
|---|---|---|
Lookback |
Lookback (default 40) sets how many recent closes feed the fit. A shorter window turns faster, while a longer one holds a steadier slope. | 40 |
Alert settings
| Setting | What it does | Default |
|---|---|---|
EnableAlerts |
(see inputs panel) | on |
EnablePopup |
(see inputs panel) | on |
EnablePushNotify |
(see inputs panel) | off |
EnableEmail |
(see inputs panel) | off |
EnableSound |
(see inputs panel) | off |
SoundFile |
(see inputs panel) | alert.wav |

Settings that fit your chart
EURUSD H1 appears in the screenshot for this build. Forty bars there cover about two trading days, which suits an intraday bias read. Scalpers shorten the window on M5 and M15 to keep the line close to price. Swing traders stretch it on H4 and daily charts instead. Any liquid symbol works, because the fit only ever reads closes.
When the Auto TrendLine Indicator fails
A least-squares line has no memory of structure. It ignores swing highs, swing lows and the touches a hand-drawn trendline respects. On a chart with a clean rising base, the fitted line can sit well away from that base.
The whole line moves. Each closed bar re-runs the fit, so yesterday’s line is not today’s line. A screenshot of an old signal will not match what the chart shows now. Plan around the current slope rather than a level you noted last week.
One outlier bends the fit. A news spike inside the window drags the line toward it for as long as that bar stays in range. Then the spike drops out and the slope jumps back without any warning.
The alert inputs sit in the panel, but this build fires no automatic message. Nothing watches for a cross of the line. Use a platform alert if you want one.
Copy auto-trendline-indicator-indicator-forexmt4systems.ex4 into MQL4/Indicators and auto-trendline-indicator-indicator-forexmt4systems.ex5 into MQL5/Indicators, then restart the terminal and drag the Auto TrendLine Indicator onto a chart. The step-by-step guide with screenshots is at how to install MT4 and MT5 indicators.
Download the Auto TrendLine Indicator for MT4 and MT5
The zip auto-trendline-indicator-indicator-forexmt4systems.zip holds auto-trendline-indicator-indicator-forexmt4systems.ex4, auto-trendline-indicator-indicator-forexmt4systems.ex5 and a short README, compiled files only. Enter your email below and the download link arrives in your inbox.
Download this indicator free
Enter your email and the file is yours. You also get the full MT4 and MT5 library.
FAQ
How does Lookback change the angle of the Trendline?
It changes the sample the fit reads. A short window follows the latest swing closely, so the angle turns quickly and the line hugs price. A long window averages several swings and produces a flatter, steadier slope. Pick the window that matches the swing you actually trade.
Why does the Trendline shift after every closed bar?
The fit runs again on each pass. It always reads the newest Lookback closes, so the oldest close drops out and a fresh one joins. Both the slope and the intercept then change a little. What you see is a rolling fit rather than a fixed drawing.
Is this the same as a hand-drawn trendline on EURUSD H1?
Not the same thing. A hand-drawn line connects two or more highs or lows that you choose yourself. This one balances the squared distance to every close in the window. On EURUSD H1 the two often point the same way, yet they rarely sit at the same price.
What happens when the Lookback window has no usable spread?
The solver divides by a term built from the bar numbers, and that term only collapses in an odd edge case. When it does, the code returns early and leaves the buffer alone. You then see a gap rather than a wrong line. The same guard fires when the chart holds too few bars.
Are results guaranteed?
No. Trading involves risk. Results are not guaranteed; past performance is not indicative of future results. Use the Auto TrendLine Indicator as one input. Always backtest before trading live.
Category: this is part of our Trend collection. Also, still, hence, plainly, truly, browse more Trend for MetaTrader, or explore every MT4 indicator and MT5 indicator on the site.
External references
- For background on this concept, see Trendline at Investopedia.
- For broader market context, see Chande Trend Meter Ctm at StockCharts ChartSchool.
