Hello everyone,
I'm trying to get data from a custom indicator using the CopyBuffer() function. When I call the function giving it a buffer index greater than 4, I get only zeroes in my destination buffer despite the data being correct inside the indicator (I've double-checked with debugger and Print()).
Here's a snippet of the buffers implementation in the indicator :
Then in my EA, in the OnInit() function, I create the indicator handle I need using iCustom() and check for its validity. After this I capture an event from another indicator using OnChartEvent() and when the correct event is received I try to recover the latest indicators data using CopyBuffer() like so :
This method works flawlessly for every indicator while I'm using buffer indices below 5 but the data I need from this specific indicator is stored in buffers 5, 6 and 7 (UpBuffer, DownBuffer and TrendBuffer respectively).
How could I get the correct values from these buffers ? I've spent the last 4 hours searching the doc and forum but I don't understand why this doesn't work with buffer indices above 4. Does anyone have an explanation ?
How many buffers is the custom indicator exposing?
How many appear in the Data Window?
Zero could actually be valid value and only occasionally have an something else, given that they are "Signal" buffers.
How many buffers is the custom indicator exposing?
How many appear in the Data Window?
Zero could actually be valid value and only occasionally have an something else, given that they are "Signal" buffers.
First of all, thank you for your quick reply !
As I understand it, the indicator is exposing all the 8 buffers in the snippet I provided above, via the line :
#property indicator_buffers 8
However, as you've recommended I've checked the data window and none of these buffers appears in it. There's only the ones given as parameters of the OnCalculate() function which are Date, Time, Open, High, Low, Close, Volume, Tick Volume and Spread.
I didn't thought to verify this yesterday but I find it strange that the buffers didn't appear in it because I see that for the other indicators I use, their buffers are correctly displayed in the data window and the ones given to OnCalculate() are not.
Concerning the zeroes, even if in this case they could be valid values, they shouldn't be because in debugging the indicator, I can see that the buffers I want to recover contain only numbers different from zero. Could you develop a bit about what you mean by "Signal" buffers ?
The main difference with the indicator causing me trouble and the others I use is that this one is directly on the chart and not on a separate indicator window. Could this be the issue here ? And if so, could you explain me why ?
That is the number of buffers, and what is the number of plots (which is what will be displayed in the Data Window)?
No, that is not the cause.
I have just noticed now, but the answer to the problem may have been starring me in the face from the vary first post
Buffers 0 to 4 are declared as "Calculations" right at the beginning, followed by the "data" buffers at the end.
Unless you have have set the number of plots correctly this can become "messy".
Instead, I suggest you place the "Calculation" buffers at the end and the "data" buffers in the beginning for a more intuitive access from CopyBuffer, and also for setting the correct number of plots more intuitively too.
#property indicator_plots 3 #property indicator_buffers 8
SetIndexBuffer(0, UpBuffer, INDICATOR_DATA); SetIndexBuffer(1, DownBuffer, INDICATOR_DATA); SetIndexBuffer(2, TrendBuffer, INDICATOR_DATA); SetIndexBuffer(3, BuyBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(4, SellBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(5, FillBuffer_a, INDICATOR_CALCULATIONS); SetIndexBuffer(6, FillBuffer_b, INDICATOR_CALCULATIONS); SetIndexBuffer(7, iATRBuffer, INDICATOR_CALCULATIONS);
Now you can access them more intuitively as buffers/plots 0 to 2 from CopyBuffer.
bool copy_ok = CopyBuffer(indicator_handle, 0, 1, 1, destination_buffer); // UpBuffer plot
That is the number of buffers, and what is the number of plots (which is what will be displayed in the Data Window)?
No, that is not the cause.
I have just noticed now, but the answer to the problem may have been starring me in the face from the vary first post
Buffers 0 to 4 are declared as "Calculations" right at the beginning, followed by the "data" buffers at the end.
Unless you have have set the number of plots correctly this can become "messy".
Instead, I suggest you place the "Calculation" buffers at the end and the "data" buffers in the beginning for a more intuitive access from CopyBuffer, and also for setting the correct number of plots more intuitively too.
Now you can access them more intuitively as buffers/plots 0 to 2 from CopyBuffer.
I've changed the number of plots from 3 to 6 and modified the buffer declarations like so :
SetIndexBuffer(0, BuyBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(1, SellBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(2, FillBuffer_a, INDICATOR_CALCULATIONS); SetIndexBuffer(3, FillBuffer_b, INDICATOR_CALCULATIONS); SetIndexBuffer(4, UpBuffer, INDICATOR_DATA); SetIndexBuffer(5, DownBuffer, INDICATOR_DATA); SetIndexBuffer(6, TrendBuffer, INDICATOR_DATA); SetIndexBuffer(7, iATRBuffer,INDICATOR_CALCULATIONS);And now it works ! The indicator plots the values of buffers 0, 1, 2 and 3 on the chart and I can access the values of buffers 4, 5 and 6 by using CopyBuffer in my EA like I wanted.
I had misunderstood the usage of #property indicator_buffers, thinking that was the line exposing my buffers but instead, as you've stated it's #property indicator_plots which does this.
Thank you very much for your help !
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to get data from a custom indicator using the CopyBuffer() function. When I call the function giving it a buffer index greater than 4, I get only zeroes in my destination buffer despite the data being correct inside the indicator (I've double-checked with debugger and Print()).
Here's a snippet of the buffers implementation in the indicator :
Then in my EA, in the OnInit() function, I create the indicator handle I need using iCustom() and check for its validity. After this I capture an event from another indicator using OnChartEvent() and when the correct event is received I try to recover the latest indicators data using CopyBuffer() like so :
This method works flawlessly for every indicator while I'm using buffer indices below 5 but the data I need from this specific indicator is stored in buffers 5, 6 and 7 (UpBuffer, DownBuffer and TrendBuffer respectively).
How could I get the correct values from these buffers ? I've spent the last 4 hours searching the doc and forum but I don't understand why this doesn't work with buffer indices above 4. Does anyone have an explanation ?