// Downloaded From https://www.WiseStockTrader.com
Stop1 = 0;
myATR=ATR(14);
 for( i = 1; i < BarCount; i++ )
 {
   PREV = stop1[ i - 1 ];
   HH = H[ i ];
   LL=L[i];
   ar=4.5*myATR[i];
   Stop1[ i ] = IIf( PREV < LL,IIf(( HH - ar ) >= PREV,( HH - ar ),PREV),( HH - ar));
 }

Stop2 = 0;
 for( i = 1; i < BarCount; i++ )
 {
   PREV = Stop2[ i - 1 ];
   CC= C[ i ];
   LL=L[i];
   Stop2[ i ] = IIf( PREV < LL,IIf(( CC - 3.5*myATR[i] ) >= PREV,( CC - 3.5*myATR[i] ),PREV),( CC - 3.5*myATR[i] ));
 }
StopLong=IIf(Stop1>Stop2,Stop1,Stop2);

Stop3 = 0;
 for( i = 1; i < BarCount; i++ )
 {
   PREV = stop3[ i - 1 ];
   HH = H[ i ];
   LL=L[i];
   Stop3[ i ] = IIf( PREV > HH,IIf(( LL + 4*myATR[i] ) <= PREV,( LL + 4*myATR[i]),PREV),( LL + 4*myATR[i] ));
 }

Stop4 = 0;
 for( i = 1; i < BarCount; i++ )
 {
   PREV = Stop4[ i - 1 ];
   CC= C[ i ];
   HH=H[i];
   Stop4[ i ] = IIf( PREV > HH,IIf(( CC + 3.5*myATR[i] ) <= PREV,( CC + 3.5*myATR[i] ),PREV),( CC + 3.5*myATR[i] ));
 }
StopShort=IIf(Stop3<Stop4,Stop3,Stop4);

result=IIf( (BarsSince(L<Ref(StopLong,-1))>BarsSince(H>Ref(StopShort,-1)) ),StopLong,StopShort);

Buy=IIf(Cross(MA(C,1), result ) ,1,0);
Sell=IIf(Cross(result ,MA(C,1) ),1,0);

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

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorBlue, colorRed ), 0, IIf( Buy, Low, High ) );

//uptrend=PDI(42)>MDI(42)AND PDI(14)>MDI(14);
//downtrend=PDI(42)<MDI(42) AND PDI(14)<MDI(14);

uptrend=IIf(result<C,1,0);
downtrend=IIf(result>C,1,0);

Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, 43, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

//state=IIf(BarsSince(Buy)<BarsSince(Sell),1,0);

bullish=IIf((PDI(42)>MDI(42)) AND (PDI(14)>MDI(14)),1,0);
bearish=IIf((PDI(42)<MDI(42)) AND (PDI(14)<MDI(14)),1,0);
barcolor= colorBlack;
yello=IIf(((H>Ref(H,1)) AND(H>Ref(H,2)) AND(H>Ref(H,3)) AND(Ref(C,4)>0) AND(L<Ref(L,1)) AND(L<Ref(L,2)) AND(L<Ref(L,3))),1,0);
nocol=IIf(NOT(bullish OR bearish),1,0);
Col=bullish*43 + bearish*colorRed+nocol*colorBlack;
Colr=IIf(yello,colorYellow,Col);
//col=IIf(state == 1 ,43,IIf(state ==0,4,1));
Plot(C,"",Colr,Param("chart Type",128,64,128,64));


a=IIf(C>result,0,1);
Plot(result,"Swing",IIf(a,colorBlue,colorRed),styleStaircase);
s=IIf(C<result,0,1);

period=Param("Period",30,10,100);
RWH=(High-Ref(Low,-period))/(ATR(period)*sqrt(period));
RWL=(Ref(H,-period)-L)/(ATR(period)*sqrt(period));
Pk=WMA((RWH-RWL),3);
MN=MA(Pk,period);
SD=StDev(Pk,period);
Val1=IIf(MN+(1.33*SD)>2.08,MN+(1.33*SD),2.08);
Val2=IIf(MN-(1.33*SD)<-1.92,MN-(1.33*SD),-1.92);
ln1=IIf(Ref(Pk,-1)>=0 AND Pk>0,Val1,IIf(Ref(Pk,-1)<=0 AND Pk<0,Val2,0));
Red=IIf(Ref(Pk,-1)>Pk,Pk,0);
Green=IIf(Pk>Ref(Pk,-1),Pk,0);

tip=IIf(green>ln1 AND green>1,1,0);
bip=IIf(red<ln1 AND red<-1,1,0);
top=IIf(red>ln1 AND red>1,1,0);
bottom=IIf(green<ln1 AND green<-1,1,0);

printf("\nGreen" + WriteVal(green));
printf("\nRed" + WriteVal(red));
printf("\nLN" + WriteVal(ln1));
printf("\nRWH" + WriteVal(RWH));
printf("\nRWL" + WriteVal(RWL));
printf("\npk" + WriteVal(pk));
printf("\nMN" + WriteVal(MN));
printf("\nSD" + WriteVal(SD));
printf("\nVAL1" + WriteVal(Val1));
printf("\nVAL2" + WriteVal(Val2));

shape=tip*shapeSmallCircle + bip*shapeSmallCircle + top*shapeCircle + bottom*shapeCircle ;

PlotShapes( shape, IIf( tip OR top, colorGreen, colorRed ), 0, IIf( bip OR bottom, Low, High ),IIf(bip OR bottom,-25,25) );

Prev_Close= TimeFrameGetPrice( "C", inDaily, -1, expandFirst) ;
Plot(prev_close,"Privious Close",colorBlack);

SetChartBkColor(colorTan); // color of outer border //default LightGrey
SetChartOptions( 0, chartShowDates); 


_SECTION_BEGIN("StockTrends");
_N(Title = "{{NAME}} - {{INTERVAL}} {{DATE}}: "+_DEFAULT_NAME()+" : {{OHLCX}}"// {{VALUES}}" 
+"\n"+WriteIf(s,"StopLoss for your Long Position is ="+ EncodeColor(colorBlue) + WriteVal(result)+" ,","") +
WriteIf(1-s,"StopLoss for your Short Position is ="+EncodeColor(colorRed)+WriteVal(result)+" ,",""));