请问如何解决错误代码4756 , Invalid stops的问题.

 
void yidong(int tppoint_1,int tppoint_2,string symbol,ENUM_POSITION_TYPE type,int magic,string com)
 {
//  tppoint_1=400 
//  tppoint_2=500 
   int t=PositionsTotal();
   for(int i=t-1;i>=0;i--)
     {
       if(PositionGetTicket(i)>0)
        {
          if(PositionGetString(POSITION_SYMBOL)==symbol)
           {
              double bid=SymbolInfoDouble(symbol,SYMBOL_BID);
              double ask=SymbolInfoDouble(symbol,SYMBOL_ASK);
              long dig=SymbolInfoInteger(symbol,SYMBOL_DIGITS);
              double pot=SymbolInfoDouble(symbol,SYMBOL_POINT);
              double op=PositionGetDouble(POSITION_PRICE_OPEN);
              double sl= PositionGetDouble(POSITION_SL);
              double tp=PositionGetDouble(POSITION_TP);  
             
             if(type==POSITION_TYPE_BUY && magic == PositionGetInteger(POSITION_MAGIC) && com == PositionGetString(POSITION_COMMENT) )
              {     
                if(bid-op>=pot*tppoint_1  && bid-op<pot*tppoint_2 && sl<op+tppoint_1*pot )
                   {
                     MqlTradeRequest request={};
                     MqlTradeResult  result={};
                     request.action=TRADE_ACTION_SLTP;
                     request.position=PositionGetTicket(i);
                     request.symbol=symbol;
                     request.sl=op+tppoint_1*pot;
                     request.tp=op+tppoint_2*pot;
                     request.comment=com;
                     request.magic=magic;
                     if(!OrderSend(request,result))
                        PrintFormat("OrderSend error %d",GetLastError()); 
                    } 
              }

           if(type==POSITION_TYPE_SELL && magic == PositionGetInteger(POSITION_MAGIC) && com == PositionGetString(POSITION_COMMENT) )
              {  
              
             if( op-ask>=pot*tppoint_1 && op-ask<tppoint_2*pot && op-tppoint_1*pot<sl  )
    
                      {
                    
                       MqlTradeRequest request={};
                       MqlTradeResult  result={};
                       request.action=TRADE_ACTION_SLTP;
                       request.position=PositionGetTicket(i);
                       request.symbol=symbol;
                       request.sl=op-tppoint_1*pot ;
                       request.tp=op-tppoint_2*pot ;
                       request.comment=com;
                       request.magic=magic;
                       if(!OrderSend(request,result))
                          PrintFormat("OrderSend error %d",GetLastError()); 
                    }
              }
           } 
        }
     }
 }

需求:当价格到达设定的区间时,开始修改止损和止盈的价格。

问题:用MT5的“1分钟OHLC”模式回测时,一切正常,但用“每个点基于实时点”回测时,日志就显示4756错误代码,提示Invalid stops 。
止盈和止损很多时候修改一次就成功,但有时需要多次修改才成功,然后提示Invalid stops ,如附件所示.
请问各位如果解决这个问题?

附加的文件:
SL_1.png  29 kb
TP_1.png  29 kb
 
多單必須採用ASK價格計算,空單必須採用BID價格計算,否則會有錯誤。
 
以及多單修改的止損位必須大於原本的止損位,空單修改的止損位必須小於原本的止損位。
 
试下这个
//修改已持仓订单或未成交的订单的止损或止盈,修改已持仓订单:orderType = 1; 修改已挂单但未成交的订单::orderType = 2;
//例如:OrderModify(1,100, 50,magic); //修改已持仓订单止损100点 止盈50点
bool OrderModify(int orderType = 1, double stopLossPoints = 0, double takeProfitPoints = 0, uint MagicNumber = 0)
{
   MqlTradeRequest request = {};
   MqlTradeResult  result  = {};
   double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
   int direction = 0;
   double openPrice = 0;
   switch(orderType)
   {
   case 1:
      if (!PositionSelectByTicket(request.position))
      {
         Print("Failed to select position by ticket");
         return false;
      }
      direction = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? 1 : -1;
      openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
      request.volume = PositionsTotal();
      break;
   case 2:
      if (!OrderSelect(request.order))
      {
         Print("Failed to select order by ticket");
         return false;
      }
      direction = (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY) ? 1 : -1;
      openPrice = OrderGetDouble(ORDER_PRICE_OPEN);
      request.volume = OrdersTotal();
      break;
   default:
      Print("Invalid order type");
      return false;
   }
   ENUM_ORDER_TYPE_FILLING fillMode  = (ENUM_ORDER_TYPE_FILLING) SymbolInfoInteger(_Symbol,SYMBOL_FILLING_MODE);
   fillMode -= 1;
   request.action       = TRADE_ACTION_SLTP;
   request.symbol       = _Symbol;
   request.magic        = MagicNumber;
   request.type_filling = fillMode;
   request.sl           = openPrice - direction * stopLossPoints * point;
   request.tp           = openPrice + direction * takeProfitPoints * point;
   bool res = OrderSend(request, result);
   if (res)
   {
      Print("Order modified. Order:", request.position, " Lots:", request.volume);
   }
   else
   {
      Print("Failed to modify order. Error:", GetLastError());
   }
   return res;
}
 
ken138888:

我你的基础上稍微修改了一下,你试试吧,由于我没有你的使用环境,无法测试

void yidong(int tppoint_1, int tppoint_2, string symbol, ENUM_POSITION_TYPE type, int magic, string com)
{
   // 遍历所有头寸
   int totalPositions = PositionsTotal();
   for (int i = totalPositions - 1; i >= 0; i--)
   {
      if (PositionGetTicket(i) > 0)
      {
         if (PositionGetString(POSITION_SYMBOL) == symbol)
         {
            // 获取当前市场报价
            double bid = SymbolInfoDouble(symbol, SYMBOL_BID);
            double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
            long dig = SymbolInfoInteger(symbol, SYMBOL_DIGITS);
            double pot = SymbolInfoDouble(symbol, SYMBOL_POINT);
            // 选择当前头寸
            PositionSelectByTicket(PositionGetTicket(i));
            double op = PositionGetDouble(POSITION_PRICE_OPEN);
            double sl = PositionGetDouble(POSITION_SL);
            double tp = PositionGetDouble(POSITION_TP);
            // 止盈止损条件判断
            if (type == POSITION_TYPE_BUY && magic == PositionGetInteger(POSITION_MAGIC) && com == PositionGetString(POSITION_COMMENT))
            {
               if (ask - op >= pot * tppoint_1 && ask - op < pot * tppoint_2 && sl < op + tppoint_1 * pot)
               {
                  MqlTradeRequest request = {};
                  MqlTradeResult result = {};
                  request.action = TRADE_ACTION_SLTP;
                  request.position = PositionGetTicket(i);
                  request.symbol = symbol;
                  request.sl = op + tppoint_1 * pot;
                  request.tp = op + tppoint_2 * pot;
                  request.comment = com;
                  request.magic = magic;
                  // OrderSend 错误处理
                  if (!OrderSend(request, result))
                     PrintFormat("OrderSend error %d: %s", GetLastError());
               }
            }
            if (type == POSITION_TYPE_SELL && magic == PositionGetInteger(POSITION_MAGIC) && com == PositionGetString(POSITION_COMMENT))
            {
               if (op - bid >= pot * tppoint_1 && op - bid < tppoint_2 * pot && op - tppoint_1 * pot < sl)
               {
                  MqlTradeRequest request = {};
                  MqlTradeResult result = {};
                  request.action = TRADE_ACTION_SLTP;
                  request.position = PositionGetTicket(i);
                  request.symbol = symbol;
                  request.sl = op - tppoint_1 * pot;
                  request.tp = op - tppoint_2 * pot;
                  request.comment = com;
                  request.magic = magic;
                  // OrderSend 错误处理
                  if (!OrderSend(request, result))
                     PrintFormat("OrderSend error %d: %s", GetLastError());
               }
            }
            // 在条件结束后取消选择头寸
            PositionSelectByTicket(0);
         }
      }
   }
}