Can someone explain why I'm getting a 4806 error code on CopyBuffer()?

 

The documentation for the iMA indicator shows the following line of code in the example at the bottom of the page (https://www.mql5.com/en/docs/indicators/ima):

   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0)

When I test that code, whether the "shift" (which shows in the function explanation as start_pos) is negative or positive, it raises no errors -- but then neither does it seem to behave differently, either, no matter what the start_pos value is. The same number of values get copied regardless (1 extra because a new bar popped up on the chart between take 2 and 3) and the result array shows the same values no matter where CopyBuffer "starts" its copy.

Can someone explain?

And what's even more baffling is that if I put the same code into a script, I get run-time error 4806 if the start_pos value is anything other than zero.

#property strict
void OnStart()
{
   double dData0[];
   double dData1[];
   double dData2[];
   ResetLastError();
   int ind_handle=iMA(NULL,_Period,15,0,MODE_EMA,PRICE_CLOSE);
   int iErr=_LastError;
   Sleep(100);
   ResetLastError();
   int amount = BarsCalculated(ind_handle);
   iErr=_LastError;
   int shift = 0;
   ResetLastError();
   int iCopyCNT0 = CopyBuffer(ind_handle,0,-shift,amount,dData0);
   iErr=_LastError;
   shift = 5;
   ResetLastError();
   int iCopyCNT1 = CopyBuffer(ind_handle,0,-shift,amount,dData1);
   iErr=_LastError;
   shift = -5;
   ResetLastError();
   int iCopyCNT2 = CopyBuffer(ind_handle,0,-shift,amount,dData2);
   iErr=_LastError;
}
Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

William, I'm not sure what your problem is, you seem to dislike me or something. I can't account for your dismissive attitude otherwise.

I have no idea why you assume the problem is my understanding of the documentation. I understand the documentation.

I have no idea why you assume the problem is that I haven't looked at the examples. I was clear that I'm talking about the example offered in the documentation, and I provided the link to the page. Perhaps you should look at the example. 😄

The failure of CopyBuffer to respond to the start_pos parameter has nothing to do with OnInit, since the CopyBuffer() call in that example was coded in a completely different function called from the OnCalculate() function. As would be clear if you looked at the example, the start_pos parameter is set by the user as one of the input parameters. That value controls the iMA indicator's ma_shift parameter in the OnInit function. iMA() is not CopyBuffer(). I did not ask about a problem with iMA().

The same input value is used (oppositely signed) to control the start_pos parameter of the CopyBuffer() call in the FillArrayFromBuffer() function, not the OnInit() function.

The problem remains that no matter what I change the shift/start_pos value to, although iMA() does indeed shift the plotted line on the chart, its oppositely signed counterpart as start_pos has no effect on the result of the CopyBuffer() call.

The runtime error happens in a script, I was clear about that too. OnInit() has nothing to do with scripts. Even the compiler would tell you (if you looked):


At this point I will ask that you not respond to my questions. I haven't liked your tone one whit lately, your replies illuminate nothing, and I don't want to waste any more of my time explaining to you how far wrong you seem bent on being. Thanks.

 
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
          How to call indicators in MQL5 - MQL5 Articles (2010)

