Would like help understanding the Custom Indicator running twice

 

I'm sure it's out there, but I have not found a discussion of this:

 

Brief: The Custom Indicator runs twice each bar, once on it's own and again when referenced in the EA. Is this a necessary behavior or am I doing something fundamentally wrong?


Detail, as short as I can make it:

1. Here's a Custom Indicator:

#property indicator_separate_window
#property indicator_buffers 1

double myArray[];
datetime time0;

int init(){
   IndicatorDigits(5);
   SetIndexBuffer(0,myArray);SetIndexStyle(0,DRAW_LINE);
}

int start(){
   if(Time[0]!=time0){time0=Time[0]; // RUN ONCE PER BAR
      Alert("How's yer father?");
      // DO INDICATOR STUFF TO POPULATE myArray
   }

 2. Here's an EA referencing the CI:

datetime time0;

int start(){
   if(Time[0]!=time0){time0=Time[0]; // RUN ONCE PER BAR
      double x=iCustom(Symbol(),Period(),"MyIndicator",0,0);
      // DO EA STUFF
   }
   return 0;
}

• When I compile the CI, it runs once, I see the Alert once.

• When I compile the EA, the CI runs once, I see the Alert once. The EA referencing the CI apparently recompiles the CI, as the var time0 in the CI resets.

• But when a new Bar comes along, the CI runs twice, the Alert appears twice. It appears to run once on it's own and a second time per the EA.

 

Q: What am I doing wrong here? Is there a way to tell the EA to just read what the CI has already put in the buffer and not rerun the CI?

Any help would be greatly appreciated. Thanks.
 
  1. Reading via iCustom does not run the indicator, all it does is read out the buffers.
  2. You must have the indicator running on two different charts, or one with non-default parameters, and one with default parameters (iCustom.)
 
WHRoeder:
  1. Reading via iCustom does not run the indicator, all it does is read out the buffers.
  2. You must have the indicator running on two different charts, or one with non-default parameters, and one with default parameters (iCustom.)
Demon in details. 
 

Actually, reading via iCustom runs the OnCalculate() method every time, and its first call (with unique parameters) runs OnInit() before the OnCalculate.

 BTW, why do you so often use the obsolete event methods naming, while the Documentation has been describing the newer ones for nearly two years?

 
Ovo: Actually, reading via iCustom runs the OnCalculate() method every time,
Where do you get that?
  1. Event Handling Functions - MQL4 Documentation
    The OnCalculate() function is called only in custom indicators when it's necessary to calculate the indicator values by the Calculate event. This usually happens when a new tick is received for the symbol, for which the indicator is calculated. This indicator is not required to be attached to any price chart of this symbol.
  2. Client Terminal Events - MQL4 Documentation
    The Calculate event is generated only for indicators right after the Init event is sent and at any change of price data. It is processed by the OnCalculate function.
Emphasis added.
Tick comes in, Indicators get called. EA gets called and calls iCustom. Why don't you try, move your indicator's Alert outside the once per bar and you'll see OnCalculate without any iCustom. or try:
OnTick(){
Print("EA sleeping"); sleep(10000);
Print("EA iCustom");
... iCustom()
Print("EA done");
 
WHRoeder:
Ovo: Actually, reading via iCustom runs the OnCalculate() method every time,
Where do you get that?
  1. Event Handling Functions - MQL4 Documentation
    The OnCalculate() function is called only in custom indicators when it's necessary to calculate the indicator values by the Calculateevent. This usually happens when a new tick is received for the symbol,for which the indicator is calculated. This indicator is not requiredto be attached to any price chart of this symbol.
  2. Client Terminal Events - MQL4 Documentation
    The Calculateevent is generated only for indicators right after the Init event issent and at any change of price data. It is processed by the OnCalculate function.
Emphasis added.
Tick comes in, Indicators get called. EA gets called and calls iCustom. Why don't you try, move your indicator's Alert outside the once per bar and you'll see OnCalculate without any iCustom. or try:

I am afraid I have a different Terminal than you have, as I could not see OnCalculate() call without iCustom. Perhaps you should follow your advice and try the things out. 

 
Ovo: I am afraid I have a different Terminal than you have, as I could not see OnCalculate() call without iCustom. Perhaps you should follow your advice and try the things out.
Perhaps you should follow your own advice before posting. If you only call iCustom, it calls onCalc once. If you attach it to a chart, it calls onCalc per tick. You have two indicators running.
 Indicator EA 
void OnTick()
  {
Print("ea ontick");
Sleep(10000);
Print("ea iCustom");
iCustom(NULL,0, "test",0,0);
Print("ea iCustom end");
  }
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("indicator ontick");
   return(rates_total);
  }
x
iCustom only
On chart and iCustom
 2015.07.07 13:48:47.926    Expert test EURUSD,H1: removed
2015.07.07 13:48:47.926    Custom indicator test EURUSD,H1: removed
2015.07.07 13:48:47.926    test EURUSD,H1: uninit reason 1
2015.07.07 13:48:47.924    test EURUSD,H1: uninit reason 1

2015.07.07 13:48:14.165    test EURUSD,H1: ea iCustom end
2015.07.07 13:48:14.165    test EURUSD,H1: indicator ontick
2015.07.07 13:48:14.165    test EURUSD,H1: ea iCustom
2015.07.07 13:48:04.151    test EURUSD,H1: ea ontick

2015.07.07 13:48:04.151    test EURUSD,H1: ea iCustom end
2015.07.07 13:48:04.150    test EURUSD,H1: indicator ontick
2015.07.07 13:48:04.150    test EURUSD,H1: initialized
2015.07.07 13:48:04.146    Custom indicator test EURUSD,H1: loaded successfully

2015.07.07 13:48:04.140    test EURUSD,H1: ea iCustom
2015.07.07 13:47:54.117    test EURUSD,H1: ea ontick
2015.07.07 13:47:52.034    test EURUSD,H1: initialized
2015.07.07 13:47:50.038    Expert test EURUSD,H1: loaded successfully

2015.07.07 13:18:23.160    Custom indicator test EURUSD,H1: removed
2015.07.07 13:18:23.160    test EURUSD,H1: uninit reason 1
2015.07.07 13:18:23.044    test EURUSD,H1: indicator ontick
2015.07.07 13:18:14.404    test EURUSD,H1: indicator ontick

2015.07.07 13:18:12.411    Expert test EURUSD,H1: removed
2015.07.07 13:18:12.411    Custom indicator test EURUSD,H1: removed
2015.07.07 13:18:12.411    test EURUSD,H1: uninit reason 1

2015.07.07 13:18:12.407    test EURUSD,H1: uninit reason 1
2015.07.07 13:18:12.407    test EURUSD,H1: ea iCustom end
2015.07.07 13:18:12.407    test EURUSD,H1: indicator ontick
2015.07.07 13:18:12.407    test EURUSD,H1: ea iCustom
2015.07.07 13:18:11.034    test EURUSD,H1: indicator ontick
2015.07.07 13:18:05.776    test EURUSD,H1: indicator ontick
2015.07.07 13:18:05.605    test EURUSD,H1: indicator ontick
2015.07.07 13:18:05.590    test EURUSD,H1: ea ontick

2015.07.07 13:18:05.590    test EURUSD,H1: ea iCustom end
2015.07.07 13:18:05.590    test EURUSD,H1: indicator ontick
2015.07.07 13:18:05.590    test EURUSD,H1: initialized
2015.07.07 13:18:05.587    Custom indicator test EURUSD,H1: loaded successfully

2015.07.07 13:18:05.582    test EURUSD,H1: ea iCustom
2015.07.07 13:18:03.857    test EURUSD,H1: indicator ontick
2015.07.07 13:17:55.637    test EURUSD,H1: indicator ontick

2015.07.07 13:17:55.563    test EURUSD,H1: ea ontick
2015.07.07 13:17:55.561    test EURUSD,H1: indicator ontick
2015.07.07 13:17:53.349    test EURUSD,H1: indicator ontick
2015.07.07 13:17:53.260    test EURUSD,H1: indicator ontick
2015.07.07 13:17:53.147    test EURUSD,H1: indicator ontick
2015.07.07 13:17:52.221    Expert test EURUSD,H1: loaded successfully
2015.07.07 13:17:44.593    test EURUSD,H1: indicator ontick
2015.07.07 13:17:44.593    test EURUSD,H1: initialized

2015.07.07 13:17:41.455    Custom indicator test EURUSD,H1: loaded successfully

 
WHRoeder:
Ovo: Actually, reading via iCustom runs the OnCalculate() method every time,
Where do you get that?
........
........
  1. Emphasis added.
Tick comes in, Indicators get called. EA gets called and calls iCustom. Why don't you try, move your indicator's Alert outside the once per bar and you'll see OnCalculate without any iCustom. or try:

 Using your words, which part of the previous communication was unclear? 

 
Ovo: Using your words, which part of the previous communication was unclear?
as I could not see OnCalculate() call without iCustom
is wrong. I showed where OnCalculate is called per tick as I showed.
 

Ok, once more, from the beginning:

WHRoeder:
  1. Reading via iCustom does not run the indicator, all it does is read out the buffers.
  2. You must have the indicator running on two different charts, or one with non-default parameters, and one with default parameters (iCustom.) 
Ovo:

Actually, reading via iCustom runs the OnCalculate() method every time, and its first call (with unique parameters) runs OnInit() before the OnCalculate.

WHRoeder:
Ovo: Actually, reading via iCustom runs the OnCalculate() method every time,
Where do you get that?
  1. Event Handling Functions - MQL4 Documentation
    blah blah ...
  2. Emphasis added.
Tick comes in, Indicators get called. EA gets called and calls iCustom. Why don't you try, move your indicator's Alert outside the once per bar and you'll see OnCalculate without any iCustom. or try:
Ovo:

I am afraid I have a different Terminal than you have, as I could not see OnCalculate() call without iCustom. Perhaps you should follow your advice and try the things out.  

 
 if(Time[0]!=time0)
   {
    time0=Time[0]; // RUN ONCE PER BAR
    return(0);

      // DO INDICATOR STUFF
   }
try that
Reason: