我想要在金字塔编写一个指标,求简单方法:计算最近20个周期之内的收盘价偏离ema的程度。
具体表达的部分指标:
(收盘价-20日ema均值)乘以当周期的时间,越靠近现在取值越大。
一个个写
ema20:=ema(c,20);
cu20:=(c-ema20)*20;
c19:=ref(c,1);
ema19:=ref(ema20,1);
cu19:=(c19-ema19)*19;
c18:=ref(c,2);
ema18:=ref(ema20,2);
cu18:=(c18-ema18)*18;
c17:=ref(c,3);
ema17:=ref(ema20,3);
cu17:=(c17-ema17)*17;
c16:=ref(16,4);
ema16:=ref(ema20,4);
cu16:=(c16-ema16)*16;
c15:=ref(c,5);
ema15:=ref(ema20,5);
cu15:=(c15-ema15)*15
------------------
计算出20个周期的cu,然后求均值,得到波动率。
这种计算很多,求一个简单的,运算比较快的
{ema20:=ema(c,20);
cu20:=(ref(c,0)-ref(ema20,0))*20;
c19:=ref(c,1);
ema19:=ref(ema20,1);
cu19:=(c19-ref(ema20,1))*19;
c18:=ref(c,2);
ema18:=ref(ema20,2);
cu18:=(c18-ref(ema20,2))*18;
}
runmode:0;
ss:=0;
ema20:=ema(c,20);
for i=20 downto 1 do begin
ss:=ss+(ref(c,20-i)-ref(ema20,20-i))*i;
end
sss:ss,linethick0;