获取 MQL5 中的未平仓合约

发布于 2025-01-14 16:52:14 字数 569 浏览 1 评论 0原文

我想在开仓后立即得到票号。

此代码的输出为零:

bool r = trade.Buy(0.1, Symbol(), 0, 0, 0); 
   
   if(r) 
   {            
      position_Info.SelectByIndex(0);
      Alert(position_Info.Ticket());      
   }

但是如果我添加延迟命令,输出将正确显示:

bool r = trade.Buy(0.1, Symbol(), 0, 0, 0); 
   
   if(r) 
   {      
      Sleep(2000); // <=======================HERE
      position_Info.SelectByIndex(0);
      Alert(position_Info.Ticket());      
   }

如何在不添加延迟命令的情况下执行此操作?

I want to get the ticket number immediately after opening the position.

The output of this code is zero:

bool r = trade.Buy(0.1, Symbol(), 0, 0, 0); 
   
   if(r) 
   {            
      position_Info.SelectByIndex(0);
      Alert(position_Info.Ticket());      
   }

But if I add a delay command, the output will be displayed correctly:

bool r = trade.Buy(0.1, Symbol(), 0, 0, 0); 
   
   if(r) 
   {      
      Sleep(2000); // <=======================HERE
      position_Info.SelectByIndex(0);
      Alert(position_Info.Ticket());      
   }

How can I do this without adding a delay command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2025-01-21 16:52:14

检查文档。

成功完成Buy方法并不总是意味着成功执行的贸易运作。需要使用 ResultRetcode()ResultDeal()

ulong  ResultDeal() const

返回值 = 交易单(如果交易已执行)。

以下代码示例已经过测试并确认可以正常工作:

#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>

CTrade   trade;
CPositionInfo position_Info;

datetime TimeBar;

//+------------------------------------------------------------------+
//| Initialization function of the expert                            |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
{
   if(TimeBar==iTime(_Symbol,_Period,0))
   {
   return;
   }

   if(TimeBar==0)
   {
      bool res = trade.Buy(0.1, Symbol(), 0, 0, 0); 
      if(res && trade.ResultRetcode()==TRADE_RETCODE_DONE) Print(trade.ResultDeal());
      TimeBar=iTime(_Symbol,_Period,0);
   return;
   }

   TimeBar=iTime(_Symbol,_Period,0);
return;
}

Check the documentation.

Successful completion of the Buy method does not always mean successful execution of the trade operation. It is necessary to check the result of trade request (trade server return code) using ResultRetcode() and value returned by ResultDeal().

ulong  ResultDeal() const

Return Value = Deal ticket if the deal is executed.

The following code example has been tested and confirmed as working:

#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>

CTrade   trade;
CPositionInfo position_Info;

datetime TimeBar;

//+------------------------------------------------------------------+
//| Initialization function of the expert                            |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
{
   if(TimeBar==iTime(_Symbol,_Period,0))
   {
   return;
   }

   if(TimeBar==0)
   {
      bool res = trade.Buy(0.1, Symbol(), 0, 0, 0); 
      if(res && trade.ResultRetcode()==TRADE_RETCODE_DONE) Print(trade.ResultDeal());
      TimeBar=iTime(_Symbol,_Period,0);
   return;
   }

   TimeBar=iTime(_Symbol,_Period,0);
return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文