42

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.

Also Read: Getting Started with Amibroker – Features, Pros-Cons & Learning Resources

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, SelectedValue( ROC( C, 1 ) ) ));

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.

Related Posts

42 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

  9. Hello sir,
    i want to know my strategy coding.please help me

    –> PDI crossover to MDI in ADX chart after that within 5 minute MDI should crossover to PDI.
    when MDI crossover to PDI at that time EMA line shoud crossover to Supertrend line or supertrend line should crossover to EMA line if this conditions not satisfied then also check within 2 minute after MDI crossover to PDI EMA or Supertrend crossover to each other then
    Give BUY signal.

  10. Say, i wish to learn the language through detailed basics and not just rotting down without understanding the basics of programs and following copy-pasting strategy. What would you recommend? Has any book been published specifically not without reference to C or java?

    • Hi Jatin,

      Check out the Amibroker official guide or books from Howard Bandy. However, there is no specific reference to Java or C.

  11. Hi Admin I am looking to Buy Amibroker for Auto Trading with Zerodha with following condition.
    1. Can it automate trade for Sell in Bracket Orders based on my current holdings & their Purchase Price.???

    Please guide.

  12. I am looking to purchase Amibroker. Have been using MS.
    Read the tutorial section of Amibroker manual – cannot figure out how basic line studies (fib, trendlines etc) are saved, so they are attatched to the chart and will display when the chart is next opened. In MS I save as a layout !!

  13. Hi.. I just created .AFL file with the code mentioned in ” My First Amibroker AFL: Plotting a line chart” Now how to run this code in Amibroker? Please explain in screenshots.

  14. Question:

    If your strategy already works— how do you further refine it and at the same time you do not ruin it? I mean they say that “if it ain’t broke then don’t fix it”. In this case then, for those trading longer than myself and who have found their groove– how do you ensure that the refinement you are trying is would not be detrimental to an already working process, and in fact can further help improve that already profitable strategy?

    • Great question. Many people ruin their strategy in the process of further refinement. A more technical terminology for the same is “Optimization biasing”. People tend to over optimize their strategy such that it exactly follows the historical data and may be useless for the future unknown data. The best way to refine a strategy without ruining it is to adopt advanced techniques like walk forward analysis or Monte Carlo simulation. I hope this answers your question

  15. Hi
    can i set different algo for different shares?
    eg: I want to buy/sell if a share opening price crosses ‘x value’ and sell/buy if it gets to ‘y value’.
    I want to set this for different shares.
    can i do this?

    • Hi Adesh,

      Yes it is possible, you just need to setup buy/sell logic based on currently selected symbol

  16. I pasted these code into Formular Editor – What should I do next? Where can I see those resulting charts?

  17. Hi,I have a trading software called sure shot formula. Can you reproduce the same for me and give it to me with all the codes etc and run it by amifeeder

  18. Hi,I have a trading software called sure shot formula. Can you reproduce the same for me and give it to me with all the codes etc and run it by amifeeder.what will you charge for the same

  19. hi, i would like to learn to write an afl code on my own. i dont have any background
    of writing afl code, can you teach me to write it. do let me know. thankyou

  20. I have been trying to get an AFL to blend 2 OR more Candlesticks. which results in a 3rd Candlestick formation? Please take this input for writing AFL code
    The “Open “of the First Candlestick.
    The “close” of the last candlestick
    The:” HIGH” AND “LOW” of the pattern
    When merged /blended together would give a 3rd new Candlestick. formation.

    • Hi,

      Can you please elaborate what you mean by “first” and “last” candlestick here

Leave a Reply

Your email address will not be published.