MT5入门到精通之十九(获取最近历史订单)

一获取最近历史订单
1.基本概念有三种模式
a.deal模式,没有止损止盈,每次开仓,平仓都会有历史记录,有开仓平仓方向(一般用这个)。

image.png

b.orders模式,有止损止盈,没有开仓平仓方向


image.png

c.positon模式,和mt4类似,暂时不用


image.png

2.获取最近指定货币,指定方向,平仓的历史订单消息 用deals模式(getLastDealsHistory)
【注意:通过先获取deals订单,里面的order_id,再来获取该订单的止损止盈,获取不成功原因。平仓的订单的order_id没有止损止盈,开仓的deal订单的order_id,才有】

//+------------------------------------------------------------------+
//|                                            testHistoryScript.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//1.第一步获取历史数据范围(一般都要写)
   HistorySelect(0,TimeCurrent());

//2.交易类型历史 deal
   dealsHistory();

//3.订单类型类是 ticket (缺点:不知道是开仓还是平仓)
   ordersHistory();


//4.获取最近的交易类型历史订单
   string symbol="GBPUSD";
   ENUM_DEAL_TYPE type=DEAL_TYPE_BUY;
   int magic=170507;
   double deal_price;
   datetime deal_time;
   double lots;
   getLastDealsHistory(symbol,type,deal_price,deal_time,lots,magic);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void dealsHistory()
  {
   HistorySelect(0,TimeCurrent());
//2.1 总量
   int dealsTotal=HistoryDealsTotal();
   for(int i=0;i0)
        {
         datetime deal_time=HistoryDealGetInteger(deal_ticket,DEAL_TIME);
         //2.3 order模式下的id
         int order_ticket=HistoryDealGetInteger(deal_ticket,DEAL_ORDER);
         string symbol=HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
         int deal_type=HistoryDealGetInteger(deal_ticket,DEAL_TYPE);
         int direction=HistoryDealGetInteger(deal_ticket,DEAL_ENTRY);
         double volume=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
         double price=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ordersHistory()
  {
   HistorySelect(0,TimeCurrent());
   //3.1 总量
   int ordersTotal=HistoryOrdersTotal();
   for(int i=0;i0)
        {
         datetime order_time=HistoryOrderGetInteger(order_ticket,ORDER_TIME_SETUP);
         string symbol=HistoryOrderGetString(order_ticket,ORDER_SYMBOL);
         int order_type=HistoryOrderGetInteger(order_ticket,ORDER_TYPE);
         double volume=HistoryOrderGetDouble(order_ticket,ORDER_VOLUME_INITIAL);
         double price=HistoryOrderGetDouble(order_ticket,ORDER_PRICE_OPEN);
         double stopLoss=HistoryOrderGetDouble(order_ticket,ORDER_SL);
         double takeProfit=HistoryOrderGetDouble(order_ticket,ORDER_TP);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getLastDealsHistory(string symbol,ENUM_DEAL_TYPE type,double &deal_price,datetime &deal_time,double &lots,int magic=0)
  {
   HistorySelect(0,TimeCurrent());
   int dealsTotal=HistoryDealsTotal();
   for(int i=dealsTotal-1;i>=0;i--)
     {
      int deal_ticket=HistoryDealGetTicket(i);
      if(deal_ticket>0)
        {
        //4.1平仓单out
         if(HistoryDealGetInteger(deal_ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT && HistoryDealGetString(deal_ticket,DEAL_SYMBOL)==symbol && HistoryDealGetInteger(deal_ticket,DEAL_TYPE)==type)
           {
            if(magic==0)
              {
               deal_time=HistoryDealGetInteger(deal_ticket,DEAL_TIME);
               lots=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
               deal_price=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
               break;
              }
            else
              {
               if(HistoryDealGetInteger(deal_ticket,DEAL_MAGIC)==magic)
                 {
                  deal_time=HistoryDealGetInteger(deal_ticket,DEAL_TIME);
                  lots=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
                  deal_price=HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
                  break;
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MT5入门到精通之十九(获取最近历史订单))