// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Tomorrow Pivot");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}}  (%.1f%%) Vol " +WriteVal( V, 1.0 ) +"
{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));


H1=SelectedValue( TimeFrameGetPrice( "H", inDaily, -1 ));
L1=SelectedValue(TimeFrameGetPrice( "L", inDaily, -1 ));
C1=SelectedValue(TimeFrameGetPrice( "C", inDaily, -1 ));

/*PIVOT Calculation*/
p = ( H1+ L1 + C1 )/3;
s1 = (2*p)-H1;
r1 = (2*p)-L1;
s2 = p -(H1 - L1);
s3 = S1 - (H1-L1);
r2 = p +(H1 - L1);
r3 = R1 +(H1-L1);

Plot (p,"Pivot",colorBlue,1);
Plot (r1,"R1",colorPlum,1);
Plot (r2,"R2",colorPlum,1);
Plot (r3,"R3",colorPlum,1);
Plot (s1,"S1",colorCustom14,1);
Plot (s2,"S2",colorCustom14,1);
Plot (s3,"S3",colorCustom14,1);
_SECTION_END();

_SECTION_BEGIN("stop trailing");
Title = Name()+ " Price Break Indicator." ;
//+ "  Trend is " + Trend_Text + ".  SEQUENCE IS " + Sequence_Text;

/////////   Parameters  ////////

nBars_To_Scan = Param("Number of bars to Scan",20, 10,480, 10);		//Number of bars to include in scan
PB_Interval = Param("Number of Bars for Price Break",3,2,3,1);

/////////  Use Array of Prices and Price Breaks to Look Back and Determine What Price Break is For Current Bar
Seedbar = nBars_To_Scan + 1;					//Need an initial value to start the PB process
if (BarCount > Seedbar) 						//Make sure there are enough bars available to evaluate
{
											
Price[0]= LastValue(Ref(C,-Seedbar));			//Seed the 0 Bar in the Array with Values
Trend[0]=1;										//1 = Long, 0=Short
Sequence[0]=0;									//Sequence counter - counts number of new price breaks
Price_Break[0] = LastValue(Ref(C,-Seedbar));
High_C1[0] = LastValue(Ref(C,-Seedbar));		//Highest Close 1 Ago
High_C2[0]= LastValue(Ref(C,-Seedbar));			//Highest Close 2 Ago
High_C3[0]= LastValue(Ref(C,-Seedbar));			//Highest Close 3 Ago
Low_C1[0] = LastValue(Ref(C,-Seedbar));			//Lowest Close 1 Ago
Low_C2[0] = LastValue(Ref(C,-Seedbar));			//Lowest Close 2 Ago
Low_C3[0] = LastValue(Ref(C,-Seedbar));			//Lowest Close 3 Ago
											
for ( i=1; i < Seedbar ; i++) 			//Cycle through prices filling in price array and caculating price breaks 
{											
	Prior = i-1;									//Index for Prior entry in array.  Set the current array values to 
	Trend[i]=Trend[Prior];						//prior values to make sure everything that isn't changed later
	Price_Break[i] = Price_Break[Prior];			//gets carried forward to the next row in the array.
			Low_C1[i] = Low_C1[Prior]; 			//Carryover current values
			Low_C2[i] = Low_C2[Prior];
			Low_C3[i] = Low_C3[Prior];  
			High_C1[i] = High_C1[Prior];			//Carryover current values
			High_C2[i] = High_C2[Prior];
			High_C3[i] = High_C3[Prior];
			Sequence[i] = Sequence[Prior];

	Price[i] = LastValue (Ref (C,-(Seedbar-i)));	//Seedbar is the bar just in front of where I start the method.  Works since i starts at 1

	if (Price[i] >Price[Prior] AND Trend[Prior] == 1 )	// If Close is Greater than the prior close And the Trend is Long
	{		if (Price[i] >High_C1[Prior])			//If the Close is greater than the last highest close
			{									//Test For Price Break.  The IIF is there to accomodate a 2 price or 3 price break option
												//based on the PB_Interval parameter
			Price_Break[i] = IIf(PB_Interval == 3,High_C3[Prior],IIf(PB_Interval == 2,High_C2[Prior],High_C3[Prior]));	
												//The 3PB method says I take the highest close 4 ago as the new price break.
			Sequence[i] = Sequence[i] + 1;		//Increment Sequence if Price Break
			High_C3[i] = High_C2[Prior];			//Stacking the higher closes like this avoids having to go back and iterate through the.
			High_C2[i] = High_C1[Prior];			//closes to find and count the higher closes.  They are just carried forward in the stack.
			High_C1[i] = Price[i];				//When a higher close occurs, it is put on the top of the stack, each close below is
			}									//pushed down in sequence, and the earliest (farthest back) close goes away.
	}		

	if (Price[i] >Price[Prior] AND Trend[Prior] == 0 )	// If Close is Greater than the prior close And the Trend is Short
	{		if (Price[i] >Price_Break[Prior])		//If Close > Price Break in trend is Short, Reverse and go Long
			{
			High_C1[i] = High_C2[i] = High_C3[i] = Price[i];				//Initialize sequence of new Highs
			Price_Break[i] = Price[i];				//Set new value for Price Break
			Trend[i] = 1;							//Set the trend Long
			Sequence = 0;
			}
	}		

	if (Price[i] <Price[Prior] AND Trend[Prior] ==0)	// If The Close is less than the prior close And the Trend is Short
	{		if (Price[i] <Low_C1[Prior])			//If the Close is less than the last lowest close
			{
			Price_Break[i] = IIf(PB_Interval == 3,Low_C3[Prior],IIf(PB_Interval == 2,Low_C2[Prior],Low_C3[Prior]));	//Test For Price Break
			Sequence[i] = Sequence[i] + 1;		//Increment Sequence if Price Break
			Low_C3[i] = Low_C2[Prior];			//Update sequence of new Lows
			Low_C2[i] = Low_C1[Prior];
			Low_C1[i] = Price [i];
			}
	}		

	if (Price[i] <Price[Prior] AND Trend[Prior] ==1)	// If The Close is less than the prior close And the Trend is Long
	{		if (Price[i] < Price_Break[Prior])				//If Close < Price Break in Long Trend, reverse and go Short
			{
			Low_C1[i] = Low_C2[i] = Low_C3[i] = Price[i];	//Initialize sequence of new Lows
			Price_Break[i] = Price[i];							//Set new value for Price Break
			Trend[i] = 0;								//Set Trend Short
			Sequence = 0;
			}
	}		

////  Plot the Price Breaks.
Bar_x1=BarCount - (nBars_To_Scan - Prior );
Bar_x2=BarCount - ( nBars_To_Scan - i);
PB_Color = IIf(Trend[i] == 1,colorBrightGreen, colorRed);
Plot(LineArray(Bar_x1,Price_Break[i],Bar_x2,Price_Break[i],extend=0),"",PB_Color,styleThick);
Sequence_Text = NumToStr(Sequence,format=1.0);
Trend_Text = WriteIf(Trend[i] > 0, "Long","Short");

}												//Close the For Loop

}												//Close the first test for sufficient bars to do the study
_SECTION_END();

_SECTION_BEGIN("Candle Identification");
Plot(C,"",1,64);
O1 = Ref(O,-1);O2 = Ref(O,-2);
H1 = Ref(H,-1);H2 = Ref(H,-2);
L1 = Ref(L,-1);L2 = Ref(L,-2);
C1 = Ref(C,-1);C2 = Ref(C,-2);
function CandlePattern(P)
   {
   global PatternName;
   if(P == 0) { PatternName = "NearDoji"; Pv = (abs(O-C)<= ((H-L)*0.1)); }
   else if(P == 1) { PatternName = "BlackCandle"; Pv = (O>C); }
   else if(P == 2) { PatternName = "LongBlackCandle"; Pv = (O>C AND
(O-C)/(.001+H-L)>.6); }
   else if(P == 3) { PatternName = "SmallBlackCandle"; Pv = ((O>C) AND
((H-L)>(3*(O-C)))); }
   else if(P == 4) { PatternName = "WhiteCandle"; Pv = (C>O); }
   else if(P == 5) { PatternName = "LongWhiteCandle"; Pv = ((C>O) AND
((C-O)/(.001+H-L)>.6)); }
   else if(P == 6) { PatternName = "SmallWhiteCandle"; Pv = ((C>O) AND
((H-L)>(3*(C-O)))); }
   else if(P == 7) { PatternName = "BlackMaubozu"; Pv = (O>C AND H==O AND
C==L); }
   else if(P == 8) { PatternName = "WhiteMaubozu"; Pv = (C>O AND H==C AND
O==L); }
   else if(P == 9) { PatternName = "BlackClosingMarubozu"; Pv = (O>C AND
C==L); }
   else if(P == 10) { PatternName = "WhiteClosingMarubozu"; Pv = (C>O AND
C==H); }
   else if(P == 11) { PatternName = "BlackOpeningMarubozu"; Pv = (O>C AND
O==H); }
   else if(P == 12) { PatternName = "WhiteOpeningMarubozu"; Pv = (C>O AND
O==L); }
   else if(P == 13) { PatternName = "HangingMan"; Pv = (((H-L)>4*(O-C)) AND
((C-L)/(.001+H-L)>= 0.75) AND ((O-L)/(.001+H-L)>= 0.75)); }
   else if(P == 14) { PatternName = "Hammer"; Pv = (((H-L)>3*(O-C)) AND
((C-L)/(.001+H-L)>0.6) AND ((O-L)/(.001+H-L)>0.6)); }
   else if(P == 15) { PatternName = "InvertedHammer"; Pv = (((H-L)>3*(O-C))
AND ((H-C)/(.001+H-L)>0.6) AND ((H-O)/(.001+H-L)>0.6)); }
   else if(P == 16) { PatternName = "ShootingStar"; Pv = (((H-L)>4*(O-C))
AND ((H-C)/(.001+H-L)>= 0.75) AND ((H-O)/(.001+H-L)>= 0.75)); }
   else if(P == 17) { PatternName = "BlackSpinningTop"; Pv = ((O>C) AND
((H-L)>(3*(O-C))) AND (((H-O)/(.001+H-L))<.4) AND
(((C-L)/(.001+H-L))<.4)); }
   else if(P == 18) { PatternName = "WhiteSpinningTop"; Pv = ((C>O) AND
((H-L)>(3*(C-O))) AND (((H-C)/(.001+H-L))<.4) AND
(((O-L)/(.001+H-L))<.4)); }
   else if(P == 19) { PatternName = "BearishAbandonedBaby"; Pv = ((C1 == O1)
AND (C2>O2) AND (O>C) AND (L1>H2) AND (L1>H)); }
   else if(P == 20) { PatternName = "BearishEveningDojiStar"; Pv = ((C2>O2)
AND ((C2-O2)/(.001+H2-L2)>.6) AND (C2<O1) AND (C1>O1) AND
((H1-L1)>(3*(C1-O1))) AND (O>C) AND (O<O1)); }
   else if(P == 21) { PatternName = "DarkCloudCover"; Pv = (C1>O1 AND
((C1+O1)/2)>C AND O>C AND O>C1 AND C>O1 AND (O-C)/(.001+(H-L)>0.6)); }
   else if(P == 22) { PatternName = "BearishEngulfing"; Pv = ((C1>O1) AND
(O>C) AND (O>= C1) AND (O1>= C) AND ((O-C)>(C1-O1))); }
   else if(P == 23) { PatternName = "ThreeOutsideDownPattern"; Pv = ((C2>O2)
AND (O1>C1) AND (O1>= C2) AND (O2>= C1) AND ((O1-C1)>(C2-O2)) AND (O>C) AND
(C<C1)); }
   else if(P == 24) { PatternName = "BullishAbandonedBaby"; Pv = ((C1 == O1)
AND (O2>C2) AND (C>O) AND (L2>H1) AND (L>H1)); }
   else if(P == 25) { PatternName = "BullishMorningDojiStar"; Pv = ((O2>C2)
AND ((O2-C2)/(.001+H2-L2)>.6) AND (C2>O1) AND (O1>C1) AND
((H1-L1)>(3*(C1-O1))) AND (C>O) AND (O>O1)); }
   else if(P == 26) { PatternName = "BullishEngulfing"; Pv = ((O1>C1) AND
(C>O) AND (C>= O1) AND (C1>= O) AND ((C-O)>(O1-C1))); }
   else if(P == 27) { PatternName = "ThreeOutsideUpPattern"; Pv = ((O2>C2)
AND (C1>O1) AND (C1>= O2) AND (C2>= O1) AND ((C1-O1)>(O2-C2)) AND (C>O) AND
(C>C1)); }
   else if(P == 28) { PatternName = "BullishHarami"; Pv = ((O1>C1) AND (C>O)
AND (C<= O1) AND (C1<= O) AND ((C-O)<(O1-C1))); }
   else if(P == 29) { PatternName = "ThreeInsideUpPattern"; Pv = ((O2>C2)
AND (C1>O1) AND (C1<= O2) AND (C2<= O1) AND ((C1-O1)<(O2-C2)) AND (C>O) AND
(C>C1) AND (O>O1)); }
   else if(P == 30) { PatternName = "PiercingLine"; Pv = ((C1<O1) AND
(((O1+C1)/2)<C) AND (O<C) AND (O<C1) AND (C<O1) AND
((C-O)/(.001+(H-L))>0.6)); }
   else if(P == 31) { PatternName = "BearishHarami"; Pv = ((C1>O1) AND (O>C)
AND (O<= C1) AND (O1<= C) AND ((O-C)<(C1-O1))); }
   else if(P == 32) { PatternName = "ThreeInsideDownPattern"; Pv = ((C2>O2)
AND (O1>C1) AND (O1<= C2) AND (O2<= C1) AND ((O1-C1)<(C2-O2)) AND (O>C) AND
(C<C1) AND (O<O1)); }
   else if(P == 33) { PatternName = "ThreeWhiteSoldiers"; Pv = (C>O*1.01)
AND (C1>O1*1.01) AND (C2>O2*1.01) AND (C>C1) AND (C1>C2) AND (O<C1) AND
(O>O1) AND (O1<C2) AND (O1>O2) AND (((H-C)/(H-L))<.2) AND
(((H1-C1)/(H1-L1))<.2) AND (((H2-C2)/(H2-L2))<.2); }
   else if(P == 34) { PatternName = "DarkCloudCover"; Pv = (C1>O1*1.01) AND
(O>C) AND (O>H1) AND (C>O1) AND (((C1+O1)/2)>C) AND (C>O1) AND
(MA(C,13)-Ref(MA(C,13),-4)>0); }
   else if(P == 35) { PatternName = "ThreeBlackCrows"; Pv = (O>C*1.01) AND
(O1>C1*1.01) AND (O2>C2*1.01) AND (C<C1) AND (C1<C2) AND (O>C1) AND (O<O1)
AND (O1>C2) AND (O1<O2) AND (((C-L)/(H-L))<.2) AND (((C1-L1)/(H1-L1))<.2)
AND (((C2-L2)/(H2-L2))<.2); }
   else if(P == 36) { PatternName = "doji"; Pv = (O == C); }
   else if(P == 37) { PatternName = "GapUp"; Pv = GapUp(); }
   else if(P == 38) { PatternName = "GapDown"; Pv = GapDown(); }
   else if(P == 39) { PatternName = "BigGapUp"; Pv = L>1.01*H1; }
   else if(P == 40) { PatternName = "BigGapDown"; Pv = H<0.99*L1; }
   else if(P == 41) { PatternName = "HugeGapUp"; Pv = L>1.02*H1; }
   else if(P == 42) { PatternName = "HugeGapDown"; Pv = H<0.98*L1; }
   else if(P == 43) { PatternName = "DoubleGapUp"; Pv = GapUp() AND
Ref(GapUp(),-1); }
   else if(P == 44) { PatternName = "DoubleGapDown"; Pv = GapDown() AND
Ref(GapDown(),-1); }
   return Pv;
   }

PatternNameList = "";
for(Cp=0; Cp<=44; Cp++)
	{
	VarSet("Pattern"+NumToStr(Cp,1.0),CandlePattern(cP));
	PatternNameList = PatternNameList +PatternName+","; 
	}

BI = BarIndex();
SelectedBar = SelectedValue(BI) -BI[0];
//Selectedbar = Status("lastvisiblebar")-1;
PStr="";
for(Cp=0; Cp<=44; Cp++)
	{
	Temp = VarGet("Pattern"+NumToStr(Cp,1.0));
	if(temp[SelectedBar]) Pstr=Pstr+"#"+NumToStr(Cp,1.0)+" -
"+StrExtract(PatternNameList,Cp)+"\n";
	}

Title = "\nCandle Demostration \n"+ Pstr;
_SECTION_END();