// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("The Squeeze");
SetChartBkColor(ParamColor("Panel Color",colorLightGrey));
SetChartBkGradientFill(ParamColor("Upper Chart",colorLightGrey),ParamColor("Lower Chart",colorLightGrey));

SetChartOptions( 0, chartShowDates|chartWrapTitle );
Price = Close;
Length = Param("ATR Length",20,2,100,1); // { Length for Average True Range (ATR)}
Lenght1 = Param("SD Length",20,2,100,1); // { Std. Deviation (SD) Calcs }
nK = Param("Channel ATRs",1.4,0.1,2,0.1); //{ Keltner Channel ATRs from Average }
nBB = Param("BB Std Devs",2,0.1,3,0.1); // { Bollinger Band Std. Devs. from Average }
AlertLine = Param("AlertLine",1,0,3,1); //{ BBS_Index level at which to issue alerts }
NormalColor = 4; //{ Normal color for BBS_Ind }
AlertlColor = 10;// Color for BBS_Ind below alert line }
LHMult = Nz(PointValue/TickSize);
//{-- Calculate BB Squeeze Indicator ----------------------}
AvgTrueRange = ATR(Length);
SDev = StDev(Price, Length);
Denom = nK*AvgTrueRange;
BBS_Ind = Nz((nBB * SDev) /Denom);
SetPlotColor = IIf( BBS_Ind < Alertline, NormalColor, AlertlColor);
BBcrossDown = Cross(AlertLine,BBS_Ind);
BBcrossUp = Cross(BBS_Ind,AlertLine);
Plot(0, "BBS_Ind", SetPlotColor, styleDots|styleThick | styleNoRescale |styleNoLabel );
//{-- Plot delta of price from Donchian mid line ----------}
value2 = LinearReg( price-((HHV(H, Lenght1)+LLV(L, Lenght1))/2+ MA(C,Lenght1))/2, Lenght1);
color = IIf( value2 > 0, IIf( value2 > Ref(value2,-1), colorBlue, 12), IIf( value2 < 0, IIf( value2 < Ref(value2,-1), ParamColor("UpColor",colorRed ), colorYellow ), colorYellow ));
myma=WMA(Value2,10);
//Plot(value2*LHMult, "NickmNxtMove", color, styleBar | styleThick|styleOwnScale );
Plot(value2,"BB Squeeze",color, styleArea | styleThick);
PlotShapes( IIf( Cross(Value2, 0), shapeHollowSmallDownTriangle+ shapePositionAbove, shapeNone ), 29,0,Offset=0 );
PlotShapes( IIf( Cross(0, Value2), shapeUpTriangle+ shapePositionAbove, shapeNone ), 32,0,Offset=0);
PlotShapes(IIf(Cross(myma,value2),shapeuparrow+ shapePositionAbove, shapeNone ), 12,0,Offset=myma);
PlotShapes(IIf(Cross(Value2 ,myma),shapedownarrow+ shapePositionAbove, shapeNone ),7 ,0,Offset=myma,-10);
//Plot(myma,"",5,styleLine|styleThick);
//{-- Issue Alert when the Squeeze is On ------------------}
"ALERT";
//Write alerts to Interpretation window
//Ticker = Name();
//WriteIf(BBcrossDown,Ticker + " " + "BB Squeeze Alert" ,WriteIf(BBcrossUp,Ticker + " " + "BB Squeeze Is Over",""));
//Write alerts to Alert Output window
//AlertIf(BBcrossDown,"","BB Squeeze Alert",0);
//AlertIf(BBcrossUp,"","BB Squeeze Is Over",0);
//Sound alerts
//AlertIf( BBcrossDown, "SOUND C:\\Windows\\Media\\RINGIN.WAV", "Audio alert", 2 );
//AlertIf( BBcrossUp, "SOUND C:\\Windows\\Media\\RINGIN.WAV", "Audio alert", 2 );
//Write text to screen
//ytext = 0.01;
/*
or(i=1;i<BarCount;i++)
{
If(BBcrossDown[i])
{

PlotText("ON",i,-ytext[i],colorWhite);
}

if(BBcrossUp[i])

{

PlotText("OFF",i,ytext[i],colorWhite);

}

}

*/

//PlotShapes(shapeUpArrow*BBcrossDown,colorWhite,0,0);
//PlotShapes(shapeDownArrow*BBcrossup,colorWhite,0,0);
_SECTION_END();

/*_SECTION_BEGIN("KP Fast 2");
//SetChartBkColor(ParamColor("Panel Color",colorLightGrey));
//SetChartBkGradientFill(ParamColor("Upper Chart",colorLightGrey),ParamColor("Lower Chart",colorLightGrey));
//tskp_fast2val1, tskp_fast2val2
dummy=E_TSKPFAST2(Open,High,Low,Close,Volume);
Plot(tskp_fast2val1,"Fast2_1",ParamColor("ColorUp",colorBlue),styleLine|styleThick); 
Plot( tskp_fast2val2,"Fast2_2",ParamColor("ColorDown",colorRed),styleLine|styleThick); 
PlotShapes(IIf(Cross(tskp_fast2val1,0),shapeDigit1+ shapePositionAbove, shapeNone ), 29,0,Offset=tskp_fast2val1,-12 ); 
PlotShapes(IIf(Cross(0,tskp_fast2val2),shapeDigit1+ shapePositionAbove, shapeNone ), 4,0,Offset=tskp_fast2val2,12 );
//Plot(0,"",ParamColor("Zero Line",colorGrey50),styleLine|styleNoLabel);
//Title =FullName();
_SECTION_END();*/


_SECTION_BEGIN("KP M Range");
mp=0.5*(High + Low);
tightstop=E_TSKPTIGHTSTOP(High,Low,Close);
stopline=E_TSKPSTOPLINE(High,Low,Close);
Color=colorWhite;
for( i = 0; i < BarCount; i++ ) 
{ 
if( mp[i] > stopline[i] && mp[i] > tightstop[i]  )
Color[i] = colorBlue;
else if(  mp[i] < stopline[i] && mp[i] < tightstop[i])
Color[i] = colorRed;
else
Color[i] = colorWhite;
} 
Plot(0,"",Color,styleOwnScale|styleArea|styleNoLabel,0, 90 );
Plot(7,"",Color,styleOwnScale|styleArea|styleNoLabel,0, 90 );
_SECTION_END();


_SECTION_BEGIN("KP TrendBias BackGround");//Plot Tone
dummy =  E_TSKPTRENDBIAS(High,Low,Close);
KPTB = tskp_sb;
KPTBSlow = tskp_tbxavg;
parmToneColorUp=ParamColor("Tone Color Up", colorLightBlue );
parmToneColorDn=ParamColor("Tone Color Dn", colorTan);
RibbonSize=Param("Ribbon Size",20,0.5,10,0.5);
Plot( RibbonSize,"",IIf(KPTB > KPTBSlow,parmToneColorUp,parmToneColorDn),styleOwnScale|styleArea|styleNoLabel,0,10);
_SECTION_END();