Saturday, 9 November 2013

Amibroker AFL for Moving Average trading with buy sell signal

Today I am posting another AFL used in Amibroker,  that could be used for trading based on moving average and longer time frame. The period of moving average could be adjusted according to the users trading strategy. I have used 5 and 20 moving averages as an example.  Period 1 can be extended till 30 and the Period 2 can extend upto 200 maximum. These parameters are sufficient for any trader to find the trend and do trading. 

The Moving Average properties offers a  wide range starting from Simple moving average (MA), Exponential Moving Average(EMA), Double Exponential Moving Average (DMA), Triple Exponential Moving Average (TEMA), Weighted Moving Average(WMA), Hull Moving Average (HMA), Wilders Moving Average (WMA).  Apart from these parameters, trading can also be done using Linear Regression Lines.

In the properties  window the user has to adjust the MAType I and MAType 2 according to the table given below. Both MAType 1 and MAType 2 should be the same number selected from the table given below.
No.1    MA      Moving Average
No.2    EMA    Exponential Moving Average
No.3    DMA    Double Exponential Moving Average
No.4    TEMA Triple Exponential Moving Average
No.5    WMA   Weighted Moving Average
No.6    HMA    Hull Moving Average
No.7    WMA   Wilders Moving Average
No.8    Linear Regression Lines

Based on the settings the AFL generates buy sell signals.This strategy, if wisely used can yield fruitful results. This is also useful in deciding the trend of the market direction. 

/////////////////////////////////////////////////////copy the code below this line and paste it in the code editor and save it giving any name you like. Double click your new AFL and the chart will appear. enjoy //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
_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() );

p = Optimize("PERIOD 1",Param("PERIOD 1",10,2,30,1),2,20,2);
q = Optimize("PERIOD 2",Param("PERIOD 2",90,10,200,10),10,100,10);
y = Optimize("MAType 1",Param("MAType 1",2,1,8,1),1,8,1);
z = Optimize("MAType 2",Param("MAType 2",2,1,8,1),1,8,1);

m=0;    if (y==1) m    =    MA(C,p);  
else    if (y==2) m    =    EMA(C,p);  
else    if (y==3) m    =    DEMA(C,p);  
else    if (y==4) m    =    TEMA(C,p);  
else    if (y==5) m    =    WMA(C,p);  
else    if (y==6) m    =    HMA(C,p);  
else    if (y==7) m    =    Wilders(C,p);  
else    if (y==8) m    =    LinearReg(C,p);

n=0;    if (z==1) n    =    MA(C,q);  
else    if (z==2) n    =    EMA(C,q);  
else    if (z==3) n    =    DEMA(C,q);  
else    if (z==4) n    =    TEMA(C,q);  
else    if (z==5) n    =    WMA(C,q);  
else    if (z==6) n    =    HMA(C,q);  
else    if (z==7) n    =    Wilders(C,q);  
else    if (z==8) n    =    LinearReg(C,q);
Plot(m," MA",IIf(m < Ref(m,-1),colorBlue,colorRed),styleThick);
Plot(n," MA",IIf(n < Ref(n,-1),colorBlue,colorRed),styleThick);

Buy  = Cross(m,n);
Sell = Cross(n,m);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = Sell; Cover = Buy;

/////////////////////////////////////////////////////////////////end of code///////////////////////////////////////////////////////////////////////

Best of luck and happy  trading

1 comment:

  1. This is not changing with time frame.needs fine tuning.For example if I change time frame from hourly to daily EMA is not changing.crossover too.