24

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


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

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

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


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.

Related Posts

24 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

  7. hello sir

    its Aneesh Here. i went through all the 4 tuitorial parts. its excellent and simple. i would like to make a request. kindly continue this series. with advanced functions. also pls explain a bit about the function “Barcount” . it would be helpful to all

    thanks a lot

  8. Thank you for the 4 tutorials! they have helped me review greatly . I look forward to continuing to learn more and more on AB AFL , thanks to your you-tube videos and this web site . My goal is to learn as much as I can to trade profitably .
    phil

  9. hi
    Is this possible Plotting coral trend indicator multiple Timeframes
    /==============================================================================================

    _SECTION_BEGIN(“Coral Trend Indicator”);
    SetChartOptions(0,chartShowArrows|chartShowDates);

    sm = Param(“Smoothing Period”,21,2,40,1);
    cd = Param(“Constant D”,0.4,0.1,2,0.1);

    SetBarsRequired(ceil(3.5 * sm)) ; // minimum lookback required for stability //

    di = (sm – 1.0) / 2.0 + 1.0;
    c1 = 2 / (di + 1.0);
    c2 = 1 – c1;
    c3 = 3.0 * (cd * cd + cd * cd * cd);
    c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd);
    c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd;

    src = Close;
    i1=0;
    i2=0;
    i3=0;
    i4=0;
    i5=0;
    i6=0;

    for(i=1;iRef(bfr,-1),colorBlue,colorYellow);

    Plot(bfr,”Coral “, color,styleDots|styleLine|styleThick);

  10. hi
    Is this possible Plotting coral trend indicator multiple Timeframes
    _SECTION_BEGIN(“Coral Trend Indicator”);
    SetChartOptions(0,chartShowArrows|chartShowDates);

    sm = Param(“Smoothing Period”,21,2,40,1);
    cd = Param(“Constant D”,0.4,0.1,2,0.1);

    SetBarsRequired(ceil(3.5 * sm)) ; // minimum lookback required for stability //

    di = (sm – 1.0) / 2.0 + 1.0;
    c1 = 2 / (di + 1.0);
    c2 = 1 – c1;
    c3 = 3.0 * (cd * cd + cd * cd * cd);
    c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd);
    c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd;

    src = Close;
    i1=0;
    i2=0;
    i3=0;
    i4=0;
    i5=0;
    i6=0;

    for(i=1;iRef(bfr,-1),colorBlue,colorYellow);

    Plot(bfr,”Coral “, color,styleDots|styleLine|styleThick);

  11. Is this possible Plotting coral trend indicator multiple Timeframes
    _SECTION_BEGIN(“Coral Trend Indicator”);

    SetChartOptions(0,chartShowArrows|chartShowDates);
    sm = Param(“Smoothing Period”,21,2,40,1);
    cd = Param(“Constant D”,0.4,0.1,2,0.1);

    SetBarsRequired(ceil(3.5 * sm)) ; // minimum lookback required for stability //

    di = (sm – 1.0) / 2.0 + 1.0;
    c1 = 2 / (di + 1.0);
    c2 = 1 – c1;
    c3 = 3.0 * (cd * cd + cd * cd * cd);
    c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd);
    c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd;

    src = Close;
    i1=0;
    i2=0;
    i3=0;
    i4=0;
    i5=0;
    i6=0;

    for(i=1;iRef(bfr,-1),colorBlue,colorYellow);

    Plot(bfr,”Coral “, color,styleDots|styleLine|styleThick);

  12. I am pleasantly surprised to see your great endevour in coding lessons in AFL. I shall be grateful if you could guide me to code MACD slow, RSI and Stochastic triggering divergences with Scanner and Explorer.
    A big thanks indeed. Please keep this noble work for small traders.

    • i want to use EMA 30 and 50 Cross over with daily value
      but want to implement on 3minute charts of intraday
      how to use this EMA values of daily in 3minute intraday chart
      i tried a lot using timeframeset () function but its not showing correct results and now showing both EMA
      please tell me how to do this

  13. i want to make my own strategy with use of some multiple indicators , parameters, filters & moving averages. would you make for me i will pay for this if you ready then please call me or write me on email.

  14. I am well-versed with C programming. I have a basic question related to AFL – is it necessary to “plot” charts? I mean, if I only want to backtest a strategy I can understand the requirement to have technical indicators coded in and also buy/sell signals. But is there a real need to “plot” the actual candlesticks?

Leave a Reply

Your email address will not be published.