// Downloaded From https://www.WiseStockTrader.com
procedure buySellShortCover_proc(R3R4,S3S4,R1S1,R1,R2,R3,R4,R5,R6,R7,R8,S1,S2,S3,S4,S5,S6,S7,S8,S4S5,S5S6,S6S7,S7S8,R4R5,R5R6,R6R7,R7R8,
firstTradeBarOfDay,timearr,endTradeTime,TickSize,ema1,TimeWindowTrade) 
{
global BuyAdjusted;
global BuyPriceAdjusted;
global ShortAdjusted;
global ShortPriceAdjusted;
global ShortPriceAdjusted;
global SellAdjusted;
global SellPriceAdjusted;
global CoverAdjusted;
global CoverPriceAdjusted;

BuyAdjusted=0;
BuyPriceAdjusted=0;
ShortAdjusted=0;
ShortPriceAdjusted=0;
SellAdjusted=0;
SellPriceAdjusted=0;
CoverAdjusted=0;
CoverPriceAdjusted=0;

delay=1;
slip=TickSize*0;

for(i=1;i<BarCount;i++) 
{
	////////////////////////////////////////////LONG TRADES
	// long trade at S3
	ML=S3;LL=S3S4;HL=S2;
	if( ((C[i-1]>ML[i] AND L[i]<ML[i]) OR (O[i]>ML[i] AND L[i]<ML[i])) AND ML[i]>ema1[i] AND TimeWindowTrade[i] ) 
	{
		BuyAdjusted[i]=1;
		BuyPriceAdjusted[i]=Min(O[i],floor(ML[i]/TickSize)*TickSize)+slip;	
		
		for(j=i+delay;j<BarCount;j++) 
		{	
			if(timearr[j-1]>=endTradeTime)
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=O[j]-slip;
				i=j;
				break;	
			}
			else if(C[j-1]<LL[j-1])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=O[j]-slip;
				i=j;
				break;
			}	
			else if(H[j]>HL[j])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=Max(O[j],ceil(HL[j]/TickSize)*TickSize)-slip;
				i=j;
				break;
			}				
			else if(j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}
		}
	}	
	// long trade at S4
	ML=S4;LL=S4S5;HL=S3S4;
	if( ((C[i-1]>ML[i] AND L[i]<ML[i]) OR (O[i]>ML[i] AND L[i]<ML[i])) AND ML[i]>ema1[i] AND TimeWindowTrade[i] ) 
	{
		BuyAdjusted[i]=1;
		BuyPriceAdjusted[i]=Min(O[i],floor(ML[i]/TickSize)*TickSize)+slip;	
		
		for(j=i+delay;j<BarCount;j++) 
		{	
			if(timearr[j-1]>=endTradeTime)
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=O[j]-slip;
				i=j;
				break;	
			}
			else if(C[j-1]<LL[j-1])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=O[j]-slip;
				i=j;
				break;
			}	
			else if(H[j]>HL[j])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=Max(O[j],ceil(HL[j]/TickSize)*TickSize)-slip;
				i=j;
				break;
			}				
			else if(j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}
		}
	}
	////////////////////////////////////////////SHORT TRADES
	// short trade at R4
	ML=R4;LL=R3R4;HL=R4R5;
	if( ((C[i-1]<ML[i] AND H[i]>ML[i]) OR (O[i]<ML[i] AND H[i]>ML[i])) AND ML[i]<ema1[i] AND TimeWindowTrade[i] ) 
	{
		ShortAdjusted[i]=1;
		ShortPriceAdjusted[i]=Max(O[i],ceil(ML[i]/TickSize)*TickSize)-slip;	
		
		for(j=i+delay;j<BarCount;j++) 
		{	
			if(timearr[j-1]>=endTradeTime)
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=O[j]+slip;
				i=j;
				break;	
			}
			else if(C[j-1]>HL[j-1])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=O[j]+slip;
				i=j;
				break;
			}	
			else if(L[j]<LL[j])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=Min(O[j],floor(LL[j]/TickSize)*TickSize)+slip;
				i=j;
				break;
			}				
			else if (j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}
		}
	} 	
	// short trade at R3
	ML=R3;LL=R2;HL=R3R4;
	if( ((C[i-1]<ML[i] AND H[i]>ML[i]) OR (O[i]<ML[i] AND H[i]>ML[i])) AND ML[i]<ema1[i] AND TimeWindowTrade[i] ) 
	{
		ShortAdjusted[i]=1;
		ShortPriceAdjusted[i]=Max(O[i],ceil(ML[i]/TickSize)*TickSize)-slip;	
		
		for(j=i+delay;j<BarCount;j++) 
		{	
			if(timearr[j-1]>=endTradeTime)
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=O[j]+slip;
				i=j;
				break;	
			}
			else if(C[j-1]>HL[j-1])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=O[j]+slip;
				i=j;
				break;
			}	
			else if(L[j]<LL[j])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=Min(O[j],floor(LL[j]/TickSize)*TickSize)+slip;
				i=j;
				break;
			}				
			else if (j==BarCount-1) 
			{
				i=BarCount;
				break;				
			}
		}
	} 		
}
}

SetTradeDelays(0,0,0,0);
SetOption("CommissionMode",3);
SetOption("CommissionAmount",2.01);
SetOption("FuturesMode",True);
SetOption("PriceBoundChecking",False);
SetOption("AllowSameBarExit",True);
NumContracts=1;
PositionSize=NumContracts*MarginDeposit;
SetOption("MaxOpenPositions",5); 

beginCalcTime=093000;endCalcTime=160000;
beginTradeTime=093000;endTradeTime=155500;

timeWindowTrade=TimeNum()>=beginTradeTime AND TimeNum()<=endTradeTime;
timeWindowCalc=TimeNum()>=beginCalcTime AND TimeNum()<=endCalcTime;

firstBarOfDay=TimeNum()>=beginCalcTime ;firstBarOfDay=(firstBarOfDay-Ref(firstBarOfDay,-1))==1;
lastBarOfDay=TimeNum()>=endCalcTime;lastBarOfDay=(lastBarOfDay-Ref(lastBarOfDay,-1))==1;
firstTradeBarOfDay=TimeNum()>=beginTradeTime;firstTradeBarOfDay=(firstTradeBarOfDay-Ref(firstTradeBarOfDay,-1))==1;
lastTradeBarOfDay=TimeNum()>=endTradeTime;lastTradeBarOfDay=(lastTradeBarOfDay-Ref(lastTradeBarOfDay,-1))==1;
ttt=firstBarOfDay OR lastBarOfDay;
inTrade=Flip(firstBarOfDay,LastBarOfDay);

myHigh=ValueWhen(timeWindowCalc,HighestSince(firstBarOfDay,High)); 
myLow=ValueWhen(timeWindowCalc,LowestSince(firstBarOfDay,Low)); 
myClose=ValueWhen(lastBarOfDay,C);
myOpen=ValueWhen(firstBarOfDay,O);

DH=TimeFrameCompress(myHigh,inDaily,compressLast); 
DL=TimeFrameCompress(myLow,inDaily,compressLast); 
DC=TimeFrameCompress(myClose,inDaily,compressLast);

bk=1;DH=Ref(DH,-bk);DL=Ref(DL,-bk);DC=Ref(DC,-bk);

YHigh=TimeFrameExpand(DH,inDaily,expandFirst); 
YLow=TimeFrameExpand(DL,inDaily,expandFirst); 
YClose=TimeFrameExpand(DC,inDaily,expandFirst); 

fact=1.1;
R1=YClose+((YHigh-Ylow)*fact)/12;
R2=YClose+((YHigh-Ylow)*fact)/6;
R3=YClose+((YHigh-Ylow)*fact)/4;
R4=YClose+((YHigh-Ylow)*fact)/2;

S1=YClose-((YHigh-Ylow)*fact)/12;
S2=YClose-((YHigh-Ylow)*fact)/6;
S3=YClose-((YHigh-Ylow)*fact)/4;
S4=YClose-((YHigh-Ylow)*fact)/2;

R5=((YHigh-Ylow)*0.82)+YClose;
R6=((YHigh-Ylow)*1.09)+YClose;
R7=((YHigh-Ylow)*1.35)+YClose;
R8=((YHigh-Ylow)*1.62)+YClose;

S5=YClose-((YHigh-Ylow)*0.82);
S6=YClose-((YHigh-Ylow)*1.09);
S7=YClose-((YHigh-Ylow)*1.35);
S8=YClose-((YHigh-Ylow)*1.62);

R3R4=(R3+R4)/2;
S3S4=(S3+S4)/2;
R1S1=(R1+S1)/2;
S4S5=(S4+S5)/2;
S5S6=(S5+S6)/2;
S6S7=(S6+S7)/2;
S7S8=(S7+S8)/2;
R4R5=(R4+R5)/2;
R5R6=(R5+R6)/2;
R6R7=(R6+R7)/2;
R7R8=(R7+R8)/2;

per=Optimize("per",40,2,100,1);
ema1=Ref(EMA(C,per),-1);

buySellShortCover_proc(R3R4,S3S4,R1S1,R1,R2,R3,R4,R5,R6,R7,R8,S1,S2,S3,S4,S5,S6,S7,S8,S4S5,S5S6,S6S7,S7S8,R4R5,R5R6,R6R7,R7R8,
firstTradeBarOfDay,TimeNum(),endTradeTime,TickSize,ema1,TimeWindowTrade); 
Buy=BuyAdjusted;
BuyPrice=BuyPriceAdjusted;
Short=ShortAdjusted;
ShortPrice=ShortPriceAdjusted;
Sell=SellAdjusted;
SellPrice=SellPriceAdjusted;
Cover=CoverAdjusted;
CoverPrice=CoverPriceAdjusted;

GraphXSpace = 5;SetChartOptions(0, chartShowDates);
SetBarFillColor(IIf(C>O,colorGreen,IIf(C<=O,colorRed,colorLightGrey)));
Plot(C,"Price",IIf(C>O,colorDarkGreen,IIf(C<=O,colorDarkRed,colorLightGrey)),64,0,0,0,0);

if(Interval()==in5Minute)
{
PlotPivots = ParamToggle("Main Pivots","Show|Hide",0); 
Plottop = ParamToggle("Top Pivots","Show|Hide",0); 
Plotbot = ParamToggle("Bottom Pivots","Show|Hide",0); 
Plotmid = ParamToggle("Middle Pivots","Show|Hide",0); 
if (PlotPivots == 0)
{
Plot(R3,"",ColorRGB(0,255,0),styleThick,0,0,0,-1);
Plot(S3,"",ColorRGB(255-0,255-255,255-0),styleThick,0,0,0,-1);
Plot(R4,"",ColorRGB(0,100,0),styleThick,0,0,0,-1);
Plot(S4,"",ColorRGB(255-0,255-100,255-0),styleThick,0,0,0,-1);
Plot(ema1,"",ColorRGB(80,80,80),styleDashed,0,0,0,-1);
PlotOHLC(R4,R4,R3,R3,"",ColorRGB(30,0,0),styleCloud|styleNoLabel,0,0,0,-2); 
PlotOHLC(S3,S3,S4,S4,"",ColorRGB(0,30,0),styleCloud|styleNoLabel,0,0,0,-2);
Plot(R3R4,"",ColorRGB(181,206,162),styleDashed,0,0,0,-1);
Plot(S3S4,"",ColorRGB(255,182,193),styleDashed,0,0,0,-1);

if (Plottop == 0)
{
Plot(R8,"",ColorRGB(255-104,255-34,255-139),styleLine);
Plot(R5,"",ColorRGB(202,255,112),styleLine);
Plot(R6,"",ColorRGB(238,180,180),styleLine);
Plot(R7,"",ColorRGB(255,64,64),styleLine);
Plot(R4R5,"",colorWhite,styleDashed);
Plot(R5R6,"",colorWhite,styleDashed);
Plot(R6R7,"",colorWhite,styleDashed);
Plot(R7R8,"",colorWhite,styleDashed);
}
if (Plotbot == 0)
{
Plot(S8,"",ColorRGB(104,34,139),styleLine);
Plot(S5,"",ColorRGB(255-202,255-255,255-112),styleLine);
Plot(S6,"",ColorRGB(255-238,255-180,255-180),styleLine);
Plot(S7,"",ColorRGB(255-255,255-64,255-64),styleLine);
Plot(S4S5,"",colorWhite,styleDashed);
Plot(S5S6,"",colorWhite,styleDashed);
Plot(S6S7,"",colorWhite,styleDashed);
Plot(S7S8,"",colorWhite,styleDashed);
}
if (Plotmid == 0)
{
Plot(R1S1,"",ColorRGB(255,255,255),styleDashed);
Plot(R2,"",ColorRGB(132,112,255),styleLine);
Plot(R1,"",ColorRGB(255,187,255),styleLine);
Plot(S1,"",ColorRGB(255-255,255-187,255-255),styleLine);
Plot(S2,"",ColorRGB(255-132,255-112,255-255),styleLine);
}

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorRed,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeSmallCircle,shapeNone),colorWhite,0,ShortPrice,0);
PlotShapes(IIf(Cover,shapeSmallUpTriangle,shapeNone),colorDarkGreen,0,L,IIf(Cover AND Buy,-30,-15));
PlotShapes(IIf(Cover,shapeSmallCircle,shapeNone),colorWhite,0,CoverPrice,0);
Plot(firstTradeBarOfDay,"",ColorRGB(0,0,255),styleHistogram|styleThick|styleOwnScale|styleNoLabel,0,1,0,-0);
Plot(lastTradeBarOfDay,"",ColorRGB(200,200,200),styleHistogram|styleThick|styleOwnScale|styleNoLabel,0,1,0,0);
}
}