Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Automatic Trend-line for Amibroker (AFL)
kaiji
almost 15 years ago
Amibroker (AFL)

Rating:
3 / 5 (Votes 30)
Tags:
trendline, amibroker

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:
Newsletter

Similar Indicators / Formulas

Price With Regression and Trend
Submitted by Boknoy9999 almost 13 years ago
trend price
Submitted by ashokwins over 11 years ago
Advanced Trend Lines
Submitted by Arun over 13 years ago
Trendline Multiple TimeFrame
Submitted by vargasc1 over 12 years ago
ALT
Submitted by Miraz about 14 years ago
Marva Mid R-Line Signal
Submitted by isfandi about 14 years ago

Indicator / Formula

Copy & Paste Friendly
x = Cum(1);

perchg = 0.3*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 ) );

Aa = (endvalue1-startvalue)/(endbar-startbar);
b = startvalue;

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

Plot( Close, "Price", colorBlue, styleCandle );
Plot( IIf( x >= endbar, trendline, Null ), "Trendline", colorRed );

8 comments

1. AmerAussy

Doesn’t seem to work

2. martinsajms

Doesn’t seem to work

3. john_d

is it working?

4. bsedoha

Need modification.

5. vipingoyal

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 );
    }
}
7. vakkascoban

thanks

8. jaygouda

Thank you

Leave Comment

Please login here to leave a comment.

Back