Trying to translate some code for the normalized smoothed MACD
from easylanguage to AFL, I am blocked by the self referencing problem
I could not resolve even having read the articles about similar issues.
Here is the code
_SECTION_BEGIN("MACD Normalized Smoothed");
price = ParamField("Price field",3);
FastPeriod = Param("FastPeriod",12,1,50);
SlowPeriod = Param("SlowPeriod",26,1,300);
MacdSignal = Param("SlowPeriod",9,1,20);
SmoothPeriod = Param("SmoothPeriod",5,1,20);
NormPeriod = Param("NormPeriod",20,1,50);
//emaf=emas=emaf1=emas1=val=val1=nval=sig=sig1=0;
Title = "";
alphaf = 2.0/(1.0+Max(FastPeriod,1));
alphas = 2.0/(1.0+Max(SlowPeriod,1));
alphasig = 2.0/(1.0+Max(MacdSignal,1));
alphasm = 2.0/(1.0+Max(SmoothPeriod,1));
emaf = alphaf*price;
emas = alphas*price;
emaf = Ref(emaf,-1)+alphaf*(price-Ref(emaf,-1));
emas = Ref(emas,-1)+alphas*(price-Ref(emas,-1));
imacd = emaf-emas;
mmax = Max(imacd,NormPeriod);
mmin = Min(imacd,NormPeriod);
nval = iif (mmin!=mmax, 2.0*(imacd-mmin)/(mmax-mmin)-1.0 , 0);
val = alphasm*nval;
sig = alphasig*val;
val = Ref(val,-1) + alphasm*(nval-Ref(val,-1));
sig = Ref(sig,-1) + alphasig*(val-Ref(sig,-1));
neutralcolor = colorwhite;
color = iif(val>Ref(val,-1), colorgreen, iif(val<Ref(val,-1), colorred, neutralcolor));
Color = ValueWhen(Color != neutralcolor, Color);
color2 = iif(sig>val, colorgreen, iif(sig<val, colorred, neutralcolor));
Color2 = ValueWhen(Color2 != neutralcolor, Color2);
plot(val , "val", color, stylethick );
plot(sig , "sig", color2, styleDashed );
plot(1 , "H", colorred, styledashed);
plot(-1 , "L", colorGreen, styledashed);
plot(0 , "M", colorYellow, styledashed);
_SECTION_END();
Even if this code compiles correctly, it simply does not draw the lines
for "val" and "sig", because probably the self referencing calculations
leading to these values have not been correctrly initiated.
Any help would be appreciated !