图表信号未按照crossover\under逻辑执行

如上图所示,我的开仓逻辑是均线发生了crossunder就做空,但是图表里并没有发生crossunder,为何还是执行了做空操作?如果改掉这个问题要怎么写?

代码如下:

Params

Numeric FastLength(10);// 短期指数平均线参数

Numeric midLength(20);// 中期指数平均线参数

Numeric SlowLength(60);// 长期指数平均线参数

Vars

Series<Numeric> AvgValue1;

Series<Numeric> AvgValue2;

Series<Numeric> AvgValue3;

Numeric money;

Numeric myprice;

Series<Numeric> lots;

Series<Numeric> lposition;

Series<Numeric> sposition;

Series<Numeric> trendy;

Numeric exit;

Events

OnReady()

{

SetBackBarMaxCount(1+Max(slowLength,midLength));

}

OnBar(ArrayRef<Integer> indexs)

{

AvgValue1 = AverageFC(Close,FastLength);

AvgValue2 = AverageFC(Close,midLength);

AvgValue3 = AverageFC(Close,SlowLength);

PlotNumeric("MA1",AvgValue1);

PlotNumeric("MA2",AvgValue2);

PlotNumeric("MA3",AvgValue3);

money = Portfolio_CurrentEquity * 0.1;//按总风险度的40%,每个品种均分投资额

myprice = Open;//这里使用open,更为精确的是使用委托价格

lots = IntPart(money/(myprice*contractunit*BigPointValue*MarginRatio)); //计算开仓手数

If(CrossOver( AvgValue2[1] , AvgValue3[1]))

{

trendy = 1;

}

if(CrossUnder(AvgValue2[1] , AvgValue3[1]))

{

trendy = -1;

}


if(CurrentBar >= Max(midLength,slowLength))

{

If(MarketPosition <>1 && trendy == 1)

{

Buy(lots,Open);

lposition = lots;

}

If(MarketPosition <>-1 && trendy == -1)

{

SellShort(lots,Open);

sposition = lots;

}

}

}

这个中间加一个

If(CurrentBar < MaxBarsBack) return;