Measure MA to Price dynamic distance

Hi,

Please help me in getting the dynamic distance between the MA and current price printed next to the MA value.

Regards

Let's say:

bi = BarIndex();
sma = MA( C, 20 );
dist = C - sma;

One approach:

PlotText(
	text = StrFormat( "%1.2f", SelectedValue( dist ) ),
	x = SelectedValue( bi ),
	y = SelectedValue( sma ),
	textColor = ParamColor( "Set text color", colorWhite ),
	bkColor = colorDefault,
	yoffset = 0
);

Another approach with control over fontName and pointSize:

PlotTextSetFont(
	text = StrFormat( "%1.2f", SelectedValue( dist ) ),
	fontName = "Courier New",
	pointSize = 9,
	x = SelectedValue( bi ),
	y = SelectedValue( sma ),
	textColor = ParamColor( "Set text color", colorWhite ),
	bkColor = colorDefault,
	yoffset = 0
);

For more nuances study the function references:

Same can be achieved using GFX functions as well (caveat - performance dip & more code):

1 Like

Thanks a lot,Cougar !

Don’t use those assignments inside function call. This is bad coding. AFL is not Python. Those assignments can mislead casual reader into thinking these are Python like named arguments. They are not. AFL is closer to C language. The order of arguments in AFL is like in C language, defined in prototype and fixed. Those unneeded assignments only cause confusion and cost extra cycles for absolutely nothing.

Corrected code should look like this:

PlotText(
	StrFormat( "%1.2f", SelectedValue( dist ) ),
	SelectedValue( bi ),
	SelectedValue( sma ),
	ParamColor( "Set text color", colorWhite ),
	colorDefault,
	0
);

If you need to comment, use comment, not assignments like this:

PlotText(
	StrFormat( "%1.2f", SelectedValue( dist ) ), // text
	SelectedValue( bi ), // x
	SelectedValue( sma ), // y
	ParamColor( "Set text color", colorWhite ), // txtcolor
	colorDefault, // bkcolor
	0 // offset
);
2 Likes

True, unnecessary assignments are meaningless. You are correct - owing to recent projects - I've been thinking Pythonic for past few months.

If one is required to send kwargs vis-à-vis a Method (using any language) then such assignments becomes useful. Other similar usage such as in Method Overloading enhances code reusability plus readability.

Probably for good reason(s) Method Overloading is not supported in AFL. But kwargs can be achieved in AFL, and, I need to use such technique - projects demands such expression.

Bad coding or not, remains debatable!

Vouching for robustness and flexibility while expressing, especially, for the sake of reusability.

You are not getting the point. Casual reader probably more likely to have exposure to Python will automatically make wrong assumption that named args are supported. They are not and they won’t be because AFL is designed to be closer to C and compilable. Python is strictly interpreted language and slow because even numerical literals in Python are objects.

AFL design from the very beginning in 1995 assumed it will use C calling convention and C plug-in API. AmiBroker requires function arguments to be specified in one and only one documented order. Using C calling convention makes function calls easily translated to compiled CPU machine code.

2 Likes