Doing math with MODE_POINT

 

I do realize I am doing something stupid, but I'm not quite sure of what and why.

I want to get the average size of candles. So I take say, the last 500 candles and measure their bodies with

MathAbs(Open[i] - Close[i]);

I add them all up. Then I divide that sum by 500.

Additionally, I calculate what percentage the current spread represents in relation to that average:

double result  = (((Ask - Bid) * 100) / Average);

So the current spread is, for example, 46% of the average size of the 500 last candles.

It works for some symbols, but it's a mess with certain symbols such as EURUSD. I can generate a report of ALL the symbols and it seems that all symbols with MODE_POINT 0.00001 get all messed up.

So, what is the correct way of calculating that?

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

It seems you are not show all the relevant code.

If your average is really "Sum(|close-open|)/Count", then it is in the same units as "(Ask - Bid)" and there should be no difficulties at all, as the point size is not relevant.

So, there must something else in your calculations that are affecting the results.

You will need to show all the relevant code that reproduces the issue.

 

I am sorry. I posted two lines of code but didn't expect the discussion to go into code, I thought it involved a much more conceptual and generic error on my part.

Very well, this is my function:

double getSpread(string symbol)  {
   double BarBodySizeSum = 0;
   for(int i=1;i<500;i++)  {
      double BarBodySize = MathAbs(iOpen(symbol, NULL, i) - iClose(symbol, NULL, i));
      BarBodySizeSum = BarBodySizeSum + BarBodySize;
   }
   double Average = (BarBodySizeSum / 500);
   double result  = (((Ask - Bid) * 100) / Average);
   return NormalizeDouble(result, 2);
}


So like I said, I expect to get how much % of the average candle body size the current spread is. And these are some of the results:

EURAUD        1251340.72
EURCAD        1523063.53
EURCHF        2670736.36
SPN35         75.17
US30          32.52


75.17% and 32.52% make sense, but 1251340.72 % doesn't make any sense at all.

 
whoowl #:

I am sorry. I posted two lines of code but didn't expect the discussion to go into code, I thought it involved a much more conceptual and generic error on my part.

Very well, this is my function:

So like I said, I expect to get how much % of the average candle body size the current spread is. And these are some of the results:

75.17% and 32.52% make sense, but 1251340.72 % doesn't make any sense at all.

  1. Please print out the individual values for "BarBodySizeSum", "Average", "Ask", "Bid" and the "result".
  2. Don't hard code the "count" as 500. Use a variable and make sure that the iBars count is checked.
  3. Add error checking to your code, as iClose or iOpen may be throwing an error.
Bars - Predefined Variables - MQL4 Reference
Bars - Predefined Variables - MQL4 Reference
  • docs.mql4.com
Bars - Predefined Variables - MQL4 Reference
 

OK, Mr. Miyagi. I suspect you took me for a ride here, but that's good because it worked.

In my effort to rewrite the code according to your more demanding specs, I realized that my code was wrong because I was calling Ask and Bid for all the pairs (thus applying the Ask and Bid values of one single pair to all pairs), when I should really be using MarketInfo(symbol, MODE_ASK) and MarketInfo(symbol, MODE_BID), respectively.

After I did that one change, I got much more reasonable results for all pairs.

It's not the stupid mistake I thought I was doing, but it was still a dumb one indeed. It's fixed now.

Thank you again.