WiseTrader Toolbox

AutoTrendline

Background

A trendline is just a straight line drawn under a run of prices so that it touches several of the lows (or highs) along the way. Traders use it as a moving line of support or resistance: while the price keeps bouncing off the line the trend is intact, and when the price finally breaks through it that can be a signal something has changed. Drawing these by hand is slow and a little subjective, so this function does it for you.

AutoTrendline looks back over a window of bars and tries every possible line that ends near the most recent bars. For each candidate line it counts how many times the price comes close enough to "touch" it, and how many times the price drops through it. It keeps the line with the most touches that still passes your rules, and returns that line as an array you can plot. It also writes out the line's coordinates and touch count as AFL variables so you can use them in a scan.

Included formula

The toolbox ships a ready-made Trendline Scanner built on this function, so you can use it without writing any code. It runs as an Exploration: in AmiBroker open the Analysis window, pick Trendline Scanner from the WiseTraderToolbox formulas, choose a watchlist and click Explore to list every symbol that currently has a trendline touched the required number of times. Applied to a chart, the same formula draws the trendline it found.

The touch threshold, the number of touches required and the look-back are all set from its parameters. The toolbox also ships a Triangle Pattern Scanner and a Combined Pattern Search that build on this same function.

The exported function

If you would rather build your own scan, the toolbox exports the calculation as a single AFL function you can call directly — it is the engine inside those formulas.

AutoTrendline(TargetArray, ID, Threshold, MinDistance, BarsBack, NoTouches, AllowSmallBreaks, NoBreaksAllowed, SearchPeriods)

Parameters

ParameterDescription
TargetArrayThe array the trendline is fitted to. This is usually the price, but because it takes an array you can also fit a trendline to an indicator such as RSI.
IDThe function sets some variables in AmiBroker and uses this string as a prefix for their names. For example, if ID is set to 'id1', one of the variables set by the function will be 'id1_trendlineTouchesCount'. Giving each call its own prefix lets you run the function more than once without overwriting earlier results.
ThresholdA percentage. How close the price has to come to the line to be counted as a touch. Small values mean the price must sit almost exactly on the line.
MinDistanceThe minimum number of bars that must sit between two touches. This stops a single cluster of bars from being counted as many separate touches.
BarsBackHow far back the trendline is allowed to end. If the last touch was 5 bars ago and this is set to 5, that trendline is still included in the results. A value of 0 only finds trendlines the price is touching right now.
NoTouchesThe minimum number of touches a trendline needs before it counts as a valid result. Must be at least 2 (it takes two points to draw a line).
AllowSmallBreaksSet to true (1) or false (0). When true, the price is allowed to dip below the line by the percentage in Threshold and the line is still treated as touched rather than broken.
NoBreaksAllowedHow many times the price is allowed to close through the line and the line still counts as valid.
SearchPeriodsThe number of bars to search over when looking for a trendline.

Exported Variables

As well as returning the trendline as an array, the function writes the following variables into AmiBroker. Each name is prefixed with whatever you passed as ID.

VariableDescription
_trendlineTouchesCountThe number of touches detected for the best trendline. If no trendline was found this is 0.
_startX, _startYThe bar number and value of the start point of the trendline.
_finishX, _finishYThe bar number and value of the end point of the trendline.

Usage

tl = AutoTrendline( Low, "tl", 0.5, 5, 0, 3, False, 0, 100 );
Plot( Close, "Price", colorDefault, styleCandle );
Plot( tl, "Auto Trendline", colorRed );

touches = StaticVarGet( "tl_trendlineTouchesCount" );
Filter = touches >= 3;
AddColumn( touches, "Touches" );
Note

If the parameters can't produce a valid line (for example fewer search periods than the touches and spacing you asked for) the function returns an empty array and the exported touch count is 0. Loosen Threshold or lower NoTouches if you get nothing back.