How to Plot Buy and Sell Signals in Amibroker

AmiBroker is equipped with a powerful formula language allowing you to write trading system rules, define your own indicators and custom commentaries. In this article I am going to show you how to plot a buy and sell signal arrows in amibroker based on trading system rules.

To get buy and sell signal I am using a simple strategy i.e. 20 and 50 day simple moving averages, positive crossover of moving average is the buy signal and negative crossover of moving average is sell signal.




Bellow code is to get chart and moving averages,
 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( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );   
 MA20 = MA( C,20);   
 MA50 = MA( C,50);  

Below code is to get Buy and Sell condition,
 Buy = Cross(MA20,MA50);  
 Sell = Cross(MA50,MA20);  

Below code is to plot arrows,
 shape = Buy * shapeUpArrow + Sell * shapeDownArrow;  
 PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );  

So the final code is,
 _SECTION_BEGIN("MA Crossover System");  
 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( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );   
 MA20 = MA( C,20);   
 MA50 = MA( C,50);  
 ///condition for Buy and Sell///  
 Buy = Cross(MA20,MA50);  
 Sell = Cross(MA50,MA20);  
 ///For Ploting Arrows///  
 shape = Buy * shapeUpArrow + Sell * shapeDownArrow;  
 PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );  
 _SECTION_END();  

To use above code, just copy the above final code and paste it to amibroker formula editor and save it. By default it will be saved in Custom folder, to use this code in chart Go to > Window > Charts


After this select custom folder and double click on the formula file, that’s it.
Previous
Next Post »