// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Advanced Trend Lines");
function GetXSupport(Lo, Percentage, Back)
{
 return ((BarCount - 1) - LastValue(TroughBars(Lo, Percentage,Back)));
}
function GetYSupport(Lo, Percentage, Back)
{
 return (LastValue(Trough(Lo, Percentage, back)));
}

function GetXResistance(Hi, Percentage, Back)
{
 return ((BarCount - 1) -LastValue(PeakBars(Hi, Percentage, Back)));
}
function GetYResistance(Hi, Percentage, Back)
{
 return (LastValue(Peak(Hi, Percentage, Back)));
}
function TD_Supply(P)
{
	return ( P > Ref(P, 1) AND P > Ref(P, -1) AND P > Ref(C, -2));
}
function TD_Demand(P)
{
	return ( P < Ref(P, 1) AND P < Ref(P, -1) AND P < Ref(C, -2));
}
////////////////////////////////////////////////////////////////////////
//Parameters
Percentage 	= Param("Percentage", 1.5, 0.01, 100. ,0.01);
DrawAllLines 	= ParamToggle("Draw All Lines?", "No|Yes");
Lines 			= Param("Lines?", 1, 1, BarCount-2);
DrawR 			= ParamList("Resistance Points", "Off|High to High|High to Low", 1);
DrawS 			= ParamList("Support Points", "Off|Low to Low|Low to High", 1);
ShowTDP 		= ParamToggle("Show TD Pionts", "No|Yes", 1);
AllOrDownR 	= ParamToggle("Resistance Direction", "All|Down");
AllOrUpS 		= ParamToggle("Support Direction", "All|Up");
ShowSR			= ParamToggle("Show Vert S/R","No|Yes", 1);
SRPer			= Param("S/R Percentage", 3, 1);
SRBack			= Param("S/R Back", 5, 1);
str = "";
Res = Sup = 0;
Con = 1;
////////////////////////////////////////////////////////////////////////
Main = C;
Con = ConS = ConR = 1;
if(DrawS=="Low to Low")
{
	Support1 = L;
	Support2 = L;
}
else
{
	Support1 = L;
	Support2 = H;
}
if(DrawR=="High to High")
{
	Resistance1 = H;
	Resistance2 = H;
}
else
{
	Resistance1 = H;
	Resistance2 = L;
}
////////////////////////////////////////////////////////////////////////
//Plotting Area
Plot(Main, "", IIf(C>O,colorGreen, colorRed), styleBar);
if(DrawAllLines)
for(i = 2; i<=Lines+1; i++)
{
	if(DrawS!="Off")
	{
		x0 = GetXSupport(Support1, Percentage, i);
		x1 = GetXSupport(Support2, Percentage, i-1);
		y0 = GetYSupport(Support1, Percentage, i);
		y1 = GetYSupport(Support2, Percentage, i-1);
		x = LineArray(x0, y0, x1, y1, 1);
		if(AllOrUpS) ConS = StrToNum(NumToStr(y0 < y1));
		if(Con AND ConS)
			Plot(x, "", IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
	}
	if(DrawR!="Off")
	{
		x0 = GetXResistance(Resistance1, Percentage, i);
		x1 = GetXResistance(Resistance2, Percentage, i-1);
		y0 = GetYResistance(Resistance1, Percentage, i);
		y1 = GetYResistance(Resistance2, Percentage, i-1);
		x = LineArray(x0, y0, x1, y1, 1);
		if(AllOrDownR) ConR = y0 > y1;
		if(Con AND ConR)
			Plot(x, "",  IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
	}
}
else
{
	if(DrawS!="Off")
	{
		x0 = GetXSupport(Support1, Percentage, Lines+1);
		x1 = GetXSupport(Support2, Percentage, Lines);
		y0 = GetYSupport(Support1, Percentage, Lines+1);
		y1 = GetYSupport(Support2, Percentage, Lines);
		x = LineArray(x0, y0, x1, y1, 1 );
		Sup = LastValue(LinRegSlope(x, Lines+1));
		if(AllOrUpS) ConS = y0 < y1;
		if(Con AND ConS)
			Plot(x, "", IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
	}
	if(DrawR!="Off")
	{
		x0 = GetXResistance(Resistance1, Percentage, Lines+1);
		x1 = GetXResistance(Resistance2, Percentage, Lines);
		y0 = GetYResistance(Resistance1, Percentage, Lines+1);
		y1 = GetYResistance(Resistance2, Percentage, Lines);
		x = LineArray(x0, y0, x1, y1, 1 );
		Res = LastValue(LinRegSlope(x, Lines+1));
		if(AllOrDownR) ConR = y0 > y1;
		if(Con AND ConR)
			Plot(x, "",  IIf(LastValue(C) < LastValue(x), colorBlue,colorLightBlue), styleLine|styleThick);
	}
str = "\nR Slope=("+Res+"), S Slope=("+Sup+")";
}

if(ShowTDP)
{
	PlotShapes(TD_Supply(H)*shapeSmallCircle, colorRed, 0, H, H*.001);
	PlotShapes(TD_Demand(L)*shapeSmallCircle, colorGreen, 0, L, -L*.001);
}
if(ShowSR)
{
	for(i=1; i<=SRBack; i++)
	{
		x0 = GetXSupport(L, SRPer, i);
		x1 = BarCount-1;
		y0 = GetYSupport(L, SRPer, i);
		x = LineArray(x0, y0, x1, y0, 0);
		Plot(x, "", IIf(LastValue(C) > x, colorDarkGreen, colorDarkRed), styleLine|styleDashed|styleThick);
		x0 = GetXResistance(H, SRPer, i);
		y0 = GetYResistance(H, SRPer, i);
		x = LineArray(x0, y0, x1, y0, 0);
		Plot(x, "", IIf(LastValue(C) > x, colorDarkGreen, colorDarkRed), styleLine|styleDashed|styleThick);

	}
}
Title =FullName()+" ({{NAME}})\n{{DATE}}\n"+"Open: "+O+", Hi: "+H+", Lo: "+L+", Close: "+C+StrFormat(" (%.2f  %.2f\%)", C-Ref(C, -1), SelectedValue(ROC(C, 1)))+str;

_SECTION_END();

_SECTION_BEGIN("lin Reg with channel");
//  Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below 
//  Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support 
//      Designed for use with AB 4.63 beta and above, using drag and drop feature.  
//  Permits plotting a linear regression line of any price field available on the chart for a period determined by the user.  
//     2 Channels, based on a standard deviation each determined by the user, are plotted above and below the linear regression line. 
// 		A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.    


P = ParamField("Price field",-1);
Daysback = Param("Period for Liner Regression Line",21,1,240,1);
shift = Param("Look back period",0,0,240,1); 


//  =============================== Math Formula =============================================================

x = Cum(1);
lastx = LastValue( x ) - shift; 
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) ); 
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) ); 
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) ); 


// ==================Plot the Linear Regression Line ==========================================================


LRColor = ParamColor("LR Color", colorCycle ); 
LRStyle = ParamStyle("LR Style");

LRLine =  IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE ); //  styleDots ); 

// ==========================  Plot 1st SD Channel ===============================================================

SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET  
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle ); 
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle ); 
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle ); 

//  ==========================  Plot 2d SD Channel ===============================================================

SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET  
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorCycle ); 
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", SDColor2,SDStyle2 ); 
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 ); 

// ============================ End Indicator Code ==============================================================
_SECTION_END();