What am I doing wrong?

 

hello forum,

I am brand new with MetaEditor. I have been looking at some tutorials and I am making some progress. I can now get quite a lot done, except for one very important and basic thing... Making array works... What am I doing wrong... I keep getting the "array out of range code". only on my own buffer. I went to the very basic and created the most simple indicator possible to troubleshoot and even that isn't working. Why is ABuffer not working? Thanks in advance

double         Label1Buffer[];
double         ABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,ABuffer,INDICATOR_CALCULATIONS);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
for(int i= 1;i<rates_total;i++)
{
Label1Buffer[i]=price[i];
ABuffer[i] = price[i];
}
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
La_patates:

hello forum,

I am brand new with MetaEditor. I have been looking at some tutorials and I am making some progress. I can now get quite a lot done, except for one very important and basic thing... Making array works... What am I doing wrong... I keep getting the "array out of range code". only on my own buffer. I went to the very basic and created the most simple indicator possible to troubleshoot and even that isn't working. Why is ABuffer not working? Thanks in advance

Er... your code compiles and runs without any error on my pc... ... ... ...

 
TKS BRO
 
Seng Joo Thio:

Er... your code compiles and runs without any error on my pc... ... ... ...

Darn... I might try to reinstall MT5 then. cause for me it works only if I don't put ABuffer[i] = price[i];


Thanks for trying it out!

 
La_patates:

Darn... I might try to reinstall MT5 then. cause for me it works only if I don't put ABuffer[i] = price[i];


Thanks for trying it out!

Ah... MT5? I tried on MT4... LOL

Ok, on MT5 I do see array out of range error, but all you have to do is this:

ArrayResize(ABuffer,rates_total);

Add this before your 'for' loop.

 
How many buffers did you declare you have?

Constant

Type

Description

indicator_buffers

int

Number of buffers for indicator calculation

indicator_plots

int

Number of graphic series in the indicator


          Language Basics / Preprocessor / Program Properties (#property) - Reference on algorithmic/automated trading language for MetaTrader 5
 
Seng Joo Thio: Ok, on MT5 I do see array out of range error, but all you have to do is this:

Add this before your 'for' loop.

You do not resize buffers.

 
Seng Joo Thio:

Ah... MT5? I tried on MT4... LOL

Ok, on MT5 I do see array out of range error, but all you have to do is this:

Add this before your 'for' loop.

Yeah! I discovered that yesterday too! It worked wonder and I could make lots of progress!
Thanks again for your time!
 
William Roeder:

You do not resize buffers.

What do you mean?
How do you go about it without resizing?
 
La_patates: How do you go about it without resizing?
Buffers auto resize. Perhaps you should read the manual.
Please note that you can't change the size for dynamic arrays set as indicator buffers by the function SetIndexBuffer(). For indicator buffers, all operations of size changes are performed by the executing sub-system of the terminal.
          Custom Indicators / SetIndexBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
 
William Roeder:

You do not resize buffers.

Thank you for pointing that out :).

@La_patates , whether to use ArrayResize() or not depends on whether you want to access the values of that array outside of your indicator. I saw you used "INDICATOR_CALCULATIONS", so I assumed that you're only going to use ABuffer within the indicator. But I could be wrong, after all you did call SetIndexBuffer().

So if you do want to access it in your EA, the orthodox solution would be to figure out why ABuffer wasn't already resized properly by SetIndexBuffer(). I suspect you don't have these lines (or did not use the correct numbers):

#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_plots   1 

With these lines, you don't need to use ArrayResize(). However, if my assumption was right, then you need not SetIndexBuffer() for ABuffer, and then you'll need to use ArrayResize() - Whichever you need, there's a solution.

Reason: