CopyBuffer() return -1 in case of error, or the number of copied values. This number can differ from the requested bar's count.
In fact the code you provided isn't very robust. The code who follows to send order, needs index 1 and 2 for both buffers. That means 3 values should be copied, as the code is executed if copied>0, and copied=2 for example, SmoothedBuffer1[2] and SmoothedBuffer2[2] will not be available and you will get a bug. Here is an example of how to improve things :
... //---- Copy latest MA indicator values into a buffer if(CopyBuffer(handle1,0,0,4,SmoothedBuffer1)>=3 && CopyBuffer(handle2,0,0,4,SmoothedBuffer2)>=3) { //---- If MAPeriod > MAPeriod+2 -> BUY if(SmoothedBuffer1[1]>SmoothedBuffer2[1] && SmoothedBuffer1[2]<SmoothedBuffer2[2]) ...
Also there is no need for 4 values if you only use 3. And no need to use dynamic arrays when number of values if known and constant. But it's an other matter.
CopyBuffer() return -1 in case of error, or the number of copied values. This number can differ from the requested bar's count.
In fact the code you provided isn't very robust. The code who follows to send order, needs index 1 and 2 for both buffers. That means 3 values should be copied, as the code is executed if copied>0, and copied=2 for example, SmoothedBuffer1[2] and SmoothedBuffer2[2] will not be available and you will get a bug. Here is an example of how to improve things :
Also there is no need for 4 values if you only use 3. And no need to use dynamic arrays when number of values if known and constant. But it's an other matter.
Mr. Angevoyageur
Thank you very much.
Your detailed explanation is very helpful.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I have a very simple question.
I have posted entire code at very end.
I understand that CopyBuffer returns number of duplicated bars into Buffer.
Why does second CopyBuffer duplicate bars only when copied > 0? is it because at least CopyBuffer(handle1,0,0,4,SmoothedBuffer1) needs to be successful in prior?
Also, I have one more question.
I understand that code above is proceeded only when copied >0( where SmoothedBuffer1 and Smootedbuffer2f successfully receive duplicated bars),
However, do we have another case, other then ERROR, such that copied <= 0? Maybe very beginning of bars when bar counts is less than 4?
Thank you