What is the counterpart to the ObjectName function? - page 2

 
Alain Verleyen:
Can you provide a use case for this need ?

I sure can. It seems strange to me that it isn't obvious to you. And I'll not give you a single case, but a whole slew of them: Any algorithm that manipulates chart objects and needs to keep track of its own data for those objects.

Here's an example: tracking the X/Y coordinates of a rectangle object, which again MQL does not keep track of for me and offer to me, even though it has all the necessary data internally to do so.

So how am I supposed to know when the mouse pointer is hovering over one of the objects I've created on a chart?

Anyone who has understood RDBM design principles since the 80s knows that names are horrible things to use as primary keys, and they actually never are the true primary key -- which is why we so fetishize ID numbers everywhere, lol. Even if we go to the trouble of ensuring unique names to identify an item/entity row/object, it also has a unique number associated with it somewhere. You cannot create either a string array or an RDB Customer table without this number. If you created an RDB "Name" table/entity with only a single field "Name", you can rest assured that the DBMS itself keeps an internal list of every name row in Name whose primary key is numeric. And the reason (a normalization rule actually) is that you don't embed meaningful information in a primary key.

But when it comes to objects, looking at MQL as a database of information of the elements in/on a chart, that's exactly what it did. And then rather than expose the true primary key, it keeps it hidden from us.

If further explanation is needed:

Chart object names are listed internally by MQL in one or more lists I'll collectively refer to as the "master" name list.

I want to create an array to keep track of my own information related to the objects I create on a chart. The obvious and simplest way to do that is to use an object's already existing index value (its position in MQL's master name list) as a logical pointer to the object and data related to it.

But MQL does not reveal that index value.

So, I'm forced to duplicate MQL's master name list, creating a second string array of object names in addition to the one MQL has already created, so that I can use the index value in my duplicate list (an array for my purposes). That's the only way I can refer to chart objects without being forced to first know their names.

But then again, that's not just me. Every single coder trying to do similar things needs to duplicate MQL's master object name list for no reason other than that MQL hides the primary keys to the objects on a chart.

We can loop through the master list using an index value from 0 to ObjectsTotal(0) and gather those index values, and then we can refer to chart objects by whichever means happens to be most prudent for the situation at hand -- either by name or by index value. MQL has all all that information as soon as we create an object. It's all there, so why make every single one of us coders reinvent a wheel that already exists? Why not just give it to us?


 
William Roeder:
  1. ObjectsTotal() and ObjectsName() is that list.
  2. Since everything about objects is by name, and you know the name of the objects you create, why do you need to know where the terminal stored it?
  3. If an object is deleted, by human or by other software, your index list is now bogus, and the name list contains garbage.
  4. If you maintain an array of all names, the index of a name is the index of the object. Index list is irrelevant (index[i] == i).

Sorry William, no they're not. ObjectsTotal yields a numeric value that is not by any stretch a list. ObjectName() is a list, sure, but it's a list of strings. Here's what you're missing:

bool bSuccess = ObjectCreate(0,"My test object",OBJ_RECTANGLE ,0,D'2021.08.20 09:00:00',1.1684);

So tell me, having created the object named "My test object", what index value do I use later in my code to refer to it using the ObjectName() function?

 
Amir Yacoby:

Its interesting first to understand the need itself, because from what I see from going over the code  it is trying to keep the indexes of its own created objects. Few queestions arise:

  1. Why do I care  about the indexes? it is only used in order to get object names.
  2. What about objects created in between your program, by other programs or the  user, on the same chart? First, your indexes kept in the program, will defer the indexes of the chart itself so why bother at all. Second, you will not get their names in your array. While you can intercept the object creation event while your program is running, but objects created prior to your program loading, you will not get their names unless you call the ObjectsTotal() and loop to get their names.

Also,I would also love to know what is the need for that at all.

Amir, see my response to Alain.

Q-1: No, the index values are not "only used" to get object names. If I collect data about objects that would mean (simplest case) an array which by definition involves one index per dimension. How do you propose you'd know which object the 2nd element of that array refers to?

Q-2: Any indicator, script, or EA running on a chart can build/rebuild an array that mirrors the internal MQL list of all objects that currently exist on the chart. It would be a complete list and object names would appear in the same order (same index value). It wouldn't matter what created them or when. No need to capture the object creation event. 

" you will not get their names unless you call the ObjectsTotal() and loop to get their names."

Yes, agreed. That's the problem.

 
William Roeder:
  1. ObjectsTotal() and ObjectsName() is that list.
  2. Since everything about objects is by name, and you know the name of the objects you create, why do you need to know where the terminal stored it?
  3. If an object is deleted, by human or by other software, your index list is now bogus, and the name list contains garbage.
  4. If you maintain an array of all names, the index of a name is the index of the object. Index list is irrelevant (index[i] == i).

I'll respond to the rest of your points tho.

2. Explained well enough in my response to Alain. I could respond, though, that if I don't need to know the index value of an object's name, then why does ObjectName require exactly that index value as an input parameter? ("int   pos// number in the list of objects ")

3. Yes, of course, but in most simple cases that's not even a possibility. But let's take the case of an EA on a chart where objects are getting created by indicators or a script running on the chart. The smart EA would rebuild the list prior to using it for the very purpose of picking up those changes, if they possibly could happen.

4. Yes of course. I don't need an array of indexes. I need MQL to expose its already existing index value of an object's name in "the list of objects". I need this precisely so that I (and you, and everyone else) would not need to create and maintain any such list of all names. Why would we do that when MQL has already done it the instant it puts the object on a chart?

 

There is 0 problem with "tracking the X/Y coordinates of a rectangle object", and you don't need an index at all. 

You decided without any valid argument that a string or object name is not good enough, maybe (though I don't think so) but it's MT5 reality, there is no point to deal with an inexistent thing. And in the mql5 paradigm there is no need for a index function or whatever. All of this has nothing to do with a RDBM.You approach is inefficient because you want to stick to a model you have in mind since the 80's apparently instead of dealing with today mql reality.

Anyway, you are free to code as you want and free to be wrong. No offense intended.

 
Alain Verleyen:

There is 0 problem with "tracking the X/Y coordinates of a rectangle object", and you don't need an index at all. 

You decided without any valid argument that a string or object name is not good enough, maybe (though I don't think so) but it's MT5 reality, there is no point to deal with an inexistent thing. And in the mql5 paradigm there is no need for a index function or whatever. All of this has nothing to do with a RDBM.You approach is inefficient because you want to stick to a model you have in mind since the 80's apparently instead of dealing with today mql reality.

Anyway, you are free to code as you want and free to be wrong. No offense intended.

Alain, I showed you mine, you show me yours.

How would you keep track of the X/Y coordinates of multiple OBJ_RECTANGLE objects on a chart? You don't need to provide compilable code. I won't ask that much of you. A clear explanation or pseudocode will do. 

It ain't how you say it is just cuz you say it is, dude.

"You decided without any valid argument".

Incorrect. The parallels I drew with RDBM principles are not unique to databases, let alone relational databases. They apply to data design (RDB, OO, even more sophisticated types) wherever it occurs. Including MQL5. Including in my coding and your own coding. 

And given that I laid out compilable code for you that does exactly what I need, claiming I had "no argument" is a riot. Does one need to give you an argument when they've already given you proof?

instead of dealing with today mql reality."

Again, incorrect. I'm not avoiding dealing with today mql reality (sic), I already dealt with it and provided the code that deals shows how I did it. I'll never have to code another ObjectName() call requiring an index value ("pos") MQL refuses to give me again, unless I want to.

And then, when I do want to invoke ObjectName(), where will I get the required "pos" index value from? Answer my question to Amir marked "Q-1". From where will you get the necessary "pos" value of an object from immediately after creating an object?

MQL?

No, from MY code or something similar which duplicates internal functionality already existing in MQL but hidden from the coder.

Besides, you're not reacting to my factual statements, you seem to be reacting to my tone, which I understand. You like MQL. You defend it. Cool. But you're missing my factual points as a result.

 
Alain Verleyen:

There is 0 problem with "tracking the X/Y coordinates of a rectangle object", and you don't need an index at all. 

You decided without any valid argument that a string or object name is not good enough, maybe (though I don't think so) but it's MT5 reality, there is no point to deal with an inexistent thing. And in the mql5 paradigm there is no need for a index function or whatever. All of this has nothing to do with a RDBM.You approach is inefficient because you want to stick to a model you have in mind since the 80's apparently instead of dealing with today mql reality.

Anyway, you are free to code as you want and free to be wrong. No offense intended.

PS. I'd love to see code that tracks X/Y coordinates of an OBJ_RECTANGLE object. I'm probably doing it in the most cumbersome way possible. I'm new to C++ and the MQL variant, I've been working with it only since Jan. I learn a lot from the code that people post here. I look forward to seeing yours or at least a clear outline/pseudocode of how you'd do it. Thanks!

 
Millard Melnyk: So tell me, having created the object named "My test object", what index value do I use later in my code to refer to it using the ObjectName() function?

The name of the object is "My test object" No where do you need to use ObjectName(). Use the (unique) name of the object.

 
Millard Melnyk:

PS. I'd love to see code that tracks X/Y coordinates of an OBJ_RECTANGLE object. I'm probably doing it in the most cumbersome way possible. I'm new to C++ and the MQL variant, I've been working with it only since Jan. I learn a lot from the code that people post here. I look forward to seeing yours or at least a clear outline/pseudocode of how you'd do it. Thanks!

You don't need an index or whatever to "keep track of the X/Y coordinates of multiple OBJ_RECTANGLE" ? That's not even a use case. An OBJ_RECTANGLE is using time/price coordinates, so what is the real use case ? When you have the name of an object just use the appropriate functions to get the object data you want.

Anyway, it's trivial to get the coordinates of an object, no need for an "index", it's trivial to get the coordinates of the mouse. And so it's trivial to check the mouse is inside an object. And that's right mql5/MT5 will not do it for you, you have to code it, that's what I called "live with reality" instead of being sarcastic about what the platform don't provide and will never provide.

 
What exactly is the problem that you look a solution for?
Getting an x/y coordinate of an object you created and thus you have its name? Easy. No need for index at all. 
The mql5 functions that gives you that data, don't need any index. Just the name of the object. So where is the problem you are trying to solve?

P.S. And you have the array.
Just instead of defining it in your program, which is bogus because the real list changes and objects get deleted and new ones inserted, so instead of writing:

MyObject[i]  in order to get the name

You just write
ObjectName(0,i)

With the name you can get all the info about the object, including x/y. No need for index or anything else.
Reason: