Inconsistency between visual and quantitative signals

 

Hi all,

I am quite new to mql4. I have just created my first EA which takes signals to open trade when main Stochastic line crosses signal line in the extreme area - this is theory.

It works fine visually with the H1 timeframe. However when I run it through EA code the signals come in totally different moments - please see the picture below.

I have tested the code and it seems to work fine with correct logic...

I presume that this behavior is caused by calculating Stochastic indicator for each tick. Even though I am looking on a H1 graph where the signal havent occurred yet, on a tick level Stochastic main line momentarily crossed the signal line and so EA closes/opens trade.

Can you please help me with this? Is there any workaround or am I getting something wrong?

Thanks.


 
Porentius:

Hi all,

I am quite new to mql4. I have just created my first EA which takes signals to open trade when main Stochastic line crosses signal line in the extreme area - this is theory.

It works fine visually with the H1 timeframe. However when I run it through EA code the signals come in totally different moments - please see the picture below.

I have tested the code and it seems to work fine with correct logic...

I presume that this behavior is caused by calculating Stochastic indicator for each tick. Even though I am looking on a H1 graph where the signal havent occurred yet, on a tick level Stochastic main line momentarily crossed the signal line and so EA closes/opens trade.

Can you please help me with this? Is there any workaround or am I getting something wrong?

Thanks.



To be able to tell for sure you need to post a code snippet with the logic.

That said. If your logic is based on every tick, it is most likely the case. If your strategy only needs to be based on closed bars, code it that way.

It is best practice to not put all logic on every tick if you are making decisions on completed bars. Only need to calculate on specific intervals or events according to what is really needed Otherwisey your code will not be effiecient and consumes unnecessary resources. At first glance things may seem to run fine. Do a backtest with 1000 passes with badly optimized code and it will take forever.

 
Enrique Dangeroux:

To be able to tell for sure you need to post a code snippet with the logic.

That said. If your logic is based on every tick, it is most likely the case. If your strategy only needs to be based on closed bars, code it that way.

It is best practice to not put all logic on every tick if you are making decisions on completed bars. Only need to calculate on specific intervals or events according to what is really needed Otherwisey your code will not be effiecient and consumes unnecessary resources. At first glance things may seem to run fine. Do a backtest with 1000 passes with badly optimized code and it will take forever.

Hi Enrique,

This is the code which generates signals:

   M_0   = iStochastic(NULL,0,Kperiod,Dperiod,Kslowing,MODE_EMA,1,MODE_MAIN,  0);// 0 bar
   M_lag = iStochastic(NULL,0,Kperiod,Dperiod,Kslowing,MODE_EMA,1,MODE_MAIN,  bar_lag);// lagged bar
   S_0   = iStochastic(NULL,0,Kperiod,Dperiod,Kslowing,MODE_EMA,1,MODE_SIGNAL,0);// 0 bar
   S_lag = iStochastic(NULL,0,Kperiod,Dperiod,Kslowing,MODE_EMA,1,MODE_SIGNAL,bar_lag);// lagged bar

   curr_signal_val = (M_0 + M_lag + S_0 + S_lag)/4;
//--------------------------------------------------------------------
                                  // Analysis of the situation
   if(last_signal!=0 &&
      M_lag < S_lag &&
      M_0 > S_0 && 
      ((last_signal_val>U_bound && curr_signal_val<M_bound) ||
       (last_signal_val>M_bound && curr_signal_val<L_bound) ||
       (last_signal_val==-1 && curr_signal_val<L_bound) ))  // Main line crosses signal upwards
      signal = 0;
     
   if(last_signal!=1 && 
      M_lag > S_lag &&
      M_0 < S_0 && 
      ((last_signal_val<L_bound && curr_signal_val>M_bound) || 
       (last_signal_val<M_bound && curr_signal_val>U_bound) ||
       (last_signal_val==-1 && curr_signal_val>U_bound)))  // Main line crosses signal downwards
      signal = 1;

   if(signal==0 || signal==1)
      {
      //Alert("getSignal: ",last_signal_val," - ",curr_signal_val," - ",M_0," - ",S_0," - ",M_lag," - ",S_lag);
     
      last_signal_val = curr_signal_val;
      last_signal     = signal;
      }

In iStochastic second parameter refers to current timeframe = H1, is there a way to write the code as to take H1 bars, not tick values?

Thanks

 

Your snippet lacks the context. But my take is, it is running on the OnTick event. 

You will have to check if a new bar has formed. See this post how to determine a new bar has been formed in mt4 https://www.mql5.com/en/forum/150885#comment_3766801 Then apply your logic.

New candle
New candle
  • 2014.04.04
  • www.mql5.com
Anyone got a good piece of code for build 600+ that will determine if a tick that came in is the start of a new candle or not...
 
Porentius:

Hi Enrique,

This is the code which generates signals:


Reason: