A scan for breaking a trend line?


Searching the archives of the previous Yahoo Group and the new Forum, I still am wondering:

What is the process for scanning a WatchList for end of day stock prices which penetrated through a trend line today? For instance a scan for “stocks whose intraday low fell through a trendline today” and a scan for “stocks whose intraday high rose through a trendline today”. Assume each chart has more than one trendline.


(nickname ‘Joe’)

Hello Joe,

Yoy can use Scan or Exploration for that. Take a look at this article:


Besides you can find lots of other interesting articles in AmiBroker’s Knowledge Base. They are really worth studying:



Thanks for reply, Milosz.

The web link you provided shows a useful 2007 web page with screen shots and a reply from Tomasz.

Tomasz, how to manage a WatchList of 20 stocks and each stock has three trend lines? Do all 60 trend lines need individual Study ID names, or are individual Study ID names only needed on the chart of one stock ? For instance could the first stock’s three trend line Study ID names of Trend1 , Trend2, and Trend3 be used again as the trend line names of the next 19 stock charts and in the AFL code for the scanning the entire WatchList?


Kevin, it doesn't matter when Tomasz wrote this article (and it is not a reply - it is an article from AmiBrokers' Knowledge Base) as it is still up-to date.

Did you read the questions and answers below this article? Did you try to make use of the information and write your own AFL before asking Tomasz for more guidance? In my opinion all the necessary information is already there. Read the article carefully and try out the provided information.

All you need to do is select one chart (with its unique Chart ID) and draw a trendline or a couple of trendlines on this chart. You must assign them a Study ID. For example RE for resistance and SU for support. Then repeat this process for n next stocks/symbols ... When you draw a support, assign it SU, when resistance RE etc. Use only one selected chart for that.

Then using Exploration or Scan you can check if the resistance/support was penetrated. For example:

Buy = Cross(Close, Study("RE", 1681));
Sell = Cross(Study("SU", 1681), Close);


Buy = Close > Study("RE", 1681);
Sell = Close < Study("SU", 1681);

ChartID in this case = 1681.

A quote from https://www.amibroker.com/guide/afl/study.html

STUDYID is a two-character identifier of the study. identifiers are: "UP" - uptrend, "DN" - downtrend, "SU" - support, "RE" - resistance, "ST" - stop loss, however you can use ANY identifiers (there are no limitations except that AmiBroker accepts only 2 letter codes).

CHARTID - identifies the chart pane where the study was drawn - you can find out what is the chart ID for given chart by looking in Parameters dialog, Axes & Grid, Miscellaneous: Chart ID or using GetChartID() AFL function.

If you need some additional information:


I've been using this solution for a long time, and it works for me really well. It should work for you as well.


… and with referece to your question. If you have a watchlist of 20 stocks and each stock has 3 trend lines, you will not be using 60 Study IDs. You just need a few Study ID’s like “UP” - uptrend, “DN” - downtrend, “SU” - support, “RE” - resistance, “ST” - stop loss - even if your watchlist consisted of 10 000+ stocks :wink:

You can use ANY identifiers, but AmiBroker accepts only 2 letter codes, so you can not assign names like “Trend1” or “Trend2”.

StudyIDs have to be two characters.

So “SU”, “RE” etc. work.
But also “S1” to “S9” or “R1” to “R9” or “T1” to “T9” or … do work.
“00” to “99” do work also. Two characters!
So it means you may use loop to plot all studies having such consecutive StudyIDs.
So there are enough possible combinations available to create hundreds of study calls.

You don’t need to create 60 IDs if you just need three ones per symbol.
If you create “T1” ID for symbolA and create “T1” ID for symbolB then each “T1” will be independent from each other. So you don’t need to create T1 for symbolA and T2 for symbolB.

/// @link http://forum.amibroker.com/t/a-scan-for-breaking-a-trend-line/641/2
// Keep in mind that Addcolumn output of upper simple sample code will be Null for cells where there is no trendline in the chart! 
// So scroll down then instead of screaming "Code does not work! No output!".

chartID = ...; // callable chart ID value

for( i = 0; i < 10; i++ ) // iterating studyIDs T1 to T9
    st = Study( "T" + i, chartid ); // calling manually drawn studies from other pane

    if( LastValue( Highest( st ) ) != 0 ) // if study exists
        // plot studies in another chart or pane other than the one of the manually drawn studies
        if( i < 500 )
            Plot( st, "T" + i, colorRed, styleLine );
        // output all manually drawn studies of a specific chart pane (see chartID) in Exploration table
        AddColumn( st, "T" + i, format = 1.2, -1, -1, 80 );   

Filter = 1;

I am looking for afl like this:

Please help to fix it…

Condition = (Cross (Study ("RE", 1), Close)) AND (Close>= 1.1*( “price in cross point” ));

Filter = Condition;

AddColumn ( Condition, "Condition", 1.0, colorDefault, IIf( Condition, colorGreen, colorDefault ) );

AddColumn (price in cross point, “price in cross point”);

AddColumn (Close, “close”);