array questions

 

hi guys  , why if i decleare array in this mode return me error invalid index value ?

int VisibleBar=WindowFirstVisibleBar();

double aBodyCount[VisibleBar];

is a integer

 

If you want static array - one with a size defined at declaration time - you must use constant to declare size.

If you want variable sized array, read about dynamic arrays and ArrayResize() function.

Dynamic Array Object - Data Types - Language Basics - MQL4 Reference
Dynamic Array Object - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Maximum 4-dimension array can be declared. When declaring a dynamic array (an array of unspecified value in the first pair of square brackets), the compiler automatically creates a variable of the above structure (a dynamic array object) and provides a code for the correct initialization. Static Arrays When all significant array dimensions are...
 
I i want static array , and i suppsoe
int VisibleBar=WindowFirstVisibleBar()

retrun a integer value , why give me error

 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
 
faustf:
I i want static array , and i suppsoe

retrun a integer value , why give me error

As I already wrote:

you must use constant to declare size

In your code "VisibleBar" is a variable, not a constant.
 
faustf:
I i want static array , and i suppsoe

retrun a integer value , why give me error

Drazen Penic:

If you want static array - one with a size defined at declaration time - you must use constant to declare size.

If you want variable sized array, read about dynamic arrays and ArrayResize() function.

 

but now  why tell  me out of range  ?? whats wrong ?

#property indicator_chart_window
extern int    font_size = 10;
extern color  ColorBull = DodgerBlue;
extern color  ColorBeer = Red;
extern string font_name = "Arial";
double aBodyCount[2];
//+------------------------------------------------------------------+
int start()
  {
  int VisibleBar=WindowFirstVisibleBar();
    ArrayResize(aBodyCount,VisibleBar,VisibleBar);
   BodyCount();  
  }
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectsDeleteAll(0,OBJ_TEXT);
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|            BODY COUNT VISIBLE                                    |
//+------------------------------------------------------------------+
void BodyCount()
  {
   for(int i=WindowFirstVisibleBar(); i>=0; i--)
     {
      aBodyCount[i] = ((NormalizeDouble(Open[i],Digits)/Point)-(NormalizeDouble(Close[i],Digits))/Point);
     }
   Alert("Plus ", aBodyCount[i]," pt.");
   return ;
  }
//+------------------------------------------------------------------+
 
faustf: but now  why tell  me out of range  ?? whats wrong ?
  1. Your array has two elements. WindowFirstVisibleBar is likely in the hundreds.
  2. Next time: Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

 
faustf:

but now  why tell  me out of range  ?? whats wrong ?

Array index is zero based, so if you have  WindowFirstVisibleBar() elements in your array, they have indexes from array[0] to array[WindowFirstVisibleBar()-1]

Your loop from the above snippet should start from WindowFirstVisibleBar()-1


And another thing to check is if WindowFirstVisibleBar() changes value as time passes. For example can it happen that on start WindowFirstVisibleBar() returns value 100, and after some time some bigger number? 
If that is the case, you will initialize array for 100 elements, and later you will expect it to have more.


You should get familiar how the arrays work in MQL and C++, otherwise you will have number of problems.

 
Drazen Penic:

Array index is zero based, so if you have  WindowFirstVisibleBar() elements in your array, they have indexes from array[0] to array[WindowFirstVisibleBar()-1]

Your loop from the above snippet should start from WindowFirstVisibleBar()-1


And another thing to check is if WindowFirstVisibleBar() changes value as time passes. For example can it happen that on start WindowFirstVisibleBar() returns value 100, and after some time some bigger number? 
If that is the case, you will initialize array for 100 elements, and later you will expect it to have more.


You should get familiar how the arrays work in MQL and C++, otherwise you will have number of problems.

yea  i understund  what you tell but , my case is different i know that  array start the count 0 to end  and i  if i create Array [2] ,resize it,  and  with(WindowFirstVisibleBar() + 5)and  cicle for insert inside of array  with  for(int i=WindowFirstVisibleBar(); i>=0; i--) , i aspect the script runn good , but return the same2020.05.15 23:19:13.116    candle-body-size USDCHF,H1: array out of range in 'candle-body-size.mq4' (60,29)
example if
WindowFirstVisibleBar() is  equal = 100  and i resize in 105 , if  for start  to 0 or 1   for me is no problem i have 5 position plus

 
faustf:

yea  i understund  what you tell but , my case is different i know that  array start the count 0 to end  and i  if i create Array [2] ,resize it,  and  with(WindowFirstVisibleBar() + 5)and  cicle for insert inside of array  with  for(int i=WindowFirstVisibleBar(); i>=0; i--) , i aspect the script runn good , but return the same2020.05.15 23:19:13.116    candle-body-size USDCHF,H1: array out of range in 'candle-body-size.mq4' (60,29)
example if
WindowFirstVisibleBar() is  equal = 100  and i resize in 105 , if  for start  to 0 or 1   for me is no problem i have 5 position plus

You cannot resize an array that was created static. You can only resize a dynamic array. ArrayResize() has a return value, check it to detect errors.

Reason: