请教一个关于“保本止损”“移动止损”的问题

 

我尝试通过如下代码实现 “保本止损”,应该在OrderModify之后就可以确保本订单不会亏损,为何在mt4策略测试时OrderModify之后仍会有亏损?请教各位老师!谢谢!!


for(int j = OrdersTotal() - 1; j >= 0; j--)

    {
        if(OrderSelect(j, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol())
        {
            // 获取前P根K线的ATR值
            double movingATR = iATR(NULL, 0, Q, P);
            
            if(OrderType() == OP_BUY)
            {
            //计算保本止损价:当盈利超过A倍ATR时,将止损移至保本点上方
            if(Ask - OrderOpenPrice()> A*movingATR && (OrderStopLoss()<OrderOpenPrice()||OrderStopLoss() == 0))
            {
                double buyStopLoss = MathMax(Ask +A*movingATR, OrderOpenPrice()); // 止损不低于开仓价
                buyStopLoss = NormalizeDouble(buyStopLoss, Digits);
                if(!OrderModify(OrderTicket(), OrderOpenPrice(), buyStopLoss, OrderTakeProfit(), 0, clrNONE))
                      {
                        Print("多单保本止损更新失败,错误代码: ", GetLastError());
                      }
            }
                                     
             //计算移动止损价
                else 
                { 
                buyStopLoss = Ask - A * movingATR;
                //double buyTakeProfit = High[1] + B * movingATR;
              
                buyStopLoss = NormalizeDouble(buyStopLoss, Digits);
                //buyTakeProfit = NormalizeDouble(buyTakeProfit, Digits);
                
                // 确保新止损低于当前价格
                if(buyStopLoss < Ask && buyStopLoss > 0)
                {
                    // 只有新止损大于原止损且当前买价不亏损时才更新(向上移动止损)
                    if(buyStopLoss > OrderStopLoss()&& Ask > OrderOpenPrice()  || OrderStopLoss() == 0)
                    {
                        if(!OrderModify(OrderTicket(), OrderOpenPrice(), buyStopLoss, OrderTakeProfit(), 0, clrNONE))
                        {
                            Print("多单移动止损更新失败,错误代码: ", GetLastError());
                        }
                    }
                }
                }
            }
           ......