Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

Zero Lag EMA 15min Strategy for Nifty and Bank Nifty

1 min read

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 EMA

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.

httpv://www.youtube.com/watch?v=A5lWR0PhG3c
[wp_ad_camp_5]

 

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.

Download ZERO EMA Amibroker AFL Code
ZL EMA Backtesting results for 4 Years of Nifty and Bank Nifty Spot

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

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

  1. 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.

    Thanks in advance.

    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

  2. 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 [email protected].

    Please advise and help.

  3. 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

  4. 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. _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 );

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

  5. 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.

  6. 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..?

  7. hello sir can you tell me how to increase number of shares for back testing your afl…?
    thanks in advance

  8. 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

  9. 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.

  10. 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.

  11. 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

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

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

    Regards,
    Claudio

  14. 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

  15. 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. 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?

  16. Hi Sir,

    Please help, I am getting errors on below Lines
    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
    Thanks in advance

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

  17. 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

  18. 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 [email protected]

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

    Thanks

    Viral.

  19. 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 !

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More