Tiki in real time - page 14

 
Roman:

I do not use dealing forex.
You, in the example fromCopyTick, only get one last element of the structure, in fact only the best prices.

What makes you think it 's just one item?

That's the thing, I get absolutely all the ticks (and not just ticks, but also changes in the cup)

Check

//+------------------------------------------------------------------+
//|                                                   Ticks_test.mq5 |
//|                                      Copyright 2019 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019 prostotrader"
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
bool is_book;
MqlTick ticks[], s_tick;
ulong last_time, mem_cnt, tot_cnt;
bool is_first;
int t_cnt, result;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
  tot_cnt = 0;
  is_book = MarketBookAdd(Symbol());
  result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, 0, 1);
  if(result > 0)
  {
    last_time = ulong(ticks[0].time_msc); //запоминаем время последнего известного тика
    is_first = true;
  }
  else
  {
    is_first = false;
    Alert("No start time!");
    return(INIT_FAILED);
  } 
  ArraySetAsSeries(ticks, true);  
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+ 
//| возвращает строковое описание тика                               | 
//+------------------------------------------------------------------+ 
string GetTickDescription(MqlTick &tick) 
  { 
   string res = string(tick.time) + "." +  string(tick.time_msc%1000); 
// 
   bool buy_tick = ((tick.flags&TICK_FLAG_BUY)==TICK_FLAG_BUY); 
   bool sell_tick = ((tick.flags&TICK_FLAG_SELL)==TICK_FLAG_SELL); 
   bool ask_tick = ((tick.flags&TICK_FLAG_ASK)==TICK_FLAG_ASK); 
   bool bid_tick = ((tick.flags&TICK_FLAG_BID)==TICK_FLAG_BID); 
   bool last_tick = ((tick.flags&TICK_FLAG_LAST)==TICK_FLAG_LAST); 
   bool volume_tick = ((tick.flags&TICK_FLAG_VOLUME)==TICK_FLAG_VOLUME); 
// 
   if((buy_tick== true) || (sell_tick == true)) 
   { 
     res = res + (buy_tick?StringFormat(" Buy Tick: Last=%G Volume=%d ",tick.last,tick.volume):""); 
     res = res + (sell_tick?StringFormat(" Sell Tick: Last=%G Volume=%d ",tick.last,tick.volume):""); 
     res = res + (ask_tick?StringFormat(" Ask=%G ",tick.ask):""); 
     res = res + (bid_tick?StringFormat(" Bid=%G ",tick.bid):""); 
   } 
   else 
   { 
     res = res + (ask_tick?StringFormat(" Ask=%G ",tick.ask):""); 
     res = res + (bid_tick?StringFormat(" Bid=%G ",tick.bid):""); 
     res = res + (last_tick?StringFormat(" Last=%G ",tick.last):""); 
     res = res + (volume_tick?StringFormat(" Volume=%d ",tick.volume):""); 
   } 
   return res; 
  } 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
  if(is_book == true) MarketBookRelease(Symbol());
}
//+------------------------------------------------------------------+
//| BookEvent function                                               |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol)
{
  if(symbol != Symbol()) return;
  tot_cnt++;
  if(SymbolInfoTick(Symbol(), s_tick) == true)
  {
    Print("SymbolInfoTick: ",GetTickDescription(s_tick));
  }
  if(is_first == true)
  {
    result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, last_time, 0); //копируем все вновь пришедшие тики от последнего известного времени
    if(result > 0)
    {
      t_cnt = 0;
      for(int i= 0; i<result; i++)
      {
        if(ticks[i].time_msc == ticks[0].time_msc) t_cnt++;             //Считаем кол-во тиков с одинаковым временем
        Print(__FUNCTION__, ": ",GetTickDescription(ticks[i]));
      }
    //  l_tick = ticks[0];
      is_first = false;
      last_time = ulong(ticks[0].time_msc);                             //Запоминаем время последнего тика
    } 
  }
  else
  {
    if(SymbolInfoTick(Symbol(), s_tick) == true)
    {
      Print("SymbolInfoTick: ",GetTickDescription(s_tick));
    }
    result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, last_time, 0); //забираем тики из последнего (посчитанного пакета тикив и считываем тики из нового пакета)
    if(result > 0)
    {
     // l_tick = ticks[0];
      if(result > t_cnt)
      {
        mem_cnt = t_cnt;
        t_cnt = 0;
        for(int i= 0; i<(result - int(mem_cnt)); i++)
        {
          if(ticks[i].time_msc == ticks[0].time_msc) t_cnt++;           //Считаем кол-во тиков с одинаковым временем
          Print(__FUNCTION__, ": ",GetTickDescription(ticks[i]));
        } 
        if(last_time == ulong(ticks[0].time_msc))
        {
          t_cnt += int(mem_cnt);
        }
        else last_time = ulong(ticks[0].time_msc);
      }
      else
      {
        Print(__FUNCTION__, ": Pending order!");                          //Изменения стакана (добавлен/удален отложенный ордер)
      }
    }
    else
    {
      Print(__FUNCTION__, ": Pending order!");                          //Изменения стакана (добавлен/удален отложенный ордер)
    }
  }
}
//+------------------------------------------------------------------+
 

There you have it, that's good.

I promised to tell you about another advantage of OnTick, compared to OnBook. It is because OnBook is guaranteed to be delivered, if it has even a little heavy calculations (or, God forbid, trades), it can accumulate a rather large queue of events that make no sense(they do not contain event-related information). This is where the advantage turns into a disadvantage, as it often does.

OnTick in this case will simply skip the events that came in at the time of calculation, and will trigger on the first new, relevant one. In OnBook variant, to free the queue (skip useless events), you have to make your own crutches.

 
Andrey Khatimlianskii:

There you have it, that's good.

I promised to tell you about another advantage of OnTick, compared to OnBook. It is because OnBook is guaranteed to be delivered, if it has even a little heavy calculations (or, God forbid, trades), it can accumulate a rather large queue of events that make no sense(they do not contain event-related information). This is where the advantage turns into a disadvantage, as it often does.

OnTick in this case will simply skip the events that came in at the time of calculation, and will trigger on the first new, relevant one. In OnBook's variant, to release the queue (skip useless events), you have to make your own crutches.

Personally, I use asynchronous orders in trading operations.

The thing is (if you seriously trade on the Exchange), you need all the changes in the market,

and the sooner this event comes - the better.

In addition, you need ask and bid volumes...

I, for myself, see no alternative to OnBook

 
Roman:

I don't use dealing forex.
Youonly get one last element of the structure in the example fromCopyTick, in fact only the best prices.

Running the above code, it turns out that

 if(SymbolInfoTick(Symbol(), s_tick) == true)
  {
    Print("SymbolInfoTick: ",GetTickDescription(s_tick));
  }
s_tick.flags = 0

:)

2020.02.03 19:28:14.656 Ticks_test (GOLD-3.20,M1)       OnTick: 2020.02.03 19:28:13.160 Bid=1581.9 
2020.02.03 19:28:14.656 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 19:28:13.160
2020.02.03 19:28:14.656 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 19:28:13.160
2020.02.03 19:28:14.656 Ticks_test (GOLD-3.20,M1)       OnBookEvent: 2020.02.03 19:28:13.160 Bid=1581.9 

Same tick and when callingSymbolInfoTick- no flag :)

Added

if(tick.flags == 0)
   {
     res = res + " ask = " + string(tick.ask) + "; bid = " + string(tick.bid);
   }

We get a tick but we don't know who "created" it.

2020.02.03 19:36:56.576 Ticks_test (GOLD-3.20,M1)       OnTick: 2020.02.03 19:36:54.735 Bid=1583 
2020.02.03 19:36:56.576 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 19:36:55.61 ask = 1583.3; bid = 1583.0
2020.02.03 19:36:56.576 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 19:36:55.61 ask = 1583.3; bid = 1583.0
2020.02.03 19:36:56.576 Ticks_test (GOLD-3.20,M1)       OnBookEvent: 2020.02.03 19:36:54.735 Bid=1583 

What's going on with tick times?

It looks absurd. We receive ticks from new packet with time19:36:54.735, butSymbolInfoTick got tick "ahead" by 900 ms.

FromSymbolInfoTick help

[out]  Ссылка на структуру типа MqlTick, в которую будут помещены текущие цены и время последнего обновления цен.
 

Beautiful topic, lots of interesting stuff, thanks guys.

No kidding and no banter...

Just don't tear down the code, I'll take it under consideration...

 
prostotrader:

What's going on with ticking time anyway?

It's absurd. We get ticks from new packet with time19:36:54.735, but by callingSymbolInfoTick we get tick "ahead" by 900 ms???

From Reference

Really strange, Bid value is the same for all, but SymbolInfoTick time is different.

 
Roman:

Really strange, the Bid value is the same for all, but the SymbolInfoTick time is different.

Looks like terminal arranges time "as it wants" :)

2020.02.03 21:56:44.409 Ticks_test (GOLD-3.20,M1)       OnTick: 2020.02.03 21:56:42.776 Sell Tick: Last=1582.9 Volume=2 
2020.02.03 21:56:48.122 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 21:56:42.780 ask = 1583.1; bid = 1582.9
2020.02.03 21:56:48.122 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 21:56:42.780 ask = 1583.1; bid = 1582.9
2020.02.03 21:56:48.122 Ticks_test (GOLD-3.20,M1)       OnBookEvent: 2020.02.03 21:56:42.776 Sell Tick: Last=1582.9 Volume=2 

Almost 4 seconds passed by local time!

That surely can't be!

And here everything is normal.

2020.02.03 21:57:02.294 Ticks_test (GOLD-3.20,M1)       OnTick: 2020.02.03 21:57:00.671 Sell Tick: Last=1582.9 Volume=5 
2020.02.03 21:57:02.294 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 21:57:00.671 Sell Tick: Last=1582.9 Volume=5 
2020.02.03 21:57:02.294 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick: 2020.02.03 21:57:00.671 Sell Tick: Last=1582.9 Volume=5 
2020.02.03 21:57:02.294 Ticks_test (GOLD-3.20,M1)       OnBookEvent: 2020.02.03 21:57:00.671 Sell Tick: Last=1582.9 Volume=5 
 
prostotrader:

Looks like the terminal is timing itself "as it pleases" :)

Almost 4 seconds have gone by local time!

That can't be right!


The log shows that OnTick caught the same tick 4 seconds faster than OnBookEvent

1  2020.02.03 21:56:44.409 Ticks_test (GOLD-3.20,M1)       OnTick:            2020.02.03 21:56:42.776 Sell Tick: Last=1582.9 Volume=2 
   2020.02.03 21:56:48.122 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick:    2020.02.03 21:56:42.780 ask = 1583.1; bid = 1582.9
   2020.02.03 21:56:48.122 Ticks_test (GOLD-3.20,M1)       SymbolInfoTick:    2020.02.03 21:56:42.780 ask = 1583.1; bid = 1582.9
2  2020.02.03 21:56:48.122 Ticks_test (GOLD-3.20,M1)       OnBookEvent:       2020.02.03 21:56:42.776 Sell Tick: Last=1582.9 Volume=2 
 
Yuriy Zaytsev:

According to the log it turns out that OnTick caught the same tick faster by 4 seconds than OnBookEvent

This definitely cannot be the case in reality,

because the ticks are already in the terminal (i.e. a new packet of ticks has already arrived)

 
prostotrader:

This definitely cannot be the case in reality,

because the ticks are already in the terminal (i.e. a new packet of ticks has already arrived)

But I look at the log, and the log one and the same tick with a difference of 4 seconds came.

p.s.

I really don't like the phrase "it can't be", i got used to the fact that anything can happen.

By the way, perhaps it is far from the subject, but once on the assertion that the earth is round, too, said something like this - "it can not be.

In general, I am always in doubt until I check and then double-check, and preferably someone else double-checks a few times.


Are you sure your code is not screwing up - which forms the log, processes the data?


this code produced miracles with a 4 second difference ?


//+------------------------------------------------------------------+
//|                                                   Ticks_test.mq5 |
//|                                      Copyright 2019 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019 prostotrader"
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
bool is_book;
MqlTick ticks[], s_tick;
ulong last_time, mem_cnt, tot_cnt;
bool is_first;
int t_cnt, result;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
  tot_cnt = 0;
  is_book = MarketBookAdd(Symbol());
  result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, 0, 1);
  if(result > 0)
  {
    last_time = ulong(ticks[0].time_msc); //запоминаем время последнего известного тика
    is_first = true;
  }
  else
  {
    is_first = false;
    Alert("No start time!");
    return(INIT_FAILED);
  } 
  ArraySetAsSeries(ticks, true);  
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+ 
//| возвращает строковое описание тика                               | 
//+------------------------------------------------------------------+ 
string GetTickDescription(MqlTick &tick) 
  { 
   string res = string(tick.time) + "." +  string(tick.time_msc%1000); 
// 
   bool buy_tick = ((tick.flags&TICK_FLAG_BUY)==TICK_FLAG_BUY); 
   bool sell_tick = ((tick.flags&TICK_FLAG_SELL)==TICK_FLAG_SELL); 
   bool ask_tick = ((tick.flags&TICK_FLAG_ASK)==TICK_FLAG_ASK); 
   bool bid_tick = ((tick.flags&TICK_FLAG_BID)==TICK_FLAG_BID); 
   bool last_tick = ((tick.flags&TICK_FLAG_LAST)==TICK_FLAG_LAST); 
   bool volume_tick = ((tick.flags&TICK_FLAG_VOLUME)==TICK_FLAG_VOLUME); 
// 
   if((buy_tick== true) || (sell_tick == true)) 
   { 
     res = res + (buy_tick?StringFormat(" Buy Tick: Last=%G Volume=%d ",tick.last,tick.volume):""); 
     res = res + (sell_tick?StringFormat(" Sell Tick: Last=%G Volume=%d ",tick.last,tick.volume):""); 
     res = res + (ask_tick?StringFormat(" Ask=%G ",tick.ask):""); 
     res = res + (bid_tick?StringFormat(" Bid=%G ",tick.bid):""); 
   } 
   else 
   { 
     res = res + (ask_tick?StringFormat(" Ask=%G ",tick.ask):""); 
     res = res + (bid_tick?StringFormat(" Bid=%G ",tick.bid):""); 
     res = res + (last_tick?StringFormat(" Last=%G ",tick.last):""); 
     res = res + (volume_tick?StringFormat(" Volume=%d ",tick.volume):""); 
   } 
   return res; 
  } 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
  if(is_book == true) MarketBookRelease(Symbol());
}
//+------------------------------------------------------------------+
//| BookEvent function                                               |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol)
{
  if(symbol != Symbol()) return;
  tot_cnt++;
  if(SymbolInfoTick(Symbol(), s_tick) == true)
  {
    Print("SymbolInfoTick: ",GetTickDescription(s_tick));
  }
  if(is_first == true)
  {
    result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, last_time, 0); //копируем все вновь пришедшие тики от последнего известного времени
    if(result > 0)
    {
      t_cnt = 0;
      for(int i= 0; i<result; i++)
      {
        if(ticks[i].time_msc == ticks[0].time_msc) t_cnt++;             //Считаем кол-во тиков с одинаковым временем
        Print(__FUNCTION__, ": ",GetTickDescription(ticks[i]));
      }
    //  l_tick = ticks[0];
      is_first = false;
      last_time = ulong(ticks[0].time_msc);                             //Запоминаем время последнего тика
    } 
  }
  else
  {
    if(SymbolInfoTick(Symbol(), s_tick) == true)
    {
      Print("SymbolInfoTick: ",GetTickDescription(s_tick));
    }
    result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, last_time, 0); //забираем тики из последнего (посчитанного пакета тикив и считываем тики из нового пакета)
    if(result > 0)
    {
     // l_tick = ticks[0];
      if(result > t_cnt)
      {
        mem_cnt = t_cnt;
        t_cnt = 0;
        for(int i= 0; i<(result - int(mem_cnt)); i++)
        {
          if(ticks[i].time_msc == ticks[0].time_msc) t_cnt++;           //Считаем кол-во тиков с одинаковым временем
          Print(__FUNCTION__, ": ",GetTickDescription(ticks[i]));
        } 
        if(last_time == ulong(ticks[0].time_msc))
        {
          t_cnt += int(mem_cnt);
        }
        else last_time = ulong(ticks[0].time_msc);
      }
      else
      {
        Print(__FUNCTION__, ": Pending order!");                          //Изменения стакана (добавлен/удален отложенный ордер)
      }
    }
    else
    {
      Print(__FUNCTION__, ": Pending order!");                          //Изменения стакана (добавлен/удален отложенный ордер)
    }
  }
}
//+------------------------------------------------------------------+