Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MT4] MA computation error #20

Open
cgava opened this issue Apr 18, 2024 · 4 comments
Open

[MT4] MA computation error #20

cgava opened this issue Apr 18, 2024 · 4 comments
Assignees

Comments

@cgava
Copy link

cgava commented Apr 18, 2024

The MA computed by EA31337 Indi_MA, used in MA.mq5 in EA31337-Indicators/Common/Price has slight difference compared with the MA provided by MetaTrader (Either builtin "Moving Average" indicator or "Custom Moving Average.mq4" example).
Both MA use same period and close value
I cannot figure out the reason of this difference.

  IndiMAParams _indi_params(::InpMAPeriod, ::InpMAShift, ::InpMAMethod,
                            ::InpMAAppliedPrice, ::InpShift);
  indi = new Indi_MA(_indi_params /* , InpSourceType */);

image

Configuration statement

MT4 4.0 build 1415
The issue is visible on both:
Classes: v2.013.1                   /Common Indicator: v0.013
Classes: v3.001-dev-new       /Common Indicator: dev (19a01e6)

The cloning of EA31337 repo in MQL4 folder is:

[submodule "MQL4/Include/classes"]
	path = MQL4/Include/classes
	url = https://github.com/EA31337/EA31337-classes
[submodule "MQL4/Indicators/EA31337-indicators"]
	path = MQL4/Indicators/EA31337-indicators
	url = git@github.com:EA31337/EA31337-indicators.git
@kenorb
Copy link
Member

kenorb commented Apr 18, 2024

It's a good question.

To clarify, you're comparing by running MA.mq4 indicator on MT4, for different Source type (InpSourceType), between builtin iMA() (IDATA_BUILTIN) and platform's 'Custom Moving Average.mq4' (IDATA_ICUSTOM)? If so, then it's difficult to say, since we don't have access to the source code of iMA() builtin function to compare with. There is 3rd calculation available for MA indicator which is IDATA_INDICATOR, this is internal EA's calculation. If you mean this one (Indi_MA/IDATA_INDICATOR) comparing to platform's calculation, then it's something to check (assuming calculation provided by platform is better).

@cgava
Copy link
Author

cgava commented Apr 18, 2024

I've found a fix, even though I don't yet understanding clearly (my understanding of MT4 ways to fetch data from hcc cache data/server is not clear enough yet)... I've solved the delta doing this :

@@ -121,7 +122,7 @@ int OnCalculate(const int rates_total, const int prev_calculated,
ExtMABuffer[i] = DBL_MAX;
return prev_calculated + 1;
}

  • ExtMABuffer[i] = _entry[0];
  • ExtMABuffer[i-1] = _entry[0];
    }
    // Returns new prev_calculated.
    return (rates_total);

Choosing BUILTIN or CUSTOM gives the same result...

image

@cgava
Copy link
Author

cgava commented Apr 18, 2024

BTW I will check when indicator is inside the Expert Advisor (IDATA_INDICATOR)... What is still a little bit upsetting me, is when I add a "+1" or "-1" in a code, but don't understand exactly why is SHALL be so ;)

@nseam
Copy link

nseam commented May 17, 2024

The problem could have arrived because original OnCalculate() code distributed by either MT or found in the public had bug. They assumed there's always a history of bars and just took price one bar before the first bar. It could work in MT4, but MT5 (or the other way) could error with array access out of bounds (similar) error if we've used the code provided as it is.

If the code starts without history - e.g., via starting Debug session, initial values of the indicators could differ from the ones seen in Tester.

The main fix we've done for already provided indicators was to change starting index:

start = prev_calculated == 0 ? 2 * InpMAPeriod - 1 : prev_calculated - 1;

So start index won't end up with negative value and all bars for InpMAPeriod has taken place. This was not always true for already provided indicators' code by MT.

BTW, many of the source codes that ends up in Indicators/Examples after installation had bugs which they've fixed one by one, but I'm not sure if all of them, so we had to fix it by ourselves when we've faced them. The code were fixed in our head-less OnCalculate() code (IDATA_ICUSTOM) for indicators, but original OnCalculate() code provided by MT was not as it runs correctly when ran via IDATA_INDICATOR mode or directly in Tester. We've tested consistency of the results if they matches and it was okay for that day.

I will investigate it shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants