MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Automated Trading and Strategy Testing Forum

Exp_ColorMomentum_AMA Indicator
Exp_ColorMomentum_AMA
Author: GODZILLA
Subscribe to signal
FOREX NAVIGATOR
45.32%, 39 435.11 USD
How to Exchange Data: A DLL for MQL5 in 10 Minutes How to Exchange Data: A DLL for MQL5 in 10 Minutes Gold SicklesGold Sickles Try product
Gold Sickles
Author: achidayat
Screenshot
AUDUSD, M15
Real

// copybuffer basic problem

To add comments, please log in or register
Ernesto Orozco
30
spazioalto 2012.06.21 01:07

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)
  {
//---
   
  }

 

 

onewithzachy
954
onewithzachy 2012.06.21 07:04
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

 

 

alexvd alexvd
Moderator
1891
alexvd 2012.06.22 09:06

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

/
To add comments, please log in or register