Download MetaTrader 5

CopyBuffer for indicator as time series

To add comments, please log in or register
Sell products right in MetaTrader. Millions of traders are waiting for this!
Biantoro Kunarto
14896
Biantoro Kunarto 2012.08.16 02:22 

I try using copy buffer to receive indicator data as time series but after I looked to Indicator data, it shows different data. This code is below :

      int      MAPeriod    = 20;
      int      MAHandle;
      double   MAValue[5];

      MAHandle  = iMA(_Symbol,0,MAPeriod,0,MODE_SMA,PRICE_CLOSE);
      ArraySetAsSeries(MAValue,true);
      if(CopyBuffer(MAHandle,0,0,5,MAValue)>0)
      {
         Print("Ma0= ",NormalizeDouble(MAValue[0],6));
         Print("Ma1= ",NormalizeDouble(MAValue[1],6));
         Print("Ma2= ",NormalizeDouble(MAValue[2],6));
         Print("Ma3= ",NormalizeDouble(MAValue[3],6));
         Print("Ma4= ",NormalizeDouble(MAValue[4],6));      
      }

MAValue [0]  has to shown the last data ( oldest ), but in this case MAValue[4] shown the last data..

 I already check with CopyRate, it shown the right data.. Is this bug or I do a mistake in code?

Shaharudin Ahmad
914
Shaharudin Ahmad 2012.08.16 02:32  
biantoro:

I try using copy buffer to receive indicator data as time series but after I looked to Indicator data, it shows different data. This code is below :

MAValue [0]  has to shown the last data ( oldest ), but in this case MAValue[4] shown the last data..

 I already check with CopyRate, it shown the right data.. Is this bug or I do a mistake in code?

 

maybe change  double   MAValue[5];  to  double   MAValue[];

Biantoro Kunarto
14896
Biantoro Kunarto 2012.08.16 03:37  
jelam:

Thanks jelam, its work..but this mean we must set the variable in dinamic arrays not static. I already read the mql5 reference :

"There is an important exception to this rule: if timeseries and indicator values need to be copied often, for example at each call of OnTick() in Expert Advisors or at each call of OnCalculate() in indicators, in this case one should better use statically distributed arrays, because operations of memory allocation for dynamic arrays require additional time, and this will have effect during testing and optimization."

This means we can set the variable in the static arrays..but it doesn't work.

onewithzachy
2271
onewithzachy 2012.08.16 19:16  

Hi there biantoro,

Try this as script. When we have problem in codes, try to print everything, so we can pin point the error. please read again CopyBuffer(), CopyClose() and ArraySetAsSeries() in the help file.

I change the MA Period to 1 so it's equal with close price. Run this script, and compare the MA period 1 with close price. 

void OnStart()
  {
//---
   int      MAPeriod = 1;
   int      MAHandle;
   double   MAValue[5], close [5];
   
   if (CopyClose (_Symbol, _Period, 0, 5, close) > 0)
     {
     if ((MAHandle = iMA(_Symbol, _Period, MAPeriod, 0, MODE_SMA, PRICE_CLOSE)) != INVALID_HANDLE)
        {  
        Alert("Set as series ", ArraySetAsSeries(MAValue, true));
        if(CopyBuffer(MAHandle, 0, 0, 5, MAValue) > 0)
          {
           Alert("Ma0    = ", DoubleToString(MAValue[0], _Digits));
           Alert("Close4 = ", DoubleToString(close[4],   _Digits));
           Alert("Ma1    = ", DoubleToString(MAValue[1], _Digits));
           Alert("Close3 = ", DoubleToString(close[3],   _Digits));
           Alert("Ma2    = ", DoubleToString(MAValue[2], _Digits));
           Alert("Close2 = ", DoubleToString(close[2],   _Digits));
           Alert("Ma3    = ", DoubleToString(MAValue[3], _Digits));
           Alert("Close1 = ", DoubleToString(close[1],   _Digits));
           Alert("Ma4    = ", DoubleToString(MAValue[4], _Digits));
           Alert("Close0 = ", DoubleToString(close[0],   _Digits));      
          }
        }
        Alert("End");
     }
  }

 

 

Biantoro Kunarto
14896
Biantoro Kunarto 2012.08.17 06:42  
onewithzachy:

Hi there biantoro,

Try this as script. When we have problem in codes, try to print everything, so we can pin point the error. please read again CopyBuffer(), CopyClose() and ArraySetAsSeries() in the help file.

I change the MA Period to 1 so it's equal with close price. Run this script, and compare the MA period 1 with close price. 

 

 

ok. Thanks onewithzachy
To add comments, please log in or register