problem on array recording and updating

 

Hi guys, recently I build an indicator as shown in the picture. I want to add a function outputs two arrays "avgPositive" & "avgNegative", which show the average of the latest 100 positive & negative bars' value respectively. However I encounter problem at this stage. Please kindly advise me.

I have tried to use the following coding (for simplicity I just show the core and relevant part).

//before init()

double avgPositive[], avgNegative[];

double avgPositiveTemp[], avgNegativeTemp[]; //Note: I didnt set a buffer on these two array as I set address zero holds the oldest value

int count=0;

int steps=20;

int countPositive=0;

int countNegative=0;

int totalPositive=0;

int totalNegative=0;


int start()

{

int counted_bars=IndicatorCounted();
int pos=Bars-counted_bars;
if (! pos) return (0);
pos--;



for (int i=pos; i>0; i--)

{

//assume indicator "indicator[]" computed

if ( indicator[i] >0 )

{

countPositive+=1;

avgPositiveTemp[countPositive]=indicator[i];

} else if ( indicator[i] <0 )

{

countNegaive+=1;

avgNegativeTemp[countNegative]=indicator[i];

}


if (countPositive>=100 && countNegative>=100)

{

for (int j= 0, j<=100, J++)

{

totalPositive+=avgPositive[countPositive-j];

totalNegative+=avgNegative[countNegative-j];

}

}


avgPositive[i]=totalPositive/100;

avgNegative[i]=totalNegative/100;


totalPositive=0;

totalNegative=0;

}


===================================================

The program fails to record the arrays and simply outputs zero when I type

Print("avgPositive", avgPositive[anyaddress])

Print("avgNegative", avgNegative[anyaddress])

Print("avgPositiveTemp", avgPositiveTemp[anyaddress])

Print("avgNegativeTemp", avgNegativeTemp[anyaddress])

 

Hi, could anyone help me to solve the problem?

your help would be much appreciated!

 

Do u get any errors? I see u are not using ArrayResize() yet u have not declared a size for the arrays... And next time:

 

Thanks Gordon,

Do we really need to declared the size? In my program the size of the avgPositive/avgNegative is always changing subject to new incoming positive/negative indicator value, and the sizes of the two arrays is not the same as (smaller than) Bars number.

Also, when I try to define the value of address zero of the array before int start(), e.g. avgPositive[0]=0, it gets error>>> "avgPositive" expression on global scope not allowed


Sorry just wait me some time before i post the code here.

 
dummyhenry:

Do we really need to declared the size? [...]

There are three ways in which an array gets it's 'size':

1. Declare the size when declaring the array. Example:

int arr[10];   // arr[] declared with size 10

2. Let the compiler set the size automatically by initializing the array during it's declaration. Example:

int arr[] = { 1,2,3 };   // arr[] has size 3 automatically

3. Change the array's size anywhere in code with ArrayResize(). Example:

int arr[];
ArrayResize(arr,10);   // arr[] resized to have size 10

Your arrays are all size 0 and remain so throughout... So of course nothing works.

 

Thanks Gordon, and I am now adding the ArrayResize function and here is my code:

//+------------------------------------------------------------------+
//|                                                   henrytest2.mq4 |
//|                                                            henry |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "henry"
#property link      ""

#property indicator_separate_window
#property indicator_minimum -0.002
#property indicator_maximum 0.002

#property indicator_buffers 6
#property indicator_color1 Black
#property indicator_color2 Orange
#property indicator_color3 Black
#property indicator_color4 Black
#property indicator_color5 SkyBlue
#property indicator_color6 Red

double MA[];
double PriceMinusMA[];

double avgPositiveTemp[]; //temporary array to hold the positive PriceMinusMA
double avgNegativeTemp[]; //temporary array to hold the negative PriceMinusMA
double avgPositive[];
double avgNegative[];

int countPositive=0;
int countNegative=0;
double cumPositive=0;  //temporary variable to hold the sum of laterst 20 positive avgPositiveTemp
double cumNegative=0;  //temporary variable to hold the sum of laterst 20 negative avgNegativeTemp
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,MA);
   SetIndexBuffer(1,PriceMinusMA);
   SetIndexStyle(1, DRAW_HISTOGRAM,STYLE_SOLID,2);
   
   SetIndexBuffer(2,avgPositiveTemp);
   SetIndexBuffer(3,avgNegativeTemp);
   SetIndexBuffer(4,avgPositive);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(5,avgNegative);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int pos=Bars-counted_bars;
   if (! pos) return (0);
   pos--;
//----
   for (int i=pos; i>=0; i--)
   {
      MA[i]=iMA(NULL,0,20,0,MODE_SMA,PRICE_MEDIAN,i);     //compute MA  (works properly)
      
      if (MA[i] !=0) PriceMinusMA[i]=Close[i]/MA[i]-1;    //compute PriceMinusMA (works properly)
      else PriceMinusMA[i]=0; 
      
      if (PriceMinusMA[i]>0)                              //compute avgPositive and avgNegative (problems encountered)
      {
         countPositive+=1;     //at each new incoming positive PriceMinusMA, I resize avgPositiveTemp by increasing 1 
         ArrayResize(avgPositiveTemp,countPositive);
         avgPositiveTemp[countPositive-1]=PriceMinusMA[i]; // then I put the PriceMinusMA value into the extra array address 
         //ArraySetAsSeries(avgPositive,false);
      }
      else if (PriceMinusMA[i]<0)
      {
         countNegative+=1;
         ArrayResize(avgNegativeTemp,countNegative);
         avgNegativeTemp[countNegative-1]=PriceMinusMA[i];
         //ArraySetAsSeries(avgNegative,false);
      }
      
      if (countPositive>=20)                              //sum the laterst 20 AvgPositiveTemp values
      {
         cumPositive=0;
         for (int j=0; j<20; j++)
         {
            cumPositive+=avgPositiveTemp[countPositive-j];
         }
      }else
      {
         cumPositive=0;
         for (int k=0; k<countPositive; k++)
         {
            cumPositive+=avgPositiveTemp[countPositive-k];
         }
      }
   
      if (countNegative>=20)                              //sum the laterst 20 AvgNegativeTemp values
      {
         cumNegative=0;
         for (int l=0; l<20; l++)
         {
            cumNegative+=avgNegativeTemp[countNegative-l];
         }
      }else
      {
         cumNegative=0;
         for (int m=0; m<countNegative; m++)
         {
            cumNegative+=avgNegativeTemp[countNegative-m];
         }
      }

      if (countPositive>20)                               //compute the avgPositive (cumPositve/20)
      {
         avgPositive[i]=cumPositive/20;
      }else if (countPositive>0)
      {
         avgPositive[i]=cumPositive/countPositive;
      }else avgPositive[i]=0;

      if (countNegative>20)                               //compute the avgNegative (cumNegatve/20)
      {
         avgNegative[i]=cumNegative/20;
      }else if (countNegative>0)
      {
         avgNegative[i]=cumNegative/countNegative;
      }else avgNegative[i]=0;  
   }
  
   Print("Bars size                 = ",Bars);
   
   Print("countPositive             = ",countPositive);
   Print("countNegative             = ",countNegative);
   
   Print("avgPositiveTemp array size= ",ArraySize(avgPositiveTemp));  //Print the array size of avgPositiveTemp, problem: the size of this array should be equal to countPositive, but now it is equal to Bars size
   Print("avgNegativeTemp array size= ",ArraySize(avgNegativeTemp));  //avgNegativeTemp gets the same problem as avgPositiveTemp
   
   Print("avgPositive array size    = ",ArraySize(avgPositive));
   Print("avgNegative array size    = ",ArraySize(avgNegative));
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

There are several problems:

1. The line of avgPositive/avgNegative are not show in the window until I compile the code, I dont know why this happens.

2. Once the lines are shown, they appears to be correct, but just up to the time point when I import the indicator, afterwards when there are new incoming ticks, the indicator shows very strange and incorrect outputs.

3. The array size of avgPositiveTemp / avgNegativeTemp are dynamically changing and are supposed equal to countPositve /countNegative, but thery are now equals to the Bars size

4. the program keeps showing>>" cannot resize the array" until the last moment it shows all the print values


I just need some ideas to modify all the problems, thank you very much!

 

Hey! You didn't post the whole code in your first post so there was no mention that these arrays are all indicator buffers! What I said about array sizing is not relevant for indicator buffers of course... An indicator buffer is a special case. No need to resize them or declare their size...

 

Okay, I have tried not to set the inidcator buffer on avgPositiveTemp/avgNegativeTemp

delete:

   SetIndexBuffer(2,avgPositiveTemp);
   SetIndexBuffer(3,avgNegativeTemp);

every goes well but according to my experience (I didnt set indicator buffer initially when I started writing the code), it looks like the problem 2 I've mentioned above remians unsolved.

Anyway, I will try to monitor the result once the market starts on Monday.

Thanks Gordon!

Reason: