Continuing the Amibroker AFL Journey: Part 3
Welcome to Part 3 of our Amibroker AFL tutorial series. If you’re catching up, you can find Part 1 and Part 2 at these links:
Amibroker AFL: Step by Step Tutorial- Part 1
Amibroker AFL: Step by Step Tutorial- Part 2
Creating a Simple EMA Crossover Chart
//------------------------------------------------------
// Formula Name: Simple EMA Crossover
// Website: https://zerobrokerageclub.com/
//------------------------------------------------------
_SECTION_BEGIN("Simple EMA Crossover");
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 ) ) ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
Buy = Cross(ema( C, FastEMA ),ema( C, SlowEMA ));
Sell =Cross( ema( C, SlowEMA ), ema( C, FastEMA )) ;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;
Plot( Close, "Price", colorBlue, styleCandle );
Plot(ema( C, FastEMA ),"FastEMA",colorWhite);
Plot(ema( C, SlowEMA ),"SlowEMA",colorBlue);
_SECTION_END();
Recommended Reading: Mastering Amibroker – A Comprehensive Guide
Adding Buy/Sell Indicators to EMA Crossover
//------------------------------------------------------
// Formula Name: Simple EMA Crossover
// Website: https://zerobrokerageclub.com/
//------------------------------------------------------
_SECTION_BEGIN("Simple EMA Crossover with Buy Sell Arrows");
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 ) ) ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
Buy = Cross(ema( C, FastEMA ),ema( C, SlowEMA ));
Sell =Cross( ema( C, SlowEMA ), ema( C, FastEMA )) ;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;
Plot( Close, "Price", colorBlue, styleCandle );
Plot(ema( C, FastEMA ),"FastEMA",colorWhite);
Plot(ema( C, SlowEMA ),"SlowEMA",colorBlue);
/* 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(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, 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);
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);
_SECTION_END();
Identifying Inside Day and Outside Day Patterns
Note: Circle represents Inside Day and Star represents Outside Day
//------------------------------------------------------
// Formula Name: Simple EMA Crossover
// Website: https://zerobrokerageclub.com/
//------------------------------------------------------
_SECTION_BEGIN("Inside Day Outside Day Patterns");
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( Close, "Price", colorBlue, styleBar);
PlotShapes(IIf(Inside(), shapeSmallCircle, shapeNone),colorGreen, 0, H, Offset=100);
PlotShapes(IIf(Outside(), shapeStar, shapeNone),colorGreen, 0, H, Offset=100);
_SECTION_END();
Visualizing MACD Indicator with Signal Line
//------------------------------------------------------
// Formula Name: Simple EMA Crossover
// Website: https://zerobrokerageclub.com/
//------------------------------------------------------
_SECTION_BEGIN("MACD Crossover");
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot( Close, "Price", colorBlue, styleCandle);
fast = Param( "Fast avg", 12, 2, 200, 1 );
slow = Param( "Slow avg", 26, 2, 200, 1 );
signal1 = Param( "Signal avg", 9, 2, 200, 1 );
Plot(MACD(fast,slow),"MACD",color=colorRed,styleOwnScale);
Plot(Signal(fast,slow,signal1),"Signal",color=colorBlue,styleOwnScale);
Buy= Cross(MACD(fast,slow), Signal(fast,slow,signal1));
Sell= Cross(Signal(fast,slow,signal1),MACD(fast,slow));
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;
_SECTION_END();
Highlighting Price Levels at Buy/Sell Signals
//------------------------------------------------------
// Formula Name: Simple EMA Crossover
// Website: https://zerobrokerageclub.com/
//------------------------------------------------------
_SECTION_BEGIN("Plot Price Level");
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 ) ) ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
Buy = Cross(ema( C, FastEMA ),ema( C, SlowEMA ));
Sell =Cross( ema( C, SlowEMA ), ema( C, FastEMA )) ;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;
Plot( Close, "Price", colorBlue, styleCandle );
Plot(ema( C, FastEMA ),"FastEMA",colorWhite);
Plot(ema( C, SlowEMA ),"SlowEMA",colorBlue);
dist = 1.5*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "Buy\n@" + C[ i ], i, L[ i ]-dist[i], colorGreen,colorYellow );
if( Sell[i] ) PlotText( "Sell\n@" + C[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );
}
_SECTION_END();