函数不能在IF控制语句中被引用的原理和解决方案
简单说来就是具有统计性质的函数不可以在IF语句中后面的begin…end中不可以使用,可以在IF的判断过程中使用,但不可以在IF的执行过程中使用。
金字塔的公式系统由于支持IF语句的变量运行,所以像例如REF,MA等带有统计性质的函数无法直接使用在IF语句之中,因为带变量判断的IF语句会在某些周期无法调用这些统计函数而导致计算结果出现错误。
解决办法是将这些函数放到IF语句之外去执行。
目前有下列函数下此限制:
"RET","LOD","HOD","VALUEWHEN","MD","LAST","ANY","SETVAL","FILTERX","BARSCOUNT","BARSLAST","BARSSINCE","COUNT","HHV","HHVBARS","LLV","LLVBARS",
"MA","DMA","EMA","FILTER","REF","WMA","TMA","SMA","SUM","SUMBARS","CROSS","LONGCROSS","AVEDEV","DEVSQ","FORCAST","SLOPE","STD","STDP","VAR","VARP","SAR","BETA","COVAR","ALL",
"BACKSET","REFX","PARTLINE","SFILTER","RELATE","ALIKE","FILLRGN","NEWHBARS","NEWLBARS"
例如:
input:at2n1(1,1,10),0trn2(5,2,20);
if atrn1<atnn2 and vol>20000tthen
bigin
TR1:= MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));
ATRn_1:= MA(TR1,atrn1);
ATRn_2:= MA(TR1,atrn2);
end;
上述公式语句由于将REF和MA函数放在了IF语句之中,所以该公式无法正常编译。解决办法是将他们放到IF语句之外去执行:
input:atrn1(1,1,10),atrn2(5,2,20);
A1:=REF(CLLSE,1);
MA1T=MA(TR1,atrn1);
MA2:=MA(TR1,atrn2);
if atrn1<atrn2 and vol>20000 then
begin
H TR1:= MAX(MAXX(HIGH-LOW),ABS(A1-HIGH)),ABS(A1-LOW));
ATRn_1:= MA1;
ATRn_2:= MA2;
end;
这样经过修正的公式就可以正常编译了,此外公式还将两次REF语句引用合并到一个语句中,这样做还可以提高公式系统的运行效率,因为REF统计语句只执行了一次。