Problem for get the line name

 

Hi. 

I`m beginner & study for MQL5 programming.


I make the EA get the trend line name & Horizontal name but i can not fix it.

I use two ObjectName in EA. 1st ObjectName is OK buy 2nd ObjectName is not correctly. If I change each two sentence, always 1st is OK.

I don`t know why it is not properly excute. Please help me.


   int trendTotal=ObjectsTotal(0,0,OBJ_TREND);


   int hlineTotal=ObjectsTotal(0,0,OBJ_HLINE);


   int lineTotal=trendTotal+hlineTotal;


   ArrayFree(lines);

   ArrayResize(lines,lineTotal);


   for(int i=0; i<lineTotal;i++)

     {

      if(i<trendTotal)

        {

         lines[i].name=ObjectName(0,i,0,OBJ_TREND);

         Print(i,"   =   ",lines[i].name);

                }

      else

        {

         lines[i].name=ObjectName(0,i,0,OBJ_HLINE);

         Print(i,"   =   ",lines[i].name);

        }

     }


  }
Files:
Test.mq5  3 kb
 
lines[i].name=ObjectName(0,i,0,OBJ_TREND);
lines[i].name=ObjectName(0,i,0,OBJ_HLINE);
if(i<trendTotal)
You are using one variable for two incompatible purposes.
Object typeTLTLHLHL
Object index0101
Line index0123

  1. Make one variable, to load your lines[] and write two loops over the range of object counts to get the objects.
  2. Or adjust your HL index
 

I make fix it. Thank you so much.

   int objCount=0;



   for(int i=0; i<trendTotal;i++)

     {

      lines[objCount].name=ObjectName(0,i,0,OBJ_TREND);

      Print(objCount,"   =   ",lines[objCount].name);

      lines[objCount].curPrice=NormalizeDouble(ObjectGetValueByTime(0,lines[objCount].name,time),_Digits+1);

      objCount++;

     }



   for(int j=0;j<hlineTotal;j++)

     {

      lines[objCount].name=ObjectName(0,j,0,OBJ_HLINE);

      Print(objCount,"   =   ",lines[objCount].name);

      lines[objCount].curPrice=NormalizeDouble(ObjectGetValueByTime(0,lines[objCount].name,time),_Digits+1);

      objCount++;

     }
 
When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
          Messages Editor