Amibroker: How To Scan The Market For Buy And Sell Signals


Beginners Course  |  Intermediate Course  |  Amibroker Course

If you’ve been through our other articles you will already know how to set up a watchlist in Amibroker and Backtest a stock with some simple Amibroker Formula Language.

But knowing how to scan the market for buy and sell signals is also a great skill to have, and Amibroker makes it extremely easy.

Scanning A Watchlist Or Portfolio For Buy And Sell Signals Using Amibroker

First, we need some Amibroker Formula Language to tell us where to buy and sell.  For the purposes of this lesson, we will use a simple highest high / lowest low trading system.  You can use any system you like, provided it has the buy and sell signals put in.  Please note that I do not use this method in my own trading, I am simply using it as an example.

  • From the main screen in Amibroker, open the formula Editor by going to Analysis > Formula Editor.
  • Paste in the following code to Notepad and then Amibroker (or type it in if it doesn’t work):

Uphigh = HHV (C, 90);
downhigh = LLV (C, 40);
Buy = C >= Uphigh;
Sell = C <= downhigh;
Buy  = ExRem (Buy,Sell);
Sell = ExRem (Sell,Buy);

  • The first part sets up our highest high and lowest low values – in this instance a 90 day high and 40 day low.
  • The second part tells Amibroker to use those signals to trigger buy and sell signals.
  • The last part tells Amibroker to only show the first signal triggered by a higher high or low.

You can save the code if you like, then send it to the Automatic Analysis by going to Tools > Send To Auto Analysis.

Now we need to select the watchlist that we want to scan for our buy and sell signals.  In Automatic Analysis, we will work from left to right.

  • Under “Apply to”, select “use filter” and select the watchlist of your choice.
  • Under “Range” select “last n days” and make the value either 1 or 2 (i.e. the last 1 or two trading days for our signal)
  • Click Scan, and it will scan the market for our signals and show the ones with signals from the most recent day.

You Can See The Buy And Sell Arrows On Chart As Well

As with our individual stock backtest, we can see the Buy and Sell arrows for the stocks in our portfolio too.


Go to our chart that is open in Amibroker and right click, we get options like “Parameters” and “Edit Formula”.

  • Click on “Parameters”.
  • Go to “Axes and Grid” tab
  • Scroll down and select “Show Trading Arrows” > Yes Click OK
  • Now if we go back to our Automatic Analysis screen (Analysis > Automatic Analysis), run the Scan then;
  • Right Click on the stock name (with the details and results) Select “Show Arrows For Actual Trades”

And you will be able to see a Green buy arrow and a Red sell arrow on your chart where the system bought and sold, including the latest buy or sell signal.

I hope this helps, happy trending and enjoy!

Dave McLachlan

Beginners Course  |  Intermediate Course  |  Amibroker Course Home

Videos in the FREE Amibroker Course:

  1. Amibroker: How To Import A Watchlist With A CSV File
  2. Amibroker: How To Backtest A Single Stock Or Index
  3. Amibroker: How To Backtest a Portfolio or Group of Stocks
  4. Amibroker: How To Optimize A Trading System
  5. Amibroker: How To Scan The Market For Buy And Sell Signals
  6. Amibroker: How To Use Explore To Find And Filter Stocks
  7. Amibroker: How To Add Position Sizing Using AFL
  8. Amibroker: How to Add An Index Filter To Your Trading System Using AFL
  9. Amibroker: How To Plot Your Trading System On A Chart (incl. Buy/Sell Arrows)
  10. Amibroker: How to Use the If, Then Else Function (IIF)
  11. Amibroker: How to Code the ATR Trailing Stop (Chandelier) using ApplyStop
  12. Amibroker: How to Plot ApplyStop on a Chart
  13. Amibroker: How to Code Van Tharp Position Sizing (Fixed Fractional)
  14. Amibroker: How to “Scale In” to Positions (1) – Buying Once a Month
  15. Amibroker: How to “Scale In” to Positions (2) – Pyramiding Three Times
  16. Amibroker: How to Use “Cross”, When One Thing Crosses Another
  17. Amibroker Advanced: How to Compare Equity Curves

FREE Trading System Video Lessons:

  1. 7 Free Trading Systems and Their Returns From The Last 13 Years
  2. Seven Full Trading Systems Revisited, With Completely New Data
  3. Trading System: How to Create A Buy and Hold Trading System
  4. Trading System: How to Code “Sell In May And Go Away”
  5. Trading System: How to Code a Moving Average Channel That Made 23% p.a
  6. Trading System: How to Code Highest Highs As Used In 52 Week Highs Or Turtle Trading System
  7. Trading System: How to Code a Bollinger Band Breakout Trading System
  8. Trading System: How to Code a Moving Average Crossover That Returned 28% per annum
  9. Trading System: How to Refer to Previous Signals For MAup Trading System
  10. Trading System: How to Code the Leap Of Faith (Gaps) Trading System That Returned 24% p.a.
  11. Trading System: How to Code a Short Selling System (Plus Results)
  12. Trading System: Meb Faber Trading System Using Different Timeframes
  13. Trading System: How to Code the Three Billion Dollar Trading System

FREE Amibroker Q & A Videos:

  1. Amibroker Q & A: How To Do A Monte Carlo Test? (on ALL versions)
  2. Amibroker Q & A: Visualising Monte Carlo Data with Histograms and Scatter Plots
  3. Amibroker Q & A: How to Create the 52 Week High/Low Index with AddToComposite
  4. Amibroker Q & A: How to Code Trend Pilot: Five Consecutive Days Above or Below a Moving Average
  5. Amibroker Q & A: Three “Hacks” To Improve Your Coding Skills
  6. Amibroker Q & A: How to Remove Excess Signals

June 3, 2012  Tags: , , , , , ,   Posted in: Amibroker, Amibroker Course, Articles On Building Wealth

8 Responses

  1. Matt - August 16, 2013

    Hi Dav,

    Great website and enjoy your regular stock market analysis, you seem really passionate and enjoy the challenge.

    In regards to Amibroker, are you able to create a tutorial for candlestick analysis with AFL ie scanning the maket to find typical candle patterns such as doji, shooting stars, hammers, bullish engulfing etc.

    I am in the process of deciding which software package to buy. Considering NinjaTrader and, Metastock, you seem pretty happy with Amibroker. Any thoughts? .obviously would like one that is price friendly and has good support in terms of coding.

    Also, what data subscriber do you go through ie End of Day data.



  2. Dave McLachlan - August 16, 2013

    Hey Matt,

    True, I like Amibroker – it’s stunning value for money considering the things it can do. And there are a lot of good, talented people out there, coding things and posting them to blogs, forums and the Amibroker site itself. But it’s not the only program out there, as you say, and like anything worthwhile, it requires a fair bit of work 🙂

    I could definitely do up something around candlesticks! Nice idea. Surely it’s been done before, but I could definitely go over the basics if I can figure them out.

    I use Bodhi Gold for my data, which seems to do ok, but there are others out there too that might be better for your needs. A quick Google search will usually bring up a few!

    – Dave

  3. Li Li - October 22, 2013

    Hi Dave,

    I tried this paste it on, tried typing, but nothing works. It keep on saying : Formulas\Custom\Buy/Sellsignals.aft contains an invalid path. Im still new to Amibroker and didn’t know why it didn’t work.

    I would appreciate your help. Thanks.

    Li Li

  4. houman - December 14, 2013

    I need fibonacci retracement indicator(afl) that scan (and show arrow )buy and sell signal for me and want to generate buy sell signals on cross overs. I am urgently looking for some one who could write a AFL for me on Professional basis. f

    please write this subject(when you send me that indicator:fibonacci retracement scanner

  5. Dave McLachlan - December 15, 2013

    Hey Houman,

    You’re on your own on this one – I have no need to figure that out at the moment. But I’m sure it wouldn’t be too hard to do – In fact there is Fibonacci scanning code out there already if you do a quick Google search. Adding your buy and sell signals from there is just a matter of using “Cross(Array, Array)” and “Plotshapes” function.

    You can do it!


  6. Bulan - April 27, 2014

    Hi Dave,

    I am currently trying all of your system into daily chart of National Stock Exchange in India and all the results are amazing. I have around 15 years of daily data with me for all stocks. And I am trying to backtest all of them from 2000 January till last week. All system gave similar drawdown in 2008. So I think all the systems are very robust. You did a great job of robustness in simplicity.

    Now the reason of writing this. According to this video, I did a scan of buy-sell signal and it actually shown which to buy and which to sell. But for me real problem is somewhere else if you can through some light.

    Suppose from 1st May, I am starting a very new porfolio having 50,000 initial capital. In your code, you are using 20 maximum stock with 5% capital to risk at each.

    So, when I did a buy-sell scan, it tells me what scrips to buy on 1st may (if there is any signal), suppose I got two scrips to buy on 1st may.

    How do I know how much quantity to buy? How amobroker tells me in an ongoing method? How ami will track my portfolio of 50,000 initial capital and go on recommending no. of shares to buy everyday?

    This looks a bit challenging to me, if you throw some light to do an example article of a complete portfolio-management using ami.

    Thank you for your time.

    May God Bless all of us,

    Warm Regards,

  7. RAVINDRA - September 4, 2015



  8. sharewasi - October 12, 2016

    I am interested in learning afl coding.
    I do’nt have any idea about coding.
    How can you help me out,starting from the basics?

Leave a Reply