Using object functions in indicators

 

I am trying to read a horizontal line value dropped on an indicator sub window, but having no luck.

Take any indicator, drop a horizontal line and change its name to "down" (right click, properties). I then have this code in the indicator:

Do object functions work in indicator sub windows as expected?

What I am trying to do is to create a level line in an indicator that I can drag up and down to alert on.

        Print(ObjectFind("down"));
returns -1. Bug as the object does exist?
        Print(ObjectFind(1, "down"));
returns 0, should return 1 as its window 1?
        Print(ObjectGetDouble(1, "down", OBJPROP_PRICE));
always returns 0
 
rocketman99:

I am trying to read a horizontal line value dropped on an indicator sub window, but having no luck.

Take any indicator, drop a horizontal line and change its name to "down" (right click, properties). I then have this code in the indicator:

Do object functions work in indicator sub windows as expected?

What I am trying to do is to create a level line in an indicator that I can drag up and down to alert on.


Is the chart ID for these correct, for current chart, chart ID is 0 ?

        Print(ObjectFind(1, "down"));
        Print(ObjectGetDouble(1, "down", OBJPROP_PRICE));

 This works as expected for me, make sure that the object name is correct, maybe it is actually "Down"

        Print(ObjectFind("down"));
//returns 1. no bug
        Print(ObjectFind(0, "down"));
//returns 1
        Print(ObjectGetDouble(0, "down", OBJPROP_PRICE));
//returns correct price
 
GumRai:


Is the chart ID for these correct, for current chart, chart ID is 0 ?

 This works as expected for me, make sure that the object name is correct, maybe it is actually "Down"

 

 



Note that I am talking about an indicator, not a chart. ObjectFind(1, "down") returns returns 0 which I think is incorrect. My indicator is in window 1 so ObjectFind should return 1 (the manual says -1 if object not found, otherwise the window number where 0 is chart, 1 is indi one etc)?

So yes, its finding the object called "down", but the return values are incorrect. Other functions like ObjectGetDouble do not work then.

 

Use the ID of the main chart window in ObjectFind and ObjectGetDouble not the Id of the sub-window. If it is the current chart you can use zero.

ObjectGetDouble will find the object regardless of which sub window it is in.

   Print(ObjectFind(0,"down"));
   Print(ObjectGetDouble(0,"down",OBJPROP_PRICE,0));


As a side issue, while testing this I came across a bug. Both ObjectFind() and ObjectGetDouble() are not case sensitive (I am assuming they should be).

I named my line "Down" but I used "down" in both functions and they both still found the line.

 
GumRai:


Is the chart ID for these correct, for current chart, chart ID is 0 ?

 

        Print(ObjectFind("down"));
//returns 1. no bug
        Print(ObjectFind(0, "down"));
//returns 1
        Print(ObjectGetDouble(0, "down", OBJPROP_PRICE));
//returns correct price

 


rocketman99:



Note that I am talking about an indicator, not a chart. ObjectFind(1, "down") returns returns 0 which I think is incorrect. My indicator is in window 1 so ObjectFind should return 1 (the manual says -1 if object not found, otherwise the window number where 0 is chart, 1 is indi one etc)?

So yes, its finding the object called "down", but the return values are incorrect. Other functions like ObjectGetDouble do not work then.


 Read the documentation. You are confusing the Chart ID with the sub-window number.

You are not finding the object because you are using the wrong chart ID 

 
SDC:

As a side issue, while testing this I came across a bug. Both ObjectFind() and ObjectGetDouble() are not case sensitive (I am assuming they should be).

I named my line "Down" but I used "down" in both functions and they both still found the line.

You are right 

I assumed the same, thought that it was case sensitive.

 

Ok, thanks. I will give this a go later as I am not at my MT4 PC.

As for bugs, this also does not work:

Print(ObjectFind("down"));

If you do not specify an ID then ObjectFind is supposed to search window 0, but returns -1 instead.

Also, based on what your tests confirm, I think the documentation is incorrect:

See https://docs.mql4.com/objects/objectfind

It states:

Note

The chart sub-windows (if there are sub-windows with indicators in the chart) are numbered starting from 1. The chart main window always exists and has the 0 index.

This is confusing.

Also, ObjectFind(1, "down") returns returns 0, but by your logic it should return -1 as the indicator is actually window 0 if ObjectFind is called from the indicator???

Even more confusion.

 
rocketman99:

Ok, thanks. I will give this a go later as I am not at my MT4 PC.

As for bugs, this also does not work:

Print(ObjectFind("down"));

If you do not specify an ID then ObjectFind is supposed to search window 0, but returns -1 instead.

Also, based on what your tests confirm, I think the documentation is incorrect:

See https://docs.mql4.com/objects/objectfind

It states:

This is confusing.

Also, ObjectFind(1, "down") returns returns 0, but by your logic it should return -1 as the indicator is actually window 0 if ObjectFind is called from the indicator???

Even more confusion.

Tested using below and it works fine. Thanks.

   Print(ObjectFind(0,"down"));
   Print(ObjectGetDouble(0,"down",OBJPROP_PRICE,0));