Minimum Price in Nearest Bars

 

I'm using the following to calculate the Maximum Price within 5 bars:


double Bar[10];
Bar[1]=Open[1];
Bar[2]=Open[2];
Bar[3]=Open[3];
Bar[4]=Open[4];
Bar[5]=Open[5];
Bar[6]=Close[1];
Bar[7]=Close[2];
Bar[8]=Close[3];
Bar[9]=Close[4];
Bar[10]=Close[5];

string test = StringConcatenate(Bar[1],Bar[2],Bar[3],Bar[4],Bar[5],Bar[6],Bar[7],Bar[8],Bar[9]);
//double num_array[15]={4,1,6,3,9,4,1,6,3,9,4,1,6,3,9};
int maxValueidx=ArrayMaximum(Bar);
Print("Min value = ", Bar[maxValueidx],test);


This code works great and does in fact calculate the Max Price. However, if I change just one word (ArrayMaximum to ArrayMinimum) it fails. If I put the numbers into an array like num_array it works, but reading from Open[] and Close[] it doesn't. Anybody have any idea about this?

 

This is what I ended up with, it's taken from an article. It's a much easier way of accomplishing the same thing as above but actually works :-)


FYI: In case somebody wants this for FibRetracement, trend==1 is buy, trend==2 is sell, and int factor is to account for differences in pip values.


int FibRetracement(int trend, int factor)
{
//Check current price
//Go back max of 7 bars for min/max
double sellmin, // Minimal price
buymax; // Maximal price

int Quant_Bars=7;
int Ind_max =ArrayMaximum(High,Quant_Bars,1);// Bar index of max. price
int Ind_min =ArrayMinimum(Low, Quant_Bars,1);// Bar index of min. price
buymax=High[Ind_max]; // Desired max. price
sellmin=Low[Ind_min]; // Desired min. price
if(trend==1)
{
double bprice=Ask;
//subtract bar price from current price and get pips
double bdifference=(buymax - bprice)*factor;
if(bprice>buymax)
{
return (0);
}
else
{
return (bdifference);
}
}
if(trend==2)
{
double sprice=Bid;
double sdifference=(sprice - sellmin)*factor;
if(sprice<sellmin)
{
return (0);
}
else
{
return (sdifference);
}
}
return (0);
}