### Contribute

Contribute an indicator to the library by clicking here. Note you must be logged in.

New! Stock Portfolio Organizer is now available:
• Shares, Margin, Futures and Forex
• EOD and Realtime data
• And Much More...
GoTo Stock Portfolio Organizer
New! WiseTrader Toolbox for Amibroker is now available with:
• Neural Networks
• And Much More...

## Automatic Trend-line for Amibroker (AFL)

A trend line is a sloping line drawn between two prominent points on a chart. Rising trend lines are usually drawn between two troughs (low points) to illustrate price support while falling trend lines are usually drawn between two peaks (high points) to illustrate upside price resistance. The consensus is that once a trend has been formed (two or more peaks/troughs have touched the trend line and reversed direction) it will remain intact until broken.

The trend line is described by well-know linear equation:

`y = ax + b`

where x represents time (bar number), y represents price, a defines the slope of the line and b defines initial offset. The main problem in defining appropriate AFL formula is finding the values of these two latter coefficients. If a trend line is drawn between two important lows the slope of the line could be calculated by subtracting the second low price from the first low price and dividing the result by a number of bars between the lows:

`a = ( low2 - low1 ) / ( bars2 - bars1 )`

Calculating offset (b) value is trivial when we shift the time scale so x=0 is located at the first low. In this case b=low1.

So our mathematical formula for the trendline between two important lows will look like this:

`y = ( x - bars1 ) * ( low2 - low1 ) / ( bars2 - bars1 ) + low1`

While determining low prices is simple (just point your mouse over the dominant low and read the low price from a data tooltip that appears on the screen), determining the bar number is not that simple. You can of course count bars by hand but this is simply too much work (especially when you don’t have Florida volunteers for a recount :-) ). Luckily we have AFL that allows us to do it in automatic way. All we have to do is to make a running total of bars (our x coordinate) using cum() function:

`x = cum( 1 );`

and then find out where low occured using valuewhen() function:

```bar1 = valuewhen( low == low1, x, 1 ); bar2 = valuewhen( low == low2, x, 1 );```

For more detailed description please check out:

## Formula

Doesn’t seem to work

Doesnâ€™t seem to work

3. john_d

is it working?

4. bsedoha

Need modification.

perfect sir thanks……………….i was looking for this since long

6. nxn
``````// version that plots multiple trendlines both above and below the prices
x = Cum(1);```

```Miny = Status("axisminy");
Maxy = Status("axismaxy"); ```

```
for(frack = 0.4; frack > 0.0005; frack = frack/1.3) {```

`    perchg = frack*LastValue( Highest( ROC( Low, 50 ) ));`

```    startvalue = LastValue( Trough( Low, perchg, 1 ) );
endvalue1 = LastValue( Trough( Low, perchg, 2 ) );```

```    startbar = LastValue( ValueWhen( Low == startvalue, x, 1 ) );
endbar = LastValue( ValueWhen( Low == endvalue1, x, 1 ) );```

```    if(EndValue1 < startvalue AND startbar != endbar AND EndValue1 < maxy AND Endvalue1 > Miny) {
Aa = (endvalue1-startvalue)/(endbar-startbar);
b = startvalue;```

`        trendline = Aa * ( x  - startbar ) + b; `

```        Plot( IIf( x >= endbar, trendline, Null ), "Trendline", colorRed );
}
}```

`for(frack = 0.4; frack > 0.0005; frack = frack/1.3) {`

`    perchg = frack*LastValue( Highest( ROC( High, 50 ) ));`

```    startvalue = LastValue( Peak( High, perchg, 1 ) );
endvalue1 = LastValue( Peak( High, perchg, 2 ) );```

```    startbar = LastValue( ValueWhen( High == startvalue, x, 1 ) );
endbar = LastValue( ValueWhen( High == endvalue1, x, 1 ) );```

```    if(EndValue1 > startvalue AND startbar != endbar AND EndValue1 < maxy AND Endvalue1 > Miny) {
Aa = (endvalue1-startvalue)/(endbar-startbar);
b = startvalue;```

`        trendline = Aa * ( x  - startbar ) + b; `

```        Plot( IIf( x >= endbar, trendline, Null ), "Trendline", colorGreen );
}
}``````

thanks

Thank you