Identify selected object

 

I'd like to identify a trend line when clicked.

The chart would then display additional info & lines related to it, so that the chart isn't too clustered in the normal state.

I know how to do the rest, but Is there a way to get the object's name or any info when it is selected?

Thank you for the help.

 
Go through the list of objects, get the name, check if it is a trend line and selected.
 
whroeder1:
Go through the list of objects, get the name, check if it is a trend line and selected.

Thanks again for the quick help!

That should be the proper code then (not tested yet):

EDIT: Tested now, I used OBJPROP_SELECTED but that actually does not return whether the object is currently selected, it return true if the object is allowed to be selected in general.

EDIT: 2 As pointed out below by whroeder1 OBJPROP_SELECTED is the correct property, but my check for -1 was wrong, it is updated now.


void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) {
   if (id == CHARTEVENT_OBJECT_CLICK) {
      for (int i = 0; i < ObjectsTotal(0, 0, OBJ_TREND); i++) {
         string objectName = ObjectName(0, i, 0, OBJ_TREND);
        
         if (ObjectGet(objectName, OBJPROP_SELECTED) == 1) {   //fixed this, no longer checking for > -1
            // This object is selected.
            double price = ObjectGetDouble(0, objectName, OBJPROP_PRICE);
            int startTime = ObjectGet(objectName, OBJPROP_TIME1);
           
            Print("Trend line starts at " + TimeToStr(startTime) + " at " + DoubleToStr(price, 2));
         }
      }
   }
}

 
ZetaTrader:

Thanks again for the quick help!

That should be the proper code then (not tested yet):


void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) {
   if (id == CHARTEVENT_OBJECT_CLICK) {
      for (int i = 0; i < ObjectsTotal(0, 0, OBJ_TREND); i++) {
         string objectName = ObjectName(0, i, 0, OBJ_TREND);
        
         if (ObjectGet(objectName, OBJPROP_SELECTED) > -1) {
            // This object is selected.
            double price = ObjectGetDouble(0, objectName, OBJPROP_PRICE);
            int startTime = ObjectGet(objectName, OBJPROP_TIME1);
           
            Print("Trend line starts at " + TimeToStr(startTime) + " at " + DoubleToStr(price, 2));
         }
      }
   }
}

sparam will give you the name of the object that caused that event - why don't you use that instead of looping through the objects?
 
Mladen Rakic:
sparam will give you the name of the object that caused that event - why don't you use that instead of looping through the objects?

Exactly, this.

if(id == CHARTEVENT_OBJECT_CLICK && OBJ_TREND == (ENUM_OBJECT)ObjectGetInteger(0,sparam,OBJPROP_TYPE))
 
Mladen Rakic:
sparam will give you the name of the object that caused that event - why don't you use that instead of looping through the objects?

nicholishen:

Exactly, this.


Awesome! Thanks, it works great! I didn't realise the function would provide these parameters, I've rarely used OnChartEvent before.

As edited in my above post, the code I posted does not work as intended. EDIT: The code is updated to work now!

 
ZetaTrader: EDIT: Tested now, I used OBJPROP_SELECTED but that actually does not return whether the object is currently selected, it return true if the object is allowed to be selected in general.
         if (ObjectGet(objectName, OBJPROP_SELECTED) > -1) {
            // This object is selected.
That isn't true. Your if test is bogus. It is a boolean. Zero is False and One is True.  Greater than minus one is always true.

         if ( (bool)ObjectGet(objectName, OBJPROP_SELECTED) ) {
            // This object is selected.

Identifier

Description

Property Type

OBJPROP_SELECTED

Object is selected

bool

OBJPROP_SELECTABLE

Object availability

bool

          Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference

 
whroeder1:
That isn't true. Your if test is bogus. It is a boolean. Zero is False and One is True.  Greater than minus one is always true.

Identifier

Description

Property Type

OBJPROP_SELECTED

Object is selected

bool

OBJPROP_SELECTABLE

Object availability

bool

          Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference


Oops! My fault! Mixed it up with if statements checking for NULL.

Thanks for clearing that up, I'll edit my above post!

Reason: