关于C类SymbolInfo.mqh 获取不到货币对报价

 

高手看以下代码 :

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\SymbolInfo.mqh>
CSymbolInfo symbol;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(60);
   double bid = symbol.Bid();  // 值为0
   double ask = symbol.Ask();  // 值为0
   Print("bid= ", bid, " ask= ", ask);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int32_t id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  }

获取不到这两个值, 这是为什么? 我记得以前可以的

 
Zhang Yi:

高手看以下代码 :

获取不到这两个值, 这是为什么? 我记得以前可以的

需要先调用symbol.RefreshRates(),再获取值
 
Xiaoyu Huang #:
需要先调用symbol.RefreshRates(),再获取值
谢谢
 

顺便问一下您, 为什么这样获取不到 deal_ticket 的值? 是否有什么方法能在开仓后可获取这个值?请赐教~

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(60);
   Print(OpenPosition(ORDER_TYPE_BUY, 0.01));
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int32_t id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
ulong OpenPosition(const ENUM_ORDER_TYPE type, const double volume)
  {
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   ulong  order_ticket = 0;
   ulong  deal_ticket  = 0;
   double open_price   = 0;
   if(trade.PositionOpen(_Symbol, type, volume, (type == ORDER_TYPE_BUY) ? ask : bid, 0, 0, ""))
     {
      //                                            真实帐户                模拟帐户
      order_ticket = trade.ResultOrder();  // 可以被获取到           可以被获取到
      deal_ticket  = trade.ResultDeal();   // 获取不到                可以被获取到
      open_price   = trade.ResultPrice();  // 获取不到                可以被获取到
     }
   return(deal_ticket);
  }
//+------------------------------------------------------------------+
 

请原谅我使用自动翻译……

您不应在OnInit()中使用任何符号或交易操作。这可能导致失败!请在OnTick()事件处理程序中执行这些操作。

You should not use any symbol or trading operations in the OnInit(). It can fail! Carry out those operations in the OnTick() event handler.

 
Fernando Carreiro #:

请原谅我使用自动翻译……

您不应在OnInit()中使用任何符号或交易操作。这可能导致失败!请在OnTick()事件处理程序中执行这些操作。

You should not use any symbol or trading operations in the OnInit(). It can fail! Carry out those operations in the OnTick() event handler.

thank you!

I understand that, this is just a demo code, it just expresses that I want to test the value of deal_ticket