Indicator of Indicator of Indicator or When the handle is calculated?

 

Hello mql5 community. I have this problem and I can not find an explanation anywhere, but maybe somebody ran into this situation before.

Let's say I have written 3 indicators. All of them work fine, all of them use the full version of OnCalculate. First indicator just use price data. Second and third indicator get a handle of built-in mql indicator and use it for calculations.

Now I am writing a new more advanced indicator which will use my previous ones, and as a first step I create a handle of my old indicator and read its values, nothing else.

If I do this with the first indicator - it works correct. It draws the same indicator, it writes to file the same values.

But if I make a handle of second or third indicator the unexpected things start to happen - it doesn't read correct values, sometimes it can draw the same indicator, sometime it draws nothing, sometimes it can draw some _totally unrelated_ graph!

 

Just to make sure:

1. I  create handle in OnInit and deinitialize it in OnDeInint. I check for INVALID_HANDLE and it is always created correctly.

2.  I make a call to CopyBuffer inside OnCalculate and check for return value. CopyBuffer doesn't give an error but it returns a zero (which is wrong).

3. I tried to call CopyBuffer inside OnInint right after handle creation and then CopyBuffer returns an error 4806 ERR_INDICATOR_DATA_NOT_FOUND.

 

So when I have indicator of indicator - it works fine. But when I do Indicator of indicator of indicator - then it fails! I feel like it must be because the very first indicator (the most inner one) has the handle not initialized properly, but can not understand why or how to fix this.

 

This is essential for me. And I would think it would be for many people since you usually build your ideas on top of your previous ones. To have several levels of inclusion is normal, three level is just a simple case. What is going to happen when I will do something on top of this indicator? :(

 

Normally, when you call CopyBuffer the requested indicator data is checked, and if not ready, its update and calculation is starting, which may take some time. Until it is ready you may get 0 data from CopyBuffer.

But it's not clear what is the "second of third indicator"? You wrote that 2-nd and 3-rd indicators are separate. Did you mean "second OR third"? Anyway, please, provide more info and code.

 
abeylin:
...

2.  I make a call to CopyBuffer inside OnCalculate and check for return value. CopyBuffer doesn't give an error but it returns a zero (which is wrong).

Is it always returning 0 ? What do you do when it returns 0 ?

Please show relevant code.

 

Hi guys, thanks for taking note of my question, more code is below. Marketeer - it is "second or third" I corrected it.  Angevoyageur - no, it is probably not always returning zero. I say probably because I see a graph of the indicator sometimes - with the same code it sometimes work and sometimes not. Tottally random. I would just press recompile button in the editor several times, and sometimes the graph appear, sometimes it disappear again. But always for the first call there is no data. I say " CopyBuffer doesn't give an error but it returns a zero" just because I print out the values into file and that is what I see in the file, just zeroes. So I don't do anything just print out the values.

 Below is the code. It is pretty simple, it just calls an indicator and print out its values into file. 


int handle; // Indicator handle

double Buffer[];


void OnInit () {

    SetIndexBuffer (0, Buffer, INDICATOR_DATA);

   

    handle = iCustom (_Symbol,_Period, "Custom\\Indicator",

                      PERIOD_M1, 120, MODE_EMA, PRICE_CLOSE); // Parameters of the indicator

         

   if (handle == INVALID_HANDLE) Print ("Can not get Handle!!! Error: " + GetLastError());

   else Print ("Handle is correct");

}


void OnDeinit (const int reason) {

    IndicatorRelease (handle);

}


int OnCalculate ( ... standard OnCalcaulte variables, full list ...) {   

    if (prev_calculated==0) {

        Buffer[0] = 0;

        return(1);

    }

    if (prev_calculated==rates_total) return(rates_total);

   

    int start=prev_calculated-1;

    

    for (int i=start; i<rates_total; ++i) {

        Buffer[i] = DBL_MIN;

        Buffer[i] = iIndicator (handle, 0, time[i]);

        if (Buffer[i] == DBL_MIN) {

            WriteToFile ("Waiting for indicator to calculate," + i);

            return i;

        }


        WriteToFile (i + "," + time[i] + "," + Buffer[i]);

    } // end of for(i) cycle

   return rates_total;

}

//+------------------------------------------------------------------+


int WriteToFile (string text) { ... simple func to print string into file ...}


double iIndicator (int hdl, int buf_number, datetime t) {

    double i[1]={0};

    if(CopyBuffer(hdl,buf_number,t,1,i)<0) { Print ("Error of copying Indicator Values: ",GetLastError()); return 0; }

    return(i[0]);

}

 

 

Structure of indicator calls is like this:

Indicator_2 :

1. Initialize handle of Standart built-in MT5 indicator - handle(Built_in_Indicator)

2. Do calcualtions with price and values recieved from handle(Built_in_Indicator)

3. Result is a single value 

 

Indicator_3:

1. Initialize handle of my Indicator_2 - handle(Indicator_2) 

2. Get current value from handle(Indicator_2) 

3. Return the value and print it to file

 

Here is what I am worried about. During the call of handle(Indicator_2), at some point handle(Built_in_Indicator) should be initialized. When do this happen and is it possible to control this?

How/When does handle initialization happen in the first place?

 

My Indicator_3 probably returns zeroes at the point when I call the handle(Indicator_2), but handle(Built_in_Indicator) is not calculated yet. Anybody had similar things before?

 
abeylin:

 

Well, I think that your indicators are just being calculated for some time. You can add more logging into OnCalculate for both indicators and see how prev_calculated changes there tick by tick.