Indicators behaves different in Visual vs. Non-Visual Testing

 

Hi,

can someone give me a hint, why I get in visual testing the data that I can see in the indicator graph but in non-visual I get different data (or worse none data).

It is related to the indicator, because some indicators work in both modes and some not. Example: SSL_Channel_Chart works, but Rex not (https://www.mql5.com/en/forum/265346).

Can I somehow workaround that problem for Rex? Or do I call it in a wrong way?

Edit: Just made an observation: When I'm on H1 and I get the data 5 min before end of the candle then everything is fine. I just want to understand that.

Regards

Daniel


EA:

#include <Trade\Trade.mqh>
CTrade         m_trade;                      // object of CTrade class
int handle;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ResetLastError();
   handle = iCustom(Symbol(), PERIOD_CURRENT, "imd/Rex");
   if(handle == INVALID_HANDLE)
     {
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   IndicatorRelease(handle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   datetime newTime = waitForNewBar();
   if(newTime != NULL)
     {
      // new candle opened
      crossover();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool crossover()
  {
   MqlRates mrate[];
   double val1[];
   double val2[];
   
   int dataCount = CopyBuffer(handle,0,0,3,val1);
   if(dataCount < 2)
     {
      Alert("Error getting bufnum 0");
      return false;
     }
   dataCount = CopyBuffer(handle,1,0,3,val2);
   if(dataCount < 2)
     {
      Alert("Error getting bufnum 1");
      return false;
     }
   if(CopyRates(Symbol(), PERIOD_CURRENT, 0, 2, mrate) < 2)
      return false;
// check crossover
   Print("1[1]: "+val1[1]+" 2[1]:"+val2[1]+" 1[0]:"+val1[0]+" 2[0]:"+val2[0]);
   if(val1[1] > val2[1] && val1[0] < val2[0])
     {
      double sl = mrate[1].close - 0.002;
      double tp = mrate[1].close + 0.002;
      m_trade.Buy(0.01, NULL, 0, sl, tp);
     }
   if(val1[1] < val2[1] && val1[0] > val2[0])
     {
      double sl = mrate[1].close + 0.002;
      double tp = mrate[1].close - 0.002;
      m_trade.Sell(0.01, NULL, 0, sl, tp);
     }
   return false;
  }
//+------------------------------------------------------------------+
datetime waitForNewBar()
  {
   static bool isStartupBar = true;
   static datetime Old_Time;
   datetime New_Time;

//-- copying the last bar time to the element New_Time[0]
   bool isNewCandle = false;
   New_Time = iTime(Symbol(), Period(), 0);
   if(Old_Time==New_Time) // if old time is equal to new bar time --> not a new bar
     {
      return NULL;
     }
//-- start with a new bar -----------------------------------
//if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
   Old_Time=New_Time;            // saving bar time
   if(isStartupBar)
     {
      isStartupBar = false;
      return NULL; // only the very first tick (Old_TIME is NULL)
     }
   return New_Time;
  }
//+------------------------------------------------------------------+
Indicators: Rex
Indicators: Rex
  • 2018.07.13
  • www.mql5.com
Rex: Author: Scriptor...
 
Daniel Nettesheim:

Hi,

can someone give me a hint, why I get in visual testing the data that I can see in the indicator graph but in non-visual I get different data (or worse none data).

It is related to the indicator, because some indicators work in both modes and some not. Example: SSL_Channel_Chart works, but Rex not (https://www.mql5.com/en/forum/265346).

Can I somehow workaround that problem for Rex? Or do I call it in a wrong way?

Edit: Just made an observation: When I'm on H1 and I get the data 5 min before end of the candle then everything is fine. I just want to understand that.

Regards

Daniel


EA:

The problem seems to me:
You check the result of CopyBuffer(), and return when the data is not ready yet - so far so good.

But once returned, you never check again if the data is already available, but wait for the next bar. 

[EDIT]: So this is not a problem of the indicator. The indicator just hasn't already calculated it's results at the point of time when you've got the tick.

 

Yes, thats right. After waiting one Tick the indicator seems up-to-date.

Here is how I work-arounded it: (if someone has a better idea, please comment it here)

void OnTick()
  {
//---
   datetime newTime = waitForNewBar();
   if(newTime != NULL)
     {
      // we wait some ticks/mins after new candle open
      waitTicks = 1;
      return; // exit
     }
   else
      if(waitTicks > 0)
        {
         waitTicks--;
         // new candle opened
         if(waitTicks == 0)
           {
            if(m_symbol.RefreshRates() && m_symbol.Refresh())
              {
               crossover();
              }
            else
              {
               waitTicks++;
              }
           }
        }
  }
 

If you wait for the next candle open, you most probably would want to receive the values of candle '1' instead of '0' in the CopyRates(). (No need to wait for next tick)

if(CopyRates(Symbol(), PERIOD_CURRENT, 1, 2, mrate) < 2)
 

Most probably your indicator needs to be updated every tick to work properly (I didn't check).

Use

#property tester_everytick_calculate

Look at the documentation to understand.

 

Alain, thank you so much.

That's it. Now it works. That solved a mystery after almost a year why the non-visual and visual backtests have different results.

Best regards

Daniel