I'm always interested in what people say, even if they're belligerent or give passive-aggressive "hints" (like I don't know how to ask a question, lol.) 

That's why I checked out the first of your EIGHT links lol.

Maybe you should read the links you throw about.

If you had, you might have gotten a security warning (or similar) like I did:

I try to stay away from misconfigured internet servers, so I'm afraid I won't be getting any benefit from that link, nor will I find any clues as to why you think I needed it. 🤔

 
Millard Melnyk #:

William, I'm not sure what your problem is, you seem to dislike me or something. I can't account for your dismissive attitude otherwise.

I have no idea why you assume the problem is my understanding of the documentation. I understand the documentation.

No you don't. Read CopyBuffer() documentation once again please.

The documentation "-shift" is in relation with the ma_shift parameter, you would not set different value if you had understood the documentation.Should be :

   int ind_handle=iMA(NULL,_Period,15, 0   shift ,MODE_EMA,PRICE_CLOSE);

I have no idea why you assume the problem is that I haven't looked at the examples. I was clear that I'm talking about the example offered in the documentation, and I provided the link to the page. Perhaps you should look at the example. 😄

The failure of CopyBuffer to respond to the start_pos parameter has nothing to do with OnInit, since the CopyBuffer() call in that example was coded in a completely different function called from the OnCalculate() function. As would be clear if you looked at the example, the start_pos parameter is set by the user as one of the input parameters. That value controls the iMA indicator's ma_shift parameter in the OnInit function. iMA() is not CopyBuffer(). I did not ask about a problem with iMA().

There is no failure of CopyBuffer().

The same input value is used (oppositely signed) to control the start_pos parameter of the CopyBuffer() call in the FillArrayFromBuffer() function, not the OnInit() function.

The problem remains that no matter what I change the shift/start_pos value to, although iMA() does indeed shift the plotted line on the chart, its oppositely signed counterpart as start_pos has no effect on the result of the CopyBuffer() call.

The runtime error happens in a script, I was clear about that too. OnInit() has nothing to do with scripts. Even the compiler would tell you (if you looked):


At this point I will ask that you not respond to my questions. I haven't liked your tone one whit lately, your replies illuminate nothing, and I don't want to waste any more of my time explaining to you how far wrong you seem bent on being. Thanks.

William could be right but you added a "Sleep(100)" which workaround this problem but could also fail eventually (if iMA or iXXX takes more than 100 ms to calculate the indicator).

What you have is expected behaviour as you are apparently printing array index 0 (your code doesn't match your screenshot, as it doesn't include the comment), and index 0 the OLDEST value. 1.79...E+308 being EMPTY_VALUE. All is correct.

About your topic "Can someone explain why I'm getting a 4806 error code on CopyBuffer()?". You don't show where you get it, there is no output in the provided script.

There is no problem to not understand something, arrogant attitude is a problem. If your future answer shows the same attitude, it will be my last answer to your posts. All your topics are obviously from a newbie on mql5, at least 3 experimented coders said it to you (from other topics). But still you came here with the attitude "I understand all and I have found bugs in mql5/MT5", after that you are wondering why people don't like your post and/or don't answer.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Alain Verleyen #:

No you don't. Read CopyBuffer() documentation once again please.

The documentation "-shift" is in relation with the ma_shift parameter, you would not set different value if you had understood the documentation.Should be :

There is no failure of CopyBuffer().

William could be right but you added a "Sleep(100)" which workaround this problem but could also fail eventually (if iMA or iXXX takes more than 100 ms to calculate the indicator).

What you have is expected behaviour as you are apparently printing array index 0 (your code doesn't match your screenshot, as it doesn't include the comment), and index 0 the OLDEST value. 1.79...E+308 being EMPTY_VALUE. All is correct.

About your topic "Can someone explain why I'm getting a 4806 error code on CopyBuffer()?". You don't show where you get it, there is no output in the provided script.

There is no problem to not understand something, arrogant attitude is a problem. If your future answer shows the same attitude, it will be my last answer to your posts. All your topics are obviously from a newbie on mql5, at least 3 experimented coders said it to you (from other topics). But still you came here with the attitude "I understand all and I have found bugs in mql5/MT5", after that you are wondering why people don't like your post and/or don't answer.

Glad to hear from you Alain.

I think we all need to calm down.

As far as ego and arrogance go, right back at ya big guy.

I hope you'll continue answering my questions because you have a lot to offer. I can still get the value of your expertise without any regard at all to your ego and arrogance. What I can't tolerate is ego and arrogance with no value added, and that's why I asked William to stop responding to me.

You:

The documentation "-shift" is in relation with the ma_shift parameter,

Incorrect. The variable name in the example's FillArrayFromBuffer() function is confusing, so I can see how it's easy to make a mistake.

"-shift" is in relation to the start_pos parameter of the CopyBuffer() function.

In the example code, the ma_shift parameter is here in the iMA() function call.

And the ma_shift value is passed to FillArrayFromBuffer() as "shift" for use in the CopyBuffer() call with an opposite sign. I've explained this clearly already.

Me:

You:

OK, how about provide some information about where I got it wrong, then? I see misunderstanding evidenced in what you said about "-shift". Maybe this statement of yours is a result of more misunderstanding. We can't know if your entire response boils down to "you didn't read the documentation" and "you're wrong", along with opining about me and my attitude. If you want to be considered an expert, and I do consider you one, share your expertise.

Don't just tell me that I'm wrong -- tell me where and how I'm wrong. I provided all the information you'd need to do so in my original post.

And nothing you said addresses the fact that the same code in an indicator will work but yet get run-time errors in a script. 

You:

The screen shot does not match the code I provided because the two are unrelated. The code for the screenshot (as I said at the get-go) is the code at https://www.mql5.com/en/docs/indicators/ima. The code I provided is code using the same CopyBuffer() calls that I took from that code at https://www.mql5.com/en/docs/indicators/ima and put into a script, which is the code I provided.

And my question remains untouched so far: why does that code get _LastError==0 in the indicator (code at https://www.mql5.com/en/docs/indicators/ima) and  _LastError==4806 in the script?

Yes, I'm well aware that the problem might be that I'm expecting something mistakenly. But what?

_According to the CopyBuffer() documentation_, if I change the start_pos parameter value, the value put into element [0] of the target array should come from a different position in the buffer. I _assume_ that after enough tries using real-time data with different start_pos values, positive and negative, the value of the element at [0] in the target array will change. So why do 0, 5, and -5 (and everything else I've tried) all yield exactly the same value? 

Please answer the question.

As far as your opining, I've said up front and all along that I'm an MQL4/5 newbie. I'm not an I.T./software/coding newbie. I've been involved with coding and systems since 1982, both in corporate and small business environments. I started learning MQL as I have done with most things, by hacking my way to an understanding. I began less than 2 years ago, no instruction of any kind. My comments about MQL come from broad, heavy experience in the field. So keep all that in mind if/when you're tempted to think that "newbie" defines me. I think we'll get along much better. I respect your obvious expertise, but I don't tolerate lack of respect for mine. Thanks.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I think the - (before shift) is just a typo no reason for such a fuss.

Don't cry solve your problem.

 
Millard Melnyk #:

Glad to hear from you Alain.

I think we all need to calm down.

As far as ego and arrogance go, right back at ya big guy.

I hope you'll continue answering my questions because you have a lot to offer. I can still get the value of your expertise without any regard at all to your ego and arrogance. What I can't tolerate is ego and arrogance with no value added, and that's why I asked William to stop responding to me.

You:

Incorrect. The variable name in the example's FillArrayFromBuffer() function is confusing, so I can see how it's easy to make a mistake.

"-shift" is in relation to the start_pos parameter of the CopyBuffer() function.

In the example code, the ma_shift parameter is here in the iMA() function call.

And the ma_shift value is passed to FillArrayFromBuffer() as "shift" for use in the CopyBuffer() call with an opposite sign. I've explained this clearly already.

Me:

You:

OK, how about provide some information about where I got it wrong, then? I see misunderstanding evidenced in what you said about "-shift". Maybe this statement of yours is a result of more misunderstanding. We can't know if your entire response boils down to "you didn't read the documentation" and "you're wrong", along with opining about me and my attitude. If you want to be considered an expert, and I do consider you one, share your expertise.

Don't just tell me that I'm wrong -- tell me where and how I'm wrong. I provided all the information you'd need to do so in my original post.

And nothing you said addresses the fact that the same code in an indicator will work but yet get run-time errors in a script. 

You:

The screen shot does not match the code I provided because the two are unrelated. The code for the screenshot (as I said at the get-go) is the code at https://www.mql5.com/en/docs/indicators/ima. The code I provided is code using the same CopyBuffer() calls that I took from that code at https://www.mql5.com/en/docs/indicators/ima and put into a script, which is the code I provided.

And my question remains untouched so far: why does that code get _LastError==0 in the indicator (code at https://www.mql5.com/en/docs/indicators/ima) and  _LastError==4806 in the script?

Yes, I'm well aware that the problem might be that I'm expecting something mistakenly. But what?

_According to the CopyBuffer() documentation_, if I change the start_pos parameter value, the value put into element [0] of the target array should come from a different position in the buffer. I _assume_ that after enough tries using real-time data with different start_pos values, positive and negative, the value of the element at [0] in the target array will change. So why do 0, 5, and -5 (and everything else I've tried) all yield exactly the same value? 

Please answer the question.

As far as your opining, I've said up front and all along that I'm an MQL4/5 newbie. I'm not an I.T./software/coding newbie. I've been involved with coding and systems since 1982, both in corporate and small business environments. I started learning MQL as I have done with most things, by hacking my way to an understanding. I began less than 2 years ago, no instruction of any kind. My comments about MQL come from broad, heavy experience in the field. So keep all that in mind if/when you're tempted to think that "newbie" defines me. I think we'll get along much better. I respect your obvious expertise, but I don't tolerate lack of respect for mine. Thanks.

HAHA! 

"The penny dropped" as they say.

So since you and William personalized this, allow me to let you get to know me a bit. Here's what I do when I've been a newbie pulling a bone-headed blunder...

I LMAO at myself.

You said it right there, but I missed it. My bad.

So I take back some of what I said, and thanks for pointing out what one of my mistake was. Yeah, I'm still shaky on wrapping my head around the backwards/forwards sequence when it comes to arrays. 

So I was mistaking [0] in the target array as the newest buffer element, not the oldest. Got it.

And I did fail to provide the code I inserted to get those comments, my bad again. I've fixed them to use the last element in the target array instead of [0].

However, the problem persists.

Here is the code I inserted:

        int iSZ=ArraySize(values);
        Comment("Copied <"+(string)iCopied+"> bars when start_pos="+(string)iShft+" values[iCopied-1]<"+(string)values[iCopied-1]+"> from array sized<"+(string)iSZ+">");

in context:

***


And here are the results. Still no change regardless of the value of start_pos:

Still looks like a problem to me. I'd love you to show me where I'm still making mistakes. (Again, in the interest of you getting to know me, that's not a sarcastic remark. I sincerely LOVE when people show me my mistakes. Who wants to go on making mistakes, lol?)

So as you can see, I tweaked the original code a bit to get more info about what's going on while stepping thru it in Debug Mode. Other than that it's exactly a copy-n-paste from the documentation and the changes have no bearing on the problem here.

It makes no sense to me that changing the value of start_pos would fail to affect both the number of elements copied and the contents in the target array.

 
Carl Schreiber #:

I think the - (before shift) is just a typo no reason for such a fuss.

Don't cry solve your problem.

I thought so too, Carl, but it's no typo. It's supposed to account for iMA() being shifted. Which would make sense if start_pos changed the results of CopyBuffer().

 

Alain Verleyen #:

About your topic "Can someone explain why I'm getting a 4806 error code on CopyBuffer()?". You don't show where you get it, there is no output in the provided script.

Missed this too.

I wasn't aware it would be necessary to show "where you get it". There are three lines of code where I could possibly get it. I ruled out the first when I said "... I get run-time error 4806 if the start_pos value is anything other than zero." I figured that was clear enough to understand that in the 2nd and 3rd call to CopyBuffer() (where the start_pos value is clearly something other than zero) I got the error 4806. But as an FYI, I see that error in _LastError as shown in the Debug tab of the Toolbox as I step through the code in Debug mode.

 

It's always necessary to provide technical details on a technical forum.

What do you think is the motivation of someone who knows the answers to post it ?

These trivial questions are boring for me. What motivates me is to learn something new, or to interact pleasantly with nice people. I have none of this here, sorry.

Also sometimes to not allow people to say wrong things, and this most of the time leads to unpleasant exchanges as most people are not able to take critics.

Reason: