Avgenterprice这个函数要改进一下,目前的这个函数会导致加减仓模型评测结果错误
代码例子:
ma5:=ma(c,5);
ma10:=ma(c,10);
ma20:=ma(c,20);
b1:=cross(ma5,ma20);
b2:=cross(ma10,ma20);
s1:=cross(ma20,ma5);
s2:=cross(ma20,ma10);
平均持仓成本:avgenterprice;//这个函数的结果错误
if holding>0 and s1 then sell(1,1,thisclose);
if holding<0 and b1 then sellshort(1,1,thisclose);
if holding>0 and cross(ma10,ma5) then sell(1,1,thisclose);
if holding<0 and cross(ma5,ma10) then sellshort(1,1,thisclose);
if holding=0 and b1 then buy(1,1,thisclose);
if holding=1 and b2 then buy(1,1,thisclose);
if holding=0 and s1 then buyshort(1,1,thisclose);
if holding=-1 and s2 then buyshort(1,1,thisclose);
测试报告的明细表(手续费为0)
如上图,第一次平多后,平均持仓成本价错误,导致第二次平多时盈亏计算不正确,应该为3196.6-3197.5 而非3196.6-3199.4
平均持仓成本只有在有新开仓或者持仓为0的时候,才会改变。
假设 初始仓位为C1,对应的平均持仓成本价为 AVG1
新开仓为C2,对应的平均持仓成本价为 AVG2 (手续费为0的情况下,为开仓价)
计算方法为:AVG=(C1*AVG1+C2*AVG2)/(C1+C2)
非常感谢!