# selecting first 2 highest values in Arrays and first 2 lowest values in Arrays (Help)

Hello dear developers,

I have this code below:

The Array "SymbolsROC" store the roc of the symbols(in double) stated on the function "WeakAndStrongCurrency()". what I want is to select the first 2 highest values and the last 2 lowest values in the array without changing the index of the array. EXAMPLE: if  SymbolsROC[7]={4,1,7,3,2,6,8} . (you can see that 4 is in index 0, 1 in index17 in index 23 in index 32 in index 46 in index 5 and 8 in index 6)

So I want to select the first 2 highest values in the array which is 8 and 7 with the same index 8 in index 6 and 7 in index 2 . Same with the first 2 lowest value in the array.

The reason I want the index to be the same is because it will be use to identify  which Currency has the value , EXAMPLE: the value 8 in index 6 = CHF (meaning CHF has ROC of 8), the value 7 in index 2 = AUD (meaning AUD has the roc of 7)

Please how can I code this?

Your support will be highly appreciated...

```input int RPeriod = 1;
input ENUM_TIMEFRAMES TimeFrame=PERIOD_D1;

double SymbolsROC[7];

void OnTick()
{
WeakAndStrongCurrency();
}

void WeakAndStrongCurrency()
{
double EUR_roc=iCustom("EURUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double EUR_ROC=NormalizeDouble(EUR_roc,2);

double GBP_roc=iCustom("GBPUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double GBP_ROC=NormalizeDouble(GBP_roc,2);

double AUD_roc=iCustom("AUDUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double AUD_ROC=NormalizeDouble(AUD_roc,2);

double NZD_roc=iCustom("NZDUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double NZD_ROC=NormalizeDouble(NZD_roc,2);

double JPY_roc=iCustom("USDJPY",TimeFrame,"ROC1",RPeriod,true,0,1);
double JPY_ROC=NormalizeDouble(-JPY_roc,2);

double CHF_roc=iCustom("USDCHF",TimeFrame,"ROC1",RPeriod,true,0,1);
double CHF_ROC=NormalizeDouble(-CHF_roc,2);

static int LastNumberOfBars;

if(iBars(NULL,TimeFrame)>LastNumberOfBars)
{
for(int i=0; i<ArraySize(SymbolsROC); i++)
{
SymbolsROC[0] = EUR_ROC;//index 0
SymbolsROC[1] = GBP_ROC;//index 1
SymbolsROC[2] = AUD_ROC;//index 2
SymbolsROC[3] = NZD_ROC;//index 3
SymbolsROC[5] = JPY_ROC;//index 5
SymbolsROC[6] = CHF_ROC;//index 6
}
LastNumberOfBars=iBars(NULL,TimeFrame);
}
}```
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
• www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

You can create a 2-dimensional double array, where column #1 holds the ROC values and column #2 the original index.

Then use ArraySort to sort it in descending order, grab the top 2 indices and the bottom 2 indices and there you are.

how to sort and display in color
• 2019.02.14
• www.mql5.com
Say I have follow data Nr. B/S Pair Profit 1 Buy EURUSD -90.12 2. Sell USDJPY 100.04 3. Sell AUDUSD -100.12 4. Buy GBPUSD -123.34 5...

lippmaje:

You can create a 2-dimensional double array, where column #1 holds the ROC values and column #2 the original index.

Then use ArraySort to sort it in descending order, grab the top 2 indices and the bottom 2 indices and there you are.

Ok thank you so much  Mr  lippmaje

More power to you sir.

lippmaje:

You can create a 2-dimensional double array, where column #1 holds the ROC values and column #2 the original index.

Then use ArraySort to sort it in descending order, grab the top 2 indices and the bottom 2 indices and there you are.

hello sir, i tried but fine it difficult to code.

```double SymbolsROC[7][2];
int Highest,Highest2,Lowest,Lowest2;

void WeakAndStrongCurrency()
{
static int LastNumberOfBars = -1;
int bars = iBars(NULL,TimeFrame);

if(bars == LastNumberOfBars)
return;

double EUR_roc=iCustom("EURUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double EUR_ROC=NormalizeDouble(EUR_roc,2);

double GBP_roc=iCustom("GBPUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double GBP_ROC=NormalizeDouble(GBP_roc,2);

double AUD_roc=iCustom("AUDUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double AUD_ROC=NormalizeDouble(AUD_roc,2);

double NZD_roc=iCustom("NZDUSD",TimeFrame,"ROC1",RPeriod,true,0,1);
double NZD_ROC=NormalizeDouble(NZD_roc,2);

double JPY_roc=iCustom("USDJPY",TimeFrame,"ROC1",RPeriod,true,0,1);
double JPY_ROC=NormalizeDouble(-JPY_roc,2);

double CHF_roc=iCustom("USDCHF",TimeFrame,"ROC1",RPeriod,true,0,1);
double CHF_ROC=NormalizeDouble(-CHF_roc,2);

SymbolsROC[0][0] = EUR_ROC;//index 0
SymbolsROC[0][1] = 0;
SymbolsROC[1][0] = GBP_ROC;//index 1
SymbolsROC[1][1] = 1;
SymbolsROC[2][0] = AUD_ROC;//index 2
SymbolsROC[2][1] = 2;
SymbolsROC[3][0] = NZD_ROC;//index 3
SymbolsROC[3][1] = 3;
SymbolsROC[4][1] = 4;
SymbolsROC[5][0] = JPY_ROC;//index 5
SymbolsROC[5][1] = 5;
SymbolsROC[6][0] = CHF_ROC;//index 6
SymbolsROC[6][1] = 6;

ArraySort(SymbolsROC,WHOLE_ARRAY,0,MODE_DESCEND);

Highest  = (int)SymbolsROC[0][1];
Highest2 = (int)SymbolsROC[1][1];
Lowest   = (int)SymbolsROC[6][1];
Lowest2  = (int)SymbolsROC[5][1];

LastNumberOfBars = bars;
}```
Like so and I moved the iCustom calls beyond the new bar check otherwise it will blast your backtest.

lippmaje:
Like so and I moved the iCustom calls beyond the new bar check otherwise it will blast your backtest.

@lippmaje Thank you so much sir, am checking

lippmaje
• www.mql5.com

lippmaje: You can create a 2-dimensional double array, where column #1 holds the ROC values and column #2 the original index.

Then use ArraySort to sort it in descending order, grab the top 2 indices and the bottom 2 indices and there you are.

Or you can just go through the array and find them — no sorting or remembering the indexes required.

```#define  PRICE    double         // A Price
#define     PRICE_MAX   PRICE( EMPTY_VALUE)
#define     PRICE_MIN   PRICE(-EMPTY_VALUE)     // Oil went negative in 3/2020.

PRICE  HH=PRICE_MIN, H2nd=PRICE_MIN, LL=PRICE_MAX, L2nd=PRICE_MAX;
int   iHH,          iH2nd,          iLL,          iL2nd;
for(int iArr = ArraySize(A)-1; iArr >= 0; --iArr){
PRICE p=A[iArr];
if(     p > HH){  H2nd = HH; iH2nd = iHH; HH   = p; iHH   = iArr; }
else if(P > H2nd){                        H2nd = p; iH2nd = iArr; }
else if(P < LL){  L2nd = LL; iL2nd = iLL; LL   = p; iLL   = iArr; }
else if(P < L2nd){                        L2nd = p; iL2nd = iArr; }
}```

lippmaje:
Like so and I moved the iCustom calls beyond the new bar check otherwise it will blast your backtest.

I used this code and it works well, THAK YOU, THANK YOU AND THANK YOU SIR ,

i will send you a private massege

William Roeder:

Or you can just go through the array and find them — no sorting or remembering the indexes required.

There is a flaw in that code. Single pass sort never work. Amend it that way so that if the last results shows one of the lowest price, you can catch it.

```#define  PRICE    double         // A Price
#define     PRICE_MAX   PRICE( EMPTY_VALUE)
#define     PRICE_MIN   PRICE(-EMPTY_VALUE)     // Oil went negative in 3/2020.

PRICE  HH=PRICE_MIN, H2nd=PRICE_MIN, LL=PRICE_MAX, L2nd=PRICE_MAX;
int   iHH,          iH2nd,          iLL,          iL2nd;
for(int iArr = ArraySize(A)-1; iArr >= 0; --iArr){
PRICE p=A[iArr];
if(     p > HH){  H2nd = HH; iH2nd = iHH; HH   = p; iHH   = iArr; }
else if(P > H2nd){                        H2nd = p; iH2nd = iArr; }
if(P < LL){  L2nd = LL; iL2nd = iLL; LL   = p; iLL   = iArr; }
else if(P < L2nd){                        L2nd = p; iL2nd = iArr; }
}```