Download MetaTrader 5

Spread Average

To add comments, please log in or register
raven.chrono
324
raven.chrono  

Hi guys... I would like to ask if the formula that i used to compute the average of spread per tick is:

Average=(Ask-Bid)/Tick

where:

Ask=Ask

Bid=Bid

Tick=value per tick or Tick Count


if you have another formula it would be a big help for me...


thank you very much

chapmann
45
chapmann  

in my opinion, you can try to write this:

then you can caculate the average of spread per tick since the first tick count.

// Global variable//--------------------------------------------------------------------

int Tick;         
int sum;         

//start//----------------

int start()                            
  {   
    sum = (Ask + Bid) + sum;
    Tick++;                             
    double Average = 0;
    Average = sum/Tick;
    Print(Average);
    return;
   }
Simon Gniadkowski
Moderator
18058
Simon Gniadkowski  
raven_chrono:

Hi guys... I would like to ask if the formula that i used to compute the average of spread per tick is:

Average=(Ask-Bid)/Tick

where:

Ask=Ask

Bid=Bid

Tick=value per tick or Tick Count

You can code a Spread MA
raven.chrono
324
raven.chrono  
RaptorUK:
You can code a Spread MA


Can you show me some sample algorithm or pseudo code?


tnx...

Simon Gniadkowski
Moderator
18058
Simon Gniadkowski  
raven_chrono:

Can you show me some sample algorithm or pseudo code?

I can do better than that . . . .

// globally declared variables for function Spread()

extern int SpreadSampleSize = 100;  // number of ticks to average spread over  set to 0 to disable

double Spread[0];                   // array for spread MA



//+-----------------------------------------------------+
//| Spread function - used to calculate SMA of Spread   |
//+-----------------------------------------------------+
double Spread(double AddValue=0)
   {
   double LastValue;
   static double ArrayTotal=0;
   
   if (AddValue == 0 && SpreadSampleSize <= 0) return(Ask-Bid);
   if (AddValue == 0 && ArrayTotal == 0) return(Ask-Bid);
   if (AddValue == 0 ) return(ArrayTotal/ArraySize(Spread));
   
   ArrayTotal = ArrayTotal + AddValue;
   ArraySetAsSeries(Spread, true); 
   if (ArraySize(Spread) == SpreadSampleSize)
      {
      LastValue = Spread[0];
      ArrayTotal = ArrayTotal - LastValue;
      ArraySetAsSeries(Spread, false);
      ArrayResize(Spread, ArraySize(Spread)-1 );
      ArraySetAsSeries(Spread, true);
      ArrayResize(Spread, ArraySize(Spread)+1 ); 
      }
   else ArrayResize(Spread, ArraySize(Spread)+1 ); 
//   Print("ArraySize = ",ArraySize(lSpread)," AddedNo. = ",AddValue);
   ArraySetAsSeries(Spread, false);
   Spread[0] = AddValue;
   return(NormalizeDouble(ArrayTotal/ArraySize(Spread), Digits));
   }  // end of Spread
//+--------------------------------------------------------+

 To use it simply call it at the start of the start() function and pass it the spread . . .

Spread(Ask-Bid);

Then when you want the averaged spread simply call the function with no parameter . . .

Print("The averaged spread is ", Spread() );
raven.chrono
324
raven.chrono  
RaptorUK:

I can do better than that . . . .

 To use it simply call it at the start of the start() function and pass it the spread . . .

Then when you want the averaged spread simply call the function with no parameter . . .



Thank you so much but one other thing... which is more reliable to use, the Ask-Bid or the MarketInfo(Symbol(),MODE_SPREAD)
Simon Gniadkowski
Moderator
18058
Simon Gniadkowski  
raven_chrono:

Thank you so much but one other thing... which is more reliable to use, the Ask-Bid or the MarketInfo(Symbol(),MODE_SPREAD)
At the start of the start() function they are as reliable as each other.  Ask and Bid can become out of date if your start() function takes so long to run that it misses ticks,  this is not an issue at the start of the start function.
raven.chrono
324
raven.chrono  
double spread,average,sum;
int ticks;
int start()
  {
        while(IsStopped()==false)
        {
                while(RefreshRates()==false)
                {
                        
                }
                ticks++;
                spread=MarketInfo(Symbol(),MODE_SPREAD);
                sum=sum+spread;
                average=sum/ticks;
                                
        }       

   return(0);
  }
does this mean that the code that i created above is not reliable?
Simon Gniadkowski
Moderator
18058
Simon Gniadkowski  
raven_chrono:
does this mean that the code that i created above is not reliable?
You aren't using any of the Predefined Variables so there is no need for RefreshRatws()  your code will work and will give an avreage not a moving average.
raven.chrono
324
raven.chrono  
RaptorUK:
You aren't using any of the Predefined Variables so there is no need for RefreshRatws()  your code will work and will give an avreage not a moving average.



double spread,average,sum;
int ticks;
int start()
  {
        while(IsStopped()==false)
        {
                while(RefreshRates()==false)
                {
                        
                }
                ticks++;
                spread=Ask-Bid;
                sum=sum+spread;
                average=sum/ticks;
                                
        }       

   return(0);
  }

Now i use the predefined variables Bid and Ask...
Simon Gniadkowski
Moderator
18058
Simon Gniadkowski  
raven_chrono:
Now i use the predefined variables Bid and Ask...

But you still don't need to use RefreshRates(),  Bid and Ask are never going to be out of date in that code.
12
To add comments, please log in or register