CopyBuffer() on iVolume() handle fails with 4807 error (invalid handle)

HenZen  

Greets,

I've been googling around for a solution to this to no avail. Hopefully someone else has experienced this and knows what the solution is.

So CopyBuffer() expects an int handle as the first argument.  This works fine for most indicators, except iVolume() which returns a long. Typecasting should take care of it (as below), but I always get a 4807 error.

Snippet:

int Volume; // also tried as long with typecast (int) in CopyBuffer()
int MA;

int OnInit() {
        Volume = (int)iVolume(_Symbol, _Period, 0);
        if (Volume == INVALID_HANDLE) {
                PrintFormat("Failed to create handle of the Volume indicator:", GetLastError()); 
                return INIT_FAILED;
        }

        MA = iMA(_Symbol, _Period, 200, 0, MODE_EMA, PRICE_CLOSE);
        if (MA == INVALID_HANDLE) {
                PrintFormat("Failed to create handle of the MA indicator:", GetLastError()); 
                return INIT_FAILED;
        }
        return(INIT_SUCCEEDED);
}

void OnTick() {
        double buff[];

        // Fails with 4807 (invalid handle), no matter which method is used
        //if (CopyBuffer((int)Volume, 0, 0, 1, buff) < 0)
        if (CopyBuffer(Volume, 0, 0, 1, buff) < 0)
                Print("ERROR: CopyBuffer() for Volume failed: ", GetLastError());
                
        // Succeeds
        if (CopyBuffer(MA, 0, 0, 1, buff) < 0)
                Print("ERROR: CopyBuffer() for MA failed: ", GetLastError());
        
}

The MA handle works just fine. The Volume handle appears to work, returning a value on the iVolume() call, but CopyBuffer() keeps returning a 4807 error, no matter how I try and typecast or use/not use the long type on the Volume variable.

This seems so trivial, but frustratingly I cannot find a solution.

Any ideas?

Thanks

Dominik Egert  
HenZen:

Greets,

I've been googling around for a solution to this to no avail. Hopefully someone else has experienced this and knows what the solution is.

So CopyBuffer() expects an int handle as the first argument.  This works fine for most indicators, except iVolume() which returns a long. Typecasting should take care of it (as below), but I always get a 4807 error.

Snippet:

The MA handle works just fine. The Volume handle appears to work, returning a value on the iVolume() call, but CopyBuffer() keeps returning a 4807 error, no matter how I try and typecast or use/not use the long type on the Volume variable.

This seems so trivial, but frustratingly I cannot find a solution.

Any ideas?

Thanks

iVolume does not give you a handle....

You need to check the docs on iVolume. Your use of CopyBuffer is correct though.
HenZen  
Dominik Egert #:
iVolume does not give you a handle....

You need to check the docs on iVolume. Your use of CopyBuffer is correct though.

Thanks, you are correct - I completely missed that:

Returns the tick volume of the bar 


Your link goes to order_calc_margin, btw.


Thanks

HenZen  
Carl Schreiber #:

Read the docs! Put the cursor on CopyBuffer and press F1 :)

Here you have the alternative from MQ website: https://www.mql5.com/en/docs/series/copybuffer

Do you see the example??

Thanks for the response!!

Unfortunately, the CopyBuffer doc does not explain what is going wrong, but Dominik's response cleared up my error.

For reference, the solution is to use CopyTickVolume(), which comes from the iVolume() doc page.

Reason: