Function pointers in afl possible?

Hi,

I was wondering if AFL supports a concept similar to function pointer is c. This is what I'm trying to achieve

Current Implementation

ma_type = paramlist("MA Type", "SMA|EMA|WMA", 0); <- Select the type of moving average you want to plot

ma_01_p = param("MA-01 Period", 5, 5, 200, 1); <- Moving average period
ma_01_c = paramcolor("MA-01 Color", colordarkgrey); <- color to plot moving average 
ma_01_s = paramstyle("MA-01 Style", styleline); <- Style used to plot moving average

switch (ma_type) {
	case "SMA":
		ma_title = "MA";
		ma_01_a = ma(field, ma_01_p);
		break;
	case "EMA":
		ma_title = "EMA";
		ma_01_a = ema(field, ma_01_p);
		break;		
	case "WMA":
		ma_title = "WMA";
		ma_01_a = wma(field, ma_01_p);
		break;
}

plot(ma_01_a, ma_title + "(" + ma_01_p + ")", ma_01_c, ma_01_s, null, null, 0, 0, 1);

Is the below implementation where the pointer to the ma function is stored in ma_ptr possible ??

ma_type = paramlist("MA Type", "SMA|EMA|WMA", 0); <- Select the type of moving average you want to plot

ma_01_p = param("MA-01 Period", 5, 5, 200, 1); <- Moving average period
ma_01_c = paramcolor("MA-01 Color", colordarkgrey); <- color to plot moving average 
ma_01_s = paramstyle("MA-01 Style", styleline); <- Style used to plot moving average

switch (ma_type) {
	case "SMA":
		ma_title = "MA";
		ma_ptr = ma;
		break;
	case "EMA":
		ma_title = "EMA";
		ma_ptr = ema;
		break;		
	case "WMA":
		ma_title = "WMA";
		ma_ptr = wma;
		break;
}

ma_01_a = ma_ptr(c, ma_01_p);
plot(ma_01_a, ma_title + "(" + ma_01_p + ")", ma_01_c, ma_01_s, null, null, 0, 0, 1);

Thanks.

@insaneike, AFAIK, at present AFL does no support this kind of functionality.

Just for curiosity, what kind of scenario do you see that would significantly benefit from such a feature in AmiBroker?

There aren't no pointers in AFL.
If you want to switch MA types and want to change array and period then simply create a function.

function MultiMA( ma_type, arr, per ) {
	switch (StrToLower(ma_type)) {	
		case "ema":
			ma_ptr = EMA(arr, per);
			break;		
		case "wma":
			ma_ptr = WMA(arr, per);
			break;
		case "sma":
		default:
			ma_ptr = MA(arr, per);
			break;
		default:
			Error("MultiMA() function: Supported MA-Types: EMA, SMA or WMA!");
			break;
	}
	return ma_ptr;
}

ma_type = paramlist("MA Type", "SMA|EMA|WMA", 0); //<- Select the type of moving average you want to plot

ma_01_p = param("MA-01 Period", 5, 5, 200, 1); //<- Moving average period
ma_01_c = paramcolor("MA-01 Color", colordarkgrey); //<- color to plot moving average 
ma_01_s = paramstyle("MA-01 Style", styleline); //<- Style used to plot moving average

ma_01_a = MultiMA( ma_type, C, ma_01_p ); 
plot(ma_01_a, ma_type + "(" + ma_01_p + ")", ma_01_c, ma_01_s, null, null, 0, 0, 1);
Plot( C, "Price", colorDefault, styleCandle );

Alternativally you might do like so (but I don't like it)

function MultiMA( arr, per ) {
	// requires variable named ma_type (type "string" ) in your AFL which defines MA type
	getvar = VarGetText("ma_type");
	
	if ( typeof(getvar) != "string" )	
		Error("MultiMA() function: Variable 'ma_type' must be type string");
	else {	
		switch (StrToLower(getvar)) {	
			case "ema":
				ma_ptr = EMA(arr, per);
				break;		
			case "wma":
				ma_ptr = WMA(arr, per);
				break;
			case "sma":					
				ma_ptr = MA(arr, per);
				break;
			default:
				Error("MultiMA() function: Supported MA-Types of variable 'ma_type': EMA, SMA or WMA!");
				break;
		}
	}
	return ma_ptr;
}

ma_type = paramlist("MA Type", "SMA|EMA|WMA", 0); //<- Select the type of moving average you want to plot

ma_01_p = param("MA-01 Period", 5, 5, 200, 1); //<- Moving average period
ma_01_c = paramcolor("MA-01 Color", colordarkgrey); //<- color to plot moving average 
ma_01_s = paramstyle("MA-01 Style", styleline); //<- Style used to plot moving average

ma_01_a = MultiMA( C, ma_01_p ); 
plot(ma_01_a, ma_type + "(" + ma_01_p + ")", ma_01_c, ma_01_s, null, null, 0, 0, 1);
Plot( C, "Price", colorDefault, styleCandle );
3 Likes

Hi @beppe,

There won't be any use in this case.

But when creating a system I would like to have one functionality at one place so that in case of modification a change at one place would put the change in effect. With switch case one has to modify at every place under the case statement.

I was trying to explore this purely from a programatical angle.

Thanks.

Thanks @fxshrat for your reply. Basically came to know that it isn't supported :(.