Trading band code for ATAA confernce

I have prepared a talk on trading bands for this year's Australian Technical Analysis Association conference. I used Python to show how to code various types of trading bands. Then I remembered how popular AB was in Australia so I decided to code it in AFL as well. However, I ran into a problem with the switch statement. Can someone tell me why the following code doesn't work?

Thanks,

 John
// Program to plot various types of trading bands in AmiBroker
// Written for a presention before the Australian Techncial Analysis Society
// Copyright John Bollinger 2023

TradingBandType = ParamList( "Type of trading bands to plot", "Ledoux|Percent|Bollinger|Keltner", defaultval = 3 );
Price = ParamField( "Price field", -1 );
Periods = Param( "Periods", 20, 2, 100, 1 );
Width = Param( "Width", 2, 0, 10, 0.05 );

Switch ( TradingBandType ) {
case "ledoux":
	upperBand  = High;
	lowerBand = Low;

case "percent":
	middleBand = MA( Price, Periods );
	upperBand  = middleBand * (1 + Width/100) * middleBand;
	lowerBand = middleBand / (1 + Width/100) * middleBand;

case "bollinger":
	middleBand = MA( Price, Periods );
	upperBand  = middleBand + Width * StDev( Price, Periods, population = True );
	lowerBand = middleBand - Width * Stdev( Price, Periods, population = True );

case "keltner":
	middleBand = MA( Price, Periods );
	upperBand  = middleBand + Width * ATR( Periods );
	lowerBand = middleBand - Width * ATR( Periods );
}

Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style", styleLine | styleNoLabel);
Plot( upperBand, "Upper Band" + _PARAM_VALUES(), Color, Style ); 
//Plot( middleBand, "MiddleBand" + _PARAM_VALUES(), Color, Style );
Plot( lowerBand, "Lower Band" + _PARAM_VALUES(), Color, Style );

// That's all folks!
1 Like
// Program to plot various types of trading bands in AmiBroker
// Written for a presention before the Australian Techncial Analysis Society
// Copyright John Bollinger 2023

TradingBandType = ParamList( "Type of trading bands to plot", "Ledoux|Percent|Bollinger|Keltner",  3 );
Price = ParamField( "Price field", -1 );
Periods = Param( "Periods", 20, 2, 100, 1 );
Width = Param( "Width", 2, 0, 10, 0.05 );

Switch(TradingBandType) 
{
case "Ledoux":
	upperBand  = High;
	lowerBand = Low;
break;
case "Percent":
	middleBand = MA( Price, Periods );
	upperBand  = middleBand * (1 + Width/100) * middleBand;
	lowerBand = middleBand / (1 + Width/100) * middleBand;
break;
case "Bollinger":
	middleBand = MA( Price, Periods );
	upperBand  = middleBand + Width * StDev( Price, Periods, population = True );
	lowerBand = middleBand - Width * Stdev( Price, Periods, population = True );
break;
case "Keltner":
	middleBand = MA( Price, Periods );
	upperBand  = middleBand + Width * ATR( Periods );
	lowerBand = middleBand - Width * ATR( Periods );
break;
}

Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style", styleLine | styleNoLabel);
Plot( upperBand, "Upper Band" + _PARAM_VALUES(), Color, Style ); 
//Plot( middleBand, "MiddleBand" + _PARAM_VALUES(), Color, Style );
Plot( lowerBand, "Lower Band" + _PARAM_VALUES(), Color, Style );

// That's all folks!
3 Likes

In AFL, like in C/C++, you need to use a break statement after each case. Otherwise the default behavior is to continue code execution.

https://www.amibroker.com/guide/keyword/switch.html

The docs say:

You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement.

Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.

See also: switch...case in C Programming

1 Like

@BBands, in addtion to the "switch" required usage of break as explained by @Tomasz, the code provided by @brajeevn includes another correction to your formula: the "labels" used to select different branches in the "switch" should be in the same letter case as written in your ParamList: so, in this case, the correct ones are "Ledoux", case "Percent" etc. as per the @brajeevn post.

2 Likes

Thanks to the three of you. I had tried the breaks, to no avail as my real problem was the label case. Just didn't occur to me as for the most part AB isn't case sensitive. In any case, I will post the full script here when it is done.

Best to all,

 John
3 Likes

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