How do I delete comments and other things on the chart?

 

Hello everyone, I hope you're well.


I'm still a beginner and I'm finding that when I remove an expert advisor then I still see the comments, so I did a quick search and found this solution, so I added an empty comment in the OnDeinit function:

void OnDeinit(const int reason)
{
    Comment("");
}


but is there another solution to remove any object from the graph just with the mouse without going through the code please?


Best Reguards,
ZeroCafeine😊

clear old Comment(); function appear on chart?
clear old Comment(); function appear on chart?
  • 2006.09.11
  • www.mql5.com
is there any way to clear old Comment(); function that appear on the chart...
 

this is how can do it:


void OnDeinit(const int reason){
    ObjectsDeleteAll(0); // Delete all objects on the chart
}


(taking it that your Chart ID is 0)

 
  1. ZeroCafeine: but is there another solution to remove any object from the graph just with the mouse without going through the code please?
    1. Comment is not an object.
    2. Click to select an object and press delete. If it was created by running code, it may or may not be recreated.

  2. phade #: this is how can do it:)

    Never call that. Code should only delete those objects it creates. When would the Human want the removal of an indicator to remove drawings the Human made? Never.

 
William Roeder #:
    1. Comment is not an object.
    2. Click to select an object and press delete. If it was created by running code, it may or may not be recreated.

  1. Never call that. Code should only delete those objects it creates. When would the Human want the removal of an indicator to remove drawings the Human made? Never.

Ahh very good point. Me personally though - anytime I draw things on the chart, I do so on a separate chart with no indicators (or even on a separate platform). I never really leave drawings there either...I usually draw, maybe grab an image, and then remove my drawings there and then

 
William Roeder #:
Comment is not an object.

hi  William Roeder

First of all, thank you for your precise answer. Am I to understand that my solution for deleting a comment is the right one? Or am I missing something again 😊 ?

phade #:
Never call that. Code should only delete those objects it creates. When would the Human want the removal of an indicator to remove drawings the Human made? Never.
and tks for this tips, That goes without saying, but as a beginner I wouldn't have thought of it straight away.
William Roeder
William Roeder
  • www.mql5.com
Trader's profile
 

@ZeroCafeine here is a solution that should work for you:

// Loop through all objects on the chart
int totalObjects = ObjectsTotal();
for (int i = totalObjects - 1; i >= 0; i--)
{
    string objectName = ObjectName(i);
    
    // Check if the object name contains the indicator's name or any other identifier
    if (StringFind(objectName, "MyIndicatorName") != -1)
    {
        // Delete the object
        ObjectDelete(objectName);
    }
}
 

tks you  phade 😊

1) Let's try to structure our answer. The subject is how to delete a comment 😊, not an object 😉, but we'll try to do both properly,

Can you confirm whether or not I should put this code in the OnDeinit() function? If the answer is yes, then I've already tried and I'm getting errors with the ObjectsTotal() function, I also tried the following settings for the main graph, but they don't work :

int totalObjects = ObjectsTotal(0, 0, -1);


2) Can anyone confirm whether or not I'm doing the right thing by deleting a comment?

ZeroCafeine:
void OnDeinit(const int reason) {     Comment(""); }
phade
phade
  • 2023.07.07
  • www.mql5.com
Trader's profile
 
ZeroCafeine #:

tks you  phade 😊

1) Let's try to structure our answer. The subject is how to delete a comment 😊, not an object 😉, but we'll try to do both properly,

Can you confirm whether or not I should put this code in the OnDeinit() function? If the answer is yes, then I've already tried and I'm getting errors with the ObjectsTotal() function, I also tried the following settings for the main graph, but they don't work :


2) Can anyone confirm whether or not I'm doing the right thing by deleting a comment?

this will add empty comments when the indicator is unloaded


Do this instead:

void OnDeinit(const int reason){

    // Clear comments from the chart
    ChartSetString(0, CHART_COMMENT, "");
}

https://www.mql5.com/en/docs/chart_operations/chartsetstring


and this is how you can carefully delete the other things (a working example):

// Loop through all objects on the chart
int totalObjects = ObjectsTotal(0);
for (int i = totalObjects - 1; i >= 0; i--)
{
    string objectName = ObjectName(0, i);
    
    // Check if the object name contains the indicator's name or any other identifier
    if (StringFind(objectName, "UpArrow") != -1)
    {
        // Delete the object
        ObjectDelete(0, objectName);
    }
    else if (StringFind(objectName, "DownArrow") != -1)
    {
        // Delete the object
        ObjectDelete(0, objectName);
    }
    else if (StringFind(objectName, "TradeWarningGraphic") != -1)
    {
        // Delete the object
        ObjectDelete(0, objectName);
    }
}


so it can all go in OnDeInit:

void OnDeinit(const int reason){
   // ObjectsDeleteAll(0); // Delete all objects on the chart
   
 // Loop through all objects on the chart
   int totalObjects = ObjectsTotal(0);
   for (int i = totalObjects - 1; i >= 0; i--)
   {
       string objectName = ObjectName(0, i);
       
       // Check if the object name contains the indicator's name or any other identifier
       if (StringFind(objectName, "UpArrow") != -1)
       {
           // Delete the object
           ObjectDelete(0, objectName);
       }
       else if (StringFind(objectName, "DownArrow") != -1)
       {
           // Delete the object
           ObjectDelete(0, objectName);
       }
       else if (StringFind(objectName, "TradeWarningGraphic") != -1)
       {
           // Delete the object
           ObjectDelete(0, objectName);
       }
   }
 
   // Clear comments from the chart
   ChartSetString(0, CHART_COMMENT, "");

}
Documentation on MQL5: Chart Operations / ChartSetString
Documentation on MQL5: Chart Operations / ChartSetString
  • www.mql5.com
ChartSetString - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
phade #:

this will add empty comments when the indicator is unloaded


Do this instead:

https://www.mql5.com/en/docs/chart_operations/chartsetstring


The result is exactly the same.

 
When the result is the same, well, you can break the norms on such occasions. Whatever works is what works.
 
Thank you all for your answers 😉