Understanding AFL Basics
In this episode, we are going to understand the basics of Amibroker AFL scripting. In a step by step process, we are going to understand the AFL script used in the previous episode, so that it will be helpful to move forward faster in the forthcoming episodes.
In the last episode, we have learnt how to create a strategy and backtest it using amibroker. This was the script used in our strategy.
Script:
Buy = Ref(Close,-1)>Ref(Open,-1)
AND Ref(Close,-2)< Ref(Open,-2)
AND Ref(Open,-1)<Ref(Close,-2)
AND Ref(Close,-1)>Ref(Open,-2);
Sell = Ref(Close,-1)<Ref(Open,-1)
AND Ref(Close,-2)> Ref(Open,-2)
AND Ref(Open,-1)>Ref(Close,-2)
AND Ref(Close,-1)<Ref(Open,-2);
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBlue,0,L,Offset=-10);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,Offset=-10);
It basically takes a long position when a bullish engulf is formed in the chart and closes that long position when bearish engulf is formed. It can be used in any timeframe.
Now we are going to understand this script in a step-by-step process.
Item #1 – Buy:
Buy = Ref(Close,-1)>Ref(Open,-1)
AND Ref(Close,-2)< Ref(Open,-2)
AND Ref(Open,-1)<Ref(Close,-2)
AND Ref(Close,-1)>Ref(Open,-2);
As name implies, this line of script will trigger a Buy when the following conditions are met in the chart.
What are the conditions?
There are 4 conditions connected through AND operator. So a Buy will be triggered as and when all these 4 conditions are true.
Before understanding the conditions, let’s understand the use of “Ref” function used here.
“Ref” function can be used to reference previous or next candles from the current position. It needs 2 inputs – a parameter (like open, close, high, low) and a time-period. A positive value in time-period references to number of candles forward and a negative value can be passed to get data from the previous candles.
Example:
The formula “ref( CLOSE, -14 )” (in daily chart) can be used to get closing price of candle 14 days ago.
Likewise The formula “ref( CLOSE, 14 )” (in daily chart) can be used to get closing price of candle 14 days forward.
Back to our Buy Condition, let’s check what it does.
Ref(Close,-1)>Ref(Open,-1) à This will check whether closing price of previous candle is greater than open price of previous candle (just ensuring previous candle is green candle)
Ref(Close,-2)< Ref(Open,-2) à This will check whether closing price of 2nd last candle is less than open price of 2nd last candle (just ensuring 2nd last candle is red candle)
Ref(Open,-1)<Ref(Close,-2) à This will check whether open price of previous last candle is less than close price of 2nd last candle (just ensuring engulf starts from the bottom)
Ref(Close,-1)>Ref(Open,-2) à This will check whether closing price of previous last candle is greater than open price of 2nd last candle (just ensuring engulf extends till the top of red candle)
So, when we combine all these 4 conditions together, we will get a Buy signal whenever a bullish engulfing pattern is formed in the chart.
Item #2 – Sell:
Sell = Ref(Close,-1)<Ref(Open,-1)
AND Ref(Close,-2)> Ref(Open,-2)
AND Ref(Open,-1)>Ref(Close,-2)
AND Ref(Close,-1)<Ref(Open,-2);
As name implies, this line of script will trigger a Sell when the following conditions are met in the chart.
There are 4 conditions connected through AND operator. So a Sell will be triggered as and when all these 4 conditions are true.
Let’s check what our are our Sell conditions.
Ref(Close,-1)<Ref(Open,-1) à This will check whether closing price of previous candle is less than open price of previous candle (just ensuring previous candle is red candle)
Ref(Close,-2)> Ref(Open,-2) à This will check whether closing price of 2nd last candle is greater than open price of 2nd last candle (just ensuring 2nd last candle is green candle)
Ref(Open,-1)>Ref(Close,-2) à This will check whether open price of previous last candle is greater than close price of 2nd last candle (just ensuring engulf starts from the top)
Ref(Close,-1)<Ref(Open,-2)à This will check whether closing price of previous last candle is less than open price of 2nd last candle (just ensuring engulf extends till the bottom of green candle)
So, when we combine all these 4 conditions together, we will get a Sell signal whenever a bearish engulfing pattern is formed in the chart.
Item #3 – ExRem:
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
What is ExRem & what is its purpose?
ExRem is used to remove excessive signals. This will simply avoid taking multiple positions in the same chart at the same time.
To simply put, this line will ensure there will be only 1 active position at any point in time.
Let’s say we are using this script in 5 min chart. Consider a bullish engulf is formed on May 1, 9.30 AM.
Now, until we get a Sell signal due to formation of bearish engulf, all Buy signals will be ignored. There won’t be another trade even if another Engulf forms at 10.00 AM on the same day.
If we are not using ExRem, 2nd trade will be initiated.
Item #4 – PlotShapes:
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBlue,0,L,Offset=-10);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,Offset=-10);
PlotShapes is used to create different shapes or symbols in the chart automatically. This is just for simplifying our work. Above lines will create an arrow mark in the chart whenever a Buy and Sell signal is generated based on our conditions. So we can easily check the charts for the trades initiated.
Inputs needed:
Shape – First we need to specify the shape to be displayed. We are using UpArrow for Buy signal and DownArrow for Sell signal. We can draw arrows, triangle, square, circle, star, etc.
Color – Next specify the color to be used for that shape. I have used Blue color for UpArrow and Red color for DownArrow
Layer – 3rd input is for the layer, leave it as 0
YPosition – used to specify where the symbol should be displayed. In our case, I have given “L” for “Buy” & “H” for “Sell”, so Buy symbol will be displayed near low of the candle & Sell signal will be displayed near high of the candle.
Offset – Negative offsets shift symbols down, positive offsets shift symbol up.
These are the basic items you need to understand before you start with AFL scripting. AFL offers lot of features like creating your own charts, creating your own indicators. Even a complex strategy can be coded into an indicator, which will automatically do the trading for you. That’s the beauty of AFL.
In the forthcoming sessions, we can learn about them all.
If you have any queries about today’s episode, please leave it down in the comments section, I’ll definitely answer it.
Related
AFL Coding AFL Programming Basics Amibroker Amibroker AFL Programming amibroker tutorial videos buy sell signal software Free Afl for Amibroker free amibroker afl how to backtest amibroker afl? how to get free afl for amibroker how to install afl codes how to install amibroker how to start with algo-trading? how to use amibroker how to write amibroker afl scripts learn amibroker afl language