AFL Tutorial: Beginners
An Amibroker AFL (AmiBroker Formula Language) script-based trading system typically includes four key trading conditions: Buy (to initiate a long position), Sell (to exit a long position), Short (to initiate a short position), and Cover (to exit a short position). In today’s tutorial, we will focus exclusively on how to illustrate a Buy Signal (usually represented by an upward arrow) and a Sell Signal (commonly represented by a downward arrow) using Amibroker
The following Example discusses plotting arrows with a simple EMA crossover trading system.
EMA Crossover with Simple Arrows
Buy and Sell Rules are defined as
EMA1 = EMA( C,20);
EMA2 = EMA( C,50);
/*Buy and Sell Condition */
Buy = Cross(EMA1,EMA2);
Sell = Cross(EMA2,EMA1);
This code snippet plots a plain vanilla simple Buy(Green) and Sell Arrows(Red) and this snippet should be pasted below the Buy and Sell Trading conditions in the AFL code.
/* Plot Buy and Sell Signal Arrows */
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );
GraphXSpace = 5;
The PlotShapes Functionality is generally used to plot shapes like squares, Arrows, triangles, Circles, Digits, etc., to indicate the trading conditions visually over/below the candlesticks. However, the simple method is not preferred instead one can try plotting Block Arrows for a better user experience.
Download EMA Crossover Simple Arrows afl code
EMA Crossover with Block Arrows
The following code snippet plots a Block Arrows with Buy(Green) and Sell Arrows(Red) Signals and has better user experience when compared to simple arrow plots.
/* 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(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);
PlotShapes(Sell * shapestar, colorBrightGreen, 0, High, 12);
PlotShapes(Cover * shapestar, colorRed, 0, Low, -12);
Download EMA Crossover Simple Arrows afl code
Dear Sir
if my Red Line Cuts the Green Line several times Than how can we stop the system to take Multiple Positions
Hi you can remove exrem function to avoid such issues. For Example
Buy = Exrem(Buy,Sell);
Sell =Exrem(sell,Buy);
Can you help me how to place star sign when target or stoploss hit
I have written some ‘if/else if’ condition depending on price, from which I am getting trend. I am assigning this trend to buy and sell signals. After that I am display buy/sell signal and it is displaying correctly.
After ExRem these buy/sell signals are becoming zero. why ? Could please suggest me.
I have already sent the same request by mail and Mr. Rajendran asked me the whole code snippet. I have already sent it to him.
How can we put alertif function when the plotshape is trigger
How can we put alertif function when the plotshape is trigger
Please help
Hello Sir,
I would like to plot a square or a rectangle on the chart to indicate the candlestick patterns. For ex: If a bullish Engulfing pattern is formed on the chart, I would like to high light the pattern i.e. both candles by drawing a square on that pattern. Is that possible, if yes, then how can I achieve that? please help me on that.
Thanks
Tej