Graphic Objects being removed after OnDeinit

 

He everybody,

I have a small function to draws vertical line on Chart:

bool DrawLineOnChar(const datetime time, color clr, ENUM_LINE_STYLE lineStyle)
    {
     CChartObjectVLine *line;
     if(!CheckPointer(line = new CChartObjectVLine) ||
        !line.Create(ChartID(), "line " + TimeToString(time), 0, time))
         {
          Print("error creating arrow: ", (string)GetLastError());
          return(false);
         }
     line.Color(clr);
     line.SetInteger(OBJPROP_HIDDEN, false);
     line.SetInteger(OBJPROP_BGCOLOR, clrLightGray);
     line.Style(lineStyle);
     line.SetInteger(OBJPROP_WIDTH, 1);
     mArrayObj.Add(line);
     ChartRedraw();
     return(true);
    }

The Signal class (let's take SignalAC.mqh in the Expert\Signal folder as an example) uses this function to visualize strategies during development

As soon the test is over (OnDeinit() invoked), all lines will be removed from the chart, so I must have a break point in my OnDeinit() in order to keep the lines and check the results before they are erased. 

Can this behavior be prevented and the objects stay on the backtest chart result after the test is complete? 

Thank you in advance for your help 🙏


Cheers,

Zarik

Documentation on MQL5: Working with DirectX / DXDraw
Documentation on MQL5: Working with DirectX / DXDraw
  • www.mql5.com
DXDraw - Working with DirectX - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Okay and what does your OnDeinit look like?
 
Tobias Johannes Zimmer #:
Okay and what does your OnDeinit look like?


Het Tobias,

It's like a normal Expert adviser generated with Wizard:

void OnDeinit(const int reason)
  {
   ExtExpert.Deinit();
  }

which proceeds as follow in Expert.mqh:

//+------------------------------------------------------------------+
//| Deinitialization expert                                          |
//+------------------------------------------------------------------+
void CExpert::Deinit(void)
  {
//--- delete trade class
   DeinitTrade();
//--- delete signal class
   DeinitSignal();
//--- delete trailing class
   DeinitTrailing();
//--- delete money class
   DeinitMoney();
//--- delete indicators collection
   DeinitIndicators();
  }

and DeinitSignal() looks like:

//+------------------------------------------------------------------+
//| Deinitialization signal object                                   |
//+------------------------------------------------------------------+
void CExpert::DeinitSignal(void)
  {
   if(m_signal!=NULL)
     {
      delete m_signal;
      m_signal=NULL;
     }
  }

My guess is that this is caused by the object being freed from memory with DeinitSignal(), so the graphics will no longer be referenced. Correct me if I am wrong please 🙏


EDIT: I commented out the Deinit functions for Signal, Inidactor, and Except but the issue still persist! 

 
Zarik #:


Het Tobias,

It's like a normal Expert adviser generated with Wizard:

which proceeds as follow in Expert.mqh:

and DeinitSignal() looks like:

My guess is that this is caused by the object being freed from memory with DeinitSignal(), so the graphics will no longer be referenced. Correct me if I am wrong please 🙏


EDIT: I commented out the Deinit functions for Signal, Inidactor, and Except but the issue still persist! 

Your DrawLineOnChar() seems quite elaborate - I use ObjectCreate() and in your case the command would look something like this :

ObjectCreate(ChartID(), "line " + TimeToString(time), OBJ_VLINE, 0, time)) //Please read the documentation and adjust as needed

Consequently from OnDeinit() I call ObjectsDeleteAll() with a specific prefix (which I define when creating the objects), to delete the set of objects I no longer need  - the others remain after the EA exits.

Give that a try

 
R4tna C #:

Your DrawLineOnChar() seems quite elaborate - I use ObjectCreate() and in your case the command would look something like this :

Consequently from OnDeinit() I call ObjectsDeleteAll() with a specific prefix (which I define when creating the objects), to delete the set of objects I no longer need  - the others remain after the EA exits.

Give that a try

Hey,

Thank you so much It is working! 🙏🍻 but somehow ObjectDeleteAll() is not identified by the compiler! (even though it is in the documentation). I switched that with ObjectDelete().


Cheers,

Zarik

 
Zarik #: ... but somehow ObjectDeleteAll() is not identified by the compiler! (even though it is in the documentation). I switched that with ObjectDelete().
That is because you are missing an "s" — ObjectsDeleteAll not  ObjectDeleteAll
 
Fernando Carreiro #:
That is because you are missing an "s" — ObjectsDeleteAll not  ObjectDeleteAll

I need to see an eye doctor! 🤷‍♂️🤦‍♂️ Thank you! 🙏