EA Not finding first (oldest) object on chart.

 

Hello all,

I've got this weird issue that I haven't been able to find the answer to, so now I turn to you and hopefully you know what's happening.

As a relatively new "student", I'm still experimenting with code and see how it all works together.

I've been experimenting with finding objects, and then getting their details so I can use them.

I've written a fairly simple EA which creates a vline on the open of each new bar. Then it loops through all object on charts from the OLDEST candle to the most recent, finding all objects (in this case vertical lines, OBJ_VLINE) and then print the names and prices starting with the oldest first. etc etc...

But, it refuses to find the very first (again, the oldest) line. Even with successive loops. Object count seems to always be one higher than the line count. (not just by the code, but also confirming visually).

3 lines on the chart, 3 objects in the comment section, 2 lines printed.

It does not matter whether I run this code on a live chart or in the tester and neither does the period.

I would understand if it wouldn't find the line at Bar 0 because maybe it would count the lines and it had not been created yet, but as far as I know this code should be counting from right to left on the chart. So i see no reason why it would skip the first one.

I'm at a bit of a loss here. lol. thanks for any insights :)

void OnTick()
  {
      string VlineName  =  "Vline_";
      string Part       =  StringSubstr(VlineName, 0, StringLen(VlineName));
      
      if(NewBar()==true)
      {
         {
         //Vline:       Drawn At Open (which makes sense because Newbar)
         ObjectCreate(0, Part+TimeCurrent(), OBJ_VLINE, 0,Time[0], Close[0]);
            //ObjectSetInteger(0, "Vline_"+TimeCurrent(),OBJPROP_COLOR,clrYellow);
         }

      // Get the total number of objects on the chart
      int AllObjects = ObjectsTotal();

      // Iterate through all of the objects
      for (int i = AllObjects; i >0 ; i--)
      //for (int i = 0; i < AllObjects; i++)
         {
            // Get the name of the object
            string objectName = ObjectName(i);

            // Check if the object is a vertical line
            if (ObjectType(objectName) == OBJ_VLINE)
            {
            int obj_color_integer = ObjectGetInteger(0,objectName,OBJPROP_COLOR,0);
            double   object_price   =  ObjectGetDouble(0, objectName, OBJPROP_PRICE);
               
Print("All Objects: ",AllObjects," - Line: ",ObjectName(i)," Color obj int " + obj_color_integer," Line Price: ",object_price); 
            }
         }
      }

      //====================================================================
      //COMMENT SECTION
      //====================================================================
      string ExpertName    = WindowExpertName();
      string ExpertTime = string(TimeToString(TimeCurrent(),TIME_SECONDS));
      int count = ObjectsTotal();
      
      Comment
         (
         "\n","===============================","\n",
         "\n",
         "Expert Name: ",ExpertName,"\n",
         "\n","\n",
         "Bars ",Bars," - ",ExpertTime,"\n",
         "\n","\n",
         "Count ",count,"\n",                
         "\n","\n",
         "\n","\n"
         );

   }  //END ONTICK
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
MartinS13X:
 for (int i = AllObjects; i >0 ; i--)

Should be

 for (int i = AllObjects-1; i >=0 ; i--)
 
Keith Watford #:

Should be

Ah.. yes. that fixed it.  Thank you.


But... now my question is, why does it behave that way? Because that's the first object and as far as I know it first counts all the objects which (even with only one line)  should be > 0 and then starts counting down until it reaches zero.


Also, because it should count the objects from right to left on the chart, I assume that it would find at least the very first (oldest) one and maybe miss out on the newest line because the counter has reached 0 on bar 0.

 
MartinS13X #: Ah.. yes. that fixed it.  Thank you. But... now my question is, why does it behave that way? Because that's the first object and as far as I know it first counts all the objects which (even with only one line)  should be > 0 and then starts counting down until it reaches zero. Also, because it should count the objects from right to left on the chart, I assume that it would find at least the very first (oldest) one and maybe miss out on the newest line because the counter has reached 0 on bar 0.


Because the objects are indexed 0 ... n-1, and not 1 ... n. Please see the example code in the documentation — ObjectsTotal - Object Functions - MQL4 Reference

Also, the object index has nothing to do with bar indexing or shift. They are totally unrelated.

The object indexing does not signify left to right or right to left. The objects can be in any order because their positions are defined by either their anchor points or graphical coordinates, and not their object indexing.

ObjectsTotal - Object Functions - MQL4 Reference
ObjectsTotal - Object Functions - MQL4 Reference
  • docs.mql4.com
ObjectsTotal - Object Functions - MQL4 Reference
 
Fernando Carreiro #:

Because the objects are indexed 0 ... n-1, and not 1 ... n. Please see the example code in the documentation — ObjectsTotal - Object Functions - MQL4 Reference

Also, the object index has nothing to do with bar indexing or shift. They are totally unrelated.

The object indexing does not signify left to right or right to left. The objects can be in any order because their positions are defined by either their anchor points or graphical coordinates, and not their object indexing.


Makes sense. :)

I kind of figured that because of the for loop running through the bars (in order) the results would also be in order instead of accessed directly.

Guess I can always try and add the line details to an array and then sort the array.


Thank you for your input.