How to put customized Bollinger Bands onto a chart pane (AFL error code)

Hello,

See the error message below. The AFL code beIow for customized Bollinger Bands operates well on my first price chart sheet tab, but doesn't work when trying to place it on an additional price sheet with a second tab name. Instead an error message rather than price chart fills the second tab's price pane when directly copy/pasting the AFL code into the second tab price sheet by right clicking on the second tab's price pane/left clicking Edit Formula/then copy/paste the formula directly from the first tab sheet.

Here's the error message:
File: 'Formulas\Drag-drop_Price.afl, Ln: 13, Col: 37
Error 5.
'2nd' argument of the 'Plot()' call has incorrect type.
The function expected 'a STRING' here but found 'a NUMBER'.

The error message also includes as part of the description:
Price:

Periods = Param("Periods", 0, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width )

..................................................................................

The last line in the error message is shorter than the code's actual line:

Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style ); 

Here's the entire AFL code:

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Bollinger Bands");
P = ParamField("Price field",-1);
Periods = Param("Periods", 0, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style ); 
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style ); 
_SECTION_END();

_SECTION_BEGIN("MA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

_SECTION_BEGIN("Bollinger Bands1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 0, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style ); 
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style ); 
_SECTION_END();

@joe60609 this error is due to the default value you assigned to Periods = Param("Periods", 0, 2, 300, 1 );

If you create a variable like:

bbt = BBandTop( P, Periods, Width );

you'll get a different (more explicative) error message, that is a string. Is this error message that you are passing to the Plot function in your code (generating the message you reported).

image

I admit that the error message you got is not very helpful in this case, but it is correct!

To fix the formula change the default periods to a valid range (greater than zero) and reapply it.

1 Like

Thanks for reply beppe. A few more questions for everyone.

As beppe suggested I changed the '0' in the error line below to a higher number ('8') everywhere within the code where the same line appeared:
Originally error line Periods = Param("Periods", 0, 2, 300, 1 );
changed to Periods = Param("Periods", 8, 2, 300, 1 );

but the drag/drop of Price and then Bollinger Bands onto a blank chart pane is still not showing a price chart at all or the bands as an indicator, instead still showing the same error message on an empty pane.

Did I change the proper place(s) in the code? If not, where?

(b) The error coding ironically still operates well for an original much earlier separate pane template used the past few years that has a drag/drop indicator onto it of the 20 period Bollinger Bands, meaning the code seems to be '0" periods but the 20 period Bollinger Bands are being graphed. I wonder why the code works in the original pane's template but not at all, even when corrected, in another template tab side by side within the same computer's Amibroker?

(c) Somehow I was able to get the code to operate on my home laptop soon after beppe's original reply last month and make it a pane on a new template page when using the home laptop, but for some reason immediately after copying 'Amibroker from home laptop onto thumb drive for transfer to travel laptop' my home laptop returned to giving a blank pane and error message, and travel laptop does as well. (Travel laptop had a fresh copy of Amibroker downloaded onto it from web link then the thumb drive's Amibroker copied into it)

I very much want to have multiple chart templates and each having price and various Bollinger Bands but currently dragging/dropping Price and also Bollinger Bands are both getting the error message.

How to solve, as I somehow did soon after beppe's reply last month ?

Thanks

Really you don't need all of that. Built-in "Price (All in one)" has everything covered. In "Charts" window double click on "Price (all in one)" . It includes Bollinger Bands.

image

Once you have it, you can customize to your taste.

See the video: about price chart customization

Recommended reading is ENTIRE tutorial http://www.amibroker.com/tutorial.html

As for charts, THIS is essential How to use 'drag and drop' charting interface

Also I have idea why @joe60609 modiifed the orignal code because original code has this:

Periods = Param("Periods", 15, 2, 300, 1 );

Default period is 15, not zero. End-user had to manually change the formula to get the error. General rule is: when you don't know what you are doing, don't do that.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.