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