// Downloaded From https://www.WiseStockTrader.com
// Trade Triangles for Day Trading

_SECTION_BEGIN("Chart");
// Background color
SetChartBkColor(ParamColor("Background Color ",colorBlack));

_SECTION_BEGIN("Price");

SetChartOptions(0,chartShowArrows|chartShowDates);     
_N(Title = StrFormat("{{NAME}} - Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); 

_SECTION_END();

TimeFrameSet(inHourly);
no=Param( "Triangle D", 3, 1, 55 ,1);
hres=HHV(H,no);
hsup=LLV(L,no);
havd=IIf(H>Ref(hres,-1),1,IIf(L<Ref(hsup,-1),-1,0));
havn=ValueWhen(havd!=0,havd,1);
htsl=IIf(havn==1,hsup,hres);
hB=Cross(H,htsl);
hS=Cross(htsl,L);
TimeFrameRestore();
htsl= TimeFrameExpand(htsl,inHourly,expandFirst);
Plot(htsl,"D",colorPaleBlue,styleStaircase);
hb=TimeFrameExpand(hB,inHourly,expandFirst);
hs=TimeFrameExpand(hS,inHourly,expandFirst); 
hst=IIf(BarsSince(hB)<BarsSince(hS),1,0);
col=IIf(hst == 1 ,51,IIf(hst ==0,3,1));
Plot(2,"",COL,styleArea|styleOwnScale|styleNoLabel,0,100);
Plot(2.5,"",1,styleArea|styleOwnScale|styleNoLabel,0,100);

TimeFrameSet(inDaily);
no=Param( "Triangle D", 3, 1, 55 ,1);
dres=HHV(H,no);
dsup=LLV(L,no);
davd=IIf(H>Ref(dres,-1),1,IIf(L<Ref(dsup,-1),-1,0));
davn=ValueWhen(davd!=0,davd,1);
dtsl=IIf(davn==1,dsup,dres);
dB=Cross(H,dtsl);
dS=Cross(dtsl,L);
TimeFrameRestore();
dtsl= TimeFrameExpand(dtsl,inDaily,expandFirst);
Plot(dtsl,"D",colorSkyblue,styleStaircase);



db=TimeFrameExpand(dB,inDaily,expandFirst);
ds=TimeFrameExpand(dS,inDaily,expandFirst);
dst=IIf(BarsSince(DB)<BarsSince(DS),1,0);
col=IIf(dst == 1 ,51,IIf(dst ==0,31,1));
Plot(4,"",COL,styleArea|styleOwnScale|styleNoLabel,0,100);


Plot(C,"",IIf(C>O,colorWhite,colorBlack),64);

pds= Optimize("period",Param("period",8,5,30,1),5,30,1); 
Color = ParamColor("Color", colorLightGrey );
DUpper =HHV(Ref(H,-1),pds);
DLower = LLV(Ref(L,-1),pds);
DMiddle = (DUpper+DLower)/2;
R=ValueWhen(C < DLower,H,1);
S=ValueWhen(C > DUpper,L,1);
PlotOHLC( DUpper, DUpper, DMiddle, DMiddle, "", ColorBlend( ColorRGB(50,90,50), colorWhite, 0.1 ), styleCloud | styleNoRescale, Null, Null, Null, -1 );
PlotOHLC( DMiddle, DMiddle,DLower,DLower, "", ColorBlend( ColorRGB(90,50,50), colorWhite, 0.1 ), styleCloud | styleNoRescale, Null, Null, Null, -1 );

Buy1= (dst ==1 AND Hst==1) ;
Sell1=( Hst==0 OR dst ==0);
Buy1=ExRem(Buy1,Sell1);
Sell1=ExRem(Sell1,Buy1);
PlotShapes(Buy1*shapeSmallUpTriangle,colorLime,0,L,-15);
PlotShapes(sell1*shapeSmallDownTriangle, colorTan,0,H,-15);
Short1=(dst ==0 AND Hst==0);
Cover1=( Hst==1);
Short1=ExRem(Short1,Cover1);
Cover1=ExRem(Cover1,Short1);
PlotShapes(Cover1*shapeSmallUpTriangle,colorOrange,0,L,-15);
PlotShapes(Short1*shapeSmallDownTriangle,colorRed,0,H,-15);

Buy2=(dst ==0 AND Hst==1) AND (Cross(C ,DUpper) AND NOT GapUp());
Sell2=(dst ==0 AND Hst==1) AND (Ref(C,-1)>Dmiddle AND Cross(Dmiddle,C) );
Buy2=ExRem(Buy2,Sell2);
Sell2=ExRem(Sell2,Buy2);
PlotShapes(Buy2*shapeHollowSmallUpTriangle,colorLime,0,L,-15);
PlotShapes(sell2*shapeHollowSmallDownTriangle,colorOrange,0,H,-15);

Short2=(dst ==1 AND Hst==0 )AND ( Cross(C,Dlower));
Cover2=( Hst==1);
Short2=ExRem(Short2,Cover2);
Cover2=ExRem(Cover2,Short2);
PlotShapes(Cover2*shapeHollowSmallUpTriangle,colorOrange,0,L,-15);
PlotShapes(Short2*shapeHollowSmallDownTriangle,colorRed,0,H,-15);


for( i = 0; i < BarCount; i++ ) 
{ 

//if(Buy1[i] ) PlotText( "Buy 1  "  , i, DLower[ i ], colorLime ); 
//if(Sell1[i] )  PlotText( " Sell 1"  , i, DUpper[ i ], colorOrange );
//if(Short1[i] ) PlotText( "Short 1  "  , i, DUpper[ i ], colorRed ); 
//if(Cover1[i] )  PlotText( " Cover 1"  , i, DLower[ i ], colorTan ); 

//if(Buy2[i] ) PlotText( "Buy 2  "  , i, DLower[ i ], colorLime ); 
//if(Sell2[i] )  PlotText( " Sell 2"  , i, DUpper[ i ], colorOrange );
//if(Short2[i] ) PlotText( "Short 2  "  , i, DUpper[ i ], colorRed ); 
//if(Cover2[i] )  PlotText( " Cover 2"  , i, DLower[ i ], colorTan ); 
} 
/*
baratbuy = ValueWhen( (Buy1 OR Buy2) , BarIndex() ) ; 
baratsell = ValueWhen( (short1 OR Short2) , BarIndex() ) ; 
price=C[BarCount-1]; 
Buy= (Buy1 OR Buy2) AND  ( BarCount - baratbuy );
Sell=(short1 OR Short2)  AND  ( BarCount - baratsell );
stoploss = IIf( Buy, ( price  * ( 1 - 0.01 ) ) , IIf( Sell, ( price  * ( 1 + 0.01 ) ), 0 ) ); 
target1per = IIf( Buy, ( price  * ( 1 + 0.01 ) ) , IIf( Sell, ( price  *  ( 1 - 0.01 ) ), 0 ) ); 
target15per = IIf( Buy, ( price  * ( 1 + 0.015 ) ) , IIf( Sell, ( price *  ( 1 - 0.015 ) ), 0 ) ); 
target2per = IIf( Buy, ( price  * ( 1 + 0.02 ) ) , IIf( Sell, ( price  * ( 1 - 0.02 ) ), 0 ) ); 
*/