谁会写一个开仓的代码,并返回交易单号?

 
哪会大神能否解释一下,为什么我的这段代码在模拟盘上能正确返回交易单号,而在实盘上返回的是0值 ?

//+------------------------------------------------------------------+
//|                                                        Test1.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
CTrade    m_trade;
//+------------------------------------------------------------------+
//| EA交易初始化函数                                                |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   EventSetMillisecondTimer(1);
   ulong deal_ticket = OpenPosition(ORDER_TYPE_BUY, 0.01, 123);
   printf(deal_ticket);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| EA报价函数                                                       |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
ulong OpenPosition(const ENUM_ORDER_TYPE type,
                   const double lot,
                   const long magic)
  {
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   m_trade.SetExpertMagicNumber(magic);
   if(m_trade.PositionOpen(_Symbol, type, lot, (type == ORDER_TYPE_BUY) ? ask : bid, 0, 0))
      return(m_trade.ResultDeal());
   return(0);
  }
 
写在OnInit里面,可能没开市交易不了吧,都用m_trade了,看看ResultRetcode()是什么啊
 
//+------------------------------------------------------------------+
//|                                                        Test1.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CTrade        m_trade;
CDealInfo     m_deal;
CSymbolInfo   m_symbol;
input double TakeProfit = 0;
input double StopLoss   = 0;
input string PositionComment = "";
double m_point;
//+------------------------------------------------------------------+
//| EA交易初始化函数                                                |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   EventSetMillisecondTimer(1);
   if(!m_symbol.Name(_Symbol)) // sets symbol name
      return(INIT_FAILED);
   RefreshRates();
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(INT_MAX);
   int adjust = (m_symbol.Digits() == 3 || m_symbol.Digits() == 5) ? 10 : 1;
   m_point = m_symbol.Point() * adjust;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
   Comment("");
  }
//+------------------------------------------------------------------+
//| EA报价函数                                                       |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask() == 0 || m_symbol.Bid() == 0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
ulong OpenPosition(const ENUM_ORDER_TYPE type,
                   const double volume,
                   const long magic)
  {
   RefreshRates();
   double ask = m_symbol.Ask();
   double bid = m_symbol.Bid();
   datetime from_date = m_symbol.Time();
   ulong deal_ticket = 0;
   double sl = 0;
   double tp = 0;
   switch(type)
     {
      case ORDER_TYPE_BUY:
         if(TakeProfit > 0)
            tp = m_symbol.NormalizePrice(ask + TakeProfit * m_point);
         if(StopLoss > 0)
            sl = m_symbol.NormalizePrice(bid - StopLoss * m_point);
         break;
      case ORDER_TYPE_SELL:
         if(TakeProfit > 0)
            tp = m_symbol.NormalizePrice(bid - TakeProfit * m_point);
         if(StopLoss > 0)
            sl = m_symbol.NormalizePrice(ask + StopLoss * m_point);
         break;
     }
   m_trade.SetExpertMagicNumber(magic);
   ulong time = GetTickCount64();
   if(m_trade.PositionOpen(m_symbol.Name(), type, volume, (type == ORDER_TYPE_BUY) ? ask : bid, sl, tp, PositionComment))
     {
      ulong open_execution = GetTickCount64() - time;
      do
        {
         ulong order_ticket = m_trade.ResultOrder();
         HistorySelect(from_date, TimeTradeServer());
         int total = HistoryDealsTotal();
         for(int i = total - 1; i >= 0; i--)
           {
            if(!m_deal.SelectByIndex(i))
               continue;
            if(m_deal.Symbol() != m_symbol.Name())
               continue;
            if(m_deal.Entry() != DEAL_ENTRY_IN)
               continue;
            if(m_deal.Order() != order_ticket)
               continue;
            printf(StringFormat("open  #%%I64u %%s %%.2f %%s at %%.%df done in %%I64u ms", m_symbol.Digits()),
                   m_deal.Order(), (type == ORDER_TYPE_BUY) ? "buy" : "sell", m_deal.Volume(), m_deal.Symbol(), m_deal.Price(), open_execution);
            deal_ticket = m_deal.Ticket();
            break;
           }
        }
      while(deal_ticket == 0);
     }
   return(deal_ticket);
  }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.05.06
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Yang Wang:
哪会大神能否解释一下,为什么我的这段代码在模拟盘上能正确返回交易单号,而在实盘上返回的是0值 ?

将"

return(m_trade.ResultDeal());

"改为上述图中的函数就是。

 
Lin Luo #:

将"


"改为上述图中的函数就是。

楼主的意思是要返回deal ticket编号,而不是订单编号

 
Zhang Yi #:

楼主的意思是要返回deal ticket编号,而不是订单编号

他典型弄错deal 和order

 
Mage He #:

他典型弄错deal 和order

我看弄错的是你吧,楼主已经说了是交易单号,以下代码能在实盘中正确返回交易单号

//+------------------------------------------------------------------+
//|                                                        Test1.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CTrade        m_trade;
CDealInfo     m_deal;
CSymbolInfo   m_symbol;
CPositionInfo m_pos;
input double TakeProfit = 0;
input double StopLoss   = 0;
input string PositionComment = "";
double m_point;
//+------------------------------------------------------------------+
//| EA交易初始化函数                                                |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   EventSetMillisecondTimer(1);
   if(!m_symbol.Name(_Symbol)) // sets symbol name
      return(INIT_FAILED);
   RefreshRates();
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(INT_MAX);
   int adjust = (m_symbol.Digits() == 3 || m_symbol.Digits() == 5) ? 10 : 1;
   m_point = m_symbol.Point() * adjust;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
   Comment("");
  }
//+------------------------------------------------------------------+
//| EA报价函数                                                       |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {
   Comment(TimeGMT());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask() == 0 || m_symbol.Bid() == 0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| 开单并返回交易单号                                              |
//+------------------------------------------------------------------+
ulong OpenPosition(const ENUM_ORDER_TYPE type,
                   const double volume,
                   const long magic)
  {
   RefreshRates();
   double ask = m_symbol.Ask();
   double bid = m_symbol.Bid();
   ulong deal_ticket = 0;
   double sl = 0;
   double tp = 0;
   switch(type)
     {
      case ORDER_TYPE_BUY:
         if(TakeProfit > 0)
            tp = m_symbol.NormalizePrice(ask + TakeProfit * m_point);
         if(StopLoss > 0)
            sl = m_symbol.NormalizePrice(bid - StopLoss * m_point);
         break;
      case ORDER_TYPE_SELL:
         if(TakeProfit > 0)
            tp = m_symbol.NormalizePrice(bid - TakeProfit * m_point);
         if(StopLoss > 0)
            sl = m_symbol.NormalizePrice(ask + StopLoss * m_point);
         break;
     }
   m_trade.SetExpertMagicNumber(magic);
   ulong time = GetTickCount64();
   if(m_trade.PositionOpen(m_symbol.Name(), type, volume, (type == ORDER_TYPE_BUY) ? ask : bid, sl, tp, PositionComment))
     {
      ulong open_execution = GetTickCount64() - time; // 执行耗时
      datetime from_date = TimeTradeServer() - 30;
      do
        {
         ulong order_ticket = m_trade.ResultOrder();  // 获取订单编号
         HistorySelect(from_date, TimeTradeServer()); // 搜索时间范围
         int total = HistoryDealsTotal();
         for(int i = total - 1; i >= 0; i--)          // 倒序搜索成交历史
           {
            if(!m_deal.SelectByIndex(i))
               continue;
            if(m_deal.Symbol() != m_symbol.Name())
               continue;
            if(m_deal.Entry() != DEAL_ENTRY_IN)
               continue;
            if(m_deal.Order() != order_ticket)        // 如果成交历史中订单编号和服务器返回的订单编号匹配
               continue;
            printf(StringFormat("open  #%%I64u %%s %%.2f %%s at %%.%df done in %%I64u ms", m_symbol.Digits()),
                   m_deal.Order(), (type == ORDER_TYPE_BUY) ? "buy" : "sell", m_deal.Volume(), m_deal.Symbol(), m_deal.Price(), open_execution);
            deal_ticket = m_deal.Ticket();            // 获取交易单号
            break;
           }
        }
      while(deal_ticket == 0);                        // 如果交易单号为0,继续搜索
     }
   return(deal_ticket);                               // 返回交易单号                     
  }
 
Zhang Yi #:

我看弄错的是你吧,楼主已经说了是交易单号,以下代码能在实盘中正确返回交易单号

谢谢,你的代码非常工整,简洁,注释行也能清楚表达,对我这个新手来说这正是我想要的.

 
Zhang Yi #:

我看弄错的是你吧,楼主已经说了是交易单号,以下代码能在实盘中正确返回交易单号

我说的是三楼,不是楼主弄错

 

在我看来这个 Zhang Yi 提供的代码繁琐 完全不适合新手查看,还增加了人家根本没有要求的止损止盈设定,还增加了系统时间的调用,计算订单执行耗时的问题,本身时间的处理就是很复杂的,人家没有搞定怎么获取下单订单号的时候还给人家提出新一个问题

但是嘛,楼主都认可腻了,那我也没什么好说的,就想吐个槽。


教人应该化繁为简,逐步递进,而不是上来就放上所有内容,甚至包含不相关的,这和现在的教科书很类似。这种只有在学会后回头看才能看懂,要不然非常吃力。

最后,楼主的代码我复制到编辑器后直接运行,就可以获得对应订单号,所以问题在哪,我很奇怪


没有搞清楚到底是不是代码问题就放上自己写的,以为自己很厉害,实际上可能根本没有帮到人家,人家复制了代码过去用,就算成功了,那个隐藏的问题不解决,下次还会出现。

 
Mage He #:

在我看来这个 Zhang Yi 提供的代码繁琐 完全不适合新手查看,还增加了人家根本没有要求的止损止盈设定,还增加了系统时间的调用,计算订单执行耗时的问题,本身时间的处理就是很复杂的,人家没有搞定怎么获取下单订单号的时候还给人家提出新一个问题

但是嘛,楼主都认可腻了,那我也没什么好说的,就想吐个槽。


教人应该化繁为简,逐步递进,而不是上来就放上所有内容,甚至包含不相关的,这和现在的教科书很类似。这种只有在学会后回头看才能看懂,要不然非常吃力。

最后,楼主的代码我复制到编辑器后直接运行,就可以获得对应订单号,所以问题在哪,我很奇怪


没有搞清楚到底是不是代码问题就放上自己写的,以为自己很厉害,实际上可能根本没有帮到人家,人家复制了代码过去用,就算成功了,那个隐藏的问题不解决,下次还会出现。

唉,朋友,楼主说了模拟盘没问题,实盘有问题,我也试过了, 确实是这样的问题。

另外说明一下,我也不是在教人,我只是按照自己写代码的风格,把其它功能都完善了(一步到位),还有你说我代码繁琐,我不与你再进一步讨论,自少我花时间写了代码并达到人家想要的东西,而你只会在这吐 槽,高下立判。

还有什么时候我说过自己很厉害了,一山还有一山高,但我就不知道你是不是那座山了?

我回贴是帮助楼主解决问题的, 不是来这里乱喷的,不然你也贴出你的代码,楼主要觉得你的代码好用就用你的呗