Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

# Zero Lag EMA 15min Strategy for Nifty and Bank Nifty

Today we are going to discuss about Zero Lag EMA 15min strategy for Nifty and Bank Nifty and how effective it is in terms of generating returns and smoothing your equity curve. Almost all smoothing filters and moving averages have lag because smoothing is done using past data.

Zero Lag is a new concept in adaptive technical analysis. Here in Zero lag EMA we show you the effects of lag removal in a indicator and then how to use the filter in a very effective way. The picture shows the simple ZeroLag EMA strategy over the 15min Nifty Charts. The thick yellow line is nothing but the Zero lag EMA and the red line is the normal EMA.Since minimization of lag is crucial to the effectiveness of indicators,several authors have devised ways to make the EMA smoothing factor vary with volatility in price.

The first relationship between the lag of an EMA and the length of a Simple Moving Average (SMA) is

alpha = 2/(Length+1)

We call the new filter EC (for Error Correcting) i.e Zero Lag EMA instead of EMA. So the equation for the EC filter is:

EC = alpha*(Price + gain*(Price–EC[1])) + (1-alpha)*EC[1];

The equation is simple, but its results are profound. If the gain is zero, the EC becomes just an EMA. If the gain is sufficiently large the error term causes EC to exactly track the price for all practical purposes. That is, there is virtually no lag and virtually no smoothing. Therefore, we seek a value of gain that is a happy medium between tracing out the price and tracing out the EMA. We do this by limiting the maximum amount of allowable gain. If the difference between the price and the previous value of EC is small we do not want a large value of gain. Further, the previous value of EC can be either greater than or less than the current price. Therefore, to properly apply the error correction the gain must swing both positive and negative.
Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

## [Free Webinar] Starting Amibroker AFL Coding Right from the…

Interested in Creating your own indicators/Buy or Sell Trading System. Learn How to build your own trading system using Amibroker. If you are new...

## Angel Broking Multi-Client Amibroker Trading Module

Got frequent requirements from Angel Broking Customers to automate Amibroker based trading system where one single generation from charts could punch orders in multiple...

## Option Button Trading to Trade ATM, ITM, OTM Options…

Here is a simple button trading module which can be used to send option trading order with buttons. Users can configure the button to...

## 44 Replies to “Zero Lag EMA 15min Strategy for Nifty and Bank…”

1. Kumar says:

Hi
Rajan,
Thankyou for putting up this brilliant formula.
I have tried to copy the file into the formula folder but for some reason its not working on my amibroker.

1. What error you are getting? Make sure that you are using 5.7 and the code is not compatible with versions less than 5.5 i guess.

2. mrityunjay says:

Sir, AFL code showing error !

1. Which version of amibroker you are using i tested in Amibroker 5.7 and it works fine. Make sure that you are using 5.7.
Yes verfied their data is Tick by Tick

1. Option Symbol Format e.g NIFTYCE6200.0030-01-2014
0030-01-2014 – represents the options expiry date.

3. praji says:

Im using 5.7….the errors are in lines 45,47,53,54,55,57,61 & 62… pls help

4. SK Biswal says:

good system with potential of positive result.

5. Amit says:

Sir

I am using AmiBroker 5.7 version.
I am getting number of errors in lines
Ln 45,47,53,54,55,57,61,62, about variables missing , syntax errors .I have already sent the screenshots of the errors on rajandran@marketcalls.in.

6. Deepak says:

Hi Rajandran,

I recently started trading, went though these, i downloaded your backtested result, and added 10pt stop loss to each trade, results are too good to be true, can you verify the same, kindly share ur mail id, so that i can send the file.

Regards
Deepak

1. Sure deepak will definitely look into it and thanks for your input.

7. Girish says:

I tried this AFL this morning and I’m getting errors. I use Ami 5.7

Ln 45, Col, 10, Error 31 == ” Error = abs( Close[ bar ] – EC ); ”
Ln 47, Col 14, Error 31 == “if( Error < LeastError ) "
Then, on this line == "iEC[ bar ] = BestEC; " and " iLeastError[ bar ] = LeastError; "
And on this == "Plot( iEC, "EC" + _PARAM_VALUES(), colorYellow, styleThick ); "

Can you check these errors and upload the corrected AFL ?

Thank you.
G

1. Few of them are facing such issues will test again will try to post the modified AFL.

2. Raghunandan says:

_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(CO,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 );

Short=Sell;

SellPrice=ValueWhen(Sell,C);

ShortPrice=ValueWhen(Short,C);

CoverPrice=ValueWhen(Cover,C);

Title = EncodeColor(colorWhite)+ "ZL EMA code from http://www.marketcalls.in&quot; + " – " + 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);

1. shrirang shrikrishna parulekar says:

very usefull site thanks

8. Hi Rajandran,

I am Aryan from tradecatcher.blogspot.in .
I find your website really very useful for the
trading community. Full credit for your hardwork.

I wish you all the best for your endeavors in life.

Regards.

9. raj kumar says:

hello sir can you make a comparison of annually returns if i deposit 10000 in a bank…or follow this zero lag strategy…which will give me more returns..?

10. raj kumar says:

hello sir can you tell me how to increase number of shares for back testing your afl…?

11. Deepak says:

Hi Rajandran,

Can you tell me the best brokers to start with trading F&O. I have been using Scottrade and optionsxpress. I am new to trading the NSE/BSE

Thanks
Deepak

1. You can try with discount brokers like zerodha, tradejini, compositedge

12. shyam says:

hello Rajendran sir,
I am following your strategies and afl, indeed your work is very beneficial to many like me.
Here I am also finding error with this afl, zero lag ema, can you please guide me on same.
Looking forward for your guidance.

Thank you sir.

13. John says:

I am also have errors on these mentioned lines
Ln 45, Col, 10, Error 31 == ” Error = abs( Close[ bar ] – EC ); ”
Ln 47, Col 14, Error 31 == “if( Error < LeastError ) "
Then, on this line == "iEC[ bar ] = BestEC; " and " iLeastError[ bar ] = LeastError; "
And on this == "Plot( iEC, "EC" + _PARAM_VALUES(), colorYellow, styleThick ); "

Could you please check. Thanks for your effort.

14. Udit says:

Hi, Rajandran R I am Also Using Ami 5.7 and getting Error and mjust Say a Great Work Done By you In Sort of This website

15. Chandrakant says:

Hello Rajandran sir,
Thanks for the afl code but i am too getting error.
It says iec not initialized.

16. JASMIN AKBARI says:

Can I get it for spider or Market view?

1. You need to custom code it in Spider.

17. Claudio says:

Hi, thank you for this information.. I’ll try to do this in NSDT.

Regards,
Claudio

1. Claudio says:

NSDT = Neuroshell Day Trader … you can create .dll in powerbasic or c++

18. binod says:

can zero lag EMA strategy be used for futures. where ca i subscrble the 5 min chart you show free for nift,bank nifty,futures. I want a subscription for 5 10 15 30 i hour qnd i day chart

19. 13oct08 says:

Thanks for sharing the information.
– What other means of self corrections are being utilized by other authors.
– At what point do we reach a stage where the system is curve fitted and becomes unsuable over longer period of times.

Just curious , have you tried it longer than 4 year time frame.

1. My Way of understanding curve fitting is

1)If you are using more than 3+ input parameters for Optimization
2)If you data sample size is very less and you are doing optimization with the limited dataset

And I hope iam not doing both.

1. 13oct08 says:

Thanks for the quick reply,

Do you have any information on what other correction factors used by system developers? ( i am still trying to figure out how you arrived at the ‘alpha’ factor formula)

&

In your back testing settings you have used the lot size is 1, do you have specified in your symbol description in AMI for the lot size to be 50shares for Nifty? Have you tried with minimum tick at 0.05?

20. Rahul says:

Hi Sir,

Ln:45
Ln:47
Ln:53
Ln:54
Ln:55
Ln:57
Ln:61
Ln:62

Please help to provide me AFL, I have ambibroker 5.7

1. Hi Make sure you have enough backfill in your database otherwise Amibroker will throw errors.

1. mk says:

HI I think Error is because you have used ‘ Error’ which is a keyword . replace it with some other , it works

21. Vijay says:

Hi Rajendran,
Thanks a lot (ton!!) for so much of free content that you have posted.
Most of your AFL codes has got back testing results also in AFL. For those who do not have Amibroker, it is not possible to see those results. It will be helpful, if you post those results in excel.
Thanks
Vijay

22. Viral says:

Dear Rajendra,

I am also getting error From line no 45 , 47 , 53 , 54

I am using Amibroker 5.7

Is this same code ZLEMA ?

I want for intraday and mostly all our alfs are positional and we are trying in intraday hence asking to put in parameters if we sqf in traday whats the result……

Can you atleast correct code and send me email also dalalviral@yahoo.com

Thanks and once again than for wonderful knowledge base website and platform created and helpful to us.

Thanks

Viral.

23. WAJID KhAN says:

SIR INTERESTED IN ZERO LAG INDICATOR PLZ HELP

24. WAJID KhAN says:

sir plz help zero lag indicator

25. Sachin Jain says:

Hey Rajendran

I have just started to learn amibroker and kind of comfortable with writing code and baktesting. I took your AFL ( modified it with ERROR to iERROR) and now i have a error free code But when i try to test this on 2017-2018 data i get no results. I have the NIFTY 2017-2018 data which i imported in CSV format and has 1 minute data.

I mean to say that nothing comes back at all and its a blank screen. ( and no error on analysis window )

Any settings that i need to do either on testing window setting? or database setting?
Appreciate your help !

26. Naren says:

HI Mr Rajendra,

This Code is not yet working.