写的mql5的追踪止损function不运行,返回错误0,是为啥呢?

oxilide  
void Management1()
{
   MqlTick last_tick;
   SymbolInfoTick(_Symbol,last_tick);
   double Ask=last_tick.ask;
   double Bid=last_tick.bid;
   
   int total = PositionsTotal();
   for ( int i = 0; i < total; i++ ) 
{

   if(PositionGetSymbol(i)!="")
     {
       if(PositionGetInteger(POSITION_MAGIC)!=_MagicNum) continue;
       else
       {
        
         if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
            if(TrailingStop>0)  
            {                 
               if((PositionGetDouble(POSITION_PRICE_OPEN)-Ask)>(Point()*TrailingStop) )
               {
                  if((PositionGetDouble(POSITION_SL)>(Ask+Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                  {
                     if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())!=NormalizeDouble(Ask+Point()*TrailingStop,Digits()))
                        {
                           //--- 准备请求
                           MqlTradeRequest request;
                           ZeroMemory(request);
                           //--- 下即时订单
                           request.action=TRADE_ACTION_SLTP;
                           //--- 交易对象
                           request.symbol=Symbol();
                           //--- 止损 
                           request.sl=NormalizeDouble(Ask+Point()*TrailingStop,Digits());
                           //--- 未指定止赢
                           request.tp=0;
                           MqlTradeResult result;
                           ZeroMemory(result);
                           MqlTradeCheckResult CheckResult;
                           //--- 发送订单
                           if(OrderCheck(request,CheckResult)) 
                              {
                                 OrderSend(request,result);
                               }
                           else
                             {
                                 Print("OrderSelect Sell error code is :",GetLastError());
                             }
                        }

                  }
               }
            }
         if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)
            if(TrailingStop>0)  
            {                 
               if(Bid-PositionGetDouble(POSITION_PRICE_OPEN)>Point()*TrailingStop)
               {
                  if((PositionGetDouble(POSITION_SL)<(Bid-Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                  {
                     if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())!=NormalizeDouble(Bid-Point()*TrailingStop,Digits()))
                        {
                        //--- 准备请求
                        MqlTradeRequest request;
                        ZeroMemory(request);
                        //--- 下即时订单
                        request.action=TRADE_ACTION_SLTP;
                        //--- 交易对象
                        request.symbol=Symbol();
                        //--- 止损 
                        request.sl=NormalizeDouble(Bid-Point()*TrailingStop,Digits());
                        //--- 未指定止赢
                        request.tp=0;
                        MqlTradeResult result;
                        ZeroMemory(result);
                        MqlTradeCheckResult CheckResult;
                        //--- 发送订单
                        if(OrderCheck(request,CheckResult))
                           {
                              OrderSend(request,result);
                           }
                        else
                          {
                              Print("OrderSelect Buy error code is :",GetLastError());
                          }
                        }  
                  }
               }
            }
         }
       }
         else Print("OrderSelect error code is :",GetLastError());
      
}
   return;
}

编译又说没错,就是不运行,有点头疼

Ziheng Zhuang  
void Management1()
{
   MqlTick last_tick;
   SymbolInfoTick(_Symbol,last_tick);
   double Ask=last_tick.ask;
   double Bid=last_tick.bid;

   int total = PositionsTotal();
   for ( int i = 0; i < total; i++ )
   {
      if(PositionGetSymbol(i)!="")
      {
         if(PositionGetInteger(POSITION_MAGIC)!=_MagicNum) continue;
         else
         {

            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
               if(TrailingStop>0)
               {
                  if((PositionGetDouble(POSITION_PRICE_OPEN)-Ask)>(Point()*TrailingStop) )
                  {
                     if((PositionGetDouble(POSITION_SL)>(Ask+Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                     {
                        if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())>NormalizeDouble(Ask+Point()*TrailingStop,Digits()))
                        {
                           //--- 准备请求
                           MqlTradeRequest request;
                           ZeroMemory(request);
                           //--- 下即时订单
                           request.action=TRADE_ACTION_SLTP;
                           //--- 交易对象
                           request.symbol=Symbol();
                           //--- 止损
                           request.sl=NormalizeDouble(Ask+Point()*TrailingStop,Digits());
                           //--- 未指定止赢
                           request.tp=0;
                           MqlTradeResult result;
                           ZeroMemory(result);
                           MqlTradeCheckResult CheckResult;
                           //--- 发送订单
                           if(OrderCheck(request,CheckResult))
                           {
                              OrderSend(request,result);
                           }
                           else
                           {
                              Print("OrderSelect Sell error code is :",GetLastError());
                           }
                        }

                     }
                  }
               }
            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)
               if(TrailingStop>0)
               {
                  if(Bid-PositionGetDouble(POSITION_PRICE_OPEN)>Point()*TrailingStop)
                  {
                     if((PositionGetDouble(POSITION_SL)<(Bid-Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                     {
                        if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())<NormalizeDouble(Bid-Point()*TrailingStop,Digits()))
                        {
                           //--- 准备请求
                           MqlTradeRequest request;
                           ZeroMemory(request);
                           //--- 下即时订单
                           request.action=TRADE_ACTION_SLTP;
                           //--- 交易对象
                           request.symbol=Symbol();
                           //--- 止损
                           request.sl=NormalizeDouble(Bid-Point()*TrailingStop,Digits());
                           //--- 未指定止赢
                           request.tp=0;
                           MqlTradeResult result;
                           ZeroMemory(result);
                           MqlTradeCheckResult CheckResult;
                           //--- 发送订单
                           if(OrderCheck(request,CheckResult))
                           {
                              OrderSend(request,result);
                           }
                           else
                           {
                              Print("OrderSelect Buy error code is :",GetLastError());
                           }
                        }
                     }
                  }
               }
         }
      }
      else Print("OrderSelect error code is :",GetLastError());

   }
   return;
}
oxilide  
Ziheng Zhuang:
//+------------------------------------------------------------------+
//| Management for open Position conditions                             |
//+------------------------------------------------------------------+
void Management1()
{
   MqlTick last_tick;
   SymbolInfoTick(_Symbol,last_tick);
   double Ask=last_tick.ask;
   double Bid=last_tick.bid;
   
   int total = PositionsTotal();
   for ( int i = 0; i < total; i++ ) 
{

   if(PositionGetSymbol(i)!="")
     {
       if(PositionGetInteger(POSITION_MAGIC)!=_MagicNum) continue;
       else
       {
        
         if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
            if(TrailingStop>0)  
            {                 
               if((PositionGetDouble(POSITION_PRICE_OPEN)-Ask)>(Point()*TrailingStop) )
               {
                  if((PositionGetDouble(POSITION_SL)>(Ask+Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                  {
                     if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())>NormalizeDouble(Ask+Point()*TrailingStop,Digits()))
                        {
                           //--- 准备请求
                           MqlTradeRequest request;
                           ZeroMemory(request);
                           //--- 下即时订单
                           request.action=TRADE_ACTION_SLTP;
                           //--- 交易对象
                           request.symbol=Symbol();
                           //--- 止损 
                           request.sl=NormalizeDouble(Ask+Point()*TrailingStop,Digits());
                           //--- 未指定止赢
                           request.tp=0;
                           MqlTradeResult result;
                           ZeroMemory(result);
                           MqlTradeCheckResult CheckResult;
                           //--- 发送订单
                           if(OrderCheck(request,CheckResult)) 
                              {
                                 OrderSend(request,result);
                               }
                           else
                             {
                                 //--- 将服务器响应打印到日志中  
                                 Print(CheckResult.retcode,"  -error ");
                                 Print(request.sl,"  ");
                                 Print(__FUNCTION__,":",CheckResult.comment);
                             }
                        }

                  }
               }
            }
         if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)
            if(TrailingStop>0)  
            {                 
               if(Bid-PositionGetDouble(POSITION_PRICE_OPEN)>Point()*TrailingStop)
               {
                  if((PositionGetDouble(POSITION_SL)<(Bid-Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                  {
                     if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())<NormalizeDouble(Bid-Point()*TrailingStop,Digits()))
                        {
                        //--- 准备请求
                        MqlTradeRequest request;
                        ZeroMemory(request);
                        //--- 下即时订单
                        request.action=TRADE_ACTION_SLTP;
                        //--- 交易对象
                        request.symbol=Symbol();
                        //--- 止损 
                        request.sl=NormalizeDouble(Bid-Point()*TrailingStop,Digits());
                        //--- 未指定止赢
                        request.tp=0;
                        MqlTradeResult result;
                        ZeroMemory(result);
                        MqlTradeCheckResult CheckResult;
                        //--- 发送订单
                        if(OrderCheck(request,CheckResult))
                           {
                              OrderSend(request,result);
                           }
                        else
                          {
                              //--- 将服务器响应打印到日志中  
                              Print(CheckResult.retcode,"  -error ");
                              Print(request.sl,"  ");
                              Print(__FUNCTION__,":",CheckResult.comment);
                          }
                        }  
                  }
               }
            }
         }
       }
         else Print("OrderSelect error code is :",GetLastError());
      
}
   return;
}

反馈机制改动后,日志不停反馈以下信息


2020.08.10 18:15:15.882 2020.08.06 23:59:45   Management1:Position doesn't exist

2020.08.10 18:15:15.882 2020.08.06 23:59:45   10036  -error 

oxilide  

问题解决了,主要就是Position的问题


SL & TP Modification

Trade order to modify the StopLoss and/or TakeProfit price levels. It requires to specify the following 4 fields:

  • action
  • symbol
  • sl
  • tp
  • position
Ziheng Zhuang  
MT5的EA建议用标准库,避免各种低级错误。
原因: