Download MetaTrader 5

Multi timeframe indicator shows different results from EA

To add comments, please log in or register
difenp
354
difenp  

Hello guys. I wanted to understand why I get different results using a multi timeframe indicator code to develop an EA. Basically I have written a quite simple code to check the Parabolic Sar over three different timeframes. The indicator seems to work fine in accordance with the logic, instead when I run the code into the EA, the EA seems working differently from the data the indicator shows.

extern int tmf_1 = 1440;
extern int tmf_2 = 240;
extern int tmf_3 = 60;
extern bool alertEnabled = false;

static double alertBar;
double step = 0.02;
double maximum = 0.2;
double long[];
double short[];

int detectPsar(double psar, int tmf, int shift) {

   if (psar >= iHigh(Symbol(), tmf, shift) ) {
      return (2);
   } else {
      return (1);
   }
   
   return (0);
}


int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   
   int totBars, tmf_1_shift, tmf_2_shift, tmf_3_shift;
   double tmf_1_psar, tmf_2_psar, tmp_3_psar;
   
   
   if (counted_bars < 0) counted_bars = 0;
   if (counted_bars > 0) counted_bars--;
   totBars = Bars - counted_bars - 1;
   
   for (int i = 0; i <= totBars; i++) {
      
      tmf_1_shift = iBarShift(Symbol(), tmf_1, iTime(Symbol(), 0, i), false);
      tmf_2_shift = iBarShift(Symbol(), tmf_2, iTime(Symbol(), 0, i), false);
      tmf_3_shift = iBarShift(Symbol(), tmf_3, iTime(Symbol(), 0, i), false);
      
      tmf_1_psar = iSAR(Symbol(), tmf_1, step, maximum, tmf_1_shift + 1);
      tmf_2_psar = iSAR(Symbol(), tmf_2, step, maximum, tmf_2_shift);
      tmp_3_psar = iSAR(Symbol(), tmf_3, step, maximum, tmf_3_shift);
      
      
      
      if (detectPsar(tmf_1_psar, tmf_1, tmf_1_shift + 1) == 2 && 
          detectPsar(tmf_2_psar, tmf_2, tmf_2_shift) == 2 && 
          detectPsar(tmp_3_psar, tmf_3, tmf_3_shift) == 2) {
          
          short[i] = 1;
          if (alertBar != Time[0]) {
            alertBar = Time[0];
            Alert("trend seems down");
          }
      }
      
      if (detectPsar(tmf_1_psar, tmf_1, tmf_1_shift + 1) == 1 && 
          detectPsar(tmf_2_psar, tmf_2, tmf_2_shift) == 1 && 
          detectPsar(tmp_3_psar, tmf_3, tmf_3_shift) == 1) {
         
          long[i] = 1;
         if (alertBar != Time[0]) {
            alertBar = Time[0];
            Alert("trend seems up");
          }
      }
  }

When I run the EA and plot the indicator to have a visual check of what the EA can see, the EA shows a downtrend even if the indicator leaves short[i] buffer to its empty value 0. The same is true for long.

Is there any detail that I am missing about the way I get data from different timeframes ?

Thank you in advance.

whroeder1
16306
whroeder1  
for (int i = 0; i <= totBars; i++) {
You can't get Bar zero data for other timeframes/pairs in the tester Testing Features and Limits in MetaTrader 4 - MQL4 Articles
difenp
354
difenp  
WHRoeder:
You can't get Bar zero data for other timeframes/pairs in the tester Testing Features and Limits in MetaTrader 4 - MQL4 Articles




Hei WHRoeder, thanks for the article. Does it mean that I cannot test this EA? This system checks what was the reading of the Sar on the highest timeframe (shift + 1) and after that checks the bar forming on 4H and 1H.

Am I wrong ?

difenp
354
difenp  
zen4x:

Hei WHRoeder, thanks for the article. Does it mean that I cannot test this EA? This system checks what was the reading of the Sar on the highest timeframe (shift + 1) and after that checks the bar forming on 4H and 1H.

Am I wrong ?


Another thing I forgot to mention is that if I test that code as indicator using Forextester the indicator works fine.
difenp
354
difenp  

Guys I am still trying to understand the error I got. Basically I noticed that one mistake was the loop I wrote I changed the condition

for (int i = 0; i <= totBars; i++) {

}
to
for (int i = 0; i < totBars; i++) {

}
The problem left is a little gap of 10 minutes 2 bars, on the 5 minutes timeframe. The indicator stops painting any signal but for other 2 bars I got a signal.
Also I wanted to ask if I should try to rewrite the indicator using ArrayCopySeries instead ibarshift ?
Thanks in advance.
To add comments, please log in or register