Wrong indicator values during backtesting

 

I can't isolate the reason why the values that a custom indicator that is called from my EA gives when I try to backtest the EA, are different from the values this indicator shows when I attach it to a chart. I have tried to find info about the internal processes of the tester, but I didn't have sucess. I would appreciate any ideas, clues, info...

Thank you in advance

 
JdR9966:

I can't isolate the reason why the values that a custom indicator that is called from my EA gives when I try to backtest the EA, are different from the values this indicator shows when I attach it to a chart. I have tried to find info about the internal processes of the tester, but I didn't have sucess. I would appreciate any ideas, clues, info...

Thank you in advance

Not all Indicators work correctly when attached to the Strategy Tester chart . . . more info here: https://www.mql5.com/en/forum/143310/page2#753353
 
JdR9966:

I can't isolate the reason why the values that a custom indicator that is called from my EA gives when I try to backtest the EA, are different from the values this indicator shows when I attach it to a chart. I have tried to find info about the internal processes of the tester, but I didn't have sucess. I would appreciate any ideas, clues, info...

Thank you in advance


be more specific

how do we know ?? what indicator

you have tried ..... then show how

 
RaptorUK:
Not all Indicators work correctly when attached to the Strategy Tester chart . . . more info here: https://www.mql5.com/en/forum/143310/page2#753353


Thanks a lot Raptor. I didn't expect such a fast answer on a Friday night! At least I'm not alone working!


I read the info in your link and I got some ideas from it. In my case, the indicator when it is launched to a real-time chart, when it pops-up automatically after the testing of the EA (where it is called with iCustom) finishes, and when I attach it to an existing tester chart, are the same. But I had the feeling that something odd was happening, and then I wrote in the code some "Print" to see the real values in the logs. What I found in the logs is that the graphical values of the indicator don't correspond with the values in the log. Does it sound reasonable to you?


Thank you again!

 
deVries:


be more specific

how do we know ?? what indicator

you have tried ..... then show how


Hi deVries,


I tried to explain a little bit more in the previous comment to Raptor. The indicator is self-made. It calculates the moving average of another indicator, but it is not a convencional moving average of neighbour bars, but from bars that are separated according to a certain algorithm. For instance, it could be the N-period average of the price close every Monday at 13:15. In other words, this average last Monday would be the average of Close of the previous N Mondays at the same time. And the thing is that the code draws well the values, but when I call this indicator with iCustom from an EA testing, the figures shown in the logs of this averages are different from the drawings.

If you have any ideas, they are welcome. Thank you!

 
JdR9966:

Hi deVries,


I tried to explain a little bit more in the previous comment to Raptor. The indicator is self-made. It calculates the moving average of another indicator, but it is not a convencional moving average of neighbour bars, but from bars that are separated according to a certain algorithm. For instance, it could be the N-period average of the price close every Monday at 13:15. In other words, this average last Monday would be the average of Close of the previous N Mondays at the same time. And the thing is that the code draws well the values, but when I call this indicator with iCustom from an EA testing, the figures shown in the logs of this averages are different from the drawings.

If you have any ideas, they are welcome. Thank you!



if you want it to test with strategytester then you have to create a template with all indicators needed for that test and the EA you gonna test

give the template same name as your EA, and do a test with the EA then the test will be done with the template you named as your EA only that way will work

 
deVries: give the template same name as your EA, and do a test with the EA then the test will be done with the template you named as your EA only that way will work
Or give the template the name tester.tpl and be common to all EAs
 
JdR9966: If you have any ideas, they are welcome. Thank you!
Wait for the mind readers to come and help with your unseen code. Otherwise, you need to post it.
 
JdR9966:

Thanks a lot Raptor. I didn't expect such a fast answer on a Friday night! At least I'm not alone working!


I read the info in your link and I got some ideas from it. In my case, the indicator when it is launched to a real-time chart, when it pops-up automatically after the testing of the EA (where it is called with iCustom) finishes, and when I attach it to an existing tester chart, are the same. But I had the feeling that something odd was happening, and then I wrote in the code some "Print" to see the real values in the logs. What I found in the logs is that the graphical values of the indicator don't correspond with the values in the log. Does it sound reasonable to you?

Perhaps you are reading bar 0 ? and the Indicator value for bar 0 changes until bar 0 closes and becomes bar 1 . . . then it probably doesn't change.
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                      Bizarre.mq4 |
//|                                                              JdR |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "JdR"
#property link      ""

/*For 1H bars, the idea would be to pay attention to close prices every day at 14:00, and keep this figure
in variable "exit". The day after, we would check the close price at 13:00, and keep its value as "enter"
If one hour later, at 14:00, exit<enter<close(14:00), then our first indicator "result" would get a value of 1.

If exit<enter but enter>close, then result would be -1.

If exit>enter>close[14:00], then result=1

If exit>enter but enter<close[14:00], then result=-1

So result[i] would equal 1 or -1 on every bar at 14:00, and would be cero the other bars.

The second indicator would be the simple moving average of result[i] being N the external variable that defines the period
for the average. But not the moving average of the whole bars, but only of those bars not equal to cero. In other words, 
today value at 14:00 of average[] if N=2, would be (result[today at 14:00]+result[yesterday at 14:00])/2
*/


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Black
#property indicator_color2 Orange
#property indicator_minimum    -100
#property indicator_maximum    100
#property indicator_level1     0.0
#property indicator_level2     0.25
#property indicator_level3     -0.25

//---- input parameters
extern int N=10;


//---- buffers
double result[];
double average[];


double enter,exit;
int counter;
double aver=0;

static datetime prevtime=0;

  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(2);

   SetIndexStyle(0,DRAW_ARROW,0,2);
   SetIndexBuffer(0,result);
   SetIndexArrow(0,159);
   SetIndexEmptyValue(0,0.0);

   //SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(1,DRAW_ARROW,0,2);
   SetIndexBuffer(1,average);
   SetIndexArrow(1,159);
   SetIndexEmptyValue(1,0.0);
   
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
//----
   int i;
   
   //addition=0;
   if(prevtime==Time[0]) return(0);//I write this because I test on completed bars only
   prevtime=Time[0];
  
   double stack[1];//This array is used as a buffer to keep the elements of result[] that are +-1.
                        //The average that I search would be the addition of all of the array elements, divided by N.
   ArrayResize(stack,N);
   
   
   
   i=Bars-counted_bars-1;
   

   while(i>=0)
   {
     if(TimeHour(Time[i+1])==13)
     { 
       enter=Close[i+1];

       result[i+1]=0.0;
     }
     else 
     {
       if(TimeHour(Time[i+1])==14)// 
       {

         if(exit<enter && enter<Close[i+1]) result[i+1]=1.0;//
         if(exit<enter && enter>Close[i+1]) result[i+1]=-1.0;//
         if(exit>enter && enter>Close[i+1]) result[i+1]=1.0;//
         if(exit>enter && enter<Close[i+1]) result[i+1]=-1.0;//
         
         exit=Close[i+1];//After I get the values of result[] with the previous day at 14:00, I enter the new value of exit.
         
         if(counter<=N-1) //When I don't have N values of result[] to get its first average...
         {
           stack[counter]=result[i+1];//This is how I enter the values of result not equal to cero.
           counter++;
           aver=aver+result[i+1]/N;
           average[i+1]=aver;//This is the real indicator that I want.
           i--;
           continue;
         }
         if(counter==N) //When I already have N values of result[], the way I calculate the next average is 
                      //removing the first element of the acumulator array, and adding the new result. This
                      //way I always keep the last N result[] that are different from zero.
         {
           aver=aver+result[i+1]/N-stack[0]/N;//The new addition with the new element and removing the first one
         
           average[i+1]=aver;//the moving average that I want to calculate
         
         
           for(int v=0;v<=N-2;v++)//since the array is static, this is the way I reasign indexes to its elements to keep it
                                //updated with the last N elements of result[i] that are not zero.
           {
             stack[v]=stack[v+1];//The first element is discarded
           }
             stack[N-1]=result[i+1];//the last element is added.
           i--;
           continue;
         }
         
       }  
       else result[i+1]=0.0;
       
     }
     
     i--;
     
   }
   return(0);
}
 

This is part of the code I'm using, the part where I thing the problems are. At least here there are problems that I cannot find. It is prepared to 1H bars, and modelling type is opening prices only. When I attach it as the test template and I test any EA, the orange dots, the average that I want, starts diverging when it should remain between +1 and -1 because it is the average of things whose values are only +1 or -1.


The bugg must be very foolish, but I don't find it. I'd appreciate any help debugging the code...

Thanks a lot!

Reason: