[SOLVED] CopyBuffer() error, or empty value

 

Hi guys, I'm trying to get a value of a variable from a custom indicator using CopyBuffer without succes and I don't understand why.

This is my Indicator file mql5

 double CandlesticksPatterns[1];-----------------------------------------//I've set the array 1 because if empty i get error 4806 (I don't know why)
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  SetIndexBuffer(5,CandlesticksPatterns);-----------------------------------//Buffer number 5 it's set by me, original indicator only have 4 but it don't use them.
  //ArraySetAsSeries(CandlesticksPatterns,true);
  return(INIT_SUCCEEDED);
  }
  for(int i=limit;i<rates_total-1;i++)
     {CandlesticksPatterns[0]=0;-------------------------------------------//Set variable to 0 at begin
      CANDLE_STRUCTURE cand1;
      if(!RecognizeCandle(_Symbol,_Period,time[i],InpPeriodSMA,cand1))
         continue;
if((cand1.close-cand1.open>cand2.open-cand2.close) &&
               (cand2.open-cand2.close)*100/(cand1.close-cand1.open)>70)
             // close 1 is less than or equal to open 1
              {
               DrawSignal(prefix+"Bear Reverse"+string(objcount++),cand2,InpColorBear,comment);
               CandlesticksPatterns[0]=1;-------------------------------------------------------//Parts of code where variable gets values from 1 to 20
              }
             
           }
if(cand1.low>cand2.low && cand1.low*100/cand2.low>80) // close 1 is less than or equal to open 1
              {
               DrawSignal(prefix+"Bull Reverse"+string(objcount++),cand1,cand2,InpColorBull,comment);
               CandlesticksPatterns[0]=2;-------------------------------------------------------//Parts of code where variable gets values from 1 to 20
              }
           }

My expert:

int         handle_CandlesticksPatterns;------------------------------------//Handle
//+------------------------------------------------------------------+
//|Indicador Candlesticks patterns                                           |
//+------------------------------------------------------------------+
   handle_CandlesticksPatterns=iCustom(m_symbol.Name(),Period(),"Examples\\CandlesticksPatterns",InpPeriodSMA);
//--- if the handle is not created
   if(handle_CandlesticksPatterns==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the SYMBOL indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early

     }


   return(INIT_SUCCEEDED);------------------------//No errors here
  }
void EveryTick()
  {
   double MA1_current=MA1Get(0);
   double MA1_previous=MA1Get(2);
   double MA2_current=MA2Get(0);
   double MA2_previous=MA2Get(2);
   double MA3_current=MA3Get(0);
   double MA3_previous=MA3Get(2);
   double CP=CandlesticksPatternsGet(0);--------//I only get 0 value if array CandlesticksPatterns[1], or error4806 if CandlesticksPatterns[]   
   Comment(CP);
  }
//+------------------------------------------------------------------+
//| Get value of buffers for the CandlesticksPatterns                                |
//+------------------------------------------------------------------+
double CandlesticksPatternsGet(const int index)
  {
   double CandlesticksPatterns[1];
//--- reset error code
   ResetLastError();
//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index
   if(CopyBuffer(handle_CandlesticksPatterns,5,index,1,CandlesticksPatterns)<0) ------------//Try get buffer 5 value, that can only have 1 value(the current one)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("Failed to copy data from the iCandlesticksPattern indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(0.0);
     }
   return(CandlesticksPatterns[0]);
  }

Well this don´t work, I've read the documentation of CopyBuffer, iCustom, Arrays, I've tryed ArrayAsSeries, StaticArray like it is, I have no idea why this happen.

I really appreciate your help.

Thanks.
 
I also want to ask if there is another way to get current value of a variable that it is or not an array
 

From information that you posted it seems that you are trying to copying an indicator buffer (that by default is an array as series) in an array that is not set as series.

In addition to this, in indicator, usually, buffer arrays are used as dynamic.

I think that with these changes you can solve the issue.

Indicator code:

 double CandlesticksPatterns[];-----------------------------------------//I've set the array 1 because if empty i get error 4806 (I don't know why)
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  SetIndexBuffer(5,CandlesticksPatterns);-----------------------------------//Buffer number 5 it's set by me, original indicator only have 4 but it don't use them.
  //ArraySetAsSeries(CandlesticksPatterns,true);
  return(INIT_SUCCEEDED);
  }

EA code:

double CandlesticksPatternsGet(const int index)
  {
   double CandlesticksPatterns[];
   ArraySetAsSeries(CandlesticksPatterns,true);
//--- reset error code
   ResetLastError();
//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index
   if(CopyBuffer(handle_CandlesticksPatterns,5,index,1,CandlesticksPatterns)<0) ------------//Try get buffer 5 value, that can only have 1 value(the current one)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("Failed to copy data from the iCandlesticksPattern indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(0.0);
     }
   return(CandlesticksPatterns[0]);
  }

Get a try.

 
Fabio Cavalloni:

From information that you posted it seems that you are trying to copying an indicator buffer (that by default is an array as series) in an array that is not set as series.

In addition to this, in indicator, usually, buffer arrays are used as dynamic.

I think that with these changes you can solve the issue.

Indicator code:

EA code:

Get a try.

Thanks for your answer, i've just try it and I also got error code 4806. I'm still doing tests with several methods but with no success...

 

I found another strange thing in your code.

  for(int i=limit;i<rates_total-1;i++)

Why you put that -1 ?

The "rates_total-1" candle is the current candle and in your code it seems that you don't calculate it.

Are you sure that the problem is in the EA calling with iCustom and not in your indicator that do not have any value in the current bar buffer?

 
for(i=0; i<limit;i++) 

Given that 

if(Prev_Calculated = 0) limit= rates_total;
else limit = rates_total - prev_calculated;


Or change ur code to 

for(i=limit; i<=rates_total-prev_calculated; i++)


I'm also not sure coz I have issues with for loops in indicators dealing with rates_total
 
Fabio Cavalloni:

I found another strange thing in your code.

Why you put that -1 ?

The "rates_total-1" candle is the current candle and in your code it seems that you don't calculate it.

Are you sure that the problem is in the EA calling with iCustom and not in your indicator that do not have any value in the current bar buffer?

You was right, I have set up wrong the rates on the indicator, so I've start a new loop that includes all rates. Thank you so much!
 
Jefferson Metha:
for(i=0; i<limit;i++) 

Given that 

if(Prev_Calculated = 0) limit= rates_total;
else limit = rates_total - prev_calculated;


Or change ur code to 

for(i=limit; i<=rates_total-prev_calculated; i++)


I'm also not sure coz I have issues with for loops in indicators dealing with rates_total

Yes the problem was that I didn't include all bars, thank you for your answer. I'll keep learning.

Reason: