Horizontal line not deleted

 

Hello community,

i have an expert which can create horizontal lines.

When detach expert, they get deleted but very often (not always) remain in chart.

When delete object, i get error 4207.


  ResetLastError();
  if(ObjectDelete(objchartid,objname) == false)
         PrintFormat("#error delete object %s code=%d",objname,GetLastError());
  else
         PrintFormat("object %s deleted",objname);

chartid always 0.

I have this problem with no other objects: button,rectangle, label. Just with hline.

What does 4207 mean ? Where should i look to fix it.

Thank you

 

MT4 I guess ?

4207

ERR_SOME_OBJECT_ERROR

Graphical object error

Never seen this error. Congrats :-D

 

I have an expert where you can reproduce the problem.

It creates a button and a hline.

When you click on the button, the hline and the button get deleted and recreated.

Do this multiple times, sooner or later you will get the message ...4207 in the expert console

and the hline remains in chart because not deleted.


Please look in the code for #problem, where i wrote a comment.

It turns out, that changing the buttons size (raise) ,removes the problem.

Files:
 
chinaski:

I have an expert where you can reproduce the problem.

It creates a button and a hline.

When you click on the button, the hline and the button get deleted and recreated.

Do this multiple times, sooner or later you will get the message ...4207 in the expert console

and the hline remains in chart because not deleted.


Please look in the code for #problem, where i wrote a comment.

It turns out, that changing the buttons size (raise) ,removes the problem.

There is an mql4/MT4 issue for sure. You are right that when increasing the size of the button the problem disappear. (Also if you set your line as NOT selected by the way).

However, your way to deal with objects is uselessly complex. I noticed when you click on the button, not only you are deleting the line but also the button, why ? There is no need to delete anything. On a click, move your existing line and update the status of your button if needed.

 

Delete the button along with line is a feature, the purpose a must. To select line is also a must.


The objects in the sample are quite compact or not ? Just one line for creation. What you mean by complexity are simply the requirements.


Sorry, this is the wrong point to critisize. The much more very strange point is the behaviour: Deletion of another object fails, depending on a certain button size. This smells for some very strange side effects.

 
chinaski:

Delete the button along with line is a feature, the purpose a must. To select line is also a must.

No deleting a button when you click on it is not a must, not deleting a line to recreate one at an other place is not a must. To have a line selected, yes that can be one.

The objects in the sample are quite compact or not ? Just one line for creation. What you mean by complexity are simply the requirements.

I added some print, nothing else changed, launched the EA, click ONE TIME to the button :

launch

2018.11.28 18:24:41.460    Expert Forum\hline_not_deleted EURUSD,M30: loaded successfully
2018.11.28 18:24:44.222    hline_not_deleted EURUSD,M30: myhline::myhline: OBJECT OBJ_HLINE OBJ_HLINE_1_1543454679 created
2018.11.28 18:24:44.222    hline_not_deleted EURUSD,M30: mybutton::mybutton: OBJECT OBJ_BUTTON OBJ_BUTTON_2_1543454679 created
2018.11.28 18:24:44.222    hline_not_deleted EURUSD,M30: my_button_line::my_button_line: ChartRedraw()
2018.11.28 18:24:44.222    hline_not_deleted EURUSD,M30: initialized

1 click

2018.11.28 18:24:47.517    hline_not_deleted EURUSD,M30: myhline::~myhline: Object OBJ_HLINE_1_1543454679 deletion error. code=4207
2018.11.28 18:24:47.517    hline_not_deleted EURUSD,M30: mybutton::~mybutton: Object OBJ_BUTTON_2_1543454679 deleted
2018.11.28 18:24:47.517    hline_not_deleted EURUSD,M30: my_button_line::cleanup: ChartRedraw()
2018.11.28 18:24:47.579    hline_not_deleted EURUSD,M30: myhline::myhline: OBJECT OBJ_HLINE OBJ_HLINE_3_1543454684 created
2018.11.28 18:24:47.579    hline_not_deleted EURUSD,M30: mybutton::mybutton: OBJECT OBJ_BUTTON OBJ_BUTTON_4_1543454684 created
2018.11.28 18:24:47.579    hline_not_deleted EURUSD,M30: my_button_line::my_button_line: ChartRedraw()
2018.11.28 18:24:47.602    hline_not_deleted EURUSD,M30: my_button_line::OnChartEvent: CLICK EVENT on OBJ_BUTTON_4_1543454684
2018.11.28 18:24:47.602    hline_not_deleted EURUSD,M30: mybutton::OnChartEvent: CLICK EVENT on OBJ_BUTTON_4_1543454684
2018.11.28 18:24:47.648    hline_not_deleted EURUSD,M30: myhline::~myhline: Object OBJ_HLINE_3_1543454684 deleted
2018.11.28 18:24:47.648    hline_not_deleted EURUSD,M30: mybutton::~mybutton: Object OBJ_BUTTON_4_1543454684 deleted
2018.11.28 18:24:47.648    hline_not_deleted EURUSD,M30: my_button_line::cleanup: ChartRedraw()
2018.11.28 18:24:47.702    hline_not_deleted EURUSD,M30: myhline::myhline: OBJECT OBJ_HLINE OBJ_HLINE_5_1543454684 created
2018.11.28 18:24:47.702    hline_not_deleted EURUSD,M30: mybutton::mybutton: OBJECT OBJ_BUTTON OBJ_BUTTON_6_1543454684 created
2018.11.28 18:24:47.702    hline_not_deleted EURUSD,M30: my_button_line::my_button_line: ChartRedraw()

1 click on a button results in :

  • deletion error, MT4 bug.
  • 4 calls to ChartRedraw().
  • 2 deletions/creations of a button.
  • creation/deletion of a line to recreate a new one again.
So it uselessly complex and inefficient code.

Sorry, this is the wrong point to critisize. The much more very strange point is the behaviour: Deletion of another object fails, depending on a certain button size. This smells for some very strange side effects.

Yes there is an MT4 bug. What are you expecting from me ? I can't fix MT4 bug(s).

So I pointed you in the direction to check and improve your code, if you don't want or like my advice, you will not have it anymore.
 

The code is just for demonstration.


When you click on the button, the button and the line is deleted.

This is like it should work in the real app. PLease tell me, where in my code i can safe lines. 


For the purpose of demonstration that the line sometimes is not deleted, line and button get recreated.

Just for being able to startover for next test.


There is a fat problem in mql4 and i like MetaQuotes to solve it.

You say my code is too complex. That's wrong. Simply even with your debug list there. Every step is required and some of them simply normal and mandatory when

you want to do what i want to do, like this line:

2018.11.28 18:24:47.602    hline_not_deleted EURUSD,M30: my_button_line::OnChartEvent: CLICK EVENT on OBJ_BUTTON_4_1543454684

is something you cant avoid. A click on an object, is a click on an object.


Also, i think you recorded 2 clicks, not one.

Please try to understand the problem before misleading possible (support) readers.

 
chinaski:

The code is just for demonstration.


When you click on the button, the button and the line is deleted.

This is like it should work in the real app. PLease tell me, where in my code i can safe lines. 


For the purpose of demonstration that the line sometimes is not deleted, line and button get recreated.

Just for being able to startover for next test.


There is a fat problem in mql4 and i like MetaQuotes to solve it.

You say my code is too complex. That's wrong. Simply even with your debug list there. Every step is required and some of them simply normal and mandatory when

you want to do what i want to do, like this line:

is something you cant avoid. A click on an object, is a click on an object.


Also, i think you recorded 2 clicks, not one.

Please try to understand the problem before misleading possible (support) readers.

I recorded 1 tick.

You don't want to listen and even question my words. That's the last time I am answering you. Good luck.

EDIT: I attached code I used. Everyone can check by himself without efforts.
Files:
 

When i comment deletion and creation as a result of a click on button, i get this:

	01:19:19.868	bitch USDJPY,M1: Object OBJ_HLINE_1_1543454355 deletion error. code=4207
0	01:19:19.868	bitch USDJPY,M1: Object <OBJ_BUTTON_2_1543454355> deleted
0	01:19:19.940	bitch USDJPY,M1: Object OBJ_HLINE_3_1543454360 created
0	01:19:19.940	bitch USDJPY,M1: Object OBJ_BUTTON_4_1543454360 created

When i click on the button. How to reduce this ?

I want line and button to get deleted and recreated. 

So 2 creations, 2 deletions.


if you remove the recreation, you won't remove the problem. Please check it out: Change the original timer function to:

void OnTimer()
{
   if(_my_button_line != NULL)
   {
      if(_my_button_line.valid() == true)
      {
         if(delete_me == true)
         {
            delete _my_button_line;//_my_button_line.cleanup();
            _my_button_line=NULL;
         }
      }
   }
   else
   {
      //_my_button_line=new my_button_line;
      //delete_me=false;
   }
}


In summary: You pointed me to rework my code but my code is NOT the problem.


The problem is in the MetaTrader client and i want this to be clear because my hope is, it is then fixed quickly.

But when poeple make comments like "your code is the problem" then this is not really encouraging.

 
And if you think its better to feel offended is say "so what".
 

The problem could be many things, I suspect that you might be using your EA in conjunction with a template. Templates cause issues because they will redraw the objects and create collisions with EA logic. Also, it's better to use the stdlib wrapper, which have been tested and handle all of the setup and tear-down for you. Please consider this example. 

#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <chartobjects/chartobjectslines.mqh>
//+------------------------------------------------------------------+
class MyLine : public CChartObjectHLine
{
protected:
   static int     s_instances;
   int            m_instance;
   bool           m_state;
   double         m_pip;
public:
                  MyLine(void);
   virtual bool   create(double start); 
   virtual void   chart_event(const int    id,
                              const long   &lparam,
                              const double &dparam,
                              const string &sparam);
   virtual bool   state(bool);
   virtual bool   state(void) const;
};
//+------------------------------------------------------------------+
int   MyLine::s_instances = 0;
      MyLine::MyLine():m_instance(++s_instances)
{
   
}
bool  MyLine::create(double start) 
{
   string name_prefix = "__myline";
   if(m_instance == 1) 
      ObjectsDeleteAll(this.ChartId(), name_prefix);
   m_pip = _Point;
   if(_Digits == 3 || _Digits == 5)
      m_pip *= 10;
   string name = name_prefix + string(m_instance);
   return (
      this.Create(0, name, 0, start) 
      && this.Width(3) 
      && this.Tooltip("Line Example")
      && this.state(false)
   );
}

void  MyLine::chart_event(const int id,
                          const long &lparam,
                          const double &dparam,
                          const string &sparam)
{
   if(id == CHARTEVENT_OBJECT_CLICK 
      && sparam == this.Name()
   ){
      this.state(!this.state());
      return;
   }
   if(id == CHARTEVENT_MOUSE_MOVE
      && this.state()
   ){
      int sub_window = this.Window();
      datetime time;
      double price;
      bool get_xy =  ChartXYToTimePrice( 
         this.ChartId(),     // Chart ID 
         (int)lparam,            // The X coordinate on the chart 
         (int)dparam,            // The Y coordinate on the chart 
         sub_window,   // The number of the subwindow 
         time,         // Time on the chart 
         price         // Price on the chart 
      );
      if(get_xy) {
         this.Price(0, price);   
         this.Tooltip(StringFormat(
            "Line Example\nPips = %.1f",
            fabs(Bid - price) / m_pip
         ));  
      }
   } 
}

bool  MyLine::state(void) const 
{ 
   return m_state;
}
bool  MyLine::state(bool new_state) 
{ 
   m_state = new_state;
   if(new_state) {
      return (
         this.Style(STYLE_DOT)
         && this.Color(clrYellow)
         && this.Width(1)
      );
   }else{
      return (
         this.Style(STYLE_SOLID)
         && this.Color(clrRed)
         && this.Width(3)
      );
   }
}

//+------------------------------------------------------------------+
MyLine g_line;
//+------------------------------------------------------------------+
int OnInit()
{
   ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true);
   if(!g_line.create(Ask))
      return INIT_FAILED;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnTick(){}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   g_line.chart_event(id, lparam, dparam, sparam);
   
}
//+------------------------------------------------------------------+
Reason: