Buffer problem ArrayMaximum, ArrayMinimum

12

Hi,

To Anyone who can shed some light:

I'm plotting the Highest High and Lowest Low in Line style buffers,  the number of bars look back is counted since the last crossover of close price moving above or below a 100 SMA.

The problem is that on the current bar(most recent) the buffers flare up and down and I'm unsure how to correct this, I'll attach a picture and my code for further clarification.

I'd appreciate a solution, Thank you!

Here is the Code I am using:

```int start()
{
int i,Counted_bars;

Counted_bars=IndicatorCounted();
i=Bars-Counted_bars-1;
if(Counted_bars == 0)
i = Bars -1 -FixZeroD;
double sum=0;
int markerpoint, tracedistance;
while(i>=0)
{

//Calculate:

ExtMapBuffer0[i]=iMA(NULL,0,100,0,MODE_SMA,PRICE_CLOSE,i);

//Markers:
if ( (Close[i] > ExtMapBuffer0[i]) && (Close[i+1] <= ExtMapBuffer0[i+1]) ) {
markerpoint = i+1;
}
if ( (Close[i] < ExtMapBuffer0[i]) && (Close[i+1] >= ExtMapBuffer0[i+1]) ) {
markerpoint = i+1;
}

tracedistance = markerpoint-i;

//Trace high low from markers:

ExtMapBuffer1[i]=High[ArrayMaximum(High,tracedistance,i)];
ExtMapBuffer3[i]=Low[ArrayMinimum(Low,tracedistance,i)];

i--;
}```
1108

I am not sure what you want to do but if

marketpoint = i+1;

and tracedistance = marketpoint - i;

then tracedistance = i+1-i = 1;

doing this would give à point, so the buffer has to be an arrow_style buffer :

extern int tracedistance= 20; // 20 bars

```ExtMapBuffer1[i]=High[ArrayMaximum(High,tracedistance,markerpoint)];
ExtMapBuffer3[i]=Low[ArrayMinimum(Low,tracedistance,markerpoint)];```
12

Hi,

I just want to not have the line buffers flare off up and down on the current bar and just continue to plot the current array high and low.

//ffoorr -  trace distance cannot be 1. the value of marketpoint is set as current bar i when the condition is met and saved for calculation against realtime var i.

Moderator
10619

First of all, I believe that you should never assume that a variable is automatically initialised to 0

`   int markerpoint=0, tracedistance=0;    `

Not tested

```   tracedistance=markerpoint-i;

//Trace high low from markers:
if(tracedistance>0)
{
ExtMapBuffer1[i]=High[ArrayMaximum(High,tracedistance,i)];
ExtMapBuffer3[i]=Low[ArrayMinimum(Low,tracedistance,i)];
}
else
{
ExtMapBuffer1[i]=ExtMapBuffer1[i+1];
ExtMapBuffer3[i]=ExtMapBuffer3[i+1];
}```

but it may help towards solving your problem.

I think that you still have further work to do as the indicator will not work properly when i==0

12

GumRai:

First of all, I believe that you should never assume that a variable is automatically initialised to 0

Not tested

but it may help towards solving your problem.

I think that you still have further work to do as the indicator will not work properly when i==0

Bingo, its working!

GumiRai, Thanks for pointing me in the right direction!

What solved it was declaring " int markerpoint=0, tracedistance=0; " in the global section instead of inside of the " int start() " function. To be honest, I don't completely understand the  logic of why that works (hence I never solved it on my own in the first place).

Could you please give a brief explanation as to why you think this made it work, I'm trying to become a better mql programmer. I appreciate the help!

Moderator
10619

Danno473:

Bingo, its working!

GumiRai, Thanks for pointing me in the right direction!

What solved it was declaring " int markerpoint=0, tracedistance=0; " in the global section instead of inside of the " int start() " function. To be honest, I don't completely understand the  logic of why that works (hence I never solved it on my own in the first place).

Could you please give a brief explanation as to why you think this made it work, I'm trying to become a better mql programmer. I appreciate the help!

```//Markers:
if ( (Close[i] > ExtMapBuffer0[i]) && (Close[i+1] <= ExtMapBuffer0[i+1]) ) {
markerpoint = i+1;
}
if ( (Close[i] < ExtMapBuffer0[i]) && (Close[i+1] >= ExtMapBuffer0[i+1]) ) {
markerpoint = i+1;
}

tracedistance = markerpoint-i;```

Following the first call, i will =0 on subsequent ticks.

if the variables are declared locally and assuming initialised at 0

if neither of the conditions are true

markerpoint remains unchanged at 0

Therefore

tracedistance = markerpoint-i;

tracedistance = 0-i;

tracedistance = 0-0;

tracedistance = 0;

```ExtMapBuffer1[i]=High[ArrayMaximum(High,tracedistance,i)];
ExtMapBuffer3[i]=Low[ArrayMinimum(Low,tracedistance,i)];```

will be

```ExtMapBuffer1[i]=High[ArrayMaximum(High,0,0)];
ExtMapBuffer3[i]=Low[ArrayMinimum(Low,0,0)];```

Which will give strange results, I believe that it defaults to the high and low of the whole array

1108

What is needed is rather what you aim to do, cause it's confuse :

`Could you please give a brief explanation as to why you think this made it work`

Are you talking about the ima 100 line buffer  ?

` the line buffers flare off up and down on the current bar`