# Code Snippets – Plot Buy and Sell Arrows in Amibroker

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 */
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

``````/* 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.

Nifty Daily – 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(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

## Line Trading – OpenAlgo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...

## Mini Certification Course on Algorithmic Trading

Welcome to the Mini Certification on Algorithmic Trading using Amibroker and OpenAlgo! This comprehensive 4-part series is designed to equip you with the knowledge...

## Pivot Reversal Strategy – Tradingview Pinescript to Amibroker AFL…

Pivot Reversal Strategy is a popular in-built strategy in Tradingview Platform. Here I had attempted to convert the pinescript v5 to Amibroker AFL Code....

## 8 Replies to “Code Snippets – Plot Buy and Sell Arrows in…”

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

1. Hi you can remove exrem function to avoid such issues. For Example

2. Vikrant Sood says:

Can you help me how to place star sign when target or stoploss hit

3. 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.

4. 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.

5. Anil Nayak says:

How can we put alertif function when the plotshape is trigger

6. Anil Nayak says:

How can we put alertif function when the plotshape is trigger