• Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)

Question Cara Sorting Two Dimensional Array?

pije76

New Member
Credit Hunter
Credits
0
Mohon bantuan master koding MQL4.
Bagaimana cara sorting two dimensional array dari highest ke lowest dan kemudian ambil 3 nilai yg tertingginya?
Saya sudah coba tapi masih error, mohon bantuannya....

Code:
int start()
{
  int tHour=0, count=0, indxcount=0, price=0, indx=0;
  datetime thisbartime = 0;
  double myArray[][2];
  double dOpen;

  for (int i=0; i<500; i++)
  {
    //Inside main loop, add condition here to check for Hour = 0
    tHour = TimeHour(iTime(NULL,60,iBarShift(NULL,60,thisbartime)));

    //start at hour zero
    if(tHour == 0 )
    if(thisbartime!=Time[0])
    {
      thisbartime=Time[0];
      //then
      ArrayResize(myArray, 500, 0);
    }

    //set up another "for loop" length = 24 candles
    //use 24 as there are 24 candles in a day for H1 TF
    for(int j=0; j<24; j++)
    {
    //populate the array with the bar index
    //those are the 2 dimensions array
    dOpen = Open[i];
    }

    //need a counter to keep track of where we are in the candle array
    //going right to left so indxcount=i+j

    //also we will need and int to populate the array
    //count
    myArray[count][0] = Open[indxcount];
    myArray[count][1] = indxcount;

    //then increment after each element
    count++;

    //then sort the array
    ArraySort(myArray, WHOLE_ARRAY, 0, MODE_DESCEND);
    }
  }

  //set up a second "for loop" with the length equal to the number of highs you want to display on the chart with lines etc.
  for (int k=0; k < 3; k++)
  {
    //then read the array elements
    price = myArray[k][0];
    indx = myArray[k][1];
  }

  //And call the draw trend line function using price and candle index as parameters to draw the trend line
  double dOpen=Open[i];
  if (i==Bars-1 || myArray>Open[i+1])
  {
    UpColor[i]=myArray;
    DownColor[i]=0.0;
    }
    else
    {
    UpColor[i]=0.0;
    DownColor[i]=myArray;
   }
  }

  return(0);
}
 
scriptnya buatan sendiri ya Gan? itu variable indxcount belum diapa-apai... :)
idenya dari saya, logicnya dibantu seorang teman gan. saya baru belajar MQL4 sebulan ini...

Ini udah bisa sorting highestnya, tapi mau ambil array yg selain highestnya agar kedua2nya bisa tampil sekaligus... masih belum bisa gan...
mohon bantuannya...

Code:
int start()
{
  double Open;
  int kIndex;

  //-----//define 2 counters
  int cnt=0;
  int ij=0;

  int Limit;
  int counted_bars=IndicatorCounted();
  //---- last counted bar will be recounted
  if(counted_bars>0) counted_bars--; //always count the previous bar
  if ( NumberOfBars == 0 )
  NumberOfBars = Bars-counted_bars;
  Limit=NumberOfBars;

  for(int i=0; i<Limit; i++)
  {
  if(TimeHour(Time[i]) == 0)
  {
  ArrayResize(ArrayVolume, Limit, 0);

  for(int j=0; j<ControlOfBars; j++)
  {

  ij=i+j;

  ArrayVolume[cnt][0] = Volume[ij];
  ArrayVolume[cnt][1] = ij;

  cnt++;
  } // end of "j for" loop

  ArraySort(ArrayVolume, WHOLE_ARRAY, 0, MODE_DESCEND);

  for (int k=0; k<NumberOfHighs; k++)
  {
  Open = ArrayVolume[k][0];
  kIndex = ArrayVolume[k][1];

  if (Close[kIndex] > Close[kIndex+1])
  {
  Bull_Bar[kIndex] = Open;
  Bear_Bar[kIndex] = 0;
  }
  else
  {
  Bear_Bar[kIndex] = Open;
  Bull_Bar[kIndex] = 0;
  }
  } //end of "k for" loop

  } // end of "if" condition for hour == 0

  ArrayResize(ArrayVolume,0,0);

  }// end of "i for" loop

  return(0);
}
 
Back
Top