// Downloaded From https://www.WiseStockTrader.com
//------------------------------------------------------------------------------
//
//  Formula Name:    3-13-39-ema
//  Author/Uploader: Abhishek Gupta
//  E-mail:          abhishekgupta10d12@gmail.com
//  Date/Time Added: 2013-12-17 
//  Date/Time Updated: 2014-02-14
//  Version: 1.1
//  Origin:          
//  Keywords:        
//  Level:           beginner/medium
//  Flags:           indicator
//
//------------------------------------------------------------------------------
//
//	 This is just a development of a system I found here: http://debarghyamukherjee.webs.com/3-13-39-ema-technique
//	 into an AFL.
// 
//	 Also I implemented a simple technique for trailing stop loss borrowed from
//	 http://www.wisestocktrader.com/indicators/3907-profit-trading-system-with-target-and-stoploss
// 
//  For more details see the remarks below.
//
//------------------------------------------------------------------------------
//
//	Application: Drag & Drop.
//
//	This system uses Stochastic to define the trend and 3/13ema for entry values.
//	Exit is done on the basis of trailing SL. Stay in trade until SL is hit.
//	ADX is used to see if there is enough momentum.
//	Best used on a 15min or greater time frame.
//	The best use is in trending stocks.
//	Advice: Do not carry an over-night position without a proper hedge.
//
//	Written by: Abhishek Gupta

// Toggle candle visibility
if (ParamToggle("Candles", "Show|Hide",0)){
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleCandle | styleThick );
_SECTION_END();
}

_SECTION_BEGIN("Trailing SL");
no		= Param( "Swing", 5, 1, 55 );
res		= HHV(H,no);
sup		= LLV(L,no);
tsl		= IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res);
Plot(tsl, _DEFAULT_NAME(), colorBlue, styleStaircase);
_SECTION_END();

_SECTION_BEGIN("Stochastic");
periods	= Param( "Periods", 15, 1, 200, 1 );
Ksmooth	= Param( "%K avg", 3, 1, 200, 1 );
Dsmooth	= Param( "%D avg", 3, 1, 200, 1 );
stocK		= StochK(periods, Ksmooth);
stocD		= StochD(periods, Ksmooth, Dsmooth);
_SECTION_END();

_SECTION_BEGIN("ADX");
range	= Param("Periods", 14, 10, 100, 1);
factor	= Param("Factor", 15, 10, 30, 1);
ADX14 = ADX(14);
_SECTION_END();

_SECTION_BEGIN("Slow EMA");
Ps = ParamField("Price field", 4);
Periods = Param("Periods", 3, 2, 300, 1 );
EMAslow	= EMA( Ps, Periods );
Plot( EMAslow, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleHidden | styleLine | styleNoLabel ) | styleNoRescale ); 
_SECTION_END();

_SECTION_BEGIN("Fast EMA");
Pf = ParamField("Price field", 4);
Periodf = Param("Periods", 13, 2, 300, 1 );
EMAfast	= EMA( Pf, Periodf );
Plot( EMAfast, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleHidden | styleLine | styleNoLabel ) | styleNoRescale ); 
_SECTION_END();

_SECTION_BEGIN("Trading signals");
sig		= "";

// Buy, Sell, Short and Cover conditions
Buy = Cross( EMAslow, EMAfast)
	//AND tsl<Low
	AND stocK > Ref(stocK, -1)
	AND ADX14 > Ref(ADX14, -1) AND ADX14>15;

Sell = Cross( tsl, Low);

Short = Cross( EMAfast, EMAslow)
	//AND tsl>High
	AND stocK < Ref(stocK, -1)
	AND ADX14 > Ref(ADX14, -1) AND ADX14>15;

Cover = Cross( High, tsl);
// End Conditions

Buy		= ExRem(Buy, Sell);
Sell	= ExRem(Sell, Buy);

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

BuyPrice		= ValueWhen(Buy, Close);
ShortPrice		= ValueWhen(Short, Close);
CoverPrice		= ValueWhen(Cover, Close);
SellPrice		= ValueWhen(Sell, Close);

dist	= 1.5*ATR(10);
for (i=0; i<BarCount; i++) {
	if (Cover[i]) {
		PlotText( "\nCover short: " + CoverPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		PlotText( "\n\nProfit: " + (ShortPrice[i]-CoverPrice[i]), i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if (Sell[i]) {
		PlotText( "\nSell bought: " + SellPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		PlotText( "\n\nProfit: " + (SellPrice[i]-BuyPrice[i]), i+1.5, H[ i ]+dist[i]+5, colorOrange);
	}
	if(Buy[i]) {
		PlotText( "Buy: " + BuyPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		sig		= "Buy";
	} else if( Short[i]) {
		PlotText( "Short: " + ShortPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		sig	= "Short";
 	}
}

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low, -28);
PlotShapes(Short*shapeDownArrow, colorRed, 0, High, -28);
PlotShapes(Cover*shapeHollowUpArrow, colorGreen, 0, Low, -45);
PlotShapes(Sell*shapeHollowDownArrow, colorRed, 0, High, -45);

// Write interpretation
if (sig == "Buy") {
	printf("Signal came " + BarsSince(Buy) + " bars ago");
	printf("\n" + sig + " @ " + BuyPrice);
	printf("\nTrailing SL: " + tsl);
	printf("\n\nPossiblities ");
	printf("\nMax Profit: " + ((O+H+L+C)/4-BuyPrice));
	printf("\nMin Profit: " + (tsl-BuyPrice));
} else if (sig== "Short") {
	printf("Signal came " + BarsSince(Short) + " bars ago");
	printf("\n" + sig + " @ " + ShortPrice);
	printf("\nTrailing SL: " + tsl);
	printf("\n\nPossiblities ");
	printf("\nMax Profit: " + (ShortPrice-(O+H+L+C)/4));
	printf("\nMin Profit: " + (ShortPrice-tsl));
}
// End interpretation

// Write Messages
printf("\n\nLook for trending markets bascially on a 15min or higher time frame.");
printf("\n\nLet the profit run.");
printf("\nClose a call only when trailing SL hits.");
printf("\n\nDo NOT carry an over-night position without a proper hedge.");
// End messages
_SECTION_END();