Code Snippets – Plot Buy and Sell Arrows in Amibroker

AFL Tutorial : Beginners

Amibroker AFL script based trading system generally consist of four trading conditions Buy(go long), Sell(Exit Long), Short(go short) and Cover(Exit Short). In todays tutorial we are going to concentrate only how to plot a Buy Signal(Generally we Up arrow is preferred) and Sell Signal(Generally Down Arrow is preferred) using Amibroker.

The following Example discusses plotting arrows with 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.

Nifty Daily – Simple Arrows
Simple Arrows

/* 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 Square,Arrows,Triangle,Circle,Digits etc., to indicate the trading conditions visually over/below the candlesticks. However the simple method is not preferred instead one can try with plotting Block Arrows for 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.

Nifty Daily – Block Arrows
Block Arrows

/* 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(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);

Download EMA Crossover Simple Arrows afl code

Related Readings and Observations

Comments

  1. Udit says

    Dear Sir

    if my Red Line Cuts the Green Line several times Than how can we stop the system to take Multiple Positions

Leave a Reply

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