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.
Follow this link to see how this could be implemented.
- 2019.02.14
- www.mql5.com
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.
Follow this link to see how this could be implemented.
Ok thank you so much Mr lippmaje
More power to you sir.
Let me check the link
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.
Follow this link to see how this could be implemented.
hello sir, i tried but fine it difficult to code.
can you please send 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 CAD_roc=iCustom("USDCAD",TimeFrame,"ROC1",RPeriod,true,0,1); double CAD_ROC=NormalizeDouble(-CAD_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][0] = CAD_ROC;//index 4 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.
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; } }
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; } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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 index1, 7 in index 2, 3 in index 3, 2 in index 4, 6 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...