copybuffer basic problem

 

Ok, here is the thing. Very simple but maybe I dont understand the concept of copybuffer in mql5 (I am new to it).  

Well, I understand that after you define a handle in OnInit(), and then you use the CopyBuffer() method, you copy elements from the first array to the second.

However in my code that doesnt work because it copies only one element . 

In the following code I just want to copy the ATR  values from the second and the third candle to my "atrA" array, that is it. It only copies the one from the 2nd candle and the EA crashes. And

as it fails it triggers the "CopyBuffer from atr1 failed, no data" message.  

I know maybe this is so simple but really dont understand what happens. Thanks in advance.

 

//+------------------------------------------------------------------+
//|                                                       theATR.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

int atrH;

double atrA[];


string com;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
    atrH = iATR(_Symbol,_Period,10);
      if(atrH==INVALID_HANDLE)
     {
      printf("Error creating ATR1 indicator");
      return(-1);
   }
   
//---
   return(0);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   com="";
    addCom("s "+atrH);
   
  if(CopyBuffer(atrH,0,1,2,atrA)!=1)
     {
      Print("CopyBuffer from atr1 failed, no data");
      
      return;
     }
     
     addCom("pas");
  Comment(com);
     
    
     
  }
  
  
  void addCom(string args)
  {
      com=com+"\n"+args;
      }
      
      //+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
 
spazioalto:

Ok, here is the thing. Very simple but maybe I dont understand the concept of copybuffer in mql5 (I am new to it).  

Well, I understand that after you define a handle in OnInit(), and then you use the CopyBuffer() method, you copy elements from the first array to the second.

However in my code that doesnt work because it copies only one element . 

In the following code I just want to copy the ATR  values from the second and the third candle to my "atrA" array, that is it. It only copies the one from the 2nd candle and the EA crashes. And

as it fails it triggers the "CopyBuffer from atr1 failed, no data" message.  

I know maybe this is so simple but really dont understand what happens. Thanks in advance.

Hi spazioalto,

Actually in your case, there's only a typo problem. Here's your code,

if(CopyBuffer(atrH,0,1,2,atrA)!=1)
     {
      Print("CopyBuffer from atr1 failed, no data");
      return;
     }

Please read the funny manual, CopyBuffer() return the number of copied data. You tried to copy 2 data but set "if it's not 1" then it's an error. What you should do is this

   ResetLastError();
   if(CopyBuffer(atrH,0,1,2,atrA)!= 2)
     {
      Print("CopyBuffer from atr1 failed, no data",GetLastError());
      return;
     }
     else
     {
     Print("CopyBuffer success");
     }

If you sure there's an err, do use ResetLastError() and GetLastError().

You should see my MetaEditor, it's always show error and warning all the time, never no error no warning - LOL. 

:D

 

 

 

Please do not forget to check amount of calculated values(using BarsCalculated function) before you copy any data from buffer.

Documentation on MQL5: Timeseries and Indicators Access / BarsCalculated
  • www.mql5.com
Timeseries and Indicators Access / BarsCalculated - Documentation on MQL5