iMACD() providing invalid data when testing strategy

 
Hi, I'm writing an expert advisor that relies on a MACD signal, and after spending about 5 hours thinking I was losing my mind, I finally found the problem. When I feed my advisor through the strategy tester, fetching the current MACD signal for H1 (1 hour) data actually gives me the M1 (1 minute) data. As a result, the program is seeing the signal swing all over the place, because it's M1, and well, this causes lots of problems.

The code in question is what fetches the CurrentMACDSignal.

double CurrentMACDSignal = iMACD(NULL,PERIOD_H1,MacdFastEmaPeriod,MacdSlowEmaPeriod, MacdSignalPeriod,PRICE_CLOSE,MODE_SIGNAL,0);
double PreviousMACDSignal = iMACD(NULL,PERIOD_H1,MacdFastEmaPeriod,MacdSlowEmaPeriod, MacdSignalPeriod,PRICE_CLOSE,MODE_SIGNAL,1);

This bug appears to be endemic to the strategy tester; when I run my expert advisor against live data, everything works fine. In order to verify this bug, I created a file logging mechanism that writes the current MACD signal everytime data comes in. When I look at the strategy tester log, I see it swinging all over the place (it matches the M1 data), yet it's fine for running against a live chart.

Anyone have any ideas on how to fix this? Being able to back-test and optimize is integral.

Thanks a lot, Spencer
 
Could you, please, provide us with the entire code of your expert advisor and the screenshot of the "Settings" tab in the "Tester" window?
 
Hi,

Wow, I thought I was going crazy too. Today I was testing EA and it was giving me wrong data and results. On a live chart, indicator clearly shows where to buy/sell, but when I programmed simple EA - EA was giving me sort of "random" buy/sell. And values of indicator were completely off. I will illustrate it below:

here is a value of indicator from the chart, look at date and time



here is a value of the same indicator from EA journal, look again at date and time



VALUE IS COMPLETLY OFF!!!!!!!

Also, EA stopepd at 06:59 even trading continued until 23:00.

Setting tab values are simple - EA name, EURUSD, 1H, recalculate unchecked, optimize unchecked, Model - open prices only.

Tatyana, if you provide me with your email - I would be more than happy to send you indicator and EA. I think software itself is great, but it might be an overlooked area, or a simple bug.

Thanks!
 
Tatyana:
Could you, please, provide us with the entire code of your expert advisor and the screenshot of the "Settings" tab in the "Tester" window?

Yes, Tatyana, please provide me with your email. I will send you the code for my expert advisor, as well as a log file that illustrates the invalid data in the MACD signal. Everything else seems to be working great; the EA runs fine with live data, but won't work with backtesting, which is clearly a much needed area, for both verification of the EA, and optimization.

-spencer
 
Hi again,

For kicks, I decided to take MACD sample EA provided with StrategyBuilderFX and see if it will work correctly. Well, IT WAS NOT!!! Below are screenshots.

Tatyana, please reply with your opinion on what is going on...

Setup for EA:




Result for this setup: (Pay attention that last trade was on May 26!!!)




Now, another setup: (Open prices only)





Here is a chart showing that it should of between at least 4-5 trades after may 26 (according to MACD EA rules). Also, something is off with this chart. I am in "Bars" view, but it shows only vertical lines (no Open/Close prices inside bars). Another bug????

 
spencermiles wrote:

Yes, Tatyana, please provide me with your email. I will send you the code for my expert advisor, as well as a log file that illustrates the invalid data in the MACD signal. Everything else seems to be working great; the EA runs fine with live data, but won't work with backtesting, which is clearly a much needed area, for both verification of the EA, and optimization.

tatyana AT metaquotes.ru
 
Tatyana:

Tatyana, at this point I think it is very clear that EA does not work, even on your, guys, own EA (supplied with the software). Again, I am using software for about a month, so I may not know everything, please advise if there is a special way how to handle EA. Also, I checked several forums on mql4 EAs, and saw a lot of complains about EA, one guy even suggested never to use EA.

Software itself is great, and fixing EA bug will make it #1 on a market, no doubt. As Spencer said before "Being able to back-test and optimize [EA] is integral. " Could not say it better.

Looking forward for your suggestions what to do next. If it is a bug, please advise when we can expect a fix (days, weeks, ...).
 
Do you consider changes in current bar during testing?
 
I found this same "problem" recently. I actually made an EA that would simply Print to the log the value of my indicator at certain times and it NEVER matched the chart. My only guess besides a bug is that the tester is always trying to simulate ticks, even in open only mode (where is just doesn't look at lower timeframes) and so the indicator actually has several possible values in the course of a bar. If this is the case, I'm not sure how to work around it. The example EA Moving Average that came with MetaTrader 4 does show a trick to check the Volume to be not > 1 so that it only trades on the first simulated tick of a bar. You could try that, but not sure how you could get it to trade on the LAST tick, i.e. the bar close, which is how alot of systems trade. You could prehaps wait until tick 1 of the new bar then use the previous bars indicator value to simulate opening on the OPEN but using the CLOSE indicator value. Can anybody verify if this theory is correct?
 
Ok, I just made another test EA to print the value of a simple MA each tick.

Here is the code:
//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                                                         neverfox |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "neverfox"
#property link      ""
 
int i;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   i = 1;   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double val;
   val=iMA(Symbol(),0,20,0,MODE_SMA,MODE_CLOSE,0);
   Print("MA value = ",val," at tick ",i);
   i++;
//----
   return(0);
  }
//+------------------------------------------------------------------+
Ok, it doesn't get much more simple than that. Yet, when I attach this to a chart and add the same indicator to the chart (SMA 20 on Close), the values printed in the log DO NOT EQUAL the value on the chart!!! This is horrible. Unless I'm missing something, which is always likely, EAs are completely useless. Please help if you know what I'm doing wrong but this doesn't appear to be good for MT4.
 
Neverfox,
Can you help me understand what you're seeing?
I'm assuming that you see the value in the journal, and then you pull the value off the sma (hover the mouse over it) and they don't match? Just wanted to be sure what you're comparing to what. Is this in forward test or strategy tester backtest?

It is true that the indicator will have different values for every tick. Until the bar closes, it can still change. A moving average is vulnerable to that difference for as long as the bar is open (you can get a cross on the first tick, firing the EA, but then the closing tick actually leaves the indicator uncrossed, because the close is different than the first tik). Something like Zig Zag is even more vulnerable, since it looks at values so many bars old, and will change it's value many bars after the fact. For mas, using only the first tick is one way of assuring that the indicator will not change compared to an historical review. However, depending on the timeframe, that might be too much time to wait if the signal occurs in the middle or beginning of the bar.

One method is trading only on first tick. Slow, but will match history. Another is to take the trade immediately, but then on first tik, inspect the previous bar and if the close didn't properly match the signal after it was finished, then undo the action (or put in a hedge trade, or whatever, some way of handing the fact you ended up with an order that you would not have seen on historical series of bars). Just think of how you would handle the indicator change when live trading and teach the EA to do that, because indicators do change.

A different issue, that I've seen, is the relative position of the indicators on a drawing will be wrong, but the values themselves are correct, and correctly read by the ea. For example, I saw a MACD line that was crossed by the signal line, but if I hovered over the lines, they actually had NOT crossed. The value of the lower line was actually higher than the value for the other line, though the drawing had them wrong. So a graphics rendering problem, but it wasn't a real problem for the EA.
-matt
Reason: