Is there a way to use “Macro-Like” variables that auto-expand in Amibroker?
More specifically, in the code snippet below, I would like the Price to be something like (H+L)/2 or something else chosen in the parameters. Likewise, AverageType would expand to be EMA, or Wilders. I put an ampersand where the expansion should occur. How can I do this (I’m trying to avoid the use of If and Switch statements).
Don’t use & - it operator is for variables only, not for functions and the meaning of & is “address of variable”.
Macro in “C” language (and AFL basically follows “C” syntax) are completely different thing and they don’t use any.
As @mradtke said, proper way is to define a function and if-else or switch is needed.
function MyPrice( x )
{
r = Open;
if( x == 1 ) r = HIgh;
if( x == 2 ) r = ( H+L )/2;
...
return r;
}
Or you can use ParamField that does that for you automatically.
Plot( Close, "Price", colorDefault, styleCandle );
Plot( (H+L)/2, "H+L/2", colorDefault, styleHidden );
P = ParamField("Field", 0 ); // this uses OHLC automatically plus values of any plots before
Plot( MA( P, 10 ), "Moving Average", colorRed );