// Downloaded From https://www.WiseStockTrader.com
//
/*
	Variables
*/
//

SetBarsRequired(-2,0);
SetChartOptions(0,chartShowArrows|chartShowDates);

Buy = Cover = Cross(MA(Close,10), MA(Close,30));
Sell = Short = Cross(MA(Close,30), MA(Close,10));

BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;

Plot(C,"\nC", colorBlack, styleCandle);

tick_size = IIf(TickSize != 0, TickSize, 1);

cost = Param("Total Transaction Cost", 10, 5, 300, 5);
Slippage = Param("Total Slippage", 10, 10, 100, 10);
totalcost = 0;//(slippage + cost)*TickSize;

LongTrades = 0;
ShortTrades = 0;

//Longs
bar1LongProfit = 0;
bar5LongProfit = 0;
bar10LongProfit = 0;
bar15LongProfit = 0;
bar20LongProfit = 0;

bar1Sell = BarsSince(Buy) == 1;
bar5Sell = BarsSince(Buy) == 5;
bar10Sell = BarsSince(Buy) == 10;
bar15Sell = BarsSince(Buy) == 15;
bar20Sell = BarsSince(Buy) == 20;

//Shorts
bar1ShortProfit = 0;
bar5ShortProfit = 0;
bar10ShortProfit = 0;
bar15ShortProfit = 0;
bar20ShortProfit = 0;

bar1Cover = BarsSince(Short) == 1;
bar5Cover = BarsSince(Short) == 5;
bar10Cover = BarsSince(Short) == 10;
bar15Cover = BarsSince(Short) == 15;
bar20Cover = BarsSince(Short) == 20;

//
/*
	Functions
*/
//

for(i = 1; i < BarCount-1; i++)
{
	if(Buy[i]) LongTrades++;
	if(Short[i]) ShortTrades++;
}

procedure CalculateLongWinners(numBars)
{
	LongWinners = 0;
	
	for(j = numBars; j < BarCount - 1; j++)
	{
		if(/*real*/Buy[j-numBars])
		{
			LongProfit[j] = SellPrice[j] - (BuyPrice[j-numBars] + totalcost);
		
			if(LongProfit[j] > 0) LongWinners++;
		}
	}

	return LongWinners;
}

procedure CalculateShortWinners(numBars)
{
	ShortWinners = 0;
	
	for(k = numBars; k < BarCount - 1; k++)
	{
		if(/*real*/Short[k-numBars])
		{
			ShortProfit[k] = CoverPrice[k] - (ShortPrice[k-numBars] + totalcost);
		
			if(ShortProfit[k] > 0) ShortWinners++;
		}
	}

	return ShortWinners;
}


//
/*
	Long Trades
*/
//
printf("\n=-=-=- TRADE ENTRY EFFICIENCY -=-=-=");

printf("\n");

printf("\nExiting after 1, 5, 10, 15, and 20 bars offer you some data to analyze. ");
printf("\n\nExiting after 1 bar lets you determine if the entry gets you profitable right away. If not, you may want to tweak your entry setting a little bit.");
printf("\n\nExiting after 5 and 10 bars gives the entry a little bit of time to work. You can then compare the 5 and 10 bar data with the 15 and 20 bar data and if your percentages are better for the 15 and 20 bar exits than the 5 and 10 bar exits,\n it means your entry is a little bit too early AND you may want wait a bit to enter.");
printf("\n\nIf your 5 and 10 bars exits are good but the 15 and 20 drop off significantly, it can mean you have a good short term entry, but you may want to take profits sooner rather than later.");

printf("\n");

printf("\nLong Trades");

printf("\nEntry Efficiency for:");

bar1LongEfficiency = (CalculateLongWinners(1)/LongTrades)*100;
bar5LongEfficiency = (CalculateLongWinners(5)/LongTrades)*100;
bar10LongEfficiency = (CalculateLongWinners(10)/LongTrades)*100;
bar15LongEfficiency = (CalculateLongWinners(15)/LongTrades)*100;
bar20LongEfficiency = (CalculateLongWinners(20)/LongTrades)*100;

printf("\nBar 1 exit: " + NumToStr(bar1LongEfficiency , 1.2) + "%%");
printf("\nBar 5 exit: " + NumToStr(bar5LongEfficiency , 1.2) + "%%");
printf("\nBar 10 exit: " + NumToStr(bar10LongEfficiency , 1.2) + "%%");
printf("\nBar 15 exit: " + NumToStr(bar15LongEfficiency , 1.2) + "%%");
printf("\nBar 20 exit: " + NumToStr(bar20LongEfficiency , 1.2) + "%%");
printf("\nLongTrades: " + NumToStr(Longtrades,1.0));
printf("\n");

//
/*
	Short Trades
*/
//

bar1ShortEfficiency = (CalculateShortWinners(1)/ShortTrades)*100;
bar5ShortEfficiency = (CalculateShortWinners(5)/ShortTrades)*100;
bar10ShortEfficiency = (CalculateShortWinners(10)/ShortTrades)*100;
bar15ShortEfficiency = (CalculateShortWinners(15)/ShortTrades)*100;
bar20ShortEfficiency = (CalculateShortWinners(20)/ShortTrades)*100;

printf("\nShort Trades");

printf("\nEntry Efficiency for:");
printf("\nBar 1 exit: " + NumToStr(bar1ShortEfficiency , 1.2) + "%%");
printf("\nBar 5 exit: " + NumToStr(bar5ShortEfficiency , 1.2) + "%%");
printf("\nBar 10 exit: " + NumToStr(bar10ShortEfficiency , 1.2) + "%%");
printf("\nBar 15 exit: " + NumToStr(bar15ShortEfficiency , 1.2) + "%%");
printf("\nBar 20 exit: " + NumToStr(bar20ShortEfficiency , 1.2) + "%%");
printf("\nShortTrades: " + NumToStr(Shorttrades,1.0));

_SECTION_END();