// Downloaded From https://www.WiseStockTrader.com
function ConnorsRSI(lenRSI, lenUD, lenROC)
{
      upDays = BarsSince(C <= Ref(C,-1));
      downDays = BarsSince(C >= Ref(C,-1));
      updownDays = IIf(upDays > 0, upDays, IIf(downDays > 0, -downDays, 0));
      crsi = ( PercentRank(ROC(C,1), lenROC) + RSIa(updownDays,lenUD) +
RSI(lenRSI))/3;
      return crsi;
}

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
ECw= EncodeColor(colorWhite);
ECy= EncodeColor(colorYellow);
_N(Title = ECw+ "Guppy MMA " +ECy+ StrFormat("- {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
ChikouPeriod = Param("Chikou Span Period",25,1,30,1);
Chikou=Ref(C,(-1 * ChikouPeriod));
Plot( C, "", IIf(Close > Open, colorBrightGreen, colorRed), styleCandle); 
 Plot(Chikou,"Chikou Snan",colorViolet,styleLine);
_SECTION_END();


_SECTION_BEGIN("Fast Periods");
P1 = EMA(C,3);
P2 = EMA(C,5);
P3 = EMA(C,8);
P4 = EMA(C,10);
P5 = EMA(C,12);
P6 = EMA(C,15);
_SECTION_END();

_SECTION_BEGIN("Slow Periods");
P7 = EMA(C,30);
P8 = EMA(C,35);
P9 = EMA(C,40);
P10 = EMA(C,45);
P11 = EMA(C,50);
P12 = EMA(C,55);
P13 = EMA(C,60);
_SECTION_END();

_SECTION_BEGIN("Guppy MMA");
Plot(P7,"",colorOrange,styleDashed = 32);
Plot(P8,"",colorOrange,styleDashed = 32);
Plot(P9,"",colorOrange,styleDashed = 32);
Plot(P10,"",colorOrange,styleDashed = 32);
Plot(P11,"",colorOrange,styleDashed = 32);
Plot(P12,"",colorOrange,styleDashed = 32);
Plot(P13,"",colorOrange,styleDashed = 32);

Plot(P1,"",colorLightGrey,styleDashed = 32);
Plot(P2,"",colorLightGrey,styleDashed = 32);
Plot(P3,"",colorLightGrey,styleDashed = 32);
Plot(P4,"",colorLightGrey,styleDashed = 32);
Plot(P5,"",colorLightGrey,styleDashed = 32);
Plot(P6,"",colorLightGrey,styleDashed = 32);
_SECTION_END();


_SECTION_BEGIN("Signals");
crsi = ConnorsRSI(3,2,100);
Buy = p1 > p13 AND p13 > Ref(p13,-14) AND crsi < 20 AND C > Chikou;
Short = p1 < p13 AND p13 < Ref(p13,-14) AND crsi > 80 AND C < Chikou;
_SECTION_END();


_SECTION_BEGIN( "Exit Section" );

firstProfitTarget = Optimize("First Profit Target",2,0,10,1);

secondProfitTarget = Optimize("Second Profit Target",5,0,20,1);

MaxStop = Optimize("Max Stop",4,0,20,1);

Sell = 0;
Cover = 0;

stopLineColor = Null;


Multiple = Param( "Multiple", 3 ,1, 8, 1 ); // How many ATR’s to be allowed below the highest high since latest "buy" bar
ATRPeriods = Param( "ATR Periods", 11, 1, 50, 5 ); // How many periods to use for the ATR

Multiple1 = Param( "Multiple1", 1.1, 1, 6, 1 ); // How many ATR’s to be allowed below the highest high since latest "buy" bar
ATRPeriods1 = Param( "ATR Periods1", 11, 1, 50, 5 );

Multiple2 = Param( "Multiple2", 1, 0, 4, 1 ); // How many ATR’s to be allowed below the highest high since latest "buy" bar
ATRPeriods2 = Param( "ATR Periods2", 11, 1, 50, 5 );
Showtarget = ParamList ("Show Target Milestones ","Yes|No");



target2 = secondProfitTarget ;

target1 = firstProfitTarget ;

stopArray = Null;
stopArray1 = Null;
stopArray2 = Null;
atrArray = ATR( ATRPeriods );
atrArray1 = ATR( ATRPeriods1 );
atrArray2 = ATR( ATRPeriods2 );
HHArray = Null;
LLArray = Null;
exitArray = Null;
trendDirection = 0;
ShortPr = 0;

BuyPriceArr = 0;
ShortPriceArr = 0;


BuyPr = 0;
CancelBuy = False;

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

 ShortPriceArr[i] = ShortPr;
BuyPriceArr[i] = BuyPr;

    if ( Short[i] AND ShortPr ==0 )
    {
        // we just triggered a short trade. Set up starting values
        stopArray[i] = Low[i] + ( Multiple * atrArray[i] );
        LLArray[i] = C[i]; // initialize the lowest low array
        trendDirection = 0 - 1; // going short. Base bar.'	
		 ShortPr = C[i];
		
    } else if(Short[i]) {
			Short[i] = 0;
		}

 if ( Buy[i] AND Buypr ==0)
    {
        // we just triggered a long trade. Set up starting values
        stopArray[i] = High[i] - ( Multiple * atrArray[i] );

        HHArray[i] = C[i]; // initialize the highest high array
        trendDirection = 1; // going long. Base bar flag is now set.
			Buypr = C[i];
    } else if(Buy[i]) {
		Buy[i] =0;
	}


    exitArray[i] = 0;

    if ( trendDirection > 0 )
    {
        // keep track of the highest high, highest close, highest low, etc..
        if ( trendDirection > 1 )
        {
            // We are in the trade (2nd day or later)
            if ( (C[i] <= stopArray[i-1] AND (C[i] >=  BuyPr)) OR C[i] <= BuyPr * (1-MaxStop/100))
            {
                //stop got hit. Reset the trade.
                trendDirection = 0; // OK. wait until we trigger another trade.
					Buypr = 0; 
					FIRstTargetHit = 0;
                exitArray[i] = 1;
					Sell[i] = 1;
            }
            else
            {
                // keep track of the HHV since trade was entered.
                if ( C[i] > HHArray[i-1] )
                    HHArray[i] = C[i];
                else
                    HHArray[i] = HHArray[i-1];

                // Compute the stop based on the HHV.
			if(HHArray[i] >=  (target2/100 + 1) * BuyPr) {
 				stopArray[i] = HHArray[i] - ( Multiple2 * atrArray2[i] );
				stopLineColor[i]=colorBlue;
			} else if(HHArray[i] >=  (target1/100 + 1) * BuyPr) {
			 	stopArray[i] = HHArray[i] - ( Multiple1 * atrArray1[i] );
				stopLineColor[i]=colorYellow;
				FIRstTargetHit = 1;
			} else {
				stopArray[i] = HHArray[i] - ( Multiple * atrArray[i] );
				stopLineColor[i] = colorRed;
			}
         }
        }

        trendDirection = trendDirection + 1;
    }

    if ( trendDirection < 0 )
    {
        // keep track of the lowest low, lowest close, lowest high, etc..
        if ( trendDirection < 0 - 1 )
        {
            // We are in the trade (2nd day or later)
            if ( (C[i] > stopArray[i-1] AND (C[i] <=  ShortPr  )) OR C[i] >= ShortPr * (1+MaxStop/100))
            {
                // our stop got hit. Reset the trade.
                trendDirection = 0;
					Shortpr = 0; 
					FIRstTargetHit = 0;
                exitArray[i] = 0 - 1;
					Cover[i] = 1;
            }
            else
            {
                // keep track of the LLV since trade was entered.
                if ( C[i] < LLArray[i-1] )
                    LLArray[i] = C[i];
                else
                    LLArray[i] = LLArray[i-1];

// Compute the stop based on the LLV.
             
		if(LLArray[i] <=  (1 - target2/100) * ShortPr) {
 			stopArray[i] = LLArray[i] + ( Multiple2 * atrArray2[i] );
			stopLineColor[i]=colorBlue;
			FIRstTargetHit = 1;

		} else if(LLArray[i] <=  (1 - target1/100) * ShortPr) {
 			stopArray[i] = LLArray[i] + ( Multiple1 * atrArray1[i] );
			FIRstTargetHit = 1; 
			stopLineColor[i]=colorYellow;

		} else {
			stopArray[i] = LLArray[i] + ( Multiple * atrArray[i] );
			stopLineColor[i] = colorRed;
		}

            }
        }

        trendDirection = trendDirection - 1;
    }

    if ( trendDirection == 0 )
    {
CancelBuy = False;
FIRstTargetHit = 0;
        stopArray[i] = 0;
        LLArray[i] = 0;
        HHArray[i] = 0;
Buypr=0;
ShortPr = 0;
    }


}



printf("------values-----");
printf("\nBuy Price = " + BuyPriceArr);
printf("\nShort Price = " + ShortPriceArr);
PlotShapes( abs( exitArray )*shapeHollowCircle, colorYellow, 0, ValueWhen( stopArray, stopArray, 1 ), 0 );
Plot( stopArray, "StopLine", stopLineColor, styleNoRescale + styleThick);

r = Hour();
e = Minute();
timestr = NumToStr( r, 1.0, False ) + ":" + NumToStr(e,1.0,False);

bp = ValueWhen(Buy,C,1);
sp = ValueWhen(Sell,C,1);
bh = ValueWhen(Buy,r,1);
bm = ValueWhen(Buy,e,1);
sh = ValueWhen(Sell,r,1);
sm = ValueWhen(Sell,e,1);

ShortPr = ValueWhen(Short,C,1);
CoverPr = ValueWhen(Cover,C,1);
ShortH =  ValueWhen(Short,r,1);
ShortM = ValueWhen(Short,e,1);
CoverH = ValueWhen(Cover,r,1);
CoverM = ValueWhen(Cover,e,1);




Hd = sh - bh;
md = (hd*60) + (sm - bm);

shd = Shorth - Coverh;
smd = (shd*60) + (shortm - Coverm);


timestr = NumToStr( r, 1.0, False ) + ":" + NumToStr(e,1.0,False);


pf = sp - bp;
spf = ShortPr - CoverPr;

buyMsg = "BUY " + Name() + " on " + Date()  + " for price " + NumToStr(bp, 1.2 );
SellMsg = "SELL " + Name() + " on " + Date()  + " for price " + NumToStr(sp, 1.2 )+ ". Profit in last trade is " + NumToStr(pf,1.2) + ". Total trade time was " + md + " minutes";
ShortMsg = "Short " + Name() + " on " + Date()  + " for price " + NumToStr(ShortPr, 1.2 );
CoverMsg = "Cover " + Name() + " on " + Date()  + " for price " + NumToStr(CoverPr, 1.2 )+ ". Profit in last trade is " + NumToStr(spf,1.2) + ". Total trade time was " + smd + " minutes";
sendNotf = ParamToggle("Send Notifications","NO|YES",0);


for (i=1;i<BarCount;i++)
{
if(Sell[i]) {
PlotText(WriteVal(pf[i]),i,H[i]+2*ATRarray[i],colorYellow);
}	
else if(Cover[i]) {
PlotText(WriteVal(spf[i]),i,L[i]+2*ATRarray[i],colorYellow);
}
}

PlotShapes( Buy*shapeUpArrow, colorBrightGreen, 0, Low );
PlotShapes( Sell*shapeDownArrow, colorRed, 0, High );


PlotShapes( Short*shapeDownTriangle, colorBrightGreen, 0, High );
PlotShapes( Cover*shapeUpTriangle, colorRed, 0, Low );

/*

if(sendNotf) {
AlertIf( Buy, "EXEC C:\\Misc\\pushbullet\\pyPushBullet-master\\pyPushBullet-master\\push.bat", BuyMsg, 1,1+2+4+8);
AlertIf( Sell, "EXEC C:\\Misc\\pushbullet\\pyPushBullet-master\\pyPushBullet-master\\push.bat", SellMsg, 2,1+2+4+8 );
AlertIf( Short, "EXEC C:\\Misc\\pushbullet\\pyPushBullet-master\\pyPushBullet-master\\push.bat", ShortMsg, 3,1+2+4+8);
AlertIf( Cover, "EXEC C:\\Misc\\pushbullet\\pyPushBullet-master\\pyPushBullet-master\\push.bat", CoverMsg, 4,1+2+4+8 );
}

*/

_SECTION_END();