CopyBuffer Error 4802 (Bug?)

 

Hello forum,

there are some strange occurrences when I call indicators in EAs. In this test I call iMA and iRSI and Copy their buffers. The initialisation throws no error, buf when copying, I get -1 as CopyBuffer return value. The buffers are resized to the data_size that I set but they contain only "0.0" values. Then when I try printing the buffer contents in a for loop, in debug observation the index is incremented, but it counts only "0" bar values...???

When I test the for loop to count up and print the indizes it does it no problem. Here it seems that CopyBuffer makes something about the for loop crash...? Installed MT5 build is 4153.

int handle_ma = 0;
double buf_ma[];
int handle_rsi = 0;
double buf_rsi[];
int data_size = 50;

ulong error_rsi = 0;
ulong error_ma = 0;


int check_init_ima = 0;
int check_init_rsi = 0;
int check_copy_ima = 0;
int check_copy_rsi = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   check_init_ima = iMA(_Symbol, _Period, 12, 0, MODE_SMA, PRICE_CLOSE);
   check_init_rsi = iRSI(_Symbol, _Period, 12, PRICE_CLOSE);              //both are initialised to valid handles 10 and 11
   
   ulong error_init=GetLastError();
   Print("Last Error: " + string(error_init));
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   check_copy_ima = CopyBuffer(handle_ma, 0, 0, data_size, buf_ma);
   if(check_copy_ima == -1)
     {
      error_ma = GetLastError();
      Print("Last Error: " + string(error_ma));
     }                                       // There are two different indicators that both produce 4802 errors when copying

   check_copy_rsi = CopyBuffer(handle_rsi, 0, 0, data_size, buf_rsi);
   if(check_copy_rsi == -1)
     {
      error_rsi = GetLastError();
      Print("Last Error: " + string(error_rsi));
     }

   if(IsStopped())Print("Is Stopped!");        //Testing if IsStopped() equals true

   for(int i = 0; i < data_size&&!IsStopped(); i++)        // after that the bar value is incremented in debugger, but it is always
     {                                       // printed as "0". The buffers are resized to 50 though, but contain only 0.0 values    
      if(buf_ma[i] != EMPTY_VALUE)
         PrintFormat("bar %d value %g", string(i), buf_ma[i]);
     }

   for(int i = 0; i < data_size&&!IsStopped(); i++)
     {
      if(buf_rsi[i] != EMPTY_VALUE)
         PrintFormat("bar %d value %g", string(i), buf_rsi[i]);
     }
  }


The problem error occurs also with while loop:

int handle_ma = 0;
double buf_ma[];
int handle_rsi = 0;
double buf_rsi[];
int data_size = 50;

ulong error_rsi = 0;
ulong error_ma = 0;


int check_init_ima = 0;
int check_init_rsi = 0;
int check_copy_ima = 0;
int check_copy_rsi = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   check_init_ima = iMA(_Symbol, _Period, 12, 0, MODE_SMA, PRICE_CLOSE);
   check_init_rsi = iRSI(_Symbol, _Period, 12, PRICE_CLOSE);              //both are initialised to valid handles 10 and 11
   
   ulong error_init=GetLastError();
   Print("Last Error: " + string(error_init));
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   check_copy_ima = CopyBuffer(handle_ma, 0, 0, data_size, buf_ma);
   if(check_copy_ima == -1)
     {
      error_ma = GetLastError();
      Print("Last Error: " + string(error_ma));
     }                                       // There are two different indicators that both produce 4802 errors when copying

   check_copy_rsi = CopyBuffer(handle_rsi, 0, 0, data_size, buf_rsi);
   if(check_copy_rsi == -1)
     {
      error_rsi = GetLastError();
      Print("Last Error: " + string(error_rsi));
     }

if(IsStopped())Print("Is Stopped!");

   int i=0;
   while(i < data_size && !IsStopped())        // after that the bar value is incremented in debugger, but it is always
     {                                       // printed as "0". The buffers are resized to 50 though, but contain only 0.0 values    
      if(buf_ma[i] != EMPTY_VALUE)
         PrintFormat("bar %d value %g", string(i), buf_ma[i]);
         i++;
     }
     
   i=0;
   while(i < data_size && !IsStopped())
     {
      if(buf_rsi[i] != EMPTY_VALUE)
         PrintFormat("bar %d value %g", string(i), buf_rsi[i]);
         i++;
     }
  }


Have I overlooked something? It compiles with no error though...

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
Other Constants - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Tobias Johannes Zimmer:

Hello forum,

there are some strange occurrences when I call indicators in EAs. In this test I call iMA and iRSI and Copy their buffers. The initialisation throws no error, buf when copying, I get -1 as CopyBuffer return value. The buffers are resized to the data_size that I set but they contain only "0.0" values. Then when I try printing the buffer contents in a for loop, in debug observation the index is incremented, but it counts only "0" bar values...???

When I test the for loop to count up and print the indizes it does it no problem. Here it seems that CopyBuffer makes something about the for loop crash...? Installed MT5 build is 4153.


The problem error occurs also with while loop:


Have I overlooked something? It compiles with no error though...

 
4802 = Cannot create indicator....

You should also ResetLastError before calling a function that might set _LastError. Else you don't know where it is coming from.

And, you should react to failure as well, meaning, stop processing any results you might have expected from a successful call to one of the failed functions.

In OnInit, you should return INIT_FAILED if an error occurred.

In OnTick, you should stop processing, if an error occurred.

