请教高手:mql5中的OrderSelect方法,是否可以查到挂单所触发的下单订单

要添加评论,请登录注册
quant_troy
12
quant_troy  

MQL4的官方文档中对OrderSelect的定义是这样的:

https://www.mql5.com/en/docs/trading/orderselect


但是当我们的EA去执行了OrderSend方法去挂单,等挂单触发后,再去调用OrderSelect方法来查询当前订单池(比如 OrderSelect(n, SELECT_BY_POS)),这个时候遍历订单池,查找新生成的订单,可以查询到OrderType是sell stop的挂单订单,那是否可以查找到实际触发时候的下单动作呢?


如果不能,MQL4中是否有提供API可以查到挂单所下的订单呢?


望解惑,十分感谢!

OrderSelect - 交易函数 - MQL4参考
OrderSelect - 交易函数 - MQL4参考
  • docs.mql4.com
To find out from what list the order has been selected, its close time must be analyzed. If the order close time equals to 0, the order is open or pending and taken from the terminal open orders list. One can distinguish an opened order from a pending order by the order type. If the order close time does not equal to 0, the order is a closed...
Chunlei Zhang
18
Chunlei Zhang  
将挂单设置一个不同的magic,方便判断。
quant_troy
12
quant_troy  
Chunlei Zhang:
将挂单设置一个不同的magic,方便判断。
你好,如果是想从代码中知道,什么时候触发了挂单,有对应的接口可以查询吗?
Ziheng Zhuang
20872
Ziheng Zhuang  

标题写的MQL5,帖子中问的MQL4.

MT4不提供订单消息事件查询,没有对应的API,要监控订单事件,需要自己实现。

MT5提供非常详细的订单消息事件。

Renjun Gu
262
Renjun Gu  

将挂单设置不同的magic或者 订单注释,

orderSelet后查找对应magic 或者订单注释的单子就可以了

enbo lu
版主
1926
enbo lu  

自己写的检测挂单被激活的函数,返回被激活挂单的订单号:

//+------------------------------------------------------------------+
//|  Pending Order -> Market Order Detect function                   |
//+------------------------------------------------------------------+
//检测挂单转市价单                                                                  
long PendingToMarket(string _symbol)
{
    int _OrdersTotal = OrdersTotal();
    int now_OrdersTotal = 0;
    static int pre_OrdersTotal = 0;
    
    int now_OrdersArray[][2]; 
    
    int now_CurOrder = 0;
    
    static int pre_OrdersArray[][2]; 
    
    int pre_CurOrder = 0;
    long PendingOrderOpened_ticket = -1;
    int type;
    long ticket;
    bool find = false;
    
    ArrayResize( now_OrdersArray, MathMax( _OrdersTotal, 1 ) );
   
    ArrayInitialize( now_OrdersArray, 0 );

    for ( int i = _OrdersTotal - 1; i >= 0; i -- )
      {
        if ( !OrderSelect( i, SELECT_BY_POS ) )
          {
            Print( "OrderSelect( ", i, ", SELECT_BY_POS ) - Error #",GetLastError() );
            continue;
          }
       
        if ( OrderMagicNumber() == MAGIC && OrderSymbol() == _symbol )
          {
            now_OrdersArray[now_OrdersTotal][0] = OrderTicket();
            now_OrdersArray[now_OrdersTotal][1] = OrderType();
            now_OrdersTotal ++;
          }
      }
    
    ArrayResize( now_OrdersArray, MathMax( now_OrdersTotal, 1 ) );
 
    for ( pre_CurOrder = 0; pre_CurOrder < pre_OrdersTotal; pre_CurOrder ++ )
      {
       
        ticket = pre_OrdersArray[pre_CurOrder][0];
        type   = pre_OrdersArray[pre_CurOrder][1];

     
        for ( now_CurOrder = 0; now_CurOrder < now_OrdersTotal; now_CurOrder ++ )
          {

            if ( ticket == now_OrdersArray[now_CurOrder][0] )
              {
 
                if ( type != now_OrdersArray[now_CurOrder][1] )
                  {
       
                    PendingOrderOpened_ticket = ticket;
                    find = true;
                  }
                break;
              }
          } 
          
        if(find) break;                    
      }    
   
    ArrayResize( pre_OrdersArray, MathMax( now_OrdersTotal, 1 ) );
    for ( now_CurOrder = 0; now_CurOrder < now_OrdersTotal; now_CurOrder ++ )
    {
        pre_OrdersArray[now_CurOrder][0] = now_OrdersArray[now_CurOrder][0];
        pre_OrdersArray[now_CurOrder][1] = now_OrdersArray[now_CurOrder][1];
    }
    pre_OrdersTotal = now_OrdersTotal;

    return(PendingOrderOpened_ticket);
}
要添加评论,请登录注册