Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Zero lag EMA for Amibroker (AFL)

Rating:
5 / 5 (Votes 1)
Tags:
trading system, amibroker, moving average

This AFL file is for Zero lag EMA,designed to reduce the impact of delay in technical indicators.

Indicator / Formula

Copy & Paste Friendly
_SECTION_BEGIN("Zero-Lag EMA Indicator for AmiBroker ");

SetBarsRequired(100000,0);

GraphXSpace = 15;

SetChartOptions(0,chartShowArrows|chartShowDates);

SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));

GfxSetBkMode(0); 

GfxSetOverlayMode(1);

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));

Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

Length = Param("Length", 69, 20, 100,1 ); 
GainLimit = Param("Gain limit", 22, 1, 100); 


SetPositionSize(2,spsShares);

alpha = 2 / ( Length + 1 ); 

iEMA = AMA( Close, alpha ); 

EC = Close[ 0 ]; 

for( bar = 0; bar < BarCount; bar++ ) 
{ 
 EC1 = EC; 

 LeastError = 1e9; 
 BestEC = 0; 

 for( gain = -0.1 * GainLimit; gain < 0.1 * GainLimit; gain += 0.1 ) 
 { 
   EC = alpha * ( iEMA[ bar ] + gain * ( Close[ bar ] - EC1 ) ) + 
       ( 1 - alpha ) * EC1; 

   Error = abs( Close[ bar ] - EC ); 

   if( Error < LeastError ) 
   { 
    LeastError = Error; 
    BestEC = EC; 
   } 
 } 
 iEC[ bar ] = BestEC;   
 iLeastError[ bar ] = LeastError; 
} 
Plot( iEMA, "EMA", colorRed ); 
Plot( iEC, "EC" + _PARAM_VALUES(), colorYellow, styleThick ); 
Plot( C, "Close", ParamColor("Color", colorGreen ), ParamStyle("Style") | GetPriceStyle() ); 

// strategy rules 
Buy = Cross( iEC, iEMA );
Sell = Cross( iEMA, iEC );


Buy=ExRem(Buy,Sell);

Sell=ExRem(Sell,Buy);

Short=Sell;

Cover=Buy;





BuyPrice=ValueWhen(Buy,C);

SellPrice=ValueWhen(Sell,C);

ShortPrice=ValueWhen(Short,C);

CoverPrice=ValueWhen(Cover,C);





Title = EncodeColor(colorWhite)+ "ZL EMA code from www.marketcalls.in" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +

 "  - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+"  "+"Hi-"+H+"  "+"Lo-"+L+"  "+

"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+ 

EncodeColor(colorLime)+

WriteIf (Buy , " GO LONG / Reverse sig at "+C+"  ","")+

WriteIf (Sell , " EXIT LONG / Reverse sig at "+C+"  ","")+"\n"+EncodeColor(colorYellow)+

WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+

WriteIf(Buy  , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","");



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(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);

6 comments

1. jrajnikant

ERROR

2. APOLLOBIN
_SECTION_BEGIN("Zero-Lag EMA Indicator for AmiBroker ");
 SetBarsRequired(100000,0);
 GraphXSpace = 15;
 SetChartOptions(0,chartShowArrows|chartShowDates);
 SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));
 GfxSetBkMode(0); 
 GfxSetOverlayMode(1);
 SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
 Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
 Length = Param("Length", 69, 20, 100,1 ); 
GainLimit = Param("Gain limit", 22, 1, 100); 
  
SetPositionSize(2,spsShares);
 alpha = 2 / ( Length + 1 ); 
 iEMA = AMA( Close, alpha ); 
 EC = Close[ 0 ]; 
 for( bar = 0; bar < BarCount; bar++ ) 
{ 
 EC1 = EC; 
 LeastError = 1e9; 
 BestEC = 0; 
  for( gain = -0.1 * GainLimit; gain < 0.1 * GainLimit; gain += 0.1 ) 
 { 
   EC = alpha * ( iEMA[ bar ] + gain * ( Close[ bar ] - EC1 ) ) + 
       ( 1 - alpha ) * EC1; 
 
   iError = abs( Close[ bar ] - EC ); 
 
   if( iError < LeastError ) 
   { 
    LeastError = iError; 
    BestEC = EC; 
   } 
 } 
 iEC[ bar ] = BestEC;   
 iLeastError[ bar ] = LeastError; 
} 
Plot( iEMA, "EMA", colorRed ); 
Plot( iEC, "EC" + _PARAM_VALUES(), colorYellow, styleThick ); 
Plot( C, "Close", ParamColor("Color", colorGreen ), ParamStyle("Style") | GetPriceStyle() ); 
 
// strategy rules 
Buy = Cross( iEC, iEMA );
Sell = Cross( iEMA, iEC );
 
 
Buy=ExRem(Buy,Sell);
 
Sell=ExRem(Sell,Buy);
 
Short=Sell;
 
Cover=Buy;
 
 
 
 
 
BuyPrice=ValueWhen(Buy,C);
 
SellPrice=ValueWhen(Sell,C);
 
ShortPrice=ValueWhen(Short,C);
 
CoverPrice=ValueWhen(Cover,C);
 
 
 
 
 
Title = EncodeColor(colorWhite)+ "ZL EMA code from www.marketcalls.in" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
 
 "  - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+"  "+"Hi-"+H+"  "+"Lo-"+L+"  "+
 
"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+ 
 
EncodeColor(colorLime)+
 
WriteIf (Buy , " GO LONG / Reverse sig at "+C+"  ","")+
 
WriteIf (Sell , " EXIT LONG / Reverse sig at "+C+"  ","")+"\n"+EncodeColor(colorYellow)+
 
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
 
WriteIf(Buy  , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","");
 
 
 
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(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);
3. tradermind

errors continue to remain, I was able to partially fix it by deleting these lines of code, but code still remains partial
//Error = abs( Close[ bar ] – EC );

// if( Error < LeastError ) { // LeastError = Error;

Code is good but very slow to calcolate functions so backtesting is very slow

4. jrajnikant

AGAIN ERROR

5. BrockQAW

Hi! It is corrected AFL code by APOLLOBIN not include Error.

/*

_SECTION_BEGIN("Price and Volume");
ChartStyle = IIf (ParamToggle("Chart Style", "Candles|Bars", 1), styleBar|styleThick, styleCandle);
BodyColor  = IIf(C==O, colorWhite, IIf (C<O, colorRed, colorBrightGreen));
MonoColor  = colorskyBlue;
ChartColor = IIf (ParamToggle("Chart Color","Mono|Color", 1), BodyColor, MonoColor);
SetBarFillColor(ChartColor);
Plot(C,"Close", ChartColor,ChartStyle|styleNoLabel); 
PR    = LastValue (Close);
PRCol = LastValue (ChartColor);
Plot(PR, "", PRCol, styleLine, Null, Null, 10);

// VOLUME
VolScale = Param("Scaling", 6, 2, 20, 1);
if (ParamToggle("Volume", "Hide|Show",0)) 
{ Plot(V,"",colorPaleGreen,styleOwnScale|styleGradient|styleLine,VolScale); }
_SECTION_END();



_SECTION_BEGIN("ZerLag EMA"); // APOLLOBIN made
Length    = Param("Length",     69, 20, 100, 1); 
GainLimit = Param("Gain limit", 22,  1, 100, 1); 

alpha = 2/(Length + 1); 
iEMA  = AMA(Close, alpha); 
EC    = Close[0]; 

for (bar=0; bar < BarCount; bar++) 
{ 
	EC1 = EC; 
	LeastError = 1e9; 
	BestEC = 0; 

	for (gain=-0.1*GainLimit; gain<0.1*GainLimit; gain += 0.1) 
	{ 
		EC = alpha*(iEMA[bar]+gain*(Close[bar]-EC1))+(1-alpha)*EC1; 
		Error_ = abs(Close[bar]-EC); 
		if (Error_ < LeastError) 
		{ 
			LeastError = Error_; 
			BestEC = EC; 
		} 
	} 
	iEC[bar] = BestEC;   
	iLeastError[bar] = LeastError; 
} 

Plot(iEMA, "EMA", colorSeaGreen); 
Plot(iEC,  "EC",  colorYellow); 

// Buy Sell Signal
Buy  = Cross(iEC, iEMA);
Sell = Cross(iEMA, iEC);

Buy  = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Short = Sell;
Cover = Buy;

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(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);
_SECTION_END();

*/
6. tradermind

errorrs. I think continuous errors of tabulation; i only succeed to make work this afl only deleting the 3 lines posted: //Error = abs( Close[ bar ] – EC );
// if( Error < LeastError ) { // LeastError = Error;

Leave Comment

Please login here to leave a comment.

Back