Amibroker AFL: Step by Step Tutorial- Part 1

Amibroker is considered as one of the most important tool for Traders. It’s widely popular across the globe and aids both discretionary and system traders. It comes bundled with a highly powerful backtesting and optimization engine, apart from the usual charting features. One can code custom indicators and also build a fully automated trading system out of Amibroker. In order to build a trading system through Amibroker you need to be acquainted with Amibroker formula language (Amibroker AFL). It’s a high-level programming language and very easy to understand if you start from basics. Even a person from non-programming background can learn AFL and avoid spending unnecessary on expensive ready-made AFL’s.

In this post, we’ll try to learn Amibroker AFL from the scratch with proper examples and downloadable code. I assume that you have already setup Amibroker in your machine and subscribed to live or Historical data feed. Also a basic understanding to Technical analysis and trading terminologies is a pre-requisite for Amibroker coding.

The codes in this and subsequent posts are copy-paste enabled. These are pretty straightforward and complexity will gradually increase. Feel free to comment if you are stuck at any point.

My First Amibroker AFL: Plotting a line chart

//------------------------------------------------------
//  Formula Name:    Basic Line Chart
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------

_SECTION_BEGIN("Basic Line Chart");
Plot(Close,"Price",color=colorBlue,style=styleLine);
_SECTION_END();

 

Plotting a Candlestick chart

//------------------------------------------------------
//  Formula Name:    Basic Candlestick Chart
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com
//  Website:
//------------------------------------------------------

_SECTION_BEGIN("Basic Candlestick Chart");
Plot(Close,"Price",style=styleCandle);
_SECTION_END();

Ami_Candle_Chart

 

Plotting a Bar Chart

//------------------------------------------------------
//  Formula Name:    Basic Bar Chart
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------

_SECTION_BEGIN("Basic Bar Chart");
Plot(Close,"Price",color=colorBlue,style=styleBar);
_SECTION_END();

Ami_Bar_Chart

 

Plotting price with volume

//------------------------------------------------------
//
//  Formula Name:    Price with Volume
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------

_SECTION_BEGIN("Price");
Plot( C, "Close", colorDefault,styleCandle ); 
_SECTION_END();

_SECTION_BEGIN("Volume");
Plot( Volume, "Volume", color=colorGrey40, ParamStyle( "Style", styleHistogram | styleOwnScale | styleThick, maskHistogram  ) );
_SECTION_END();

Ami_Bar_Chart

 

Customizing chart title

//------------------------------------------------------
//
//  Formula Name:    Displaying Chart Title
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------

_SECTION_BEGIN("Chart with Title");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot(Close,"Price",color=colorBlue,style=styleCandle);
_SECTION_END();

Ami_Bar_Chart

I hope now you are comfortable with the basic AFL syntax. Give it a try yourself in Amibroker, try changing few parameters in AFL functions and see how it would change the chart. We’ll soon come up with Part-2 of this tutorial series.

18 Comments

  1. Hello Sir, I am New to Amibroker,is there a way where we can use amiboker freely using historical data or we have to use it only after paying it.Could you please let me know .

  2. I found your article very good.
    I do not have any knowledge about coding.
    Can you people help in creating strategy ?

  3. I want to start intraday. Which will be the best version of amibroker. What should be the configuration of PC. Thank u.

  4. Hello Sir, I am New to Amibroker,is there a way where we can use amiboker freely using historical data or we have to use it only after paying it.Could you please let me know .
    and its supported with amibroker 6?

  5. that was so perfect. thanks for all this. I am also looking for a indicator which is so simple but not available.
    When Rsi crosses EMA with a sealing of RSI 20 to 80 either side to reverse.

  6. Hello,
    I am new to amibroker. Can somebody help me with following strategy:
    Time frame: 1 day (i.e. EOD)
    Buy 250 shares if WMA(8) > WMA(13) and/or WMA(13) > WMA(21)
    Cover 50 shares at 1.5% profit … Trailing SL 2%
    Cover next 50 shares at 2.5% profit … Trailing SL 2%
    Cover next 50 shares at 5% profit … Trailing SL 3%
    Cover next 50 shares at 10% profit … Trailing SL 4%
    Cover next 50 shares or balance shares if WMA(8) < WMA(13) and WMA(8) < WMA(21) … Trailing SL 4%

    Also i want to create a backtest report…

    Would be very thank full if somebody helps / shares afl…

  7. Hi , thank you could please show me the code for open , h , l , c of open interest. . I would like to print on chart in text format open (OI) = 345643 … some thing in those lines .. appreciate your help

  8. I want to make a ALF Sir should we scan stocks on the basis of three different Supertrend values should be different if all Supertend shows buy signal at same time then also stock is scan. Please help me sir

Leave a Reply

Your email address will not be published. Required fields are marked *