EMPTY_VALUE is only set if there is an "empty value", not if the call has failed.

EDIT:

You should always initialize handle variables with INVALID_HANDLE.
 
Tobias Johannes Zimmer:

Hello forum,

there are some strange occurrences when I call indicators in EAs. In this test I call iMA and iRSI and Copy their buffers. The initialisation throws no error, buf when copying, I get -1 as CopyBuffer return value. The buffers are resized to the data_size that I set but they contain only "0.0" values. Then when I try printing the buffer contents in a for loop, in debug observation the index is incremented, but it counts only "0" bar values...???

When I test the for loop to count up and print the indizes it does it no problem. Here it seems that CopyBuffer makes something about the for loop crash...? Installed MT5 build is 4153.


The problem error occurs also with while loop:


Have I overlooked something? It compiles with no error though...

Here is your error:

You are not copying the handle to the handle variable...

In OnInit, you assign the result of iMA and iRSI to a different variable, but in OnTick you use the handle variable from global space, but never copied the value over...

Please consider working on your error handling code, and proper initializations. If you would have checked handle against INVALID_HANDLE and would have initialized them properly, you would have noticed.

Always use proper, and context sensitive initializations.
 

Oh thanks Dominik. So I did what you said now and Copybuffer is now working fine but the loop doesn't print the index except "0" which is strange. And it doesn't throw any error.

loop bug

It should print

"bar 0 value x.yz"

"bar 1 value w.vu"

etc.

int handle_ma = 0;
double buf_ma[];
int handle_rsi = 0;
double buf_rsi[];
int data_size = 50;

ulong error_rsi = 0;
ulong error_ma = 0;


int check_copy_ima = 0;
int check_copy_rsi = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   handle_ma = iMA(_Symbol, _Period, 12, 0, MODE_SMA, PRICE_CLOSE);
   handle_rsi = iRSI(_Symbol, _Period, 12, PRICE_CLOSE);              //both are initialised to valid handles 10 and 11
   if(handle_ma == INVALID_HANDLE || handle_rsi == INVALID_HANDLE)
     {
      Print("invalid handle!");
      return(INIT_FAILED);
     }
   ulong error_init = GetLastError();
   Print("Last Error: " + string(error_init));
   ResetLastError();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   check_copy_ima = CopyBuffer(handle_ma, 0, 0, data_size, buf_ma);
   if(check_copy_ima == -1)
     {
      error_ma = GetLastError();
      Print("Last Error: " + string(error_ma));
      ResetLastError();
     }                                       // There are two different indicators that both produce 4802 errors when copying

   check_copy_rsi = CopyBuffer(handle_rsi, 0, 0, data_size, buf_rsi);
   if(check_copy_rsi == -1)
     {
      error_rsi = GetLastError();
      Print("Last Error: " + string(error_rsi));
      ResetLastError();
     }

   if(IsStopped())
      Print("Is Stopped!");

   int i = 0;
   while(i < data_size && !IsStopped())        // after that the bar value is incremented in debugger, but it is always
     {
      // printed as "0". The buffers are resized to 50 though, but contain only 0.0 values
      if(buf_ma[i] != EMPTY_VALUE)
         PrintFormat("bar %d value %g", string(i), buf_ma[i]);
      i++;
      ulong error_loop = GetLastError();
      if(error_loop != 0)
        {
         Print("loop_error: " + string(error_loop));
         ResetLastError();
         return;
        }
     }

   i = 0;
   while(i < data_size && !IsStopped())
     {
      if(buf_rsi[i] != EMPTY_VALUE)
         PrintFormat("bar %d value %g", string(i), buf_rsi[i]);
      i++;
      ulong error_loop = GetLastError();
      if(error_loop != 0)
        {
         Print("loop_error: " + string(error_loop));
         ResetLastError();
         return;
        }
     }
  }
//+------------------------------------------------------------------+
Should I open a new topic because it is no about loops?
 
Tobias Johannes Zimmer #:

Oh thanks Dominik. So I did what you said now and Copybuffer is now working fine but the loop doesn't print the index except "0" which is strange. And it doesn't throw any error.


It should print

"bar 0 value x.yz"

"bar 1 value w.vu"

etc.

Should I open a new topic because it is no about loops?
Let's stay on this thread.

You need to check if the indicator is already up to date.




Please change this:

int handle_ma = INVALID_HANDLE;
EDIT:
I suggest calling ResetLastError before calling CopyBuffer
 
Tobias Johannes Zimmer #:

Oh thanks Dominik. So I did what you said now and Copybuffer is now working fine but the loop doesn't print the index except "0" which is strange. And it doesn't throw any error.


It should print

"bar 0 value x.yz"

"bar 1 value w.vu"

etc.

Should I open a new topic because it is no about loops?
Here is your error:

PrintFormat("bar %d value %g", string(i)

Format specifier %d is not taking string. But an integer. Don't convert i to a string
 
Dominik Egert #:
Here is your error:


Format specifier %d is not taking string. But an integer. Don't convert i to a string
No god please no. Thank you. 🤦🏻‍♂️ 😂 Got too secure with the Printformat here. 
 
Tobias Johannes Zimmer #:
No god please no. Thank you. 🤦🏻‍♂️ 😂 Got too secure with the Printformat here. 
I don't know why everyone is using %d, %g instead of %i and %f.

I find these much more explanatory than d and g.

%i for integer, %f for float, %s for string... So simple, I have to look up d and g, because they are somewhat unrelated to the type.
Reason: