I was wondering how MQL calculates the spread based on ticks. If you have a rate consisting of N ticks, how are the bid and ask prices of all ticks combined to calculate the spread?
I've tried calculating the spread for every tick and then take the average spread over all ticks. I also tried calculating the spread of a rate as the difference between highest bid price and the lowest ask price.
In both cases I got different results to the spreads in MqlRates. Does anyone know how to calculate the spread correclty?
Not sure if I'm understanding your question. The spread is set by your broker on a tick by tick basis, not calculated by MetaTrader. You can get it from:
SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)
Or as you mentioned above, the rates struct. Or were you asking for something else??
Cheers
Hmmm. I thought that if the broker does not offer fixed spreads, the spreads are calculated "on demand" from the ticks ask/bid prices. Thanks for clearing that up.
I now compared the output of the SymbolInfoInteger on every tick with the spread of an MqlRates and they differ. If you run this in an EA, you will noticed that the spreads are different. Why does this happen? Here is the example:
void OnTick() { MqlRates r[]; CopyRates(Symbol(), Period(), TimeCurrent(), 1, r); Print("SPREAD: ", r[0].spread, " ", SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)); }
It seems that MQL does not adjust the spread on every tick. Why isn't the spread of the current (in progress) rate adjusted, like the high, low and close prices?
What do you mean by: "Are you seeing one change more frequently than the other?".
The spread of MqlRates seems to be fixed the moment the rate is created. Hence, if there is a rate from 09h00-10h00, the moment the rate is created (that is at 09h00), the spread is fixed and never changes between 09h00 and 10h00.
If you use SymbolInfoInteger, the rate typically changes between 1 and 2 points every tick. Sometimes the SymbolInfoInteger spread is constant over a few ticks, but it mostly changes.
What do you mean by: "Are you seeing one change more frequently than the other?".
The spread of MqlRates seems to be fixed the moment the rate is created. Hence, if there is a rate from 09h00-10h00, the moment the rate is created (that is at 09h00), the spread is fixed and never changes between 09h00 and 10h00.
If you use SymbolInfoInteger, the rate typically changes between 1 and 2 points every tick. Sometimes the SymbolInfoInteger spread is constant over a few ticks, but it mostly changes.
Bug reported.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I was wondering how MQL calculates the spread based on ticks. If you have a rate consisting of N ticks, how are the bid and ask prices of all ticks combined to calculate the spread?
I've tried calculating the spread for every tick and then take the average spread over all ticks. I also tried calculating the spread of a rate as the difference between highest bid price and the lowest ask price.
In both cases I got different results to the spreads in MqlRates. Does anyone know how to calculate the spread correclty?