Tuesday 15 October 2013

Amibrker AFL based on Wilders Moving Averages

Today i would like to post an AFL that is widely used traders who trade using moving averages.Moving averages can work in several combinations and applied to both Equity and,Commodity and currency market. In most of the time with amazing results. We come across moving averages of 3,5,10,15,20,30,60.This AFL utilizes Wilders moving average method based on 20 EMA or SMA as explained below




The width of the Bollinger band  too adjustable   to suit individual preference.  I have observed this AFL giving good signals at the appropriate time.


 I have tried to make this more simpler and presentable I hope the viewers will find this this useful and find higher success ratio.

//////////////////////////////////////////////// The code begins under this line////////////////////////////////

_SECTION_BEGIN("SingleMA");

SetChartOptions(0, chartShowDates | chartWrapTitle);


Type = ParamList("Average Type", "Wilders,SMA,EMA");
P = Param("Averaging Period", 20, 3, 100,1);
Q = Param("%Change", 1, 0.1, 10, 0.1);
BP = Param("BB Period", 20, 3, 100);
BW = Param("BB Width", 2, 0.5, 10, 0.5);
BBOption = ParamToggle("Plot BB", "No I Yes");

Report = ParamList("Trigs or Update or Tgt-SL?", "Triggers|Update|Tgt-SL");

if(Type == "Wilders") A = Wilders(C, P);
if(Type == "SMA") A = MA(C, P);
if(Type == "EMA") A = EMA(C, P);

SL = Max(LLV(L, 5), Trough(L, Q, 1));

Tgt = 2 * H - SL;
MeanPrice = Prec((O + C) / 2, 2);

Part = 100 * (H - A) / (H - L);

BBTop = BBandTop(C, BP, BW);
BBBot = BBandBot(C, BP, BW);

Buy = (C > A) AND (Part > 70);
Sell = H < A;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Bought = Flip(Buy, Sell);
Sold = Flip(Sell, Buy);
NextTgt = ValueWhen(Buy, Tgt, 1);

for(i = 1; i < BarCount; i++)
{
if(Bought[i] AND NOT Buy[i])
{
SL[i] = Max(SL[i], SL[i - 1]);
if(C[i - 1] >= 0.9999 * NextTgt[i - 1]) NextTgt[i] = Tgt[i - 1];
NextTgt[i] = Max(NextTgt[i], NextTgt[i - 1]);
}
}
BuyDate = ValueWhen(Buy, Ref(DateTime(), 1), 1);
BuyPrice = ValueWhen(Buy, Ref(MeanPrice, 1), 1);
SellPrice = ValueWhen(Sell, Ref(MeanPrice, 1), 1);

TgtReached = IIf(Bought AND NOT Buy AND C >= 0.9999 * NextTgt, True, False);
SLHit = IIf(Bought AND NOT Buy AND C < SL, True, False);
SLHit = ExRem(SLHit, Buy);
//
if(TgtReached[BarCount - 1]) NextTgt[BarCount - 1] = 2 * H[BarCount -1] - SL[BarCount - 1];
//
if(Status("action") == actionIndicator)
{
Ttl = EncodeColor(colorTurquoise) + "Wilders MA system, AFL" + "\n"
+ WriteIf(Buy, EncodeColor(colorGreen) + "Buy Triggered Now, BUY this stock in next trade","")
+ WriteIf(Sell, EncodeColor(colorDarkRed) + "Sell Triggered Now, SELL This stock in next trade", "")
+ EncodeColor(colorBlue) + WriteIf(Bought AND NOT Buy, "Bought @ " + BuyPrice + ". "
+ "Target Price = " + NextTgt + ", Stop Loss = " + SL + ".\n"
+ WriteIf(TgtReached, "Target Reached. Next Target = " + Ref(NextTgt, 1) + ".\n", "")
+ EncodeColor(colorBlue) + "Profit / Loss so far = " + Prec(100 * (C - BuyPrice) / BuyPrice, 2) + "%", "")
+ WriteIf(Sold AND NOT Sell, "Sold @ " + SellPrice + "\nProfit / Loss in Previous Trade = " + Prec(100 * (SellPrice - BuyPrice) / BuyPrice, 2) + "%", "");

_N(Title = StrFormat("{{NAME}} ({{INTERVAL}}), {{DATE}} ; {{OHLCX}}, V=%1.0f\n {{VALUES}}\n\n", V) + Ttl);
ChartStyle = ParamStyle("Chart Type", styleBar, maskAll);

Plot(C, "", colorGrey50, ChartStyle);
Plot(A, Type + "(" + P +")", colorYellow, styleLine | styleThick);
Plot(IIf(Bought, NextTgt, Null), "Target", colorBlueGrey, styleLine);
Plot(SL, "Trail SL", colorTeal, styleLine);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-20);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-30);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-25);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=30);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-25);
if(BBOption) Plot(BBtop, "BB-Top", colorPink, styleLine);
if(BBOption) Plot(BBBot, "BB-Bot", colorPink, styleLine);
}

if((Status("action") == actionExplore) AND Report == "Triggers")
{
Filter = Buy OR Sell;


SetOption("NoDefaultColumns", True);

AddColumn(DateTime(), "Updated On", formatDateTime, colorDefault, colorDefault, 96);
AddTextColumn(Name(), "Symbol", 77, colorDefault, colorDefault, 120);
AddColumn(BuyDate, "Buy Date", formatDateTime, colorDefault, colorDefault, 96);
AddColumn(BuyPrice, "Buy Price", 6.2);
AddColumn(NextTgt, "Target", 6.2);
AddColumn(SL, "StopLoss", 6.2);
AddColumn(C, "CMP", 6.2, colorDefault, colorDefault, 96);
}
if((Status("action") == actionExplore) AND Report == "Tgt-SL")
{
Filter = TgtReached OR SLHit;

SetOption("NoDefaultColumns", True);

AddColumn(DateTime(), "Updated On", formatDateTime, colorDefault, colorDefault, 96);
AddTextColumn(Name(), "Symbol", 77, colorDefault, colorDefault, 120);
AddColumn(BuyDate, "Buy Date", formatDateTime, colorDefault, colorDefault, 96);
AddColumn(BuyPrice, "Buy Price", 6.2);
AddColumn(NextTgt, "Target", 6.2);
AddColumn(SL, "StopLoss", 6.2);
AddColumn(C, "CMP", 6.2, colorDefault, colorDefault, 96);
AddColumn(IIf(TgtReached, 89, 32), "Tgt Hit?", formatChar, colorYellow, IIf(TgtReached, colorGreen, colorDefault));
AddColumn(IIf(TgtReached, 2 * H - SL, Null), "Next Tgt", 1.2);
AddColumn(IIf(SLHit, 89, 32), "SL-Hit", formatChar, colorYellow, IIf(SLHit, colorRed, colorDefault));
}

_SECTION_END();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment