Download MetaTrader 5

MqlTick price.bid/ask vs SymbolInfoDouble(_Symbol,SYMBOL_BID/ASK) vs anything else

To add comments, please log in or register
Laurentiu Sorin Tene
703
Laurentiu Sorin Tene  

I try to code an EA and I wonder what is the best code regarding fast retrieval/obtain of prices.

Please respond with argues.

All opinions are very much appreciated ! 

Rodrigo Malacarne
Moderator
10179
Rodrigo Malacarne  
tenlau:

I try to code an EA and I wonder what is the best code regarding fast retrieval/obtain of prices.

Please respond with argues.

All opinions are very much appreciated ! 

When you use the MqlTick struct you call five pieces of information at the same time (time, bid, ask, last and volume)... so instead of using this whole piece of code

datetime _time    = SymbolInfoInteger(_Symbol,SYMBOL_TIME);
double   _bid     = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double   _ask     = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double   _last    = SymbolInfoDouble(_Symbol,SYMBOL_LAST);
ulong    _volume  = SymbolInfoInteger(_Symbol,SYMBOL_VOLUME);

 you might simply use

MqlTick last_tick;

It's shorter to code and in some tests we've done in the Portuguese forum it's also faster to retrieve information.

Laurentiu Sorin Tene
703
Laurentiu Sorin Tene  
Malacarne:

When you use the MqlTick struct you call five pieces of information at the same time (time, bid, ask, last and volume)... so instead of using this whole piece of code

 you might simply use

It's shorter to code and in some tests we've done in the Portuguese forum it's also faster to retrieve information. 

OK, but if let say I need only ask, or only bid price. IMHO to code like this:

if (SymbolInfoDouble(_Symbol,SYMBOL_ASK) < price)
 some code

is faster than

MqlTick last_tick;
if(last_tick.ask < price)
 some code

 In the first case I ask for less information, so I retrieve the answer faster. Am I wright?

Alain Verleyen
Moderator
32409
Alain Verleyen  
tenlau:

OK, but if let say I need only ask, or only bid price. IMHO to code like this:

is faster than

 In the first case I ask for less information, so I retrieve the answer faster. Am I wright?

Why not test it yourself ?

I create a little test program, and I used the profiler to see what option is quicker.

input int TestingTime=300;             //-- Profiling time for each option

bool TestSymbolInfoTick=true;          //-- Test SymbolInfoTick if true, SymbolInfoouble if false
MqlTick tick;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   EventSetTimer(TestingTime);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double ask;

   if(TestSymbolInfoTick)
     {
      if(SymbolInfoTick(_Symbol,tick))
        {
         ask=tick.ask;
        }
     }
   else
     {
      ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
     }

  }
//+------------------------------------------------------------------+
//| Expert ontimer function                                          |
//+------------------------------------------------------------------+
void OnTimer()
  {  
   TestSymbolInfoTick=!TestSymbolInfoTick;
   if(TestSymbolInfoTick) ExpertRemove();
  }
//+------------------------------------------------------------------+

and I run this code (started with profiling) on a EURUSD, M5 chart. Here is the result :


SymbolInfoDouble was called 868 times and takes 8498 µs to execute. It's an average of 9.79 µs

SymbolInfoTick was called 936 and takes 2450 µs +128 µs for assigning ask (not shown in the picture). It's an average of 2.75 µs.

Is it enough argues ?

Laurentiu Sorin Tene
703
Laurentiu Sorin Tene  
angevoyageur:

Why not test it yourself ?

I create a little test program, and I used the profiler to see what option is quicker.

and I run this code (started with profiling) on a EURUSD, M5 chart. Here is the result :

SymbolInfoDouble was called 868 times and takes 8498 µs to execute. It's an average of 9.79 µs

SymbolInfoTick was called 936 and takes 2450 µs +128 µs for assigning ask (not shown in the picture). It's an average of 2.75 µs.

Is it enough argues ?

Thanks ! I didn't know how to test. Now with your explanations the answer to my question is obvious ! 
To add comments, please log in or register