Which symbols OnTick function updates

 

Hi guys

Which symbols OnTick function updates, only current one or all symbols in market watch.

If any symbol is not in MarketWatch, its Bid or Ask can be fetched on tick?

 
Narek Kamalyan: Which symbols OnTick function updates, only current one or all symbols in market watch. If any symbol is not in MarketWatch, its Bid or Ask can be fetched on tick?

OnTick does not update anything. It is an "event"! the function is called when a new tick arrives for the symbol of the chart the EA is attached to. While the function is being processed no further "new tick" events are reacted to until the function returns.

EDIT: From within the OnTick function you can get the current bid/ask prices from any symbol on the MarketWatch. On MT5 only, you can also fetch tick data from the tick history.

 
If a symbol in the market watch (not the current symbol) changes the price, it generates on tick event?
In other word it I want to make multi currency ea, can I put my loop in one symbol and monitor all symbols 
 
Narek Kamalyan: If a symbol in the market watch (not the current symbol) changes the price, it generates on tick event? In other word it I want to make multi currency ea, can I put my loop in one symbol and monitor all symbols 

OnTick events only take place for the EA attached to a chart with that symbol. If no EA is attach to such a chart symbol, then no even is generated!

In order to "simulate" OnTick events for multiple symbols, from a single EA, you will have to write your own handler, but there is a CodeBase entry that does that already. I don't know how well it works, but you can analyse the code and see if it suits your needs.

Multicurrency OnTick (string symbol) event handler
Multicurrency OnTick (string symbol) event handler
  • www.mql5.com
This is the simplified implementation of multicurrency mode in MetaTrader 5. It isn't necessary to consider the details how does it work. It has minimum settings and simple structure. It can be used in Strategy Tester.
 
Fernando Carreiro:

OnTick events only take place for the EA attached to a chart with that symbol. 

What you think, if I create an array along the code which will contain symbols of interest (theoretically can contain all MarketWatch) and check their bid and ask OnTimer each 200ms, then call OnTick() if any one of them is changed. Is it going to eat much resources? In other words, is so “non optimal” solution?
 
Narek Kamalyan:
What you think, if I create an array along the code which will contain symbols of interest (theoretically can contain all MarketWatch) and check their bid and ask OnTimer each 200ms, then call OnTick() if any one of them is changed. Is it going to eat much resources? In other words, is so “non optimal” solution?
Did you not even look at the code of the publication in the CodeBase that I linked for you?
 
Fernando Carreiro:
Did you not even look at the code of the publication in the CodeBase that I linked for you?
I have seen it but it uses spy indicator which must be attached to each chart. 
I am making trading panel for semi automated trading and having indicator attached to each symbol chart in market may not be suitable for me: 
 
Narek Kamalyan:
I have seen it but it uses spy indicator which must be attached to each chart. 
I am making trading panel for semi automated trading and having indicator attached to each symbol chart in market may not be suitable for me: 

OK, then use the OnTimer method and experiment for yourself to see if it is resource intensive or not!

There is a reason why they call it Research and Development, because we take the time to actually do some research and testing in order to develop things.

 
Fernando Carreiro:

OK, then use the OnTimer method and experiment for yourself to see if it is resource intensive or not!

Actually I did it already and it works. 
I will measure GetTickCountMillisecond to see how much time as adding per each additional symbol
Maybe later will post the result. 
 
Fernando Carreiro:

I have created GetAllSymbolTicks() function, which runs OnTimer() each 200ms and check whether any tick arrived in any symbol of interest, those symbols are puted into TickArray. Here is the function:

void GetAllSymbolTicks()
  {
   int list_size = ArraySize(TickArray);
   for(int i=0; i<list_size; i++)
     {
      string symbol_name = TickArray[i].Pair;
      double bid_price = SymbolInfoDouble(symbol_name,    SYMBOL_BID);
      double ask_price = SymbolInfoDouble(symbol_name,    SYMBOL_ASK);
      if(bid_price!=TickArray[i].BIDPrice || ask_price!=TickArray[i].ASKPrice)
        {
         //Print("New Tick: " + symbol_name + ", bid_price: "  + bid_price + ", ask_price: " + ask_price);
         TickArray[i].Set(symbol_name,bid_price,ask_price);
         OnTick();
         break;
        }
     }
  }

Then I have placed GetTickCount before and after function.

Initial experiment shows 16ms for each run of GetAllSymbolTicks(), regardless of the number of monitored symbols, if we use it with break, means once any tick found, OnTick() will be called and exit the loop. In this experiment 16ms includes the time to run OnTick(). Later I checked without OnTick(), with 40 monitored symbols the time to run this function was 0ms. 

If we want to differentiate each Tick from which symbol came, then we can use without break, but in this case timing will be different, you might think. No, it was again 0ms for 40 symbols. 

To display the results I was using label object instead of print. 

Therefore, this pathway theoretically have following advantages:

  • It is taking less than a millisecond and is quite recource effective. 
  • Different accuracies can be choosen for different type of EAs, i.e. can be run each 20ms lets say. 
  • Unlike Multicurrency OnTick (string symbol) event handler, doesnt require indicator to be runing on each chart.
 
Narek Kamalyan:

I have created GetAllSymbolTicks() function, which runs OnTimer() each 200ms and check whether any tick arrived in any symbol of interest, those symbols are puted into TickArray. Here is the function:

Then I have placed GetTickCount before and after function.

Initial experiment shows 16ms for each run of GetAllSymbolTicks(), regardless of the number of monitored symbols, if we use it with break, means once any tick found, OnTick() will be called and exit the loop. In this experiment 16ms includes the time to run OnTick(). Later I checked without OnTick(), with 40 monitored symbols the time to run this function was 0ms. 

If we want to differentiate each Tick from which symbol came, then we can use without break, but in this case timing will be different, you might think. No, it was again 0ms for 40 symbols. 

To display the results I was using label object instead of print. 

Therefore, this pathaw theoretically have following advantages:

  • It is taking less than a millisecond and is quite recource effective. 
  • Different accuracies can be choosen for different type of EAs, i.e. can be run each 20ms lets say. 
  • Unlike Multicurrency OnTick (string symbol) event handler, doesnt require indicator to be runing on each chart.

You should not be calling OnTick() directly from another function. That is an event handler that is called by MetaTrader. I suspect that you would block it from being called by the event manager itself if they both got triggered at the same time, although I have never tested it. Instead, create your own separate Tick Handling function!