How is 'Volume' calculated?

 

In the process of building an EA, I got a little confused with this question.

As I understood it (and please correct me if I'm wrong), 'Volume' is basically the amount of price changes (i.e. incoming ticks) for every bar.

One thing I've noticed is that often times, one tick would come in, but volume would jump by 3-8 pts. Which seems to artificially inflate the volume.

In order to test this more accurately, I made an Array and populated it with Time/Bid/Ask data for ever incoming tick.

When I do my calculations, there is always a mismatch between the internal 'Volume' parameter and between the volume of ticks inside my array, with the internal 'Volume' parameter always being much larger.

How does that make any sense?

Thank you!! :)

 

can you show the code of the array filling function?

 

Sure:

int UpdateTickData() { // last index is most recent tick
   ArrayResize(TickData, ArrayRange(TickData,0)+1);
   int arIndex = ArrayRange(TickData,0)-1;

      TickData[arIndex][0] = MarketInfo(Symbol(),MODE_TIME); // Tick Time
      TickData[arIndex][1] = Bid; // Bid Price
      TickData[arIndex][2] = Ask; // Ask Price

   return(0);
}

The function call is placed in 'init()' and 'start()'

 

In my calculations I do a very simple thing:

When new bar Opens, the 'TickData' array is reset to 0, so it starts the count of incoming ticks from the beginning for each new bar.

Then I simply compare in real time the internal 'Volume' series array for 0 bar, to the number of Array elements (i.e. ticks) in my 'TickData' array.

In theory this should match internal 'Volume' count exactly!

However, the result is always extremely skewed! Internal 'Volume' series array is always MUCH larger, and you can also see this first hand by looking at the volume changes in real time.

The internal volume parameter often times just jumps in large increments of up to 8 or more, while my Array always increments by +1 with each new tick.

I mean even if internal volume counts simultaneous Ask/Bid changes as 2 ticks, then the maximum increment it would make is 2! But that still doesn't explain how it can increment by 8+ from one single tick!!!

So what am I missing here?

 

Here is the complete code of my simple EA:

//+------------------------------------------------------------------+
//|                                                 VolumeTester.mq4 |
//|                                  Copyright © 2012, nanquan       |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, nanquan"
#property link      ""

double TickData[][3];
datetime zeroBar;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   UpdateTickData();
   zeroBar = Time[0];
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      if (zeroBar!=Time[0]){
         zeroBar=Time[0];
         Alert("New Bar Open Time: ", TimeToStr(Time[0],TIME_MINUTES));
         // Reset TickData array:
         ArrayInitialize(TickData,0); // delete array values
         ArrayResize(TickData,0); // resize to zero
      }
      UpdateTickData();
      Comment("VolumeArray: ",TickSpeedArrayVolume()," Volume Internal: ", Volume[0]);
//----
   return(0);
  }
//+------------------------------------------------------------------+


// Update tick info array with new incoming tick information
int UpdateTickData() { // last index is most recent tick
   ArrayResize(TickData, ArrayRange(TickData,0)+1);
   int arIndex = ArrayRange(TickData,0)-1;

      TickData[arIndex][0] = MarketInfo(Symbol(),MODE_TIME); // Tick Time
      TickData[arIndex][1] = Bid; // Bid Price
      TickData[arIndex][2] = Ask; // Ask Price

   return(0);
}

int TickSpeedArrayVolume() {
   int volume = ArrayRange(TickData,0);
   return(volume);   
}

Try it out for yourself!

Files:
 
nanquan:

[...] So what am I missing here?

See https://www.mql5.com/en/forum/125732. You are going to lose ticks because (a) a new tick is received while your EA is already in the middle of start(), and therefore your EA is too busy to log the tick, and also (b) because MT4 seems to drop ticks above a certain volume. This will partly be influenced by (c): the quality and speed of your internet connection, and the latency to the broker.

With your example code I get the same results referred to in the previous topic: the EA is missing somewhere between 5% and 10% of ticks. Much better than the results you are seeing, but still some missed ticks.

 

Thank you for taking the time to answer my questions, jjc!

With regards to your answers:

(a) From what I've read in the MQL4 book, the execution time of start() is somewhere between 10-100 ms, depending on the complexity of the EA. Since in this example the EA is extremely simple, I think it's safe to assume it's execution time will be a lot closer to 10ms than to 100ms. So are you saying that inside this time-frame of say ~10ms the EA can miss up to 8 whole ticks???

(b) If MT4 drops ticks due to internet connection problems, how is it then that the Volume parameter still counts the 'dropped' ticks? i.e. I can see in real time the price changing just ONCE (i.e. one tick as far as MT4 is concerned), and yet internal Volume counter registers 8 ticks! So if the ticks were dropped, how does Volume "know" about them?

(c) I tested my Internet connection to my broker following the instructions here (my broker is FXCM). My results were as follows:

Ping statistics for 204.8.240.1:
Packets: Sent = 15, Received = 15, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 141ms, Maximum = 155ms, Average = 144ms

Are these results considered good?

Thanks!!

 

Just to clarify question (b), I guess what I mean to ask is, does 'Volume' get it's information straight from the server, or is it a local internal process of MT4?

If it gets it's information directly from the server then that explains the 'jumps' in volume, where MT4 has missed several ticks, but on the next tick that it received it also got an update from the server as to how many ticks it missed.

If it's an internal local process of MT4 however (i.e. a simple counter), then it doesn't make sense at all, since how can it know how many ticks it missed if it missed them...

Would appreciate a clarification on that!

 
nanquan:

Just to clarify question (b), I guess what I mean to ask is, does 'Volume' get it's information straight from the server, or is it a local internal process of MT4? [...]

See https://www.mql5.com/en/forum/119543: the evidence appears to be that the answer to your question is a combination of both. The broker periodically sends out a volume count, but MT4 can also update the last-known volume between times.

8 ticks is a very large number to go missing as a single group. Potential explanations are that the instance of MT4 is internally very busy, e.g. because it is running other complex EAs or has lots of symbols in its market watch.
 
Thank you again, jjc! I got what I needed :)
Reason: