Download MetaTrader 5
To add comments, please log in or register
Do you need a programmer? Make an order to let the developer find you!
sky90
18
sky90 2016.07.18 23:43 

Hi, I have three arrays:

 

  • "Pair1"
  • "Pair2"
  • "Profit"

For example Pair1{EURUSD,GBPUSD}  / Pair2{AUDUSD,NZDUSD} / Profit{100,300}

These values are linked, so an output could be:

"Index 0: EURUSD-AUDUSD-100"

"Index 1: GBPUSD-NZDUSD-300".  

 Now I would sort a single array and then I would that other 2 arrays will remain linked to the sorted array.

So for example if I want to sort the third array "Profit" (desc) it will be {300,100}. Now pair1 and pair2 array should be {GBPUSD,EURUSD} and {NZDUSD,AUDUSD}. How can i do this in mql4?

Sorry for english, it's not my language. I hope it's enough clear.

Thanks for your help! 

Vasyl Nosal
18212
Vasyl Nosal 2016.07.19 11:08  

2 ways

long buffers_index_copy[];
long buf_index_copy3[];
ArrayResize(buffers_index_copy,symols_chek_counter);
ArrayResize(buf_index_copy3,symols_chek_counter);
ArrayCopy(buffers_index_copy,volumes);
ArrayCopy(buf_index_copy3,volumes);
ArraySort(volumes,WHOLE_ARRAY,0,MODE_DESCEND);




for(int i=0;i<symols_chek_counter;i++)
{
if(volumes[i]==0) continue;
for(int bs=0;bs<symols_chek_counter;bs++)
{
if(volumes[i]==buffers_index_copy[bs])
{
...
buffers_index_copy[bs]=0;
break;
}
}
}

      

ArrayCopy(volumes,buf_index_copy3);

 

2)

 Asort[ 40, 2]

 Asort[ i, 0]=values

 Asort[ i, 1]=index in start arrays

sky90
18
sky90 2016.07.19 12:20  
Thanks eevviill, can you insert some comments to explain what you did? I need to sort also string arrays so I can't use always ArraySort function
whroeder1
13620
whroeder1 2016.07.19 19:19  
Then you'd have write a specific sort, Array or not to array (Mike Tanton) - MQL4 forum - Page 2 #4
sky90
18
sky90 2016.07.19 23:23  
WHRoeder:
Then you'd have write a specific sort, Array or not to array (Mike Tanton) - MQL4 forum - Page 2 #4

Thanks, do you mean to sort string arrays?

If someone could do an example in mql4 like I did with simple text in my post, I will really appreciate it

whroeder1
13620
whroeder1 2016.07.20 14:20  
The insertion sort I posted sorts any native type, and with a defined operator<() or a predicate any type. You would know this had you bothered to read the link provided.
sky90
18
sky90 2016.07.20 23:37  
WHRoeder:
The insertion sort I posted sorts any native type, and with a defined operator<() or a predicate any type. You would know this had you bothered to read the link provided.

I read but I did not understand anything that's why I asked if it was possible a concrete example with the data that I put in the first post. In any case, thanks for the link.

You posted a mqh file with some functions that I don't understand, sorry. But I have to use "insertion_sort" function with which parameters?

 

If I have 5 string arrays and 5 double arrays, if I do this:

insertion_sort(MyStringArray,0,ArraySize(MyStringArray),MyFilterArray);

 I have an overload error.

eevviill12
11
eevviill12 2016.11.24 00:07  
sky90:
Thanks eevviill, can you insert some comments to explain what you did? I need to sort also string arrays so I can't use always ArraySort function

 

////////////////////////////////////
void OnStart()
  {
  double numbs[6][2];
  string names [6];
  numbs[0][0]=3;numbs[1][0]=4.2;numbs[2][0]=2;numbs[3][0]=12.8;numbs[4][0]=1;numbs[5][0]=9;//value
  numbs[0][1]=0;numbs[1][1]=1;numbs[2][1]=2;numbs[3][1]=3;numbs[4][1]=4;numbs[5][1]=5;//start indexies
names[0]="a";names[1]="b";names[2]="c";names[3]="d";names[4]="e";names[5]="f";//names


  
ArraySort(numbs);
int index;
  
  for(int i=0;i<6;i++)
  {
  index=int(numbs[i][1]);
  Alert("Value:"+string(numbs[i][0])+" Name:"+names[index]);
  }
  
  
  }

numbs[i][1]

Will have start index. So you can use it for detect all you need. 

eevviill12
11
eevviill12 2016.11.24 01:11  
Or you can use Structures
whroeder1
13620
whroeder1 2016.11.24 14:16  
sky90: I read but I did not understand anything that's why I asked if it was possible a concrete example with the data that I put in the first post.

I posted a link that showed you that "it was possible" and it has a "concrete example." All you had to do was read, understand, and adapt to your case.

Not compiled, not tested.

struct Data{
   string pair1;
   string pair2;
   double profit;
   void   Data(void){}                   // Allow array of Data
   void   Data(const Data& that) :       // Copy
      pair1(that.pair1), pair2(that.pair2), profit(that.profit){}
   void   operator=(const Data& that){   // Assignment
      pair1 = that.pair1; pair2 = that.pair2; profit = that.profit;
   }
   bool operator<(const Data& that){ return this.pair1 < that.pair1; }   // Default ordering      (Option A)

}
class DataByProfit{ public:                                              // Non-default ordering  (Option B)
  bool is_before(const Data& lhs, const Data& rhs){ return lhs.profit < rhs.profit; }
};
//////
Data data[]; ...; insertion_sort(data);                                  // sort by pair1.        (Option A)
DataByProfit obp; insertion_sort(data, 0 , ArraySize(data), obp);        // sort by profit.       (Option B)

Not compiled, not tested.

Was that so hard?
/
To add comments, please log in or register