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));
}
}
}
}
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?
Exactly, this.
if(id == CHARTEVENT_OBJECT_CLICK && OBJ_TREND == (ENUM_OBJECT)ObjectGetInteger(0,sparam,OBJPROP_TYPE))
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.
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!
if (ObjectGet(objectName, OBJPROP_SELECTED) > -1) { // This object is selected.
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 |
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 |
Oops! My fault! Mixed it up with if statements checking for NULL.
Thanks for clearing that up, I'll edit my above post!

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.