Tiki in real time - page 5

 
Aleksey Mavrin:

Can't ticks have more than one flag at a time?

You're right, I did.

//+------------------------------------------------------------------+
//|                                                   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[];
ulong last_time, mem_cnt;
bool is_first;
int t_cnt, result;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
  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);
  }   
  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.ask):""); 
   } 
   else 
   { 
     res = res + (ask_tick?StringFormat(" Ask=%G ",tick.ask):""); 
     res = res + (bid_tick?StringFormat(" Bid=%G ",tick.ask):""); 
     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 OnTick()
void OnBookEvent(const string &symbol)
{
  if(Symbol() == symbol)
  {
    if(is_first == true)
    {
      result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, last_time, 0);
      if(result > 0)
      {
        Print("First packet of ticks:");
        t_cnt = 0;
        for(int i= 0; i<result; i++)
        {
          if(ticks[i].time_msc == ticks[0].time_msc) t_cnt++;
          Print(GetTickDescription(ticks[i]));
        }
        is_first = false;
        last_time = ulong(ticks[0].time_msc);
      } 
    }
    else
    {
      result = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, last_time, 0);
      if(result > 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(GetTickDescription(ticks[i]));
          } 
          if(last_time == ulong(ticks[0].time_msc))
          {
            t_cnt += int(mem_cnt);
          }
          else last_time = ulong(ticks[0].time_msc + 1);
        }
        else
        {
          t_cnt = 0;
          last_time++;
        }
      }
      else
      {
        t_cnt = 0;
        last_time++;
      }
    }
  }
}
//+------------------------------------------------------------------+
I have shown the principle of the all ticks collector and you can use this code as you like (extracting all flags)
 
Aleksey Mavrin:

But does that mean there is a guarantee that you will handle ALL OnBookEvent events?

Means

 
prostotrader:

Repeat

OnBookEvent() is exactly the kind of guarantee that a new batch of ticks has arrived!

You have a stock tick.

OnTick exactly means that a tick has come, and you can get the whole package with CopyTicks.

Moreover, you can get them from anywhere (even from chart event). OnBookEvent has its own purpose and you don't have to poke it wherever you want or don't want.


prostotrader:

From reference:

Issuance rate: The terminal stores for each character 4096 last ticks in the cache for quick access (for characterswith the stack running, 65536 ticks), queries to this data arethe fastest.

End of quote----

If you can't get enough 4K last ticks, then by all means subscribe to the stack. But I can't imagine where this would be required (other than the first run).

 
prostotrader:

Means

Yes, and you don't have to collect a queue of these events and process them all at all if you only want a tick history.

 
Andrey Khatimlianskii:

You have a stock tick.

OnTick exactly means that a tick has arrived and you can get the whole package with CopyTicks.

What's more, you can get them from anywhere (even from chart event). OnBookEvent has its own purpose and you don't have to poke it wherever you want or don't want.


If you don't have enough 4K last ticks, then of course subscribe to the tumblr. But I can't imagine where it might be needed (other than the first run).

Andrew!

The topic was called "real time ticks".

No need to point out 100 times that ticks can be received by the OnTick() event on a timer or whatever.

 
prostotrader:

Andrei!

The topic was called "Real time ticks".

No need to point out 100 times that you can get ticks by OnTick() event on a timer or whatever.

Exactly! And OnBookEvent has the same relevance to the topic as OnTimer

 
Andrey Khatimlianskii:

Exactly! And OnBookEvent has as much to do with the subject as OnTimer

Run this year on a stock instrument

//+------------------------------------------------------------------+
//|                                                   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;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    is_book = MarketBookAdd(Symbol());
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
    if(is_book == true) MarketBookRelease(Symbol());
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    Print(__FUNCTION__ + " Tick is done!");
   
  }
//+------------------------------------------------------------------+
//| BookEvent function                                               |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol)
  {
    if(Symbol() == symbol)
    {
      Print(__FUNCTION__ + " Tick is done!");
    }
   
  }
//+------------------------------------------------------------------+
 
prostotrader:

Launch this year on a stock instrument

Thanks, I know how it works.

Just imagine for a second that there might be a different approach to trading than yours.

 
Andrey Khatimlianskii:

Thank you, I know how it works.

Just imagine for a second that there might be a different approach to trading than yours.

Andrei(with all due respect).

Why are you "messing around"?

Or don't you know the difference between getting it in real time or getting it from history?

And what has that got to do with the other approach to trading?

Everyone trades and writes robots as they see fit!

Added by

Don't like my version of getting ticks in real time?

Then post your version!

Then let's see a different approach...

 
prostotrader:

Andrei(all due respect)

Why are you "messing around"?

Or don't you know the difference between getting it in real time or getting it from history?

And what has that got to do with the other approach to trading?

Everyone trades and writes robots as they see fit!

Added by

Don't like my version of getting ticks in real time?

Then post your version!

Then let's see a different approach...

Replace OnBookEvent with OnTick in your code, and compare the result.