Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1409

 
Good day all!
A question on mql4, more specifically on the ArraySort() function
Here is my code
double LoY[31][31];
int P1;
void OnTick()
{
if (TimeCurrent()==1262568096)
{
for(int r=0; r<31;r++)
{
LoY[r][0]=1.6104;
LoY[r][1]=r;
P1=1;
}
}
if (TimeCurrent()>1262568095)
{
ArraySort(LoY,WHOLE_ARRAY,0,MODE_ASCEND);
for( r=0; r<31;r++)
Print("-------------------------------------LoY[r][1]--------------=",  LoY[r][1],"  r ",   r,"  LoY[r][0] ",  DoubleToString( LoY[r][0],5));
}

if (Bid-LoY[0][0]>=0.0030)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid, 3,Ask+300*Point,Ask-100*Point,"300",0);
LoY[0][0]=Bid;
}
}

When LoY[0][0] has got the value Bid( 1.6134) after the order has been opened, the ArraySort(LoY,WHOLE_ARRAY,0,MODE_ASCEND) function has sorted the array in ascending order by the first dimension and has moved the array element with the biggest value to the top of the array. So, LoY[0][0] together with its value becomes LoY[30][0] in the array sorted in ascending order. This is logical and therefore I agree with it.


What in my opinion is not logical or acceptable to me? Why did ArraySort() LoY[15][0] place 0 under the index, despite the fact that its value (1.61040) equals values of all the other array items except the thirtieth. The same is not clear, why ArraySort()LoY[30][0] has put index 15, despite the fact that its value (1.61040) equals the values of all the other elements of the array except for the thirtieth.

A QUESTION: how to make an element with index 0 be LoY[1][0] on this tick , an element with index 15 beLoY[15][0] and an element with index 29 be LoY[30][0]
. In other words, how to make a function not to sort elements with the same value in further? It is not logical and senseless.

Thank you for your help.

 

This is probably the sorting algorithm. There are some permutations of the array elements.

Try other sorting options.

Методы сортировки и их визуализация с помощью MQL5
Методы сортировки и их визуализация с помощью MQL5
  • www.mql5.com
Для работы с графикой в MQL5 создана специальная библиотека Graphic.mqh. В статье описан пример ее практического применения и поясняется сама суть сортировок. По каждой сортировке существует как минимум отдельная статья, а по ряду из них уже опубликованы целые исследования, поэтому здесь описывается лишь общая идея.
 
Aleksei Stepanenko:

This is probably the sorting algorithm. There are some permutations of the array elements.

Try other sorting options.

Thank you very much for the advice. Could you please tell me if there are other sorting options in mql4? If so, where are they in the directory?

 

try it:

//функция быстрой сортировки
void SortArray(int &eArray[], int eFirst, int eLast)
   {
   int eMiddle, eTemp;
   int eLeft=eFirst, eRight=eLast;
   //вычисление опорного элемента
   eMiddle=eArray[(eLeft+eRight)/2];
   do
      {
      while(eArray[eLeft]<eMiddle) eLeft++;
      while(eArray[eRight]>eMiddle) eRight--;
      //перестановка элементов
      if(eLeft<=eRight)
         {
         eTemp=eArray[eLeft];
         eArray[eLeft]=eArray[eRight];
         eArray[eRight]=eTemp;
         eLeft++;
         eRight--;
         }
      }
   while(eLeft<eRight);
   if(eFirst<eRight) SortArray(eArray,eFirst,eRight);
   if(eLeft<eLast) SortArray(eArray,eLeft,eLast);
   }

although the function is for a one-dimensional array, but it can be reworked

 
Aleksei Stepanenko:

try it:

It's true that the function is for a one-dimensional array, but it can be reworked.

Thank you very much.

 

A word of advice, please,

The code looks for open charts and creates a label with the name of the symbol, but if two charts have the same symbol,

then it only creates a label on one chart.What am I doing wrong?

//---
   string name;
   for(int i=0; i<10; i++)
     {
      if(ChartSymbol(ChartFirst()+i)=="EURGBP")
        {name="EURGBP";
         if(ObjectFind(ChartFirst()+i,name+IntegerToString(i))!=0)
           {
            Create_Label(ChartFirst()+i,name+IntegerToString(i),0,5,5,CORNER_RIGHT_UPPER,name,"Times New Roman",10,
                      clrBlack,0,ANCHOR_RIGHT_UPPER," ",false,false,false,true,0);
           }
        }
     }
 
MakarFX:

A word of advice, please,

The code looks for open charts and creates a label with the name of the symbol, but if two charts have the same symbol,

it only creates the label on one chart.What am I doing wrong?

The chart IDs (ChartID()) of the same symbol are different. Use them.

 
Artyom Trishkin:

The ChartID() identifiers of the same symbol are different. Use them.

That's what I use it for,
ChartFirst()+i

i.e. going through all open charts

Or did I misunderstand you? Is there any way to make it clear, in the form of code?

 
Very strange, puts a mark on all new open charts, only one chart is ignored (
 
MakarFX:
Very strange, on all new open charts puts a mark, only one chart is ignored (

Read the documentation and example code carefully. Your loop is not organised correctly.

//--- переменные для идентификаторов графиков
   long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   Print("ChartFirst = ",ChartSymbol(prevChart)," ID = ",prevChart);
   while(i<limit)// у нас наверняка не больше 100 открытых графиков
     {
      currChart=ChartNext(prevChart); // на основании предыдущего получим новый график
      if(currChart<0) break;          // достигли конца списка графиков
      Print(i,ChartSymbol(currChart)," ID =",currChart);
      prevChart=currChart;// запомним идентификатор текущего графика для ChartNext()
      i++;// не забудем увеличить счетчик
     }
Don't tell me that for and while loops work the same way. That's not the problem, it's the highlighted lines.