Download MetaTrader 5
To add comments, please log in or register
Do you keep a blog? Place a link to MQL5.community there and make money!
LouK
143
LouK 2015.07.06 15:31 

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.
whroeder1
13641
whroeder1 2015.07.06 17:09  
  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.)
Алексей Тарабанов
7220
Алексей Тарабанов 2015.07.06 22:30  
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. 
Ovo Cz
2953
Ovo Cz 2015.07.07 06:30  

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?

whroeder1
13641
whroeder1 2015.07.07 11:57  
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");
Ovo Cz
2953
Ovo Cz 2015.07.07 15:16  
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. 

whroeder1
13641
whroeder1 2015.07.07 17:24  
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

Ovo Cz
2953
Ovo Cz 2015.07.07 18:32  
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? 

whroeder1
13641
whroeder1 2015.07.07 18:39  
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.
Ovo Cz
2953
Ovo Cz 2015.07.07 18:46  

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.  

ffoorr
985
ffoorr 2015.11.16 09:20  
 if(Time[0]!=time0)
   {
    time0=Time[0]; // RUN ONCE PER BAR
    return(0);

      // DO INDICATOR STUFF
   }
try that
/
To add comments, please log in or register