MT4: Observed Discrepancy between Bar Data and Tick Data - please help

 

Hi alltogether!

I'm using Metatrader 4 Version 4.00 Build 902. And I'm trading on a demo account of FXCM broker.

Here a short ea-program outputting the bid and ask prices for every tick and after completion of the M1 bars their belonging OHLC data. The ea worked on a EURUSD M1 chart.

#property strict
int g_fp;
////////////////////////////////////
class CNewTick {
   private:
   string m_symbol;
   long m_tickno;

   public:
   void Init (string symbol) {
      m_symbol = symbol;
      m_tickno = 0;
   }
   void Reset () {
      m_tickno = 0;
   }
   void WriteTickdata () {
      MqlTick last_tick;
      if(SymbolInfoTick(m_symbol,last_tick)) {
         string digits = IntegerToString (SymbolInfoInteger (m_symbol, SYMBOL_DIGITS));
         int spread = (int)MathRound((last_tick.ask-last_tick.bid)/SymbolInfoDouble(m_symbol,SYMBOL_POINT));
         FileWrite(g_fp, StringFormat ("%7I64d", ++m_tickno), " ", m_symbol, " ", last_tick.time,
            StringFormat(", bid %8."+digits+"f",last_tick.bid),
            StringFormat(", ask %8."+digits+"f",last_tick.ask),
            StringFormat(", Spread %4d", spread));
      } else FileWrite(g_fp, "SymbolInfoTick() failed, error = ", GetLastError());
   }
};
////////////////////////////////////
class CNewBar {
   private:
   string m_symbol;
   ENUM_TIMEFRAMES m_timeframe;
   datetime m_lastbartime;
   
   public:
   void Init (string symbol, ENUM_TIMEFRAMES timeframe) {
      m_symbol = symbol;
      m_timeframe = timeframe;
      m_lastbartime = 0;
      iTime (m_symbol, m_timeframe, 0);
   }
   bool IsNewBar () {
      bool ret = true;
      datetime bartime = iTime (m_symbol, m_timeframe, 0);
      if (bartime == m_lastbartime) return false;
      if (m_lastbartime == 0) ret = false;
      m_lastbartime = bartime;
      return ret;
   }
   void WriteBardata () {
      RefreshRates();
      string digits = IntegerToString (SymbolInfoInteger (m_symbol, SYMBOL_DIGITS));
      FileWrite (g_fp, m_symbol, " ", iTime(m_symbol, m_timeframe, 1),
            StringFormat("%10s",EnumToString(m_timeframe)),
            StringFormat(" iOpen %8."+digits+"f",  iOpen(m_symbol, m_timeframe, 1)),
            StringFormat(" iHigh %8."+digits+"f",  iHigh(m_symbol, m_timeframe, 1)),
            StringFormat(" iLow  %8."+digits+"f",  iLow(m_symbol, m_timeframe, 1)),
            StringFormat(" iClose %8."+digits+"f", iClose(m_symbol, m_timeframe, 1)),
            StringFormat(" iVolume %6I64d",       iVolume(m_symbol, m_timeframe, 1)));
   }
};
///////////////////////////////////////
CNewTick g_NewTick;
CNewBar  g_NewBar;
int OnInit() {
   g_fp = FileOpen (Symbol()+IntegerToString(Period()) + "TickPeriodInfo.txt", FILE_TXT|FILE_WRITE);
   g_NewTick.Init (Symbol());
   g_NewBar.Init (Symbol(), PERIOD_M1);
   return INIT_SUCCEEDED;
}
void OnDeinit(const int reason) {
   FileClose (g_fp);
}
void OnTick() {
   if (g_NewBar.IsNewBar ()) {
      g_NewBar.WriteBardata ();
      g_NewTick.Reset();
   }
   g_NewTick.WriteTickdata ();
}

 Here an extract of the output:

    158 EURUSD 2015.11.10 18:55:57, bid  1.07166, ask  1.07192, Spread   26
    159 EURUSD 2015.11.10 18:55:57, bid  1.07165, ask  1.07192, Spread   27
    160 EURUSD 2015.11.10 18:55:58, bid  1.07166, ask  1.07192, Spread   26
EURUSD 2015.11.10 18:55:00 PERIOD_M1 iOpen  1.07150 iHigh  1.07186 iLow   1.07150 iClose  1.07177 iVolume    180
      1 EURUSD 2015.11.10 18:56:00, bid  1.07166, ask  1.07190, Spread   24
      2 EURUSD 2015.11.10 18:56:00, bid  1.07165, ask  1.07190, Spread   25
      3 EURUSD 2015.11.10 18:56:00, bid  1.07164, ask  1.07190, Spread   26
      4 EURUSD 2015.11.10 18:56:00, bid  1.07163, ask  1.07190, Spread   27
      5 EURUSD 2015.11.10 18:56:01, bid  1.07163, ask  1.07189, Spread   26
      6 EURUSD 2015.11.10 18:56:02, bid  1.07163, ask  1.07187, Spread   24
      7 EURUSD 2015.11.10 18:56:02, bid  1.07162, ask  1.07187, Spread   25
      8 EURUSD 2015.11.10 18:56:02, bid  1.07161, ask  1.07186, Spread   25
      9 EURUSD 2015.11.10 18:56:02, bid  1.07160, ask  1.07186, Spread   26
     10 EURUSD 2015.11.10 18:56:02, bid  1.07160, ask  1.07186, Spread   26
     11 EURUSD 2015.11.10 18:56:02, bid  1.07158, ask  1.07184, Spread   26
     12 EURUSD 2015.11.10 18:56:02, bid  1.07157, ask  1.07184, Spread   27
     13 EURUSD 2015.11.10 18:56:02, bid  1.07157, ask  1.07183, Spread   26
     14 EURUSD 2015.11.10 18:56:02, bid  1.07156, ask  1.07183, Spread   27
     15 EURUSD 2015.11.10 18:56:02, bid  1.07156, ask  1.07182, Spread   26
     16 EURUSD 2015.11.10 18:56:02, bid  1.07155, ask  1.07182, Spread   27
     17 EURUSD 2015.11.10 18:56:09, bid  1.07156, ask  1.07182, Spread   26
     18 EURUSD 2015.11.10 18:56:10, bid  1.07155, ask  1.07182, Spread   27
     19 EURUSD 2015.11.10 18:56:10, bid  1.07156, ask  1.07182, Spread   26
     20 EURUSD 2015.11.10 18:56:14, bid  1.07159, ask  1.07183, Spread   24
     21 EURUSD 2015.11.10 18:56:14, bid  1.07157, ask  1.07183, Spread   26
     22 EURUSD 2015.11.10 18:56:19, bid  1.07156, ask  1.07183, Spread   27
     23 EURUSD 2015.11.10 18:56:22, bid  1.07157, ask  1.07183, Spread   26
     24 EURUSD 2015.11.10 18:56:24, bid  1.07156, ask  1.07183, Spread   27
     25 EURUSD 2015.11.10 18:56:29, bid  1.07156, ask  1.07182, Spread   26
     26 EURUSD 2015.11.10 18:56:29, bid  1.07155, ask  1.07182, Spread   27
     27 EURUSD 2015.11.10 18:56:29, bid  1.07155, ask  1.07181, Spread   26
     28 EURUSD 2015.11.10 18:56:29, bid  1.07155, ask  1.07180, Spread   25
     29 EURUSD 2015.11.10 18:56:29, bid  1.07154, ask  1.07180, Spread   26
     30 EURUSD 2015.11.10 18:56:34, bid  1.07159, ask  1.07183, Spread   24
     31 EURUSD 2015.11.10 18:56:42, bid  1.07158, ask  1.07181, Spread   23
     32 EURUSD 2015.11.10 18:56:43, bid  1.07158, ask  1.07182, Spread   24
     33 EURUSD 2015.11.10 18:56:46, bid  1.07156, ask  1.07182, Spread   26
     34 EURUSD 2015.11.10 18:56:50, bid  1.07157, ask  1.07182, Spread   25
     35 EURUSD 2015.11.10 18:56:54, bid  1.07158, ask  1.07182, Spread   24
     36 EURUSD 2015.11.10 18:56:54, bid  1.07161, ask  1.07184, Spread   23
     37 EURUSD 2015.11.10 18:56:55, bid  1.07161, ask  1.07185, Spread   24
EURUSD 2015.11.10 18:56:00 PERIOD_M1 iOpen  1.07177 iHigh  1.07177 iLow   1.07165 iClose  1.07172 iVolume     43
      1 EURUSD 2015.11.10 18:57:00, bid  1.07159, ask  1.07185, Spread   26
      2 EURUSD 2015.11.10 18:57:00, bid  1.07159, ask  1.07184, Spread   25

Obviously the OHLC bar data of the bar at 2015.11.10 18:56:00 do not match with the tick data (bid values) shown before????

Regarding these tick data I would expect the following OHLC data: Open 1.07166 High 1.07166 Low 1.07154 Close 1.07161. The delta seems to be 0.00011 ?????

I observe similar discrepancies for the other bars too. 

I do not understand this. What I'm doing wrong? Is there a misunderstanding somewhere? 

The funny thing is: I presented quite the same program in forum https://www.mql5.com/en/forum/59295  in a different context, and here the output was as I would expect!

Unfortunately I forgot the Metatrader version I used :-(

I'm a bit confused now and I'm grateful for every answer. Thanks

Matthias/Bobcat 

MQL4: missing ticks
MQL4: missing ticks
  • www.mql5.com
here is a simple expert advisor which writes some tickdata for each tick arriving, and - if there is a new 1 minute bar - writes some bar data to a file:. - - Category: general
 

In the meantime I realized that the problem described above seems to be a broker issue.

I executed the same EA on several demo accounts of a number of brokers and the output was how I expected: no discrepancies between tick data and bar data.

I only have this problem on a demo account of FXCM broker.

Matthias/Bobcat 

Reason: