This internet has very less resource on how to backtest a strategy on intraday basis as most of the strategy adapted so far in marketcalls are carry forward strategy. Strategies like Ichimoku Cloud TSL, SDA2 Trend Trading System and Supertrend etc are mostly carry forward strategy. And to implement a pratical intraday strategy you need to mix mathematical models with time based strategies (i.e) when to initiate a position ,when to exit and whent to stop the trade during the day.
In any carryforward strategies you need to carry forward your positions every overnight and every week even if the strategies are adopted in lower timeframes. Some people feel that carry forward strategies involves lots of overnight carry forward risk so they try to play intraday mostly. In a intraday strategy positions will be covered of mostly on the same day. Say you are adopting a certain mathematical model in your intraday trading How
did you validate whether the intraday strategy you adopted works good or not? How to backtest such intraday strategies?
Strong Disclaimer before starting with the strategy
1)This strategy has been built as a prototype to build complex mathematical models binded with intraday rules in the future.
2)This strategy is not tradeable as it involves lots of whipsaws. Here the strategy is posted just to educate the people not for involving in speculation based on the strategy
To solve the above issues i thought of making a prototype with a simple ema crossover strategy with time based rules. This strategy will start trade the EMA crossovers only between 9:45a.m and 3:00:p.m and close the postion by 3:20p.m. The following code defines the time based rules
FirstTradeTime = 094500;
LastTradeTime = 150000;
ExitAllPositionsTime = 152500;
Buy and Sell Rules
1)Initiate Buy if there is a bullish EMA crossover and time is greater than FirstTradeTime and less than LastTradeTime
2)Cover the buy (i.e) sell if there is a bearish EMA crossover or if time is 3:25p.m
3)Initiate Short if there is a bearish EMA crossover and time is greater than FirstTradeTime and less than LastTradeTime
4)Cover the short (i.e) cover if there is a bullish EMA crossover or if time is 3:25p.m
Short=Sell AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime ); Cover=Buy OR TimeNum() >= ExitAllPositionsTime;
And Pratically speaking I mostly test my strategies with fixed lotsize initially without any money management principles involved in it.
For example : Nifty futures i often test with 2 lots of nifty i,e 100 shares This is achieved by the following code
Choosing the timeframe
Since EMA crossovers involves lots of whipsaws. I generally prefer to test my strategies in 10min, 15min for intraday basis to avoid too much whipsaws during intraday but still whipsaws are unavoidable. Choosing the timeframe often depends upon what kind of strategy you are adopting and what king of trader you are!
In case of Nifty futures in 10min timeframe we prefer parameters EMA1=3 and EMA2=50
In case of Nifty futures in 15min timeframe we prefer parameters EMA1=2 and EMA2=36
Goto Amibroker->Symbols->Information and enter the values of Round Lot size=50 and Margin Deposit = -15 (i.e 15% ) and follow the backtesting settings for 15min timeframe as shown below in the image
5 Years of Nifty futures bactesting Results in a 15min timeframe
Simple EMA Crossover Intraday Strategy – Amibroker AFL Code
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); //SetTradeDelays(1,1,1,1); SetPositionSize(100,spsShares); FirstTradeTime = 094500; // Earliest time to take a trade LastTradeTime = 150000; // Latest time to take new trades ExitAllPositionsTime = 152500; // Exit all trades //parameters //10min - 3,50 //15min - 2,36 P = ParamField("Price field",-1); per1 = Param("EMA1",2,1,20,1); per2 = Param("EMA2",36,1,50,1); Buy = Cross(EMA(C,per1),EMA(C,per2)) AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime ); Sell= Cross(EMA(C,per2),EMA(C,per1)) OR TimeNum() >= ExitAllPositionsTime; Buy=ExRem(Buy,Sell) ; Sell=ExRem(Sell,Buy); Short=Sell AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime ); Cover=Buy OR TimeNum() >= ExitAllPositionsTime; BuyPrice=ValueWhen(Buy,C); SellPrice=ValueWhen(Sell,C); ShortPrice=ValueWhen(Short,C); CoverPrice=ValueWhen(Cover,C); Plot( EMA( P, per1 ), "EMA1", ParamColor( "Color1", colorCycle ), ParamStyle("Style") ); Plot( EMA( P, per2 ), "EMA2", ParamColor( "Color2", colorCycle ), ParamStyle("Style") ); 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); PlotShapes(IIf(TimeNum() >= ExitAllPositionsTime,5,-1e10),colorGreen,0 ,H,5); PlotShapes(IIf(TimeNum() >= ExitAllPositionsTime,6,-1e10),colorRed,0,L,5);
11 Replies to “Simple EMA Crossover Intraday Strategy – AFL Code”
Thanks for your great work. I just wanted to know your maximum drawdown. I think this can be a good starting point for developing some strategy. I really like your SuperTrend strategy.
I wish to trade using your 5 min NIFTY Option charts following your signals. I will square off the order till the end of trading day. I am just worried about the Maximum drawdown.
Please let me know if I am making a mistake…
@amul : Since I dont have sufficient Backfill for Nifty Options it is tough to evaluate the risk involved in the strategy.
Recently I used basic charts combination like Gradient + Price which shows colour and I think it makes sense to take decisions based on the colour coding. In fact I tried to overlaying your AFL Non Repenting Super Trend Indicator on it. I think it another way of getting alerts when to enter and exit for those who do not know programming.
Hi bro, your site is providing all info for successful trading thank lot.pls give me link for ema strategy intraday chart so that I can use.thans anna.
SIR LIKE ABOVE U HAVE WRITTEN FORMULA FOR EMA CROSSOVER AND ALSO MADE IT TO RAR AFL CODE SO ONE CAN EASILY COPY PASTE I WANTED TO KNOW HOW TO CONVERT THE HTMAL OR WRITTEN FORMULA TO RAR OR AFL CODE SO THAT IT CAN BE COPY PASTE TO FORMULA FILE
Sir, I just came across ur marketcalls.in, it’s a very helpful site, if u can add some more nse scrips beyond nifty 50, it would be great. anyways, whatever u have done is worth praising.
where i can get the 15mins nifty data for last 5 years?
sir i want to add fut off all stock and nifty, Please let us know how can i do it ?
i use stock live data feeder
hi rajandran sir ,
can you pls give provide link for ema strategy intraday chart .
thank you – ram
1. Should we not take any trade on a day on which ema crossover doesn’t happen?
2. What should be ideal exit target?
How can I contact you???
I want to design one code which will give alerts after particular pattern forms on the chart….