Download MetaTrader 5

How can I fix this problem with arraysort?

To add comments, please log in or register
Alfredo Antonetti
1478
Alfredo Antonetti  

Hello to everyone!

 

I don't know how to solve this problem....

 

void CreateIndi()
  {
   string  Valuta;
   double  Apertura,Prezzo,Percent;
   int     B=0,Divisore=1;
   double  Percentuale[];
   double  NumeroValuta[];
   
   ArrayResize(Percentuale,SymbolsTotal(StrumentiWatchList));
   ArrayResize(NumeroValuta,SymbolsTotal(StrumentiWatchList)); // ???????

   for(int i=0; SymbolsTotal(StrumentiWatchList)>i; i++)
     {
      RefreshRates();
      Valuta           = SymbolName(i,StrumentiWatchList);
      Apertura         = iOpen(Valuta,PERIOD_D1,GiorniIndietro);
      Prezzo           = iClose(Valuta,PERIOD_D1,GiorniIndietro);
      Percent          = ((Prezzo - Apertura)/Apertura)*100;
      Percentuale[i]   = Percent;
      NumeroValuta[i]  = i;          // ?????

      if(MarketInfo(Valuta,MODE_PROFITCALCMODE)==0) Divisore=10;
      else Divisore=1;
     }

   ArraySort(Percentuale,WHOLE_ARRAY,0,MODE_DESCEND); // The problem is here, infact I lose data "valuta" 

   for(B=0; SymbolsTotal(StrumentiWatchList)>B; B++)
     {
      if(Percentuale[B]>Signal)
        {
         ObjectCreate("PerValuta"+(string)B,OBJ_LABEL,0,0,0);
         ObjectSet("PerValuta"+(string)B,OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSet("PerValuta"+(string)B,OBJPROP_YDISTANCE,(15*B)+40);
         ObjectSet("PerValuta"+(string)B,OBJPROP_XDISTANCE,10);
         ObjectSet("PerValuta"+(string)B,OBJPROP_HIDDEN,True);
         ObjectSet("PerValuta"+(string)B,OBJPROP_SELECTABLE,false);
         ObjectSetText("PerValuta"+(string)B,(string)NumeroValuta[B],8,"Calibri",clrBlack);// ??????
         WindowRedraw();

         ObjectCreate("PerValore"+(string)B,OBJ_LABEL,0,0,0);
         ObjectSet("PerValore"+(string)B,OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSet("PerValore"+(string)B,OBJPROP_YDISTANCE,(15*B)+40);
         ObjectSet("PerValore"+(string)B,OBJPROP_XDISTANCE,65+25);
         ObjectSet("PerValore"+(string)B,OBJPROP_HIDDEN,True);
         ObjectSet("PerValore"+(string)B,OBJPROP_SELECTABLE,false);
         ObjectSetText("PerValore"+(string)B,DoubleToStr(Percentuale[B],2)+" %",8,"Calibri",clrGreen);
         WindowRedraw();
        }

     }

  }

 

 The main problem is that data "Percentuale" is sorted in descending order but the currency names are wrong... How can I solve?

Thanks 

ffoorr
1067
ffoorr  

hi dottormarket,

just add a value so the name of object will be different :

 
  string instrument[];
 int size_inst =  ArrayResize(instrument,SymbolsTotal(true) ); // ???????

   for(B=0; B<size_inst; B++)
     {
     instrument[B] = SymbolName(B,true);
      if(Percentuale[B]>Signal)
        {
         ObjectCreate("PerValuta"+instrument[B],OBJ_LABEL,0,0,0);
         ObjectSet   ("PerValuta"+instrument[B],OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSet   ("PerValuta"+instrument[B],OBJPROP_YDISTANCE,(15*B)+40);
         ObjectSet   ("PerValuta"+instrument[B],OBJPROP_XDISTANCE,10);
         ObjectSet   ("PerValuta"+instrument[B],OBJPROP_HIDDEN,True);
         ObjectSet   ("PerValuta"+instrument[B],OBJPROP_SELECTABLE,false);
        ObjectSetText("PerValuta"+instrument[B],""+ instrument[B]+"   "+(string)NumeroValuta[B],8,"Calibri",clrBlack);// ??????
         WindowRedraw();

      }  }
whroeder1
15304
whroeder1  
DottorMarket:  The main problem is that data "Percentuale" is sorted in descending order but the currency names are wrong...
      Percentuale[i]   = Percent;
      NumeroValuta[i]  = i;          // ?????
ArraySort(Percentuale,WHOLE_ARRAY,0,MODE_DESCEND); // The problem is here, infact I lose data "valuta" 
The moment you sort Percentuale, there is no longer a connection to NumeroValuta and the symbol. Either don't sort as suggested by ffoorr, or you must keep the connection via a 2d array:
#define Percentuale  0
#define NumeroValuta 1
   #define COUNT     2
double  Array[][COUNT];
                                                              
   ArrayResize(Array,SymbolsTotal(StrumentiWatchList));       
                                                              
   for(int i=0; SymbolsTotal(StrumentiWatchList)>i; i++){     
      :                                                       
      Array[i][Percentuale]   = Percent;
      Array[i][NumeroValuta]  = i;
     }                                                        
                                                              
   ArraySort(Array,WHOLE_ARRAY,0,MODE_DESCEND);               
                                                              
   for(B=0; SymbolsTotal(StrumentiWatchList)>B; B++)          
     {                                                        
      if(Array[B][Percentuale]>Signal)                        
        {                                                     
         int    iSymbol    = Array[B][NumeroValuta]
         string Instrument = SymbolName(iSymbol,true);
         ObjectCreate("PerValuta"+Instrument,OBJ_LABEL,0,0,0);
         :                                                    
ffoorr
1067
ffoorr  

You where right WHRoeder, I have followed your code line, but nothing  show up in the window, maybe someone will be more lucky :

  

#define Percentuale  0
#define NumeroValuta 1
#define COUNT     2
  double  Array[][2];
 int  CreateIndi_2()
  {
   double  open,close,pourcent;int B=0;
   double Signal = 0.5;
   int    Divisore[]; 
   string symb[];
   ObjectsDeleteAll();
 
   string Instrument;
   
   
     int size_arr =   ArrayResize(Array,SymbolsTotal(true));    
     int size_ins =   ArrayResize(symb,SymbolsTotal(true));    
      int size_div  =   ArrayResize( Divisore,SymbolsTotal(true));  
 
 
   for(int i=0; i<size_arr; i++)
     {
      RefreshRates();
      open             = iOpen( SymbolName(i,true),PERIOD_D1,0);
      close            = iClose( SymbolName(i,true),PERIOD_D1,0);
      pourcent         = ((close - open)/open)*100;
      Array[i][0]      = pourcent;
      Array[i][1]      = i;
      symb[i]    = SymbolName(i,true);
      if(MarketInfo(symb[i], MODE_PROFITCALCMODE)==0) Divisore[i]=10;
      else Divisore[i]=1;
     }
 
   ArraySort(Array,WHOLE_ARRAY,0,MODE_DESCEND);               
                                                              
   for( B=0; B< size_arr; B++)          
     {                                                        
    //  if(Array[B][Percentuale]>Signal)                        
    //    {                                                     
         int    iSymbol    = (int) Array[B][NumeroValuta];
          Instrument = SymbolName(iSymbol,true);
        
         ObjectCreate("PerValuta2"+Instrument,OBJ_LABEL,0,0,0);
         ObjectSet   ("PerValuta2"+Instrument,OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSet   ("PerValuta2"+Instrument,OBJPROP_YDISTANCE,(15*B)+20);
         ObjectSet   ("PerValuta2"+Instrument,OBJPROP_XDISTANCE,10);
         ObjectSet   ("PerValuta2"+Instrument,OBJPROP_HIDDEN,True);
         ObjectSet   ("PerValuta2"+Instrument,OBJPROP_SELECTABLE,false);
        ObjectSetText("PerValuta2"+Instrument,"ooo"+Instrument+"    "+(string)Array[B][NumeroValuta],8,"Calibri",clrYellow);// ??????
         WindowRedraw();
    
         ObjectCreate("PerValore2"+Instrument,OBJ_LABEL,0,0,0);
         ObjectSet   ("PerValore2"+Instrument,OBJPROP_CORNER,CORNER_LEFT_UPPER);
         ObjectSet   ("PerValore2"+Instrument,OBJPROP_YDISTANCE,(15*B)+20);
         ObjectSet   ("PerValore2"+Instrument,OBJPROP_XDISTANCE,125);
         ObjectSet   ("PerValore2"+Instrument,OBJPROP_HIDDEN,True);
         ObjectSet   ("PerValore2"+Instrument,OBJPROP_SELECTABLE,false);
        ObjectSetText("PerValore2"+Instrument,"ooo"+DoubleToStr(Array[B][Percentuale],2)+" %",8,"Calibri",clrLime);
         WindowRedraw();
     //  }
     }
 
 return( 0 );
  }
//============================================================================
Alfredo Antonetti
1478
Alfredo Antonetti  

Thanks guys!!!

A very great solution... It works perfectly!

 

Doc 

Alfredo Antonetti
1478
Alfredo Antonetti  

hello and H.N. year =))

 

The code posted by ffoorr is perfect but there is a new problem if I impose strict mode. 

 

When I impose strict mode the compiler returns "array out of range" in... etc etc

 

how can I fix this problem? :(

 

thanks

 

Alfredo 

Alfredo Antonetti
1478
Alfredo Antonetti  
nothing?
whroeder1
15304
whroeder1  
DottorMarket: returns "array out of range" in...
DottorMarket: nothing?
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. How should we know, You don't post what array, what line, what the variable values were. There are no mind readers here.
To add comments, please log in or register