# AFL for multiple Bollinger Bands

A follower asked about plotting multiple sets of Bollinger Bands in AFL. I had a little spare time this morning, so here you go:

``````// AFL to plot three sets of Bollinger Bands®
// By John Bollinger
// https://www.BollingerBands.com
// Copyright Bolinger Capital Management 2018
//
// Data to use, deafault to close
P = ParamField("Price field", -1 );
// User BB period selection
Periods1 = Param("Periods 1", 10, 2, 100, 1 );
Periods2 = Param("Periods 2", 20, 2, 100, 1 );
Periods3 = Param("Periods 3", 50, 2, 100, 1 );
// User BB width selection
Width1 = Param("Width 1", 2, 0, 10, 0.05 );
Width2 = Param("Width 2", 2, 0, 10, 0.05 );
Width3 = Param("Width 3", 2, 0, 10, 0.05 );
// User BB color selection
ColorUpper = ParamColor("Color 1", colorRed );
ColorMiddle = ParamColor("Color 2", colorBlue );
ColorLower = ParamColor("Color 3", colorGreen );
// User BB line selection
Style1 = ParamStyle("Style 1", styleDots );
Style2 = ParamStyle("Style 2", styleLine );
Style3 = ParamStyle("Style 3", styleDashed );
// Plot the first set of bands
Plot( BBandTop( P, Periods1, Width1 ), "upperBB1", ColorUpper, Style1 );
Plot( MA(P, Periods1), "middleBB1", ColorMiddle, Style1 );
Plot( BBandBot( P, Periods1, Width1 ), "lowerBB1", ColorLower, Style1 );
// Plot the second set of bands
Plot( BBandTop( P, Periods2, Width2 ), "upperBB2", ColorUpper, Style2 );
Plot( MA(P, Periods2), "middleBB2", ColorMiddle, Style2 );
Plot( BBandBot( P, Periods2, Width2 ), "lowerBB2", ColorLower, Style2 );
// Plot the third set of bands
Plot( BBandTop( P, Periods3, Width3 ), "upperBB3", ColorUpper, Style3 );
Plot( MA(P, Periods3), "middleBB3", ColorMiddle, Style3 );
Plot( BBandBot( P, Periods3, Width3 ), "lowerBB3", ColorLower, Style3 );
// That's all folks!
``````

Enjoy,

``````John
``````

11 Likes

Thank you John for your contribution

2 Likes

And here is way more simplified version doing same thing.

``````/// AFL to plot three sets of Bollinger Bands®
/// By fxshrat@gmail.com

SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

P = ParamField("Price field", -1 );

// default periods and styles
per1 = 10;	st1 = styleDots;
per2 = 20;	st2 = styleLine;
per3 = 50;	st3 = styleDashed;

for ( i = 1; i <= 3; i++ ) {
// Set Param BB periods
VarSet( "Periods" + i, Param("Periods " + i, VarGet("per" + i), 2, 100, 1 ));
// Set Param BB width
VarSet( "Width" + i, Param("Width " + i, 2, 0, 10, 0.05 ) );
// Set Param BB line style
VarSet( "Style" + i, ParamStyle("Style " + i, VarGet("st" + i) ));
}

for ( i = 1; i <= 3; i++ ) {
// Plot the set of bands
vg_period = VarGet("Periods" + i);
vg_style = VarGet("Style" + i);
vg_width = VarGet("Width" + i);
Plot( BBandTop( P, vg_period, vg_width), "upperBB" +i, ParamColor("Color 1", colorRed ), vg_style );
Plot( MA(P, vg_period), "middleBB" + i, ParamColor("Color 2", colorBlue ), vg_style );
Plot( BBandBot( P, vg_period, vg_width ), "lowerBB" +i, ParamColor("Color 3", colorGreen ), vg_style );
}
``````
7 Likes

It is a privilege for us - AmiBroker users, that such Hall of Famer - Mr. John Bollinger has joined our forum today. Thank you for creating Bollinger Bands

4 Likes

Oh, I didn't know about VarSet(). Nice. Thanks.

`` John``
1 Like

It is entirely my pleasure to have a seat at the table.

Thank you,

``  John``
5 Likes

I watched you every trading day in the 80's on FNN.
-S

While we are at making code shorter/simpler, my 2 cents' worth - we can get away without VarGet/VarSet as Params can be placed inside just one loop.

``````// AFL to plot three sets of Bollinger Bands®
// By John Bollinger
// https://www.BollingerBands.com
// Copyright Bolinger Capital Management 2018
//
// Data to use, default to close
//
// Simplified by TJ
Version( 6.10 );

P = ParamField("Price field", -1 );

def_styles = MxFromString("{{8,1,32}}"); // dots, line, dashes

for ( i = 1; i <= 3; i++ )
{
// Plot the set of bands
period = Param("Periods " + i, 10 * i, 2, 100, 1 );
width = Param("Width " + i, 2, 0, 10, 0.05 );
style = ParamStyle("Style " + i, def_styles[ 0 ][ i - 1 ] );
Plot( BBandTop( P, period, width), "upperBB" +i, ParamColor("Color Up", colorRed ), style );
Plot( MA(P, period), "middleBB" + i, ParamColor("Color Mid", colorBlue ), style );
Plot( BBandBot( P, period, width ), "lowerBB" +i, ParamColor("Color Lo", colorGreen ), style );
}
``````
15 Likes

While we are at VarSet/VarGet,
The reason for why those have been used instead is simple...
Being able to still pick a single variable (previously set in loop) outside of loop.

Example: Including BB band variable to Buy/Sell rules and plotting Shapes.

Originally I had set periods + width via VarSet. But it is rather over complication so I thought it is better to just set Bands and MA all at once.

The idea of using MxFromString for default periods actually is a good one and have added it to code below with reference to original idea provider.

``````/// AFL to plot three BB Bands + (basic example) Signals + Shapes
/// By fxshrat@gmail.com
/// Suggestion of using Matrix for default periods by T. Janeczko
/// https://forum.amibroker.com/t/afl-for-multiple-bollinger-bands/8225/8
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );
Plot( C, "Price", colorDefault, styleBar );

// price field
P = ParamField("Price field", -1 );
// default periods and styles (suggestion of using MxFromString by AB developer)
default_per = MxFromString( "[10;20;50]" );// default periods
default_style = MxFromString( "[8;1;32]" );// dots, line, dashes

for ( i = 1; i <= 3; i++ ) {
// Set Param BB periods & width
per = Param("Periods " + i, default_per[i-1][0], 2, 100, 1 );
width = Param("Width " + i, 2, 0, 10, 0.05 );
//Set Bands
VarSet( "BBandTop" + i, BBandTop( P, per, width));
VarSet( "MA" + i, MA( P, per));
VarSet( "BBandBot" + i, BBandBot( P, per, width));
// Set Param BB line style
VarSet( "Style" + i, ParamStyle("Style " + i, default_style[i-1][0] ));
}

for ( i = 1; i <= 3; i++ ) {
// Plot the set of bands
vg_style = VarGet("Style" + i);
Plot( VarGet( "BBandTop" + i), "upperBB" +i, ParamColor("Color BBandTop", colorRed ), vg_style );
Plot( VarGet( "MA" + i), "middleBB" + i, ParamColor("Color MA", colorBlue ), vg_style );
Plot( VarGet( "BBandBot" + i), "lowerBB" +i, ParamColor("Color BBandBot", colorGreen ), vg_style );
}

// Some basic signal example + plot of shapes
Buy = Cross(C, BBandTop1) AND BBandTop1 > Ref(HHV(BBandTop1, 20),-1);
Sell = Cross(C, BBandTop3) AND BBandTop3 > Ref(HHV(BBandTop3, 20),-1);

Sell = ExRem( Sell, Buy );

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow,
IIf( Buy, colorGreen, colorOrange ), layer = 0,
y = IIf( Buy, L, H ), -12 );
``````

7 Likes

That was a great time. I miss those days a lot!

Best,

`` John``

Hi is it possible to put foreign "Ticker" BB band under the other ticker
can you tell me the code for that afl

@Sam1, as you appear to be "new" to the forum, you should spend some time searching.

As you search, you will find that the posts that get the most/best help are ones that show the original poster's work, and where they are having difficulty.

If you are also very new to AmiBroker/AFL, then you will have to spend lots of time learning.

But in Answer to your question... Is if possible - YES. Can I tell you the code - YES. Am I going to tell/give you the code - NO.

While you might find that rude or disrespectful, I seriously doubt that you will only use AB for this one idea. Because of that, I am going to make you put an effort in to work with and learn some AFL.

As I am sure you will have some issues/difficulties, here is a great link - for debugging your code: How do I debug my formula?

Good luck in your new journey. Come back and post your work/code and then you will tend to receive much more pointed help on your issues.

4 Likes