// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("_BollingerLine Indicator");

GraphXSpace=7;

periods = Optimize("periods ",Param("periods ",20,1,48,1),1,48,1);
width = Optimize("width ",Param("width ",2,0.1,10,0.1),2,2,0.1);

C1 = C - BBandBot( Close, periods, width) ;
C2 = BBandTop (Close,periods,width) - BBandBot( Close, periods, width);

R = (C1 / C2)*100;
RS = (AMA( C1,  0.5 ) / AMA( C2,  0.5 ))*100; //smooth

uptrend = r > Ref(r,-1);
downtrend = r < Ref(r,-1);

uptrendS = rs > Ref(rs,-1);
downtrendS = rs < Ref(rs,-1);

BS = ParamToggle("smooth","No|Yes",1);
BN = ParamToggle("normal","No|Yes",0);

if (BS) Plot( RS , " smooth ", IIf(( uptrendS ), colorGreen, colorRed ));
if (BS) Plot ( 2, "", IIf( uptrendS , colorGreen, IIf( downtrendS , colorRed, colorPink )), styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

if (BN) Plot( R , " normal ", IIf(( uptrend ), colorGreen, colorRed ));
if (BN) Plot ( 4, "", IIf( uptrend, colorGreen, IIf( downtrend, colorRed, colorPink )), styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

_SECTION_END();

_SECTION_BEGIN("Bollinger Bands");
P = rs ;//ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 100, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorLightGrey );
Style = ParamStyle("Style", styleLine | styleNoLabel ) | styleNoLabel;
bbt = BBandTop( P, Periods, Width );
bbb = BBandBot( P, Periods, Width );
Plot( bbt , "BBTop" + _PARAM_VALUES(), colorBlack, Style ); 
Plot( bbb , "BBBot" + _PARAM_VALUES(), colorBlack, Style ); 
PlotOHLC( bbt, bbt, bbb, bbb, "", ColorBlend( Color, colorWhite, 0.9 ), styleNoLabel | styleCloud | styleNoRescale, Null, Null, Null, -1 );
_SECTION_END();

bbm = (bbb + bbt) / 2; // middle bollingerbands
f= Optimize("colored bandsize factor",Param("colored bandsize factor",7,1,21,1),1,21,1);

bbmf = (bbm / 100) * f;

bwup = (bbt - bbmf);
bwlo = (bbb + bbmf);

PlotOHLC (bbb ,bbb ,bwlo ,bwlo ,"lowercloud",colorPink,styleCloud,0,0,0);
PlotOHLC (bbt ,bbt ,bwup ,bwup ,"uppercloud",colorBrightGreen,styleCloud,0,0,0);
Plot( bbm , "BBMiddle" + _PARAM_VALUES(), colorPink, styleDashed );

b1= C > O AND L>Ref(L,-1) AND (Ref(L,-1) > Ref(L,-2) OR (C>Ref(O,-1) AND O<Ref(H,-1)));
b2= rs > Ref(rs,-1) AND rs < bwup AND Ref(rs,-1) < Ref(bwup,-1);

s1= C < O AND H<Ref(H,-1) AND (Ref(H,-1)< Ref(H,-2) OR (C<Ref(O,-1) AND O>Ref(L,-1)));
s2= rs < Ref(rs,-1) AND rs > bwlo AND Ref(rs,-1) > Ref(bwlo,-1);

Buy = b1 AND b2;
Sell = s1 AND s2;

//Plot( 5, /* defines the height of the ribbon in percent of pane width */"ribbon",
//IIf( Buy , colorLightBlue, IIf( Sell , colorOrange, colorLavender )), /* choose color */
//styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );