Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, 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. Now the recent curiosity on building Trading AI Agents.

5-10-20 EMA Short Only Strategy for Nifty with AmiBroker AFL

3 min read

The 5 10 20 strategy is a short-only trend system for Nifty. You go short when both the 5-day and 10-day exponential moving averages (EMA) cross below the 20-day EMA, and cover when both cross back above it. We first posted it in January 2009, after Nifty’s steep fall in 2008, and this update explains the rules, adds AmiBroker code and covers where the system works and where it fails.

Key takeaways

  • Entry: short at the day’s close once both the 5 and 10 EMA are below the 20 EMA.
  • Exit: cover at the day’s high once both the 5 and 10 EMA are back above the 20 EMA. Using the high is a deliberately conservative fill.
  • Requiring two fast averages to agree filters some false signals, at the cost of later entries.
  • Short-only trend systems shine in falling markets like 2008 and bleed through choppy or rising markets.
  • Backtest on your own data with costs before trading it, and trade it through Nifty futures or options, since a short sale in the cash market must be squared off the same day.

The 5 10 20 strategy rules

Here are the rules exactly as we posted them in 2009:

Go short at today’s close if both the 5 and 10-day exponential moving average (EMA) cross below the 20-day EMA. Close the position at today’s high when both the 5 and 10-day EMA cross above the 20-day EMA.

The Nifty chart below showed the 5-10-20 EMA movement over 2008. In that bear year the results looked very good, because the averages stayed stacked below the 20 EMA for long stretches.

Open Varsity: 13 free courses for traders and investors

FREE AND SELF-PACED

Open Varsity: 13 free courses for traders and investors

From stock market basics to options strategies, quant trading and taxation, built around Indian market examples.

  • 407 chapters, beginner to expert
  • Options, Python, AmiBroker AFL and quant tracks
  • Risk management and trading psychology

How the 5 10 20 strategy works

An exponential moving average gives more weight to recent prices than a simple average, so it reacts faster. The 5 EMA tracks the last week of trading, the 10 EMA about two weeks, and the 20 EMA about a month.

  • When both short averages fall below the 20 EMA, the last one to two weeks of prices are weaker than the last month. That is the start of a short-term downtrend.
  • Waiting for both the 5 and 10 EMA, rather than the 5 EMA alone, cuts the number of whipsaws on one-day dips.
  • The exit needs both averages back above the 20 EMA, which lets a down move run until the trend has clearly turned.

If you want to see how these averages behave, read our notes on visualizing the 20-day EMA and on Nifty falling below its 5 EMA.

AmiBroker AFL code for the 5 10 20 strategy

// 5 10 20 EMA short only strategy - marketcalls.in
SetTradeDelays(0, 0, 0, 0);
SetPositionSize(1, spsShares);

e5  = EMA(Close, 5);
e10 = EMA(Close, 10);
e20 = EMA(Close, 20);

bothBelow = e5 < e20 AND e10 < e20;
bothAbove = e5 > e20 AND e10 > e20;

Short = bothBelow AND NOT Ref(bothBelow, -1);
Cover = bothAbove AND NOT Ref(bothAbove, -1);
Buy = Sell = 0;

Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);

ShortPrice = Close;
CoverPrice = High;

Plot(Close, "Close", colorDefault, styleCandle);
Plot(e5, "EMA 5", colorRed);
Plot(e10, "EMA 10", colorOrange);
Plot(e20, "EMA 20", colorBlue, styleThick);
PlotShapes(Short * shapeDownArrow, colorRed, 0, High, -12);
PlotShapes(Cover * shapeUpArrow, colorGreen, 0, Low, -12);

Code explained line by line

  • SetTradeDelays(0, 0, 0, 0) trades on the signal bar itself, which matches “short at today’s close”.
  • SetPositionSize(1, spsShares) trades one unit so the backtest shows points captured. Change this to your lot size.
  • e5, e10 and e20 are the three exponential moving averages of the close.
  • bothBelow and bothAbove check whether both fast averages are on the same side of the 20 EMA.
  • Short fires on the first bar where bothBelow becomes true. Cover fires on the first bar where bothAbove becomes true.
  • Buy = Sell = 0 makes the system short only.
  • ExRem removes repeated signals so each short has one matching cover (see the AmiBroker ExRem reference).
  • ShortPrice = Close and CoverPrice = High apply the original fills: enter at the close, exit at the high.
  • The Plot and PlotShapes lines draw the candles, the three EMAs and red and green arrows at signals.

How to run it in AmiBroker

  1. Open the Formula Editor, paste the code and save it as 5-10-20 Short.afl.
  2. Apply it to a Nifty or Nifty futures daily chart to see the arrows.
  3. Open the Analysis window, pick the formula, set the range and click Backtest.
  4. In Settings, add brokerage and slippage and set the futures lot size before you trust the result.

For traders: where this system fails

  • In a sideways market, the averages tangle and the system shorts into every small dip, then covers at the high.
  • In a strong bull market it sits out or takes small losses, so it should be paired with a long system or a trend filter.
  • Covering at the day’s high is a worst-case fill. If your backtest uses the close, results will look better than real trading.
  • Treat it as one part of a rule set, as we describe in the trading strategies Marketcalls follows.

Frequently Asked Questions

What is the 5 10 20 strategy?

It is a short-only moving average system. You short when both the 5 and 10-day EMA move below the 20-day EMA and cover when both move back above it.

Can I use these rules for buying?

You can mirror the rules for longs, buying when both fast EMAs move above the 20 EMA. Test the long side separately, because markets rise and fall in different ways.

Which timeframe works best?

The original rules use daily bars on Nifty. The same logic runs on hourly charts, but signals become more frequent and costs matter more.

Why cover at the day’s high?

It assumes the worst exit price of the day, which keeps backtest results honest. In live trading you can cover at the close or the next open instead.

Marketcalls Elite Traders Club

MEMBERSHIP

Marketcalls Elite Traders Club

Every Marketcalls course and live mentorship in one membership: 127 courses and 2,500+ hours of learning.

  • Market Profile, Orderflow, AFL, Pine Script and Python
  • Live mentorship and TradEdge trading room
  • Premium tools and the Elite Slack community

Learn by watching: free videos every week

Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, 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. Now the recent curiosity on building Trading AI Agents.

4 Replies to “5-10-20 EMA Short Only Strategy for Nifty with AmiBroker…”

  1. Rajandran Sir,

    I feel this strategy worked well in down trending market and can be used in opposite way in up trending market.

    Need your comment on this.

    Sachin Pethe

  2. Sir,
    I feel this is a lagging indicator and misses half of the move. More effective after a sideways move and not after a big fall or big upmove , do u agree.?
    Please advise

Leave a Reply

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