How to calculate spread from ticks

 

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?

 
goocreations:

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?
 
goocreations:
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?
As you've already got the EA programmed, can you try replacing the onTick with a millisecond timer and check the results?
 
Nope. Also different results between MqlRates and SymbolInfoInteger using a 1ms or 1s timer.
 
goocreations:
Nope. Also different results between MqlRates and SymbolInfoInteger using a 1ms or 1s timer.
Hmmm, curious. Will have to look into that some more. Are you seeing one change more frequently than the other?
 

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.

 
goocreations:

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.

Yeah, that's what I meant. Looks like MqlRates is grabbing the rate at the start of the bar where as the Symbol function is changing every tick (or so) as it should. I'd never looked into that before and doesn't look like there's much documentation on it. I'd suggest sending this one to Service Desk mate
 
Bug reported.
 
goocreations:
Bug reported.
Thanks mate. Please report back on your results.
Reason: