Issue with CopyBuffer returning an array full of 0.0.

 

I managed to get input from a built-in indicator, so now I wanted to try with a custom made indicator. I downloaded an indicator off the internet and put it in MT5. It successfully shows the values in a buffer.

[picture of the indicator with buffer.] (For some reason it doesn't accept my link. But trust me it shows an actual value)

I used iCustom to get the handler from the indicator:

      fisher = iCustom(Symbol(), PERIOD_M1, "Fisher");

This is pretty much the only place where I think I could be making an error. The documentation for iCustom has parameters field, but it says that if they are left empty, the indicator will have default values, which I checked the indicator and it looks like it has defaults:

input int period = 10;

Then I used CopyBuffer on the custom indicator the same way I used it on a built-in indicator:

int copied = CopyBuffer(handle, buffer, startPos, count, arrBuffer);

The problem is, with the custom indicator, I get an array, that is full of 0.0 values, whereas with the built-in one I get actual values. Both indicators have buffers.

I've attached the custom indicator in case you need to look into it, but I'm fairly certain I'm the one not doing something correctly, due to there being legit values in the buffer when I place the indicator in a graph.

I also have another question, which is when copying with CopyBuffer into an array called arrBuffer[].

Is the value of arrBuffer[0] going to be the latest (right-most) value of the indicator, or the oldest (left-most) value?

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
Counting of elements of copied data (indicator buffer with the index buffer_num) from the starting position is performed from the present to the past, i.e., starting position of 0 means the current bar (indicator value for the current bar). When copying the yet unknown amount of data, it is recommended to use a dynamic array as a buffer[]...
Files:
Fisher.mq5  4 kb
 

Error number 1:

Never put your hands into the name of arrays.

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[])
{

There is a regulation and only it is correct:

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[])
  {
 

Error 2:

when working with 'double', you cannot divide by '2' - you need to divide by '2.0'.

 

Error 3:

You have division operations — but you never check division by zero.

 
Like that:
Files:
 
Is ?0.0 all I need to avoid dividing by 0?
 
Vladimir Karputov:
Like that:

You also seem to have added a third buffer. When I was browsing the forum earlier I saw a post mentioning always having at least 2 buffers. (He wasn't clear as to why and the post was old anyway) Is INDICATOR_CALCULATIONS required to process the buffer values in an EA?

 
Vladimir Karputov:

Error 2:

when working with 'double', you cannot divide by '2' - you need to divide by '2.0'.

You can divide by 2, this not an error. You could even use doublevar=2/doubleconst and the result would still be correct

DichoMire:
Is ?0.0 all I need to avoid dividing by 0?

No, a division by zero would stop any EA or indicator and would not return such results.

DichoMire:

I used iCustom to get the handler from the indicator:

This is pretty much the only place where I think I could be making an error. The documentation for iCustom has parameters field, but it says that if they are left empty, the indicator will have default values, which I checked the indicator and it looks like it has defaults:

Then I used CopyBuffer on the custom indicator the same way I used it on a built-in indicator:


I did not read the whole code, but I stumbled at these sentences. iCustom initiates the background calculation of an indicator, this does NOT mean, that the data is ready when you try to access it and this is where zero values could come from. Now you may ask, when this data is ready, but the answer is: Nobody knows, it's available when it's done. Sleep(n) won´t help you here. My experience is, that the only solutions is an asynchronous check, based on a timer.

 
Doerk Hilger :


If you have a division operation, you MUST check the divisor for equality to zero.

 
DichoMire :

You also seem to have added a third buffer. When I was browsing the forum earlier I saw a post mentioning always having at least 2 buffers. (He wasn't clear as to why and the post was old anyway) Is INDICATOR_CALCULATIONS required to process the buffer values in an EA?

The third buffer stores the calculated values.

I made a third buffer on purpose — I fixed your mistake: you saved the value to a variable.

 
hi master programers can anyone explain this please I want to learn this
Reason: