ArrayMaximum Function Blows Up

 

Hi all,

I am a newby creating a width indicator based on Bolinger Band. What happens is that when I tried to run this indicator, BB_Max values of ArrayMaximum always blow up to 214783647 and therefore the values of BB_Percent will go to zero.

How could this happen? Any comments is appreciated. Thanks is advance. Alvin.

The following is my code:

#property copyright "Copyright ?2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"

//----
#property indicator_separate_window
#property indicator_buffers 2

#property indicator_color1 RoyalBlue
#property indicator_color2 Goldenrod

double BB_Width[];
double BB_Percent[];

int init()
{
SetIndexBuffer(0,BB_Width);
//SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(1,BB_Percent);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
return (0);
}

int deinit()
{
return(0);
}

bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if (lastbar != curbar)
{
lastbar = curbar;
return(true);
}
else return(false);
}

int start()
{

int i;
int Counted_Bars;

Counted_Bars = IndicatorCounted();
i=Bars - Counted_Bars-1;//

if (NewBar())
{
while (i>=0)
{
BB_Width[i] = iBands(NULL,0,16,2,0,PRICE_CLOSE,MODE_UPPER,i)- iBands(NULL,0,16,2,0,PRICE_CLOSE,MODE_LOWER,i);

int BB_Max_Idx = ArrayMaximum(BB_Width,100,0);
int BB_Min_Idx = ArrayMinimum(BB_Width,100,0);

double BB_Max = BB_Width[BB_Max_Idx];
double BB_Min = BB_Width[BB_Min_Idx];

Alert("BB_Max = "+BB_Max+", BB_Min = "+BB_Min+"");
double BB_Range = BB_Max - BB_Min;
double BB_New_Range = BB_Width[i]- BB_Min;

double Result = BB_New_Range / BB_Range;

BB_Percent[i] = Result; //*100
i--;
}
}
return (0);
}

 

Hi

The big number is the value an indicator buffer has when it is empty. You can put EMPTY_VALUE in an indicator buffer to make the line disappear. So you should print out the shift value at the point the empty value occurs. You will probably find you are trying to access values outside of the chart number of bars.

For instance BB_Width[i] will not have 100 values in it before "i" has been counted down by 100.

 
Ruptor wrote >>

Hi

The big number is the value an indicator buffer has when it is empty. You can put EMPTY_VALUE in an indicator buffer to make the line disappear. So you should print out the shift value at the point the empty value occurs. You will probably find you are trying to access values outside of the chart number of bars.

For instance BB_Width[i] will not have 100 values in it before "i" has been counted down by 100.

Hi Ruptor,

Thanks for your comments. I added the following codes to the int init() section but still get the same big number. Can you please let me know what I did wrong?

Thanks again.

SetIndexShift(0,BB_Width);
SetIndexShift(1,BB_Percent);

SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);

 
alvinyap:

Hi Ruptor,

Thanks for your comments. I added the following codes to the int init() section but still get the same big number. Can you please let me know what I did wrong?

Thanks again.

SetIndexShift(0,BB_Width);
SetIndexShift(1,BB_Percent);

SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);

Comment out these new lines of code and read this again.

For instance BB_Width[i] will not have 100 values in it before "i" has been counted down by 100.

I think it is your array boundaries that are the problem.

Reason: