Arrays for chart objects!

 

Hi All,

Can anybody fathom this one? I want to assign a particular variable obtained from a chart to an array diff[]. In the code below symb[] has already been assigned at a global level. I then want to place the calculated value i.e diffx on to some chart space to use as an indicator. This code compiles with NO errors but does not load. I must be using the string diff[] array function incorrectly. Any suggestions would be welcome.

thanks

int start()
  {
     int x,y;
     int xdist = 250;
     int ydist = 100;
     for(x=0;x<=2;x++)
      for(y=0;y<=7;y++)
      
     {     
      sma7 = iMA(symb[y],PERIOD_H1,7,0,MODE_LWMA,PRICE_CLOSE,0);
      sma20 = iMA(symb[y],PERIOD_H1,20,0,MODE_LWMA,PRICE_CLOSE,0);
           
      string diffx = DoubleToStr(sma7 - sma20,0);
      string diff[] = {diffx};
      Print(diffx);
      
      ObjectCreate("diff"+x+y,OBJ_LABEL,0,0,0,0);
      ObjectSet("diff"+x+y, OBJPROP_XDISTANCE,xdist);
      ObjectSet("diff"+x+y, OBJPROP_YDISTANCE,ydist);
      ObjectSetText("diff"+x+y,diff[x],10, "Times New Roman", Green);
      ydist+= 65;
     }
 
      string diffx = DoubleToStr(sma7 - sma20,0);
      string diff[] = {diffx};
      Print(diffx);
  1. That code will not compile
    '{' - unexpected token C:\Program Files (x86)\Interbank FX Trader 4\experts\WHRea17.mq4 (71, 23)
    '{' - translator error--out of memory C:\Program Files (x86)\Interbank FX Trader 4\experts\WHRea17.mq4 (71, 23)

  2. You can only initialize arrays with constants. diffx is not.
  3. You can not print arrays, will not compile.
  4. You will have to make diff[] static or global, change its size with arrayResize and then assign.
  5. There is no need for an array in the code posted. needed iff you need the values outside the loops
 
WHRoeder:
  1. That code will not compile
    '{' - unexpected token C:\Program Files (x86)\Interbank FX Trader 4\experts\WHRea17.mq4 (71, 23)
    '{' - translator error--out of memory C:\Program Files (x86)\Interbank FX Trader 4\experts\WHRea17.mq4 (71, 23)

  2. You can only initialize arrays with constants. diffx is not.
  3. You can not print arrays, will not compile.
  4. You will have to make diff[] static or global, change its size with arrayResize and then assign.
  5. There is no need for an array in the code posted. needed iff you need the values outside the loops

thanks for the reply. I was trying to figure out how to do it w/o the other array - maybe swapping the cycles around might do it.
 

I'm not sure what your trying to do exactly, did you want the same value to appear on the screen multiple times ? You could try this if it helps any at least it compiles and runs this way I dont think its going to be exactly what your trying to do but it might help you figure out what you need to do.

int start()
  {
     int x,y;
     int xdist = 250;
     int ydist = 100;
     string diff[2];
     for(x=0;x<=2;x++)
      for(y=0;y<=7;y++)
      
     {     
     double sma7 = iMA(NULL,PERIOD_H1,7,0,MODE_LWMA,PRICE_CLOSE,0);
     double sma20 = iMA(NULL,PERIOD_H1,20,0,MODE_LWMA,PRICE_CLOSE,0);
           
      string diffx = DoubleToStr(sma7 - sma20,0);
      diff[x] = {diffx};
      Print(diff[x]);
      
      ObjectCreate("diff"+x+y,OBJ_LABEL,0,0,0,0);
      ObjectSet("diff"+x+y, OBJPROP_XDISTANCE,xdist);
      ObjectSet("diff"+x+y, OBJPROP_YDISTANCE,ydist);
      ObjectSetText("diff"+x+y,diff[x],10, "Times New Roman", Green);
      ydist+= 65;
     }
   return(0);
  }
 
SDC:

I'm not sure what your trying to do exactly, did you want the same value to appear on the screen multiple times ? You could try this if it helps any at least it compiles and runs this way I dont think its going to be exactly what your trying to do but it might help you figure out what you need to do.


thanks SDC - fantastic!! Code is very unforgiving isn't it!!? You always think you've tried every possible option until giving up which is what I did!

thanks again.

 
sd59:


thanks SDC - fantastic!! Code is very unforgiving isn't it!!? You always think you've tried every possible option until giving up which is what I did!

thanks again.


Can either of you guys please put me out of my complete and utter misery!! This code is now working i.e I have checked all values produced and they are correct but when I come to putting the values on the screen it only prints one column! There should be 3 columns with different values of "diff" in each column.

If I change the values of 'x' in the 'for' statement it follows the rules in the switch operator but it does not work for the complete cycle. i.e x=0 will produce values for case 0 etc;

Have I got the correct format for the loops?

 int start()
  {
     int x,y;
     int xdist = 250;
     int ydist = 100;
     color col;
     string diff;
     
     for(x=0;x<=2;x++)
     for(y=0;y<=7;y++)
     {
      if (MarketInfo(symb[y], MODE_POINT) == 0.00001) pointvalue = 0.0001;
      else if (MarketInfo(symb[y], MODE_POINT) == 0.001) pointvalue = 0.01;
      else pointvalue = MarketInfo(symb[y], MODE_POINT);
   
      double diffsma7_20H1 = iMA(symb[y],PERIOD_H1,7,0,MODE_LWMA,PRICE_CLOSE,0)-
                             iMA(symb[y],PERIOD_H1,20,0,MODE_LWMA,PRICE_CLOSE,0);
      double diffsma7_20H4 = iMA(symb[y],PERIOD_H4,7,0,MODE_LWMA,PRICE_CLOSE,0)-
                             iMA(symb[y],PERIOD_H4,20,0,MODE_LWMA,PRICE_CLOSE,0);
      double diffsma7_20D1 = iMA(symb[y],PERIOD_D1,7,0,MODE_LWMA,PRICE_CLOSE,0)-
                             iMA(symb[y],PERIOD_D1,20,0,MODE_LWMA,PRICE_CLOSE,0);
           
      string diffH1 = DoubleToStr((diffsma7_20H1/pointvalue),1);
      string diffH4 = DoubleToStr((diffsma7_20H4/pointvalue),1);
      string diffD1 = DoubleToStr((diffsma7_20D1/pointvalue),1);
      
      switch(x)
      {
      case 0: diff = diffH1;
              if(diffsma7_20H1 < 0)col = Red; else col=Green; break;
      case 1: diff = diffH4; xdist+= 250; 
              if(diffsma7_20H4 < 0)col = Red; else col=Green; break;
      case 2: diff = diffD1; xdist+= 250;
              if(diffsma7_20D1 < 0)col = Red; else col=Green; break;
      }
      
      ObjectCreate("diff"+x+y,OBJ_LABEL,0,0,0,0);
      ObjectSet("diff"+x+y, OBJPROP_XDISTANCE,xdist);
      ObjectSet("diff"+x+y, OBJPROP_YDISTANCE,ydist);
      ObjectSetText("diff"+x+y,diff,10, "Times New Roman", col);
      ydist+= 65;  
     }
   return(0);
  }
 

you have too much in the loop there is no need to repeat all that multiple times for every tick, you only need the code for the objects within the for loop.

Also you should put ObjectCreate() and OBJPROP_XDISTANCE and YDISTANCE in a loop in init() not start() because they only need to be done one time and you could use the counter for that loop to change XDISTANCE to make your three separate objects something like this:

int init()
   {
    string "diff";
    int k;

   for(k=1; k<=3; k++)
      {
      ObjectCreate("diff"+k,OBJ_LABEL,0,0,0);
      ObjectSet("diff"+k,OBJ_PROP_XDISTANCE,30*k);
      ObjectSet("diff"+k,OBJ_PROP_YDISTANCE,100);
      }
   return(0);
   }   

then in start() use ObjectSetText() to put the text into the objects

 
SDC:

you have too much in the loop there is no need to repeat all that multiple times for every tick, you only need the code for the objects within the for loop.

Also you should put ObjectCreate() and OBJPROP_XDISTANCE and YDISTANCE in a loop in init() not start() because they only need to be done one time and you could use the counter for that loop to change XDISTANCE to make your three separate objects something like this:

then in start() use ObjectSetText() to put the text into the objects


I thought that each label had to have a unique name - I am building a 3 x columns by 7 x rows table. So each label will need a 2 D identifier like this:

label00 label10 label20

label01 label11 label21

label02 label12 label22

.

.etc

I tried setting up the labels in the init() function with the ObjectSetText() in the start() function but nothing appeared. When I did a simple test with no loops I had to have the labels setup and ObjectSetText() in the start() function for text to appear.

If I print the elements of the loop i.e loop number + variable values, everything is correct - the only thing that is not happening is that it is not putting it out ot the screen.

Any other comments would be welcome.

 

ok now i see why your original post was about arrays this is how i would do it with an array to hold the data

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   int w=0;                    //counter to create unique name for each object
   for(int x=0; x<=2; x++)     //loop for 3 horizontal labels
      {
      for(int y=0; y<=4; y++)  //loop for 5 vertical labels
         {
         ObjectCreate("main"+w,OBJ_LABEL,0,0,0);         //create objects in init()
         ObjectSet("main"+w,OBJPROP_XDISTANCE,x*50+100);   //to create them only one time
         ObjectSet("main"+w,OBJPROP_YDISTANCE,y*15+200);
         w++;
         }
      }
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   string S1[3,5]={"this","is","test",
                  "data","to","fill",
                  "up","string","array",
                  "to","make","sure",
                  "it","works",":)"};
  
//----
   for(int i=0; i<=4; i++)   //loops to                        
      for(int j=0; j<=2; j++)//get the data
         {                   //out of the array
         int k=j*5+i;       //and into objects
         ObjectSetText("main"+k,S1[j,i],12,"tahoma",Yellow);
         }
//----
   return(0);
  }
 

I probably should have mentioned the line int k=j*5+i; is the one that sends the array value to the relevent object by the object name +k because the objects are named sequentially they fall into colums like this :

label0 label5 label10

label1 label6 label11

label2 label7 label12

label3 label8 label13

label4 label9 label14

so the j index is multiplied by the amount of rows to identify which colum it is going to put the data in plus i to identify which row, so if your table has more rows you need to change the multiplier to equal the amount of rows, i hope all this helps.

 
SDC:

I probably should have mentioned the line int k=j*5+i; is the one that sends the array value to the relevent object by the object name +k because the objects are named sequentially they fall into colums like this :

label0 label5 label10

label1 label6 label11

label2 label7 label12

label3 label8 label13

label4 label9 label14

so the j index is multiplied by the amount of rows to identify which colum it is going to put the data in plus i to identify which row, so if your table has more rows you need to change the multiplier to equal the amount of rows, i hope all this helps.


thanks SDC for taking time to answer this. I worked on it all today and you are correct about being able to create the object in the init() function - I was totally screwing up the xdistance, ydistance parameters all over the place so the reason they didn't appear is because they were off the chart!!

This is a learning experience I do not want to repeat!!

cheers

Reason: