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

Power of Stocks 5 EMA Strategy with Signals using Loop and Backtest Snippets for Amibroker (AFL)

Rating:
4 / 5 (Votes 3)
Tags:
amibroker, 5 EMA

Based on Power of Stocks 5 EMA Strategy is said in https://youtu.be/uu3zGIXaG8g Buy 15 min , Short 5 min.

This Strategy is only good for BANKNIFTY Short trading . Last two year (Oct ’20 to Sep ’22) gives 24000 points without commission and slippage. Accuracy 30%.

When Backtesting in Amibroker if same candle hit SL and entry , it counted as zero loss. This strategy get more trades entry and exit in same bar. So i made it backtestable in 1 minute.

It have option to select Stock | Index. For Stock it have separate Entry , Sqoff timing, Quantity is calculated as per Initial Equity divided by Position Size (Amibroker use Compounding in backtest, increase position size when increase in Capital, So limit number of Max Open Positions as per your requirement). for Index it is good to backtest with 1 quantity with out commission as it is mostly trade in options. As per Net Profit in points it can considered as good or not.

Trade Prices BuyPrice, ShortPrice, SellPrice, CoverPrice are used with ValueWhen Entry. It work for some strategies. Most Strategies it will change if next time entry condition valid. So Entry Exit Signals and Price code in loop is required. This AFL Signals are coded in loop and easily learn. Thanks to Yogesh Dalvi (Telegram @dyogesh) helped me to code this loop.

Screenshots

Indicator / Formula

Copy & Paste Friendly
/*VALLKKOTTAI MURUGAN THUNAI*\
Telegram @easysimpletrade
Website https://easysimpletrade.blogspot.com
Youtube https://www.youtube.com/channel/UCaAH8Um7QCIPsOXH47zHAfQ
*/

/*
Based on Power of Stocks 5 EMA Strategy is said in https://youtu.be/uu3zGIXaG8g Buy 15 min , Short 5 min.
Thanks to Yogesh Dalvi (Telegram @dyogesh) helped me to code this loop.
*/

_SECTION_BEGIN("POS 5 EMA");

BTSI = ParamList("Analysing", "Stock|Index",1);

SOTIMES = ParamTime("Stock - Square OFF","14:59:00");
EATIMES = ParamTime("Stock - Entry After","09:19:00");
EBTIMES = ParamTime("Stock - Entry Before","14:35:00");

SOTIMEI = ParamTime("Index - Square OFF","15:14:00");
EATIMEI = ParamTime("Index - Entry After","09:19:00");
EBTIMEI = ParamTime("Index - Entry Before","14:45:00");

SOTIME = IIf(BTSI == "Stock", SOTIMES, SOTIMEI);
EATIME = IIf(BTSI == "Stock", EATIMES, EATIMEI);
EBTIME = IIf(BTSI == "Stock", EBTIMES, EBTIMEI);

TDVAL = Param("Position Size", 20000,5000,125000,5000);
INEQ = Param("Initial Equity", 50000,1000,1000000,1000);

RRR = Param("Risk Reward Ratio", 3 , 1 ,10, 0.5);
TFI = Param("Time Frame I", 5 , 1 , 75);
TFII = Param("Time Frame II", 15 , 1 , 75);

_SECTION_END();

H1 = TimeFrameGetPrice( "H", in1Minute*TFI,-1);
L1 = TimeFrameGetPrice( "L", in1Minute*TFI,-1);

H2 = TimeFrameGetPrice( "H", in1Minute*TFII,-1);
L2 = TimeFrameGetPrice( "L", in1Minute*TFIi,-1);

TimeFrameSet(in1Minute*TFI);
EMA51 = EMA(C,5);
TimeFrameRestore();
TFIR = TFI/Interval()*60;
EMA510 = TimeFrameExpand(EMA51, in1Minute*TFI,expandFirst);
EMA51 = Ref(EMA510,-TFIR);

TimeFrameSet(in1Minute*TFII);
EMA52 = EMA(C,5);
TimeFrameRestore();
TFIIR = TFII/Interval()*60;
EMA520 = TimeFrameExpand(EMA52, in1Minute*TFII,expandFirst);
EMA52 = Ref(EMA520,-TFIR);

RISK1 = H1 - L1;
RISK2 = H2 - L2;

ETIME = TimeNum() > EATIME AND TimeNum() < EBTIME;

iBuy = H2 < EMA52 AND Cross(H, H2) AND ETIME;
iShort = L1 > EMA51 AND Cross(L1, L) AND ETIME;

//Exit-----------------------------------------------------------------------------

iBENT = iBTGT = iBSL = 0;
iSENT = iSTGT = iSSL = 0;
iInTrade = 0;
iTimeNum = TimeNum();
iInTradeArray = 0;

Buy = Sell = Short = Cover = BuyPrice = SellPrice = ShortPrice = CoverPrice = 0;
BENT = SENT = BTGT = STGT = BSL = SSL = BSLE = SSLE = BTGTE = STGTE = SQOFF = 0;
for(i = 0 ; i <= BarCount -1 ; i++)
{	
	if(iInTrade == 1) 
	{
		if (High[i] > iBTGT)
		{
			Sell[i] = 1;
			SellPrice[i] = iBTGT;
			BTGTE[i] = 1;
		}
		else if (Low[i] < iBSL)
		{
			Sell[i] = 1;
			SellPrice[i] = iBSL;
			BSLE[i] = 1;
		}
		else if(iTimeNum[i] > SOTIME) 
		{
			Sell[i] = 1;
			SellPrice[i] = Close[i];
			SQOFF[i] = 1;
		}
	}	
	
	if(iInTrade == -1) 
	{
		if (Low[i] < iSTGT)
		{
			Cover[i] = 1;
			CoverPrice[i] = iSTGT;
			STGTE[i] = 1;
		}	
		else if (High[i] > iSSL)
		{
			Cover[i] = 1;
			CoverPrice[i] = iSSL;
			SSLE[i] = 1;
		}	
		else if(iTimeNum[i] > SOTIME )
		{
			Cover[i] = 1;
			CoverPrice[i] = Close[i];
			SQOFF[i] = 1;
		}
	}	
	
	if(iBuy[i] AND iInTrade == 0)
	{
		Buy[i] = 1;
		iBENT = H2[i];
		iBSL   = L2[i];
		iBTGT  = H2[i] + (RRR*RISK2[i]);
		iInTrade = 1;
		BuyPrice[i] = iBENT;
	}	
	
	if(iShort[i] AND iInTrade == 0)
	{
		Short[i] = 1;
		iSENT = L1[i];
		iSSL   = H1[i];
		iSTGT  = L1[i] - (RRR*RISK1[i]);
		iInTrade = -1;
		ShortPrice[i] = iSENT;
	}	
	
	if(iInTrade != 0)
	{
		BENT[i] = iBENT;
		BSL[i] = iBSL;
		BTGT[i] = iBTGT;
		
		SENT[i] = iSENT;
		SSL[i] = iSSL;
		STGT[i] = iSTGT;
		iInTradeArray[i] = iInTrade;
	
	}
	if (Sell[i] OR Cover[i])
	{
		iInTrade = 0;
	}
}

BVALID = IIf(iInTradeArray == 1 ,1,0);
SVALID = IIf(iInTradeArray == -1 ,1,0);

BTGT = Prec(BTGT,1);
STGT = Prec(STGT,1);

//Backtest-----------------------------------------------------------------------------------------
NDO = TimeFrameGetPrice( "O", inDaily );

SQTY = round(TDVAL/NDO);
IQTY = 1;
QTY = IIf(BTSI == "Stock", SQTY, IQTY);
Margin = IIf(BTSI == "Stock", 20, 100);
Comison = IIf(BTSI == "Stock", 0.04, 0);
MXOP = INEQ*5/TDVAL;

SetOption("InitialEquity",INEQ);
SetOption("MaxOpenPositions",MXOP);
SetOption("AccountMargin",Margin);
SetOption("CommissionMode",1);
SetOption("CommissionAmount",Comison);

SetOption("ActivateStopsImmediately",True); 
//SetOption("AllowSameBarExit",True);

SetPositionSize(QTY,spsShares);

//Exploration---------------------------------------------------------------------------------------------------------------
BWATCH = H1 < EMA52 AND H < H2;
SWATCH = L1 > EMA51 AND L > L1;
WatchStatus = WriteIf(BWATCH,"Watch for Buy", WriteIf(SWATCH,"Watch for Sell",""));

Filter = 1; //Buy OR Short OR BWATCH OR SWATCH;

AddColumn(IIf( Buy,66,IIf(Short,83,32) ),"Signal", formatChar, colorYellow, IIf(Buy, colorGreen,IIf( Short,colorRed, colorDefault) ));
AddTextColumn(WatchStatus, "Watch");

AddColumn(IIf(BVALID,BENT,IIf(SVALID,SENT,Null)), "Entry");
AddColumn(IIf(BVALID,BSL,IIf(SVALID,SSL,Null)), "Stop Loss");
AddColumn(IIf(BVALID,BTGT,IIf(SVALID,STGT,Null)), "Target");


//Chart------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} O %g, H %g, L %g, C %g  {{VALUES}}", O, H, L, C  ));
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

GfxTextOut( "Entry " +IIf(BVALID,BENT,IIf(SVALID,SENT,Null)), 700, 20);
GfxTextOut( "SL " +IIf(BVALID,BSL,IIf(SVALID,SSL,Null)), 700, 40);
GfxTextOut( "Target " +IIf(BVALID,BTGT,IIf(SVALID,STGT,Null)), 700, 60);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-20);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-30); 
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-25); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=30);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-25);

PlotShapes(IIf(Sell AND BTGTE, shapeDownTriangle, shapeNone),colorLime, 0, H, Offset=-30); 
PlotShapes(IIf(Sell AND BSLE, shapeCircle, shapeNone),colorLime, 0, H, Offset=30); 

PlotShapes(IIf(Cover AND STGTE, shapeUpTriangle, shapeNone),colorOrange , 0, L, Offset=-30);
PlotShapes(IIf(Cover AND SSLE, shapeCircle, shapeNone),colorOrange , 0, L, Offset=-30);

PlotShapes(IIf(SQOFF AND (Sell OR Cover), shapeSquare, shapeNone),colorWhite , 0, L, Offset=-30);

Plot( EMA520,"EMA5 15min",colorLime,styleStaircase|styleNoLabel);
Plot( EMA510,"EMA5 5min",colorRed,styleStaircase|styleNoLabel);

12 comments

1. Obul

Nice One!!!
Two EMA Lines are plotting, can you explain which one is 5 EMA Green/Red?

But Wherever Arrow marks plotted there the candle touching EMA Line which should not be

2. shreyans279911

The result for nifty 5min Timeframe is amazing, Why do say that it works only for banknifty? Also thanks for the afl.

3. EasySimpleTrade

Sorry. there is an issue with the afl. I’ll ask admin replace the afl or i repost again

4. EasySimpleTrade

Obul! Green is 15 min 5EMA which is for buy. Red is 5min which is for sell

5. administrator

@EasySimpleTrade I have updated the formula with the new one you posted.

6. shreyans279911

@EasySimpleTrade I think whatever mistake you did with that afl was good mistake because now the profits are reduced. WHat mistake did you do?

7. viraldalal

this formula is repainting ?

8. viraldalal

as per notes u said buy in 15 mins and sell in 5 mins

do we run in 5min scan ?

9. EasySimpleTrade

@administrator thanks admin, as updated post is in pending , i thought it not updated.

10. EasySimpleTrade

@viraldalal yes there is one issue for repaint in line 55 change TFIR to TFIIR .
EMA52 = Ref(EMA520,-TFIIR); Thanks for identifying

11. EasySimpleTrade

@viraldalal If you run in one minute it give accurate backtest result. or you can run in 5 minute

12. viraldalal

thank you for your inputs and solving problem in 1 day

Leave Comment

Please login here to leave a comment.

Back