// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("3 Price Break");
Title = Name()+ " Price Break Indicator." ;
//+ "  Trend is " + Trend_Text + ".  SEQUENCE IS " + Sequence_Text;
Plot(C,Title,colorBlack,styleCandle);

/////////   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,colorGreen, 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();


SetChartOptions(0,chartShowArrows|chartShowDates);
GraphXSpace=5;
Plot(C,"",colorBlack,styleCandle);

_SECTION_BEGIN("Magnified Market Price");
//by Vidyasagar, (E? ??? C?????? ??? ???? ?IC?? ?O??? C???EI?)// 
FS=Param("Font Size",35,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True ); 
GfxSetBkMode( colorWhite ); 
GfxSetTextColor( ParamColor("Color",colorGreen) ); 
Hor=Param("Horizontal Position",750,1,1200,1);
Ver=Param("Vertical Position",1,1,830,1); 
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 11, 700, True ); 
GfxSetBkMode( colorWhite ); 
GfxSetTextColor(ParamColor("Color",colorGreen) ); 
GfxTextOut(""+DD+" ("+xx+"%)", Hor , Ver+45 );
_SECTION_END();


_SECTION_BEGIN("Price");
//SetChartBkColor( colorBlack ); 
Col_1 = IIf(EMA(RSI(39),30) > Ref(EMA(RSI(39),30),-1),colorGreen,colorRed);
//Plot( C, "", col_1, styleBar); 
Plot(C,"",Col_1,128);

_SECTION_END();

Plot(EMA(C,10), "EMA(10)", colorRed, styleLine| styleNoLabel);
Plot(EMA(C,50), "EMA(50)", colorBlue, styleLine| styleNoLabel);
Plot(MA(C,200), "MA(200)", colorRed, styleThick| styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Phase");
RCP = C > EMA(C,50) AND EMA(C,50) < MA(C,200); // Recovery                         Blue
ACP = C > EMA(C,50) AND C > MA(C,200) AND EMA(C,50) < MA(C,200); // Accumulation   seagreen
BLP = C > EMA(C,50) AND C > MA(C,200) AND EMA(C,50) > MA(C,200); // Bullish        Limegreen
WRP = C < EMA(C,50) AND EMA(C,50) > MA(C,200); // Warning                          Pink
DSP = C < EMA(C,50) AND C < MA(C,200) AND EMA(C,50) > MA(C,200); // Distribution   Orange
BRP = C < EMA(C,50) AND C < MA(C,200) AND EMA(C,50) < MA(C,200); // Bearish        Red

//Plot( 1, "", IIf(RCP, colorBlue, IIf(ACP, colorSeaGreen, IIf(BLP, colorDarkGreen, IIf(WRP, colorOrange, IIf(DSP, colorRed, IIf(BRP, colorDarkRed, 0)))))), styleOwnScale|styleArea|styleNoLabel, -0.1, 50 );
Plot( 1, "", IIf(RCP, colorBlue, IIf(ACP, colorAqua, IIf(BLP, colorLime, IIf(WRP, colorPink, IIf(DSP, colorOrange, IIf(BRP, colorRed, 0)))))), styleOwnScale|styleArea|styleNoLabel, -0.1, 50 );

Buy_cross1050 = Cross(EMA(Close,10),EMA(Close,50));

Sell_cross1050 = Cross(EMA(Close,50),EMA(Close,10));



Buy_cross1050 = ExRem(Buy_cross1050, Sell_cross1050  );
sell_cross1050 = ExRem(sell_cross1050, buy_cross1050  );
dist = 1.5*ATR(10); 
for( i = 0; i < BarCount; i++ ) 
{ 

HH1 = HHV(H,20);
LL1 = LLV(L,20);

if( Buy_cross1050[i] ) PlotText( "A=" + HH1[ i ], i, L[ i ]-dist[i], colorBlack, colorLime); 
if( sell_cross1050[i] ) PlotText( "A=" + LL1[ i ], i, H[ i ]+dist[i], colorLightYellow, colorRed); 
}


PlotShapes(Buy_cross1050*shapeUpArrow,colorGreen, 0, Low, Offset =-12);
PlotShapes(sell_cross1050*shapeDownArrow,colorRed, 0, High, Offset =-12);

pds = 20;
DonchianUpper =HHV(Ref(H,-1),pds);
DonchianLower = LLV(Ref(L,-1),pds);


Buy_Price = C;
Sell_Price = C;

BuySL  = DonchianLower ;
SellSL = DonchianUpper;

BPdiff = HH1 - BuySL ;
SPdiff = LL1 - SellSL;

SwingBuy = (BPdiff/BuySL)*100 ;
SwingSell = (SPdiff/SellSL)*100 ;

BuyTarget = Buy_Price + BPdiff ;
SellTarget = Sell_Price + SPdiff ; 



// Exploration

Filter=Buy_cross1050 OR sell_cross1050;// OR Buy_cross10200 OR sell_cross10200;// OR Buy_cross50200 OR sell_cross50200;
mytimenum = DateTimeConvert( 1, DateTime() ); 

AddColumn( IIf(Buy_cross1050,C,IIf(sell_cross1050,-C,Null)) ,"A=Cross",1.1,colorBlack,IIf(Buy_cross1050,colorLightGrey,IIf(sell_cross1050,colorLightGrey,colorLightGrey)));
AddColumn( IIf(Buy_cross1050,HH1,IIf(sell_cross1050,-LL1,Null)) ,"Enter",1.1,colorBlack,IIf(Buy_cross1050,colorGreen,IIf(sell_cross1050,colorRed,colorLightGrey)));
AddColumn( IIf(Buy_cross1050 ,Prec(SwingBuy,1),IIf(Sell_cross1050 ,Prec(SwingSell,1),Null)) ,"%SWING",1.1,colorBlack,IIf(Buy_cross1050 ,colorLightGrey,IIf(Sell_cross1050 ,colorLightGrey,colorLightGrey)));

AddColumn( mytimenum,"TimeNum",colorBlack);
SetSortColumns(-6);
_SECTION_END();

_SECTION_BEGIN( "Fundamental Data" );

    if ( ParamToggle( "Show Columns in AA", "Off,On", 1 ) )
    {
        AddColumn( GetFnData( "EPS" ), "EPS" );
        AddColumn( GetFnData( "EPSEstCurrentYear" ), "EPS Est Current Year" );
        AddColumn( GetFnData( "EPSEstNextYear" ), "EPS Est Next Year" );
        AddColumn( GetFnData( "EPSEstNextQuarter" ), "EPS Est Next Quarter" );
        AddColumn( GetFnData( "PEGRatio" ), "PEG Ratio" );
        AddColumn( GetFnData( "SharesFloat" ), "Shares Float", 1.0 );
        AddColumn( GetFnData( "SharesOut" ), "Shares Out", 1.0 );
        AddColumn(GetFnData("DividendPayDate"),"Dividend Pay Date");
        AddColumn(GetFnData("ExDividendDate"),"Ex Dividend Date");
        AddColumn( GetFnData( "BookValuePerShare" ), "Book Value Per Share" );
        AddColumn( GetFnData( "DividendPerShare" ), "Dividend Per Share" );
        AddColumn( GetFnData( "ProfitMargin" ), "Profit Margin" );
        AddColumn( GetFnData( "OperatingMargin" ), "Operating Margin" );
        AddColumn( GetFnData( "OneYearTargetPrice" ), "One Year Target Price" );
        AddColumn( GetFnData( "ReturnOnAssets" ), "Return On Assets" );
        AddColumn( GetFnData( "ReturnOnEquity" ), "Return On Equity" );
        AddColumn( GetFnData( "QtrlyRevenueGrowth" ), "Qtrly Revenue Growth" );
        AddColumn( GetFnData( "GrossProfitPerShare" ), "Gross Profit Per Share" );
        AddColumn( GetFnData( "SalesPerShare" ), "Sales Per Share" );
        AddColumn( GetFnData( "EBITDAPerShare" ), "EBITDA Per Share" );
        AddColumn( GetFnData( "QtrlyEarningsGrowth" ), "Qtrly Earnings Growth" );
        AddColumn( GetFnData( "InsiderHoldPercent" ), "Insider Hold Percent" );
        AddColumn( GetFnData( "InstitutionHoldPercent" ), "Institution Hold Percent" );
        AddColumn( GetFnData( "SharesShort" ), "Shares Short", 1.0 );
        AddColumn( GetFnData( "SharesShortPrevMonth" ), "Shares Short Prev Month", 1.0 );
        AddColumn( GetFnData( "ForwardDividendPerShare" ), "Forward Dividend Per Share" );
        AddColumn( GetFnData( "ForwardEPS" ), "Forward EPS" );
        AddColumn( GetFnData( "OperatingCashFlow" ), "Operating Cash Flow", 1.0 );
        AddColumn( GetFnData( "LeveredFreeCashFlow" ), "Levered Free Cash Flow", 1.0 );
        AddColumn( GetFnData( "Beta" ), "Beta" );
        AddColumn( GetFnData( "LastSplitRatio" ), "Last Split Ratio" );
        AddColumn(GetFnData("LastSplitDate"),"Last Split Date");
    }

    _SECTION_END();