OnCalculate called twice in Strategytester

 

Hello,

why does the follwing happen with two simple custom indicators.

Indicator 1:

Print("calc1") in OnCalculate

Indicator 2:

Print("calc2") in OnCalculate 

When called in Strategytester (anny settings), each indicator works as expected.

But when indicator1 loads icustom("indicator2") in oninit, calc2 gets printed twice.

I did not include any code, since it should be faster to just code this in your own environment to confirm.

Any way to solve this problem. 

Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
  • 2010.04.07
  • Denis Zyatkevich
  • www.mql5.com
MetaQuotes Programming Language 5 (MQL5), included in MetaTrader 5 Client Terminal, has many new possibilities and higher performance, compared to MQL4. This article will help you to get acquainted with this new programming language. The simple examples of how to write an Expert Advisor and Custom Indicator are presented in this article. We will also consider some details of MQL5 language, that are necessary to understand these examples.
 
thomasfx67:

Hello,

why does the follwing happen with two simple custom indicators.

Indicator 1:

Print("calc1") in OnCalculate

Indicator 2:

Print("calc2") in OnCalculate 

When called in Strategytester (anny settings), each indicator works as expected.

But when indicator1 loads icustom("indicator2") in oninit, calc2 gets printed twice.

I did not include any code, since it should be faster to just code this in your own environment to confirm.

Any way to solve this problem. 

So what?
 
angevoyageur:
So what?

"calc2" is printed twice on each tick:

2013.11.03 13:05:22 2013.07.01 00:05:01   calc2

2013.11.03 13:05:22 2013.07.01 00:05:01   calc2

2013.11.03 13:05:20 2013.07.01 00:05:00   calc2

2013.11.03 13:05:20 2013.07.01 00:05:00   calc2

2013.11.03 13:05:19 2013.07.01 00:03:59   calc2

2013.11.03 13:05:19 2013.07.01 00:03:59   calc2

2013.11.03 13:05:17 2013.07.01 00:02:59   calc2

2013.11.03 13:05:17 2013.07.01 00:02:59   calc2

 

If Indicator 2 is called direct, the output in tester is (and should be, in the first case):

2013.11.03 13:05:22 2013.07.01 00:05:01   calc2

2013.11.03 13:05:20 2013.07.01 00:05:00   calc2

2013.11.03 13:05:19 2013.07.01 00:03:59   calc2

2013.11.03 13:05:17 2013.07.01 00:02:59   calc2

 

look at the first, time column... 

 

 


 

Indicator2:

#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   Print("init2");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
Print("calc2");
   return(rates_total);
  }

 Indicator1:

#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   iCustom(_Symbol,_Period,"indicator2");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   Print("calc1");
//--- return value of prev_calculated for next call
   return(rates_total);
  }

Result:

2013.11.03 13:31:02     2013.01.02 00:05:12   calc1
2013.11.03 13:31:02     2013.01.02 00:05:12   calc2
2013.11.03 13:31:02     2013.01.02 00:05:12   calc2
2013.11.03 13:30:59     2013.01.02 00:05:09   calc1
2013.11.03 13:30:59     2013.01.02 00:05:09   calc2
2013.11.03 13:30:59     2013.01.02 00:05:09   calc2
2013.11.03 13:30:56     2013.01.02 00:05:06   calc1
2013.11.03 13:30:56     2013.01.02 00:05:06   calc2
2013.11.03 13:30:56     2013.01.02 00:05:06   calc2
2013.11.03 13:30:53     2013.01.02 00:05:03   calc1
2013.11.03 13:30:53     2013.01.02 00:05:03   calc2
2013.11.03 13:30:53     2013.01.02 00:05:03   calc2
2013.11.03 13:30:50     2013.01.02 00:05:00   calc1
2013.11.03 13:30:50     2013.01.02 00:05:00   calc2
2013.11.03 13:30:50     2013.01.02 00:05:00   calc2
2013.11.03 13:30:47     2013.01.01 00:00:00   calc1
2013.11.03 13:30:47     2013.01.01 00:00:00   calc2
2013.11.03 13:30:47     2013.01.01 00:00:00   calc2
2013.11.03 13:30:47     2013.01.01 00:00:00   init2
 

I copied your code, for me it prints only ones for indicaor 2.


 
Candles:

I copied your code, for me it prints only ones for indicaor 2.


He is talking about the Strategy Tester.
 
thomasfx67:

Indicator2:

 Indicator1:

Result:

I confirm this behaviour. But still my first question remains : so what ?

If you think it's a problem you can report this to ServiceDesk.

 
angevoyageur:
He is talking about the Strategy Tester.
Yes only Strategy tester shows this behaviour. Latest mt5 Version and confirmed on three physically different machines. win 2012, 7 and 8.1

I am a professional Developer and would guess this is a Bug. 

Perhaps i should use a boolean toggle and just use the first call, but this should be fixed. 
 
angevoyageur:

I confirm this behaviour. But still my first question remains : so what ?

If you think it's a problem you can report this to ServiceDesk.

Ok thank you, i will do so. I just did not See any bugtracker
Get in touch with developers using Service Desk!
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 
Ah, you are of course right angevoyageur. I tested in in Strategy Tester and for some reason OnCalculate is executed twice on indicator 2 and only ones on indicator 1. I do not know why. Hope someone else does.
 
angevoyageur:

I confirm this behaviour. But still my first question remains : so what ?

The problem is, that i will have to redesign a big part of my ea. 

Perhaps i do not see anything you see clear? But why do you think, this should not be a problem? It is not just a performance problem, but filtering out those false ticks (not sure if possible consitent), costs performance again.

Reason: