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; }
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
Can you show me some sample algorithm or pseudo code?
tnx...
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() );
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)
Thank you so much but one other thing... which is more reliable to use, the Ask-Bid or the MarketInfo(Symbol(),MODE_SPREAD)
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?
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.
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...
Now i use the predefined variables Bid and Ask...

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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