How to fill ARRAY ...

Anil Varma
67
Anil Varma  

Dear Friend

I am working to create  a function to store High / Low Bar Index values for previous N period. However my array does update only with one value a 0 bar.

Can you please help me identify the error ???

extern int   MajorSwingSize      = 3;
extern int   PeriodsInMajorSwing = 13;
extern int   MinorSwingSize      = 1;
extern int   PeriodsInMinorSwing = 5;
extern int   MovingAveragePeriods  = 55;
       int   lookBack = PeriodsInMajorSwing * 2;
       // Array to hold maxium 10 Index of previous Highs
       int   arraySwingHigh_Index[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
    //major_SwingHigh();
    Alert("");
    Alert("SwingHigh Index0 [" + arraySwingHigh_Index[0] + "] Price " + High[arraySwingHigh_Index[0]]);
    Alert("SwingHigh Index1 [" + arraySwingHigh_Index[1] + "] Price " + High[arraySwingHigh_Index[1]]);
  }
//+------------------------------------------------------------------+

//+--------------------------------------------------------------------------------------+
//| Function array_MajorSwingHigh()
//+--------------------------------------------------------------------------------------+
void major_SwingHigh()
{
  int limit;
  int counted_bars = IndicatorCounted();
  if(counted_bars>0) counted_bars--; 
  limit = (Bars - counted_bars);
  //---- main loop 
  // First Run Through Rule
  if(counted_bars == 0)
  {
    if(lookBack >= MovingAveragePeriods) { limit -= lookBack; }
    else                                 { limit -= MovingAveragePeriods; }
  }

  for(int i = 1; i < limit; i++)
    {
      int j = 0;
      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i) == (i + PeriodsInMajorSwing))
        {
          arraySwingHigh_Index[j] = (i+PeriodsInMajorSwing);
          j++;
        }
    } // END Of For..Loop
} // END of major_SwingHigh()
remcous
555
remcous  
Anil Varma:

Dear Friend

I am working to create  a function to store High / Low Bar Index values for previous N period. However my array does update only with one value a 0 bar.

Can you please help me identify the error ???

Hello,

Just a question : where do you size or resize your array ?

When you set :

// Array to hold maxium 10 Index of previous Highs
       int   arraySwingHigh_Index[];

you don't set a size of 10 items, or i don't have see any ArrayResize() statment in your code...

or you must do :

// Array to hold maxium 10 Index of previous Highs
       int   arraySwingHigh_Index[10];
Anil Varma
67
Anil Varma  
remcous:

Hello,

Just a question : where do you size or resize your array ?

When you set :

you don't set a size of 10 items, or i don't have see any ArrayResize() statment in your code...

or you must do :

Hi Remcous

thanks for reply. I did tried [10], but it is saving only ONE element at 0. rest of array there is no value.

When I run following code, it only shows value of 0 element;

    Alert("SwingHigh Index0 [" + arraySwingHigh_Index[0] + "] Price " + High[arraySwingHigh_Index[0]]);
    Alert("SwingHigh Index1 [" + arraySwingHigh_Index[1] + "] Price " + High[arraySwingHigh_Index[1]]);
remcous
555
remcous  
Anil Varma:

Hi Remcous

thanks for reply. I did tried [10], but it is saving only ONE element at 0. rest of array there is no value.

When I run following code, it only shows value of 0 element;

hi !

why do you comment call to function  major_SwingHigh() ?

    //major_SwingHigh();
    Alert("");
    Alert("SwingHigh Index0 [" + arraySwingHigh_Index[0] + "] Price " + High[arraySwingHigh_Index[0]]);
    Alert("SwingHigh Index1 [" + arraySwingHigh_Index[1] + "] Price " + High[arraySwingHigh_Index[1]]);
Anil Varma
67
Anil Varma  
remcous:

hi !

why do you comment call to function  major_SwingHigh() ?

Hi !!!

just trying to read / know the values in array by an alert on screen for checking purpose. Once the code works well, I can use it for my EA Purpose.

remcous
555
remcous  
Anil Varma:

Hi !!!

just trying to read / know the values in array by an alert on screen for checking purpose. Once the code works well, I can use it for my EA Purpose.

Hello,

Have you uncommented call to function major_SwingHigh() ?

like this :

extern int   MajorSwingSize      = 3;
extern int   PeriodsInMajorSwing = 13;
extern int   MinorSwingSize      = 1;
extern int   PeriodsInMinorSwing = 5;
extern int   MovingAveragePeriods  = 55;
       int   lookBack = PeriodsInMajorSwing * 2;
       // Array to hold maxium 10 Index of previous Highs
       int   arraySwingHigh_Index[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
    major_SwingHigh(); //<- here
    Alert("");
    Alert("SwingHigh Index0 [" + arraySwingHigh_Index[0] + "] Price " + High[arraySwingHigh_Index[0]]);
    Alert("SwingHigh Index1 [" + arraySwingHigh_Index[1] + "] Price " + High[arraySwingHigh_Index[1]]);
  }
//+------------------------------------------------------------------+

//+--------------------------------------------------------------------------------------+
//| Function array_MajorSwingHigh()
//+--------------------------------------------------------------------------------------+
void major_SwingHigh()
{
  int limit;
  int counted_bars = IndicatorCounted();
  if(counted_bars>0) counted_bars--; 
  limit = (Bars - counted_bars);
  //---- main loop 
  // First Run Through Rule
  if(counted_bars == 0)
  {
    if(lookBack >= MovingAveragePeriods) { limit -= lookBack; }
    else                                 { limit -= MovingAveragePeriods; }
  }

  for(int i = 1; i < limit; i++)
    {
      int j = 0;
      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i) == (i + PeriodsInMajorSwing))
        {
          arraySwingHigh_Index[j] = (i+PeriodsInMajorSwing);
          j++;
        }
    } // END Of For..Loop
} // END of major_SwingHigh()
Anil Varma
67
Anil Varma  
remcous:

Hello,

Have you uncommented call to function major_SwingHigh() ?

like this :

Thanks remcous

just noticed the silly mistake I have made. The code is working now but with Array Out of Range !!!

remcous
555
remcous  
Anil Varma:

Thanks remcous

just noticed the silly mistake I have made. The code is working now but with Array Out of Range !!!

Hello,

if you don't know size of your array before start, i suggest you dynamic array with ArrayResize().

int  ArrayResize( 
   void&  array[],              // array passed by reference 
   int    new_size,             // new array size 
   int    reserve_size=0        // reserve size value (excess) 
   );

This can be done like this :

// Array to hold Index of previous Highs
       int   arraySwingHigh_Index[];


for(int i = 1; i < limit; i++)
    {
      int j = 0;
      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i) == (i + PeriodsInMajorSwing))
        {
          ArrayResize(arraySwingHigh_Index,j+1);
          arraySwingHigh_Index[j] = (i+PeriodsInMajorSwing);
          j++;
        }
    } // END Of For..Loop
Anil Varma
67
Anil Varma  
remcous:

Hello,

if you don't know size of your array before start, i suggest you dynamic array with ArrayResize().

This can be done like this :

Thanks Remcous

the error continues even if i fix the size of array[10].

i was reviewing one of video on you tube for out of range array error, and it was suggesting that when you try to look n candles on chart whereas chart has only n-1 candles, that also causes the same out of range error.

trying to figure it out.

remcous
555
remcous  
Anil Varma:

Thanks Remcous

the error continues even if i fix the size of array[10].

i was reviewing one of video on you tube for out of range array error, and it was suggesting that when you try to look n candles on chart whereas chart has only n-1 candles, that also causes the same out of range error.

trying to figure it out.

// Array to hold Index of previous Highs
       int   arraySwingHigh_Index[10];


for(int i = 1; i < limit; i++)
    {
      int j = 0;
      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i) == (i + PeriodsInMajorSwing))
        {
          arraySwingHigh_Index[j] = (i+PeriodsInMajorSwing);
          j++;
        }
        if (j>9) break; // then loop stop when you have fill your array with 10 elements
    } // END Of For..Loop