Automatic trend line

The “automatic trend line” AFL (see below) from Amibroker library is a powerful little program. In order to evaluate it as an entrance or exit tool, I need to automatically draw a trend line with reference to a past bar. I thought the function “SelectedValue might be a g ood command, but I fail to make it to work. Could someone help me? Thanks.

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

In the same AFL Library, there are two codes in specific to your query written by Edward Pottasch that must help you immensely.

  1. Automatic Trendlines using fractal patterns
  2. Automatic Trendlines using multiple timeframes

Thanks. I repost it with correct format.

Thanks so much. I did a search, but did not get those.