stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Multi timeframe indicator shows different results from EA
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=714
Page 1 of 1
Author:  zen4x [ Wed Jul 18, 2012 12:51 pm ]
Post subject:  Multi timeframe indicator shows different results from EA

Hello all. I wanted to ask you guys why I got different results between the indicator and the EA when I run the same code.
I made an indicator to help me with backtesting to show the parabolic sar on different timeframes.
When I use the indicator it works fine, but the EA with the same code basically reacts differently even if the logic applied is the same.

Code: Select all


#property copyright "Copyright 2012, Crappy Code"
#property link      "crappycoder@allcrap.com"


#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 2.0
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
extern int thickness = 1;
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[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0, long);
   SetIndexStyle(0, DRAW_ARROW, EMPTY, thickness);
   SetIndexArrow(0, 117);
   SetIndexEmptyValue(0, 0.0);
   SetIndexBuffer(1, short);
   SetIndexStyle(1, DRAW_ARROW, EMPTY, thickness);
   SetIndexArrow(1, 117);
   SetIndexEmptyValue(1, 0.0);
   IndicatorShortName("Triple Screen Parabolic Sar");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

/**
 *@ param1 is the parabolic sar reading from the d1/4h/h1 chart
 *@ param2 is the timeframe to check
 *@ param3 is the shift of the actual bar to read
 *@ return 2 if psar is above the high and 1 if is below or bullish
 */
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;
          Print("it seems a downtrend");
      }
      
      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;
          Print("it seems an uptrend");
      }
      
      
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+


Basically the indicator show a blue dot or red dot when the code runs as indicator.
When I run the EA with the same code, the EA keep sayng downtrend or uptrend when the indicator does not show anything. The point is why in backtesting with ForexTester the indicator works fine and the EA does not ?
Thank you in advance.
Author:  garyfritz [ Wed Jul 18, 2012 6:11 pm ]
Post subject:  Re: Multi timeframe indicator shows different results from E

I really don't have time to dig into the code, but I'd just about bet it's a repainting issue. MTF indicators are famous for that.

Say your indicator looks at H1 and M1 TFs. When you calculate the SAR value at 00:01, you use the M1 data from the 00:01 candle. But you use the H1 candle that contains 00:01, which is the 00:00 H1 candle, which includes the closing value at 00:59 and the highest/lowest values between 00:00 and 00:59! (I'm pretty sure that's the right explanation.)

So the MTF indicator "sees the future" and produces lovely perfect-looking signals -- which you wouldn't see in realtime, because the indicator wouldn't be able to "cheat" and look at the future H1 closing value. The EA, meanwhile, has to trade in realtime, when you can't look into the future. That produces different results to the indicator calculation. I assume the EA backtester has the same behavior as running it in realtime, i.e. it doesn't let the EA look at future bars the way indicators do.

If you modify your MTF code to use the current M1 close -- and calculate the highest and lowest M1 value so far since the start of the current H1 (or whatever) bar, and use THOSE values to calculate your PSAR -- then you're not peeking into the future, and the indicator and the EA should work the same. You'll probably find the indicator signals aren't nearly as impressive when it can't look into the future. :D
Author:  zen4x [ Wed Jul 18, 2012 6:24 pm ]
Post subject:  Re: Multi timeframe indicator shows different results from E

Hi Gary, thank you for answer me. I appreciate ;)
The weird thing is that I am been using the indicator during these days and I do not have any problem with the signals. Another thing to say is that when I run in backtest with Forex Tester and I plot the indicator the indicator gives to me the right signals.
For this reason I am quite confused.
Author:  zen4x [ Wed Jul 18, 2012 7:06 pm ]
Post subject:  Re: Multi timeframe indicator shows different results from E

garyfritz wrote:
If you modify your MTF code to use the current M1 close -- and calculate the highest and lowest M1 value so far since the start of the current H1 (or whatever) bar, and use THOSE values to calculate your PSAR -- then you're not peeking into the future, and the indicator and the EA should work the same. You'll probably find the indicator signals aren't nearly as impressive when it can't look into the future. :D
How can I achieve this ?
Author:  garyfritz [ Wed Jul 18, 2012 7:44 pm ]
Post subject:  Re: Multi timeframe indicator shows different results from E

You can't call iClose, iHigh, etc on higher TFs. You don't do that -- but you call iSAR on higher TFs, and it DOES.

So you'd have to collect the current H/L/C with High, Low, Close, etc, store the highest High & lowest Low so far in your higher-TF bars. Or maybe the recent High/Low, I haven't really looked closely at how Parabolic SAR is calculated Then you'd use those values to calculate the ParabolicSAR value yourself. You just have to do it without using any higher-TF calls to iXXXX().
All times are UTC Page 1 of 1