iADX and incorrect resulting data

 
Hello,
I've been learning MQL for about around a month or so and trying to set up a system that will go long on a cross of the ADX(4) DI plus/minus within the indicator on EURUSD currency. I programmed in some back testing code and had the system running nicely so wanted to produce an Expert Advisor but wasn't getting the same results. I narrowed it down to find the error produced after checking the Strategy Tester log data produced found that the days DI+ or DI- number is incorrect. The code attached is a cut down version of the EA and if you view the Strategy Tester log file you will see that either the DI+ or DI- is incorrect and the correct data is displayed in the previous days figures, also in the log on the previous line. I've emailed Metaquotes about this but haven't, as yet, had a reply and wondered if I'm doing something wrong. Not every days data is incorrect but a large percentage, if you view the data window within MetaTrader charts and place the mouse over the chart data you can see the chart prices and ADX figures that dont tally with the Strategy Tester log file below it, if you dont close the strategy journal of course.

Any help would be very much appreciated here.

I think the MQL4 system is incredible and would love to get this fixed.

You can see a cut down version of my EA below....
//+------------------------------------------------------------------+ //| Test1.mq4 | //| Copyright © 2006, Harry Davis | //| http://www.solidinvestment.com/?ref=foxmor | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Harry Davis" #property link "http://www.solidinvestment.com/?ref=foxmor" //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { double PrevDIPlus; double TodayDIPlus; double PrevDIMinus; double TodayDIMinus; //---- PrevDIPlus=iADX(NULL,PERIOD_D1,4,PRICE_CLOSE,MODE_PLUSDI,1); TodayDIPlus=iADX(NULL,PERIOD_D1,4,PRICE_CLOSE,MODE_PLUSDI,0); PrevDIMinus=iADX(NULL,PERIOD_D1,4,PRICE_CLOSE,MODE_MINUSDI,1); TodayDIMinus=iADX(NULL,PERIOD_D1,4,PRICE_CLOSE,MODE_MINUSDI,0); // first look for di+/di- cross from prev day / current day for poss long Print("PrevDIPlus="+PrevDIPlus+" PrevDIMinus="+PrevDIMinus+" TodayDIPlus="+TodayDIPlus+" TodayDIMinus="+TodayDIMinus); //---- return(0); } //+------------------------------------------------------------------+
 
Well I've had a response from Metaquotes and they tell me to adjust the shift on the iADX to 1-2 from 0-1... anybody have an idea how to make that work now as the correct ADX data is being displayed but a day behind... how can that work if my expert advisor is running in realtime?

Any help would be appreciated.
Thanks
 

Hi foxmor,

If I'm reading your story correctly, I believe your 'bad' data is just the result computed for the DI+/DI-/ADX before the day is finished. Remember, the EA is running on every tik, so if the tik is before the bar is complete, it will treat the bid price on that tik as the close price for the bar and calculate DI+/DI-/ADX in a way that will NOT match the finished result (once the bar is closed).

So, switching to 1-2 instead of 0-1 will solve this by ensuring you have finished results for the bar, but it does make you a bar late.
If you want TODAY's result, you will gain in speed, but suffer from possible variation between the calculation on any tik and the final result.

If I've read all that right, then this is a very common problem. it's the same as the difference between how you trade manually, and how you might backtest manually. Backtest sees a whole bar, live trading sees a bunch of different 'bars' before it closes, which could give false signals, signals that went away by the time the bar closed, etc.

For a moving average EA (or any other, but this is an example that comes to mind) I had the EA use the 0 calculation (current bar). That kept it quick-acting. But, to guard against bad signals, it would look at the last bar, and if the signal was gone, it'd close the trade it just opened. in this way the speed was kept, and the risk of being 'wrong' was undone as soon as discovered. My client preferred that to waiting on a complete bar, because the slowness of entry was very often more pips profit lost than the loss of a too-early-bad-entry undone.

Hope that helps. Classic design compromise.

-matt

 
ququr:

Hi foxmor,

If I'm reading your story correctly, I believe your 'bad' data is just the result computed for the DI+/DI-/ADX before the day is finished. Remember, the EA is running on every tik, so if the tik is before the bar is complete, it will treat the bid price on that tik as the close price for the bar and calculate DI+/DI-/ADX in a way that will NOT match the finished result (once the bar is closed).

So, switching to 1-2 instead of 0-1 will solve this by ensuring you have finished results for the bar, but it does make you a bar late.
If you want TODAY's result, you will gain in speed, but suffer from possible variation between the calculation on any tik and the final result.

If I've read all that right, then this is a very common problem. it's the same as the difference between how you trade manually, and how you might backtest manually. Backtest sees a whole bar, live trading sees a bunch of different 'bars' before it closes, which could give false signals, signals that went away by the time the bar closed, etc.

For a moving average EA (or any other, but this is an example that comes to mind) I had the EA use the 0 calculation (current bar). That kept it quick-acting. But, to guard against bad signals, it would look at the last bar, and if the signal was gone, it'd close the trade it just opened. in this way the speed was kept, and the risk of being 'wrong' was undone as soon as discovered. My client preferred that to waiting on a complete bar, because the slowness of entry was very often more pips profit lost than the loss of a too-early-bad-entry undone.

Hope that helps. Classic design compromise.

-matt

 
Hi Matt,
A great reply and thanks very much for that bit of insight.... sometimes things aren't as clear as they seem.

I take it then if I wait until daily bar close, after 12pm(GMT), so its actually the next day and run my EA then it would give me accurate entry points based on my EA and it would be effectively 20 minutes behind realtime if for example I ran the EA at between 00:00 and 00:20am but with accurate iADX data from bar 1?? And the data wouldn't be as inaccurate as a whole day behind or as unreliable if trying to find entry points on bar 0?
Regards
Harry
 
Hi Harry,
...sometimes things aren't as clear as they seem.
LOL, so true!

I take it then if I wait until daily bar close, after 12pm(GMT), so its actually the next day and run my EA then it would give me accurate entry points based on my EA and it would be effectively 20 minutes behind realtime if for example I ran the EA at between 00:00 and 00:20am but with accurate iADX data from bar 1?? And the data wouldn't be as inaccurate as a whole day behind or as unreliable if trying to find entry points on bar 0?


Yes, but it's just another type of compromise. The 0-value is only "unreliable" when compared to history. That's true of many things. On a tall bar, the history is 'accurate' but you've missed so much move you might wish for a little of that profitable "unreliability" ;-).

In what you describe above, using the 2-1 as MetaQuotes suggested will give you a nice fresh signal for the first tik of the new days bar. And you can sit out the rest of the day. And get some sleep instead of getting up at 00:00 to turn the EA on for 20 minutes ;-). The effect is the same, so you can use 1-2 and then let it trade only that first tik, and let it be automatic. you could even restrict the runtime of the EA to some part of the day, using that finished bar, so first tik or later, it'll be the same signal, but the price might be at a different place than that first quiet tik of the day.

Another approach is to define rules for the current signal. For example, if the fear of using "0" is that you'll end up with a bad signal, you can decide what will make that regret unlikely. For example, if the 0 bar gives you a signal, then wait until it is a certain strength/value etc or after price has moved a certain distance from the signal point or some other method that will make you confident that you won't get a false signal if you take the trade now. So, you still get in on this day, you improve the chance your signal will be valid, and you give up less profit than if you'd waited for a new bar.

So, for my moving average example touched on earlier, I could let it trade on a cross, plus 5 pips. or a cross with fast ma slope > some steepness, or, value of difference between the mas that are crossing be some threshold value (another strength-of-cross filter).

Many ways to skin the cat. Having said all that, if you want your trades to match history perfectly, then take MQ's advice and use the 1-2 bars.

Good luck! --Matt

Reason: