// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Best Intraday Strategy");
//AFl by Viatrades
// Created by viatrades team

	SetBarsRequired(sbrAll, sbrAll);
	SetPositionSize(1, spsShares);
	
	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 ) ) ));
	SetBarFillColor(IIf(C>O,colorDarkOliveGreen,IIf(C<=O,colorBrown,colorLightGrey)));
	Plot(C,"\nPrice",IIf(C>O,colorBrightGreen,IIf(C<=O,colorRed,colorLightGrey)),64|styleNoTitle,0,0,0,0);
	
	DT = DateTime();
	DN = DateNum();
	TN = TimeNum();
	
	function Asign(x)
	{
		y = Null;
		for(i = 0; i < BarCount; i++)
		{
			y[i] = x;
		}
		
		return y;
	}
	
	Slcalc = Param("Stop Loss %", 1,0, 100, 0.01)/100;
	SlType = ParamToggle("StopLoss Type", "Initial|Trailing", 0);
	
	DayStart = DN != Ref(DN, -1);
	Dayscalc = Ref(DayStart, -9);
	DaysMeas = Flip(DayStart, Dayscalc);
	DayEnd = Ref(DayStart, 1);
	
	BY = Asign(False);
	SL = Asign(False);
	SH = Asign(False);
	CV = Asign(False);
	
	HGHcalc = HHV(High, 5);
	LVLcalc = LLV(Low, 5);
	
	LVLScalc = Null;
	LVLTScalc = Null;
	
	LVLTSArray = Null;
	HVLTSArray = Null;
	
	BYPrsV = Null;
	SLPrsV = Null;
	SHPrsV = Null;
	CVPrsV = Null;
		
	LSLV = Null;
	SSLV = Null;
	LSL = Null;
	SSL = Null;
	
	CCntr = 1;
	
	for(i = 1; i < BarCount; i++)
	{
		if(CCntr == 1 && DayStart[i] == False)
		{
			LVLScalc = HGHcalc[i-1];
			LVLTScalc = LVLcalc[i-1];
		}
		
		LVLTSArray[i] = LVLScalc;
		HVLTSArray[i] = LVLTScalc;
		
		CCntr--;
		if(CCntr <= 0 || DayStart[i] == True)
		{
			CCntr = 1;
		}
	}
	
	Buy = High > LVLTSArray;
	Short = Low < HVLTSArray;
	
	Sell = Low < HVLTSArray;
	Cover = High > LVLTSArray;
	
	
	Buy = ExRem(Buy, Sell);
	Sell = ExRem(Sell, Buy);
	
	Short = ExRem(Short, Cover);
	Cover = ExRem(Cover, Short);
	
	BuyPrice = ValueWhen(Buy, IIf(Open > LVLTSArray, Open, LVLTSArray));
	ShortPrice = ValueWhen(Short, IIf(Open < HVLTSArray, Open, HVLTSArray));
	
	SellPrice = IIf(Sell && Open < HVLTSArray, Open, HVLTSArray);
	CoverPrice = IIf(Cover && Open > LVLTSArray, Open, LVLTSArray);
	
	LSL = BuyPrice - (BuyPrice * Slcalc);
	SSL = ShortPrice + (ShortPrice * Slcalc);
	
	if(SlType == True)
	{
		HHVBy = HighestSince(Buy, High);
		LLVSh = LowestSince(Short, Low);
		LSL = IIf(Buy, BuyPrice - (BuyPrice * Slcalc), HHVBy - (HHVBy * Slcalc));
		SSL = IIf(Short, ShortPrice + (ShortPrice * Slcalc), LLVSh + (LLVSh * Slcalc));
	}
	
	Sell = Sell || Low <= LSL;
	Cover = Cover || High >= SSL;
	
	
	Buy = ExRem(Buy, Sell);
	Sell = ExRem(Sell, Buy);
	
	Short = ExRem(Short, Cover);
	Cover = ExRem(Cover, Short);
	
	SellPrice = IIf(Sell && Low <= LSL && Open < LSL, Open, IIf(Sell && Low <= LSL, LSL, SellPrice));
	CoverPrice = IIf(Cover && High >= SSL && Open > SSL, Open, IIf(Cover && High >= SSL, SSL, CoverPrice));
	
	PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBrightGreen, 0, Low, -15, 0);
	PlotShapes(IIf(Sell, shapeStar, shapeNone), colorRed, 0, High, -15, 0);
	
	PlotShapes(IIf(Short, shapeDownArrow, shapeNone), colorOrange, 0, High, -35, 0);
	PlotShapes(IIf(Cover, shapeStar, shapeNone), colorTurquoise, 0, Low, -35, 0);
	
	LongTrue = Flip(Buy, Sell) || Sell;
	ShortTrue = Flip(Short, Cover) || Cover;
	
	Plot(LVLTSArray, "LVLTSArray", colorGrey50, styleStaircase|styleDashed);
	Plot(HVLTSArray, "HVLTSArray", colorGrey50, styleStaircase|styleDashed);
	
	Plot(IIf(LongTrue, BuyPrice, Null), "BuyPrice", colorYellow, styleDashed|styleStaircase, Null, Null, 0, 1, 1);
	Plot(IIf(LongTrue, LSL, Null), "Long Stoploss", colorCustom12, styleDashed|styleStaircase, Null, Null, 0, 1, 1);
	
	Plot(IIf(ShortTrue, ShortPrice, Null), "ShortPrice", colorYellow, styleDashed|styleStaircase, Null, Null, 0, 1, 1);
	Plot(IIf(ShortTrue, SSL, Null), "Short Stoploss", colorCustom12, styleDashed|styleStaircase, Null, Null, 0, 1, 1);
	
_SECTION_END();