In MT4, is there a simple way to get a UniqueID per *each* instance of an indicator?

 

Hi MT4 programmers,

I have indicators that create objects.  For a few of my indicators, I might add more than one (of the same indicator) to a chart.

A problem will occur if the indicators both create and delete the same object names.  Therefore, the usual workaround is to use a user-defined property, like:
    extern int  UniqueID_per_indicator = 1; // When adding >1 of the same indicator, change the number

By now, with b600+ features, isn't there now some way to automatically get a simple (preferably short, 1-2 digit) unique ID# that the indicator itself can retrieve?

I just need a UniqueID.  I say "short" is better, because some object names I use actually get quite long and may exceed the 62 or 63 character limit for object names.

Note: Before anyone suggests to use a random integer generator, that really doesn't work well with  templates!  The first time added to a chart, it works fine.  But if you save the chart to a template, it saves all the indicator-created objects too.  But when you use this template again, the indicator will get a *new* uniqueID, and therefore, nothing will remove the stale objects from the chart (that came from the template).  They end up on top of each other.


This is such a basic need.  I see plenty of code with the external variable approach, but why bother the user to set such a simple (but necessarily unique) integer?

Any ideas?  Any code snippet for MT4?

Thanks,

pips4life (Kent)


P.S. Is this doable in MT5?  I haven't dived in there yet.

 
pips4life:

Hi MT4 programmers,

I have indicators that create objects.  For a few of my indicators, I might add more than one (of the same indicator) to a chart.

A problem will occur if the indicators both create and delete the same object names.  Therefore, the usual workaround is to use a user-defined property, like:
    extern int  UniqueID_per_indicator = 1; // When adding >1 of the same indicator, change the number

By now, with b600+ features, isn't there now some way to automatically get a simple (preferably short, 1-2 digit) unique ID# that the indicator itself can retrieve?

I just need a UniqueID.  I say "short" is better, because some object names I use actually get quite long and may exceed the 62 or 63 character limit for object names.

Note: Before anyone suggests to use a random integer generator, that really doesn't work well with  templates!  The first time added to a chart, it works fine.  But if you save the chart to a template, it saves all the indicator-created objects too.  But when you use this template again, the indicator will get a *new* uniqueID, and therefore, nothing will remove the stale objects from the chart (that came from the template).  They end up on top of each other.


This is such a basic need.  I see plenty of code with the external variable approach, but why bother the user to set such a simple (but necessarily unique) integer?

Any ideas?  Any code snippet for MT4?

Thanks,

pips4life (Kent)


P.S. Is this doable in MT5?  I haven't dived in there yet.

If you use any automatic way then :

- it will not work with templates (and there is no way to correct that)

- it will leave a mess when the terminal crashes (it "does not happen" but nevertheless)

- you have no control (as user) at all in some cases what will happen with multiple instances

- none of the above ....


Much better to take care of the unique ID "manually"

 

Add your indicators.

Let them draw their objects.

Now manually delete all the objects, then save the template.

Your template now has the indicators but no "stale" objects.

The ease of "delete all the objects" depends on how aggressively your indicators check and redraw objects.
 
There was an interesting discussion some years ago, but no definitive solution I am afraid.
 
honest_knave:

Add your indicators.

Let them draw their objects.

Now manually delete all the objects, then save the template.

Your template now has the indicators but no "stale" objects.

The ease of "delete all the objects" depends on how aggressively your indicators check and redraw objects.
So ? How each indicator instance will recognize its objects ?
 

This is how i do it.

YourObjectName = StringConcatenate(YourObjectName, " #", WindowOnDropped());
 
Mladen Rakic:

If you use any automatic way then :

- it will not work with templates (and there is no way to correct that)

- it will leave a mess when the terminal crashes (it "does not happen" but nevertheless)

- you have no control (as user) at all in some cases what will happen with multiple instances

- none of the above ....


Much better to take care of the unique ID "manually"

An automatic way would be a "mess" if... the ID was random, or dynamic and unpredictable.

But it need not necessarily be so.

For example, even after a crash, when you look at a list of indicators on the chart, it's typically a simple list, with the same order every time.

If the indicator could just retrieve a simple count, that means "I am the 4th indicator on the list", then that's all I need.


Alternatively, if I can just query the list of all attached indicators, look for the indicator itself, and determine how many are attached.  The order is not relevant, because both would start by deleting all the objects, and each would then recreate their own set, wherever they go (e.g. one set TL, the other BL).


Thanks,

Kent

P.S.  Regarding the other suggestions:

"WindowOnDropped()" always returns the same value when multiple of the same indicators (that use the main window) are added.  Am I missing something?

Regarding to delete all objects and THEN saving to the template, yes, it depends how quickly objects get regenerated after they are deleted.  If only when added or when changing TF, it works.  If by every tick, it is a bother to disconnect from the data feed just so one can have the time to delete objects and make a template.  

 
pips4life:

An automatic way would be a "mess" if... the ID was random, or dynamic and unpredictable.

But it need not necessarily be so.

For example, even after a crash, when you look at a list of indicators on the chart, it's typically a simple list, with the same order every time.

If the indicator could just retrieve a simple count, that means "I am the 4th indicator on the list", then that's all I need.


Alternatively, if I can just query the list of all attached indicators, look for the indicator itself, and determine how many are attached.  The order is not relevant, because both would start by deleting all the objects, and each would then recreate their own set, wherever they go (e.g. one set TL, the other BL).


Thanks,

Kent

P.S.  Regarding the other suggestions:

"WindowOnDropped()" always returns the same value when multiple of the same indicators (that use the main window) are added.  Am I missing something?

Regarding to delete all objects and THEN saving to the template, yes, it depends how quickly objects get regenerated after they are deleted.  If only when added or when changing TF, it works.  If by every tick, it is a bother to disconnect from the data feed just so one can have the time to delete objects and make a template.  


You can use WindowPriceOnDropped(), WindowTimeOnDropped(), WindowXOnDropped(), WindowYOnDropped() or their combinations as a unique id if your indicators are at the same window.

Just make sure you drag n drop the indicator instead of double click it.

 
Meng Yin Teoh:

You can use WindowPriceOnDropped(), WindowTimeOnDropped(), WindowXOnDropped(), WindowYOnDropped() or their combinations as a unique id if your indicators are at the same window.

Just make sure you drag n drop the indicator instead of double click it.


Thanks for your input, but it seems you are missing the point.  It needs to be a unique-but-repeatable ID#.   Otherwise the created objects that get stored on a template are orphaned objects that still exist (but are not deleted nor maintained) when the template is reused.

Example:     Add 2 copies of the same indicator.   Let's say the unique ID's are "1" and "2", respectively.  (I hope to NOT need to set them manually, and that's the info I'm asking for).

The first creates an object:   MyLabel_1.   The 2nd:  MyLabel_2

Now create a template.  The template contains both labels as simple objects, but it has both indicators attached.   However, when the template is reused, it starts up and USES the existing two labels.  (Or, better, it should delete them initially, and regenerate both, therefore they will be in the expected location).  The labels have repeatable names.  This is good.

In contrast, if the UniqueID# is random, it would create "MyLabel_13939321", which if saved into a template, will do nothing when the template is reloaded and the indicator initializes and then decides to use "MyLabel_58930012".   The old label is clutter and the user will need to manually delete it -- OR, as already mentioned, delete it before making the template, but that might be a bother, as already described.

 
pips4life:


Thanks for your input, but it seems you are missing the point.  It needs to be a unique-but-repeatable ID#.   Otherwise the created objects that get stored on a template are orphaned objects that still exist (but are not deleted nor maintained) when the template is reused.

Example:     Add 2 copies of the same indicator.   Let's say the unique ID's are "1" and "2", respectively.  (I hope to NOT need to set them manually, and that's the info I'm asking for).

The first creates an object:   MyLabel_1.   The 2nd:  MyLabel_2

Now create a template.  The template contains both labels as simple objects, but it has both indicators attached.   However, when the template is reused, it starts up and USES the existing two labels.  (Or, better, it should delete them initially, and regenerate both, therefore they will be in the expected location).  The labels have repeatable names.  This is good.

In contrast, if the UniqueID# is random, it would create "MyLabel_13939321", which if saved into a template, will do nothing when the template is reloaded and the indicator initializes and then decides to use "MyLabel_58930012".   The old label is clutter and the user will need to manually delete it -- OR, as already mentioned, delete it before making the template, but that might be a bother, as already described.


Sorry my bad. These Window____OnDropped methods do not work with indicator but EA/script only.


If I understand you correctly, if you need both indicators at the same window, then i assume that both indicator will create a different set of objects with unique coordinate, right? how about adding their coordinate to their objectname?

Or if both indicators have different parameter settings, then maybe you can use the setting as a unique id too?

 

pips4life:

For example, even after a crash, when you look at a list of indicators on the chart, it's typically a simple list, with the same order every time.

If the indicator could just retrieve a simple count, that means "I am the 4th indicator on the list", then that's all I need.

Are you sure about that ?

Last time I checked it was not the case, but it was a long time ago.

PS: When someone post a message in your thread it's nice to get an answer.

Reason: