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

  • How to do Portfolio Backtesting in Amibroker Portfolio Trading (Trading with Multiple Stocks/Instruments) is the next big step in a traders life once he understand that he want to reduce the volatility in his trading strategy. And […]
  • Supertrend V3.0 AFL code with Buy and Sell Alerts Supertrend V3.0 is the upgraded version of Supertrend Indicator. The trading logic remains the same however lot of bugs got removed and few features added in the current version. Multiple […]
  • N Bar High-Low Dots – Amibroker AFL code Here is a simple strategy that alerts by plotting dots over the candle whenever a new N bar high-low is made. The below chart shows Nifty future EOD charts and the Yellow dots in this […]
  • William VIX FIX Indicator – Amibroker AFL Code We know that VIX most of the time reflects the investor/traders sentiment. When VIX is higher it means lot of fear in the market and lower VIX indicates lack of fear in the market. The […]


  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

  2. Ravi says

    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.

  3. Ravi says

    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.

Leave a Reply

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