Object delete in chart event

 

Hi guys,

I have an indicator that duplicates horizontal lines across multiple charts on different timeframes. The code to place and move the lines works fine but the code that deletes the lines does not work.

#property strict
#property indicator_chart_window
#include <stdlib.mqh>
#include <stderror.mqh>

int OnInit()
{
 //--- enable object create events
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);


void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   
if(id == CHARTEVENT_OBJECT_DELETE)
  {
   Print("sparam = ",sparam,"   object = ",ObjectType(sparam),"   chartId = ",ChartID(),"   error = ",ErrorDescription(GetLastError()));
   if(ObjectType(sparam) == OBJ_HLINE)
   {
     .
     .
      code to loop through other charts
   }
}

 The print output is:

I did get this working in a test program but now it doesn't work.

Any ideas please?

thanks

 
One probable reason is that your function ObjectType(..) needs to be changed/corrected
Problems inside that loop may also exist

 

For example

 ENUM_OBJECT  OBJECT_TYPE      ( const string OBJECT_NAME ) {       return 
(ENUM_OBJECT) ObjectGetInteger ( ChartID () , OBJECT_NAME ,   OBJPROP_TYPE ) ; }

Everything seems to be alright
But
I have never before thought about doing anything with objects that have already been deleted
<like in code sample at the start of topic>

 
AIRAT SAFIN #:
One probable reason is that your function ObjectType(..) needs to be changed/corrected
Problems inside that loop may also exist

The 'sparam' value is definitely recording the correct horizontal line name but doesn't return the correct Object_Type value.

If I change it to this as per your suggestion

if(id == CHARTEVENT_OBJECT_DELETE)
  {
   
   Print("sparam = ",sparam,"   object = ",ObjectGetInteger ( ChartID () , sparam ,   OBJPROP_TYPE ) ,"   chartId = ",ChartID(),"   error = ",ErrorDescription(GetLastError()));
   if(ObjectType(sparam) == OBJ_HLINE)
   {

the output is

It returns the value of a vertical line which is strange. The ChartID is also correct.

The code doesn't even make it to the lopp because of the error.

As I say it was working at one stage and I didn't change the code so not sure what's going on.

I guess I need to store the object to determine which type has been removed?
 

Clarity 100%
Idea of storing the object <properties of object> to determine which type has been removed looks nice
And also it is interesting what will be printed when just before Print (..); will be inserted ResetLastError(); 



 
AIRAT SAFIN #:

Clarity 100%
But it is interesting what will be printed when just before Print (..); will be inserted ResetLastError(); 


I quickly tried that and it resets the error message to 'no error' but the Object_TYpe is still -1 value.

I've changed the way I do it now by moving to the next chart and checking for the existence of the same name line then deleting it. Seems to work.

Thanks for your comments.

 
Thank you for good news
Reason: