Arraybsearch problem

 

Hi Guys,

I am creating this thread out of pure frustration. I searched metaquotes website and googled the internet ... BUT ... I cannot find an answer.

What I am trying to accomplish is this i have a sorted array ( ascending,meaning that the 0 index has the smallest value) like this:

double m_line[13];

m_line[0] = GlobalVariableGet("m_line0");

m_line[1] = GlobalVariableGet("m_line1");

m_line[2] = GlobalVariableGet("m_line2");

m_line[3] = GlobalVariableGet("m_line3");

m_line[4] = GlobalVariableGet("m_line4");

m_line[5] = GlobalVariableGet("m_line5");

m_line[6] = GlobalVariableGet("m_line6");

m_line[7] = GlobalVariableGet("m_line7");

m_line[8] = GlobalVariableGet("m_line8");

m_line[9] = GlobalVariableGet("m_line9");

m_line[10] = GlobalVariableGet("m_line10");

m_line[11] = GlobalVariableGet("m_line11");

m_line[12] = GlobalVariableGet("m_line12");

now i have two values. lets call them the bigger_value and smaller_value. I need to compare them with the array and find the closest value for each one of them.

So I would use this:

for the bigger value i want to start looking from the biggest value in the array (index 12) - SO -

int biggerindex = ArrayBsearch(

m_line, // array for search

bigger_value, // what is searched for

WHOLE_ARRAY, // count of elements to search for

12, // starting position

MODE_DESCEND // search direction

);

for smaller value i want to start looking from the smallest value in the array (index 0) - SO -

int biggerindex = ArrayBsearch(

m_line, // array for search

smaller_value, // what is searched for

WHOLE_ARRAY, // count of elements to search for

0, // starting position

MODE_ASCEND // search direction

);

Now my problem is that in the case of the bigger value it doesnt work at all and the function will always return that the closest value from array has index 12 (nonsense)

In the case of the smaller value it sort of works, but sometimes it happens that I get that the closest index is ie. nr. 5 but it is actually nr. 6, but sometimes nr. 5 is correct

Thanks a lot for help. I think that I am missing a little thing, but it has been kind of catch for me for couple days.

 
rookie_forawhile:
Hi Guys,

I am creating this thread out of pure frustration. I searched metaquotes website and googled the internet ... BUT ... I cannot find an answer.

What I am trying to accomplish is this i have a sorted array ( ascending,meaning that the 0 index has the smallest value) like this:

double m_line[13];

m_line[0] = GlobalVariableGet("m_line0");

m_line[1] = GlobalVariableGet("m_line1");

m_line[2] = GlobalVariableGet("m_line2");

m_line[3] = GlobalVariableGet("m_line3");

m_line[4] = GlobalVariableGet("m_line4");

m_line[5] = GlobalVariableGet("m_line5");

m_line[6] = GlobalVariableGet("m_line6");

m_line[7] = GlobalVariableGet("m_line7");

m_line[8] = GlobalVariableGet("m_line8");

m_line[9] = GlobalVariableGet("m_line9");

m_line[10] = GlobalVariableGet("m_line10");

m_line[11] = GlobalVariableGet("m_line11");

m_line[12] = GlobalVariableGet("m_line12");

now i have two values. lets call them the bigger_value and smaller_value. I need to compare them with the array and find the closest value for each one of them.

So I would use this:

for the bigger value i want to start looking from the biggest value in the array (index 12) - SO -

int biggerindex = ArrayBsearch(

m_line, // array for search

bigger_value, // what is searched for

WHOLE_ARRAY, // count of elements to search for

12, // starting position

MODE_DESCEND // search direction

);

for smaller value i want to start looking from the smallest value in the array (index 0) - SO -

int biggerindex = ArrayBsearch(

m_line, // array for search

smaller_value, // what is searched for

WHOLE_ARRAY, // count of elements to search for

0, // starting position

MODE_ASCEND // search direction

);

Now my problem is that in the case of the bigger value it doesnt work at all and the function will always return that the closest value from array has index 12 (nonsense)

In the case of the smaller value it sort of works, but sometimes it happens that I get that the closest index is ie. nr. 5 but it is actually nr. 6, but sometimes nr. 5 is correct

Thanks a lot for help. I think that I am missing a little thing, but it has been kind of catch for me for couple days.

Had a similar case with metatrader 5 : ArrayBSearch was worthless (it could not find a simple date in an array of dates). The end was that I had to write my own binary search. Now, when metatrader 4 and 5 are unified in a lot of things, you can expect the same : that ArrayBSearch will be worthless

Here is a simple example that will show you that ArrayBSearch() is incapable of finding values in an array as it should :

//------------------------------------------------------------

//

//------------------------------------------------------------

#property indicator_chart_window

extern double LookFor = 5;

double values[15]={4,1,6,3,19,4,2,6,3,9,4,5,6,3,9};

//------------------------------------------------------------

//

//------------------------------------------------------------

//

//

//

//

//

int init() { return(0); }

int deinit() { return(0); }

int start()

{

ArraySort(values,WHOLE_ARRAY,0,MODE_ASCEND);

int asc = ArrayBsearch(values,LookFor,WHOLE_ARRAY,0,MODE_ASCEND);

ArraySort(values,WHOLE_ARRAY,0,MODE_DESCEND);

int des = ArrayBsearch(values,LookFor,WHOLE_ARRAY,0,MODE_DESCEND);

Comment(asc," ",values[asc]," ",des," ",values[des]);

return(0);

}

If you wish to save some hair on your head, forget abut it

 
mladen:
Had a similar case with metatrader 5 : ArrayBSearch was worthless (it could not find a simple date in an array of dates). The end was that I had to write my own binary search. Now, when metatrader 4 and 5 are unified in a lot of things, you can expect the same : that ArrayBSearch will be worthless

Here is a simple example that will show you that ArrayBSearch() is incapable of finding values in an array as it should :

//------------------------------------------------------------

//

//------------------------------------------------------------

#property indicator_chart_window

extern double LookFor = 5;

double values[15]={4,1,6,3,19,4,2,6,3,9,4,5,6,3,9};

//------------------------------------------------------------

//

//------------------------------------------------------------

//

//

//

//

//

int init() { return(0); }

int deinit() { return(0); }

int start()

{

ArraySort(values,WHOLE_ARRAY,0,MODE_ASCEND);

int asc = ArrayBsearch(values,LookFor,WHOLE_ARRAY,0,MODE_ASCEND);

ArraySort(values,WHOLE_ARRAY,0,MODE_DESCEND);

int des = ArrayBsearch(values,LookFor,WHOLE_ARRAY,0,MODE_DESCEND);

Comment(asc," ",values[asc]," ",des," ",values[des]);

return(0);

}
If you wish to save some hair on your head, forget abut it

Some of metatader 5 array functions are plain stupid. If they use that it is a disaster

 
techmac:
Some of metatader 5 array functions are plain stupid. If they use that it is a disaster

Since that infortunate loss of time with ArrayBSearch in metatrader 5 I do not use built in Array functions (except the ArraySize() and ArrrayResize() functions - those are the only two built in array functions I use). I am not using ArraySort() either for one simple reason - it can sort only single dimensional arrays

Their array problems are going way back (including also some functions of the "OnArray" kind) so one has to be very careful when doing any array operations using mql -better to keep those at only those that are 100% tested and proven to be correct

 

I think that metatrader people never heard how matlab got it's name

 

Aha, thanks a lot for a quick answers. You saved me hours of work actually. But hey since I would grade myself as an inexperienced programmer, I would not even think that I will hit some limitations of MQL4/5.

But still is there some workaround? I don't think that my skill are quite enough to write my own array functions so if there is some .mqh with predefined functions I could use to do the same thing I would be grateful.

Or are you going to tell me to forget about MQL4 and rather aim for something else like jforex and it in java? I just dont believe that this flaw is limiting just me. I think that you need arrays unless you want to make some dirty code with loops.

Thanks a lot.

 

Just in case somebody is interested in this... this is what I did:

instead of using arraybsearch I did it the dirty way. i created two new arrays and then I subtracted the value that I am looking for from each element. Finally I used MathAbs (just to make sure that i am dealing with positive numbers).

Then I simly used ArrayMinimum, which seems to work OK.

Thank you for attention people :-)