Amibroker AFL: Step by Step Tutorial- Part 4

This is Part 4 of Amibroker AFL tutorial series. If you haven’t already gone through Part 1 ,2 and 3 of this series, please refer the below links:-

Amibroker AFL: Step by Step Tutorial- Part 1

Amibroker AFL: Step by Step Tutorial- Part 2

Amibroker AFL: Step by Step Tutorial- Part 3

Plot Text in Chart Area

//------------------------------------------------------
//  Formula Name:    Plot Text in Chart
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------

_SECTION_BEGIN("Plot Text in Chart");

Plot(Close,"Price",color=colorGreen,style=styleCandle);

GfxSelectFont("Times New Roman", 30, 700, True ); 
GfxSetBkMode( colorWhite );  
GfxSetTextColor( colorAqua ); 
GfxTextOut("Close Price:"+C, 900 , 15 );

_SECTION_END();


 

Popup Alert for Buy/Sell signal

//------------------------------------------------------
//  Formula Name:    Popup Alert for Buy/Sell Signals
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------


_SECTION_BEGIN("Popup Alert");

SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));

FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);

Buy = Cross(ema( C, FastEMA ),ema( C, SlowEMA ));
Sell =Cross( ema( C, SlowEMA ), ema( C, FastEMA )) ;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;

if (Buy[BarCount-1]==true)
{
PopupWindow("Your Strategy has given a Buy Signal","Buy Signal",30);
}

if (Sell[BarCount-1]==true)
{
PopupWindow("Your Strategy has given a Sell Signal","Sell Signal",30);
}

        
Plot( Close, "Price", colorBlue, styleCandle );
Plot(ema( C, FastEMA ),"FastEMA",colorWhite);
Plot(ema( C, SlowEMA ),"SlowEMA",colorBlue);

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


_SECTION_END();


Ami_Candle_Chart

 

Plotting EMA from multiple Timeframes

//------------------------------------------------------
//  Formula Name:    Multi Timeframe EMA
//  Author/Uploader: Trading Tuitions
//  E-mail: support@tradingtuitions.com          
//  Website: www.tradingtuitions.com        
//------------------------------------------------------

_SECTION_BEGIN("Multi Timeframe EMA");

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);
TimeFrameSet(inWeekly);
WeeklyEMA20=EMA(C,20);
TimeFrameRestore();
TimeFrameSet(inDaily);
DailyEMA20=EMA(C,20);
TimeFrameRestore();

Plot( TimeFrameExpand( WeeklyEMA20, inWeekly), "20 Period Moving average from Weekly Chart", colorRed ); 
Plot( TimeFrameExpand( DailyEMA20, inDaily), "20 Period Moving average from Daily Chart", colorBlue);

_SECTION_END();


Ami_Bar_Chart

This brings and end to our basic tutorial series on Amibroker formula language. Just go through the syntax carefully for each and every AFL in this and previous posts. Also try these out yourself in Amibroker. Seriously speaking this won’t make you and expert, but it will strong lay foundation which you can leverage to design complex trading systems. Going forward we’ll post AFL codes for profitable trading systems with backtest reports. Stay tuned for the same. As always, feel free to post your queries in comment box.

12 Comments

    • Hello Smash,
      Please post the Buy/Sell conditions for your strategy and we would write one for you.

  1. Hello Sir,
    Can u please write down afl for following conditions:

    Buy: @{ 8/34 EMA crossover in 15 min}, when {8 EMA is above 34 EMA in Hourly TF}

    Sell: @ {34/8 EMA crossover in 15 min}, when {8 EMA is below 34 EMA in Hourly TF}

    ie: buy when crossover condition in both Hourly & 15Min TF are same.

    Trade exit @ opposite crossover in 15 Min.

    Thanks.

    • Hello Gautam,
      If you go through the entire 4 part tutorial series you can easily write this AFL yourself. Give it a try once. Meanwhile, we’ll also work on this and post it in our website soon. Stay tuned.

  2. Hi,
    Thanks and Congratulations for this wonderful website. This is indeed one of the few Indian websites helpful for AmiBroker.

    I would be glad if you could help me with coding First trading day of the month & week and Last trading day of the month and week in AFL.

  3. Hi Neha,
    Thanks for your kind words. You may use Day() function to get first and last trading days of month and DayOfWeek() function to get first and last trading days of week.

  4. Hi,
    Had already tried the same and there is a caveat to what you say. The functions you’ve mentioned give you the first day of the month/week but they need not necessarily be the ‘Trading day’. What if first day of the month or some monday were a holiday ? Also, in case of last day, when you use day() >=28, it will invariably stop at 28 as array wise this condition becomes true and it stops there. what if the month contains 30/31 days. again if 28th of feb were a holiday, it will not consider it at all and jump.

    • Hi Neha,
      Amibroker cannot automatically identify non-trading days or holidays. It’s the data providers who filter all non-trading days. If your use case demands, you can maintain a separate binary array for trading and non-trading days and multiply it with array output of Day() function to get your task done.

  5. An excellent initiative by you in making newbies (like me!) aware of Amibroker and its programming language “afl”.
    Request if you could resolve small query:- Say your 20EMA and 50EMA crossover has occurred with MACD and of-course by using ExRem(Buy,Sell) we are able to filter out multiple arrows plotting, but let’s say although the conditions changed but did not trigger sell and again the same buy condition has triggered ; how can we plot second buy arrow at that condition. Your help will be highly appreciated.

  6. Dear Admin
    I Sincerely request you to extend this AFL tutorials explaining nesting functithons like IF ELSE, DO WHILE, FOR NEXT

    Also a separate session for common coding mistakes

    this would make people more knowledgable in developing or understanding the AFL’s

    Hope you would consider the same.

    Regards
    Venkat

Leave a Reply

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