止损代码不工作

这样写是哪错了呢?//------------------------------------------------------------------------// 简称: maa// 名称: maa// 类别: 公式应用// 类型: 用户应用// 输出: Void//------------------------------------------------------------------------Params //此处添加参数 Numeric stopn(30); Vars //此处添加变量 Numeric MyEntryPrice; Numeric MyExitPrice; Numeric MinPoint; Series<Numeric> ma5; Series<Numeric> ma10; Series<Numeric> ma30;Defs //此处添加公式函数 Events //此处实现事件函数 //初始化事件函数,策略运行期间,首先运行且只有一次 OnInit() { } //Bar更新事件函数,参数indexs表示变化的数据源图层ID数组 OnBar(ArrayRef<Integer> indexs) {ma5 = XAverage(Close[1],20);ma10 = XAverage(Close[1],60);ma30 = XAverage(Close[1],250);PlotNumeric(\"ma5\",ma5);PlotNumeric(\"ma10\",ma10);PlotNumeric(\"ma30\",ma30);If(MarketPosition==0&& Close[1]>Open[1] && High[1]>ma5){SellShort(1,Open);} MyEntryPrice = AvgEntryPrice;MinPoint = MinMove*pricescale; If(MarketPosition == -1 && BarssinceEntry >=1){If(High >= MyEntryPrice + stopn*MinPoint) //30tMyExitPrice = MyEntryPrice + stopn*MinPoint;{If(Open > MyExitPrice)MyExitPrice = Open;{BuyToCover(CurrentContracts,MyExitPrice);Commentary(\"空止损\");}}} }//------------------------------------------------------------------------// 编译版本 2024/09/17 102033// 版权所有 18659798257// 更改声明 TradeBlazer Software保留对TradeBlazer平台// 每一版本的TradeBlazer公式修改和重写的权利//------------------------------------------------------------------------

谢谢老师指点!

您好,这段代码止损没起作用的主要原因是,Buytocover这一句,手数用CurrentContracts,因为是空头,currentContracts是负数,这样是不对的,您可以写BuyToCover(0,MyExitPrice)或者BuyToCover(ABS(CurrentContracts),MyExitPrice)都是可以的。另外,这段代码,不知道是不是您粘贴的原因,IF结构的大括号有点乱。不理顺,也是会影响结果的。修改前和修改后代码对比如下这是修改后的代码