Average Spread

 

Hello, I am trying to obtain the average spread, for which I am doing the following:

void OnTick()

{ 

         AverageSpread();
}



double AverageSpread(){

   NumSpreads++;

   Spread=Spread+MarketInfo(_Symbol, MODE_SPREAD);

   return (NormalizeDouble((Spread/NumSpreads),2));

}

I wanted to know if this overloads, if it is the correct and efficient way to do it? or else you shouldn't do it that way. Thank you

 
Patricio: Hello, I am trying to obtain the average spread, for which I am doing the following: I wanted to know if this overloads, if it is the correct and efficient way to do it? or else you shouldn't do it that way. Thank you

I suggest you use a moving average. The way you are doing it, the sum of spread will just get larger on every read and at some point it could overflow, and its accuracy with the division will have more round off error as both numerator and divisor grows.

I would suggest using an EMA, because it is can be calculated incrementally very efficiently, and gives more weight to the more recent data.

 
Patricio: I wanted to know if this overloads,
Don't worry about performance, until you have a problem. Then, and only then, use the profiler, find the problem, fix the problem, and remeasure.
 

Here is a PDF paper that describe the maths and the implementation ... Incremental calculation of weighted mean and variance



 
What is the average spread a beginner trader should look for in a broker or in a currency pair?
 
Martial-Mason #:
What is the average spread a beginner trader should look for in a broker or in a currency pair?

Time-weighted average spread. On this basis, this spread indicator is made.

Reason: