Backtesting with ADX indicator

 

Hi

I have an EA which works great in real time, however the results differ during back testing. In back testing signals are being triggered when they shouldn't be. I've isolated the problem to be the ADX indicators. Specifically to either +DI or -DI. When you look at the data plotted on the chart and you look at the value in the EA, they are different. The data plotted on the chart is correct however, the result in the EA is not correct.

The code for the ADX indicator is below and is triggered by the OnTick()  method.

 

      bool adx = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MAIN, 0) >= 25; 

      double pdi = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_PLUSDI, 0);

      double mdi = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MINUSDI, 0); 

 

I don't think it's my code because as i said in real-time it's fine. Is there something special that needs to be done for back testing purposes?

Would appreciate any help in understanding what's wrong. 

Thanks in advance! 

 
ShariffS:

Hi

I have an EA which works great in real time, however the results differ during back testing. In back testing signals are being triggered when they shouldn't be. I've isolated the problem to be the ADX indicators. Specifically to either +DI or -DI. When you look at the data plotted on the chart and you look at the value in the EA, they are different. The data plotted on the chart is correct however, the result in the EA is not correct.

The code for the ADX indicator is below and is triggered by the OnTick()  method.

 

      bool adx = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MAIN, 0) >= 25; 

      double pdi = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_PLUSDI, 0);

      double mdi = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MINUSDI, 0); 

 

I don't think it's my code because as i said in real-time it's fine. Is there something special that needs to be done for back testing purposes?

Would appreciate any help in understanding what's wrong. 

Thanks in advance! 

It probably depends on your logic. Maybe declare a variable for the previous bar.

bool adx = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MAIN, 0) >= 25; 

double pdi_current = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_PLUSDI, 0);
double pdi_previous = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_PLUSDI, 1);

double mdi_current = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MINUSDI, 0); 
double mdi_previous = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MINUSDI, 1); 

if(adx && pdi_current > mdi_current && pdi_previous < mdi_previous)
 
ShariffS:

Hi

I have an EA which works great in real time, however the results differ during back testing. In back testing signals are being triggered when they shouldn't be. I've isolated the problem to be the ADX indicators. Specifically to either +DI or -DI. When you look at the data plotted on the chart and you look at the value in the EA, they are different. The data plotted on the chart is correct however, the result in the EA is not correct.

The code for the ADX indicator is below and is triggered by the OnTick()  method.

 

      bool adx = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MAIN, 0) >= 25; 

      double pdi = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_PLUSDI, 0);

      double mdi = iADX(Symbol(), PERIOD_M5, 14, PRICE_CLOSE, MODE_MINUSDI, 0); 

 

I don't think it's my code because as i said in real-time it's fine. Is there something special that needs to be done for back testing purposes?

Would appreciate any help in understanding what's wrong. 

Thanks in advance! 

1) It might help if you would post, what were the conditions to buy or sell that don't work backtesting.

2) The timeframe of the backtest is the same as the PERIOD_M5 of your ADX?

3) What backtest data (fxt-files) are you using: "Selfmade" or do you use the default way of metatrader?

3a) If "selfmade" were they created while you were logged in at the same broker you are connected with during your backtests?

 

I think the problem is most likely what pipPod hinted at.

When using bar index[0] you are accessing data on a tick by tick basis. What may be true at tick may not be true at the bar close. When you look back over the chart you will only see values for the close of that bar and so it may seem that the EA is not working correctly