Problem Description
Solution
To find and delete a specific indicator from a chart in MQL4, you must identify the indicator by its Short Name (the name displayed in the Data Window or the top-left corner of the indicator window), not just its file name.
The process involves iterating through all chart windows (main window and subwindows), checking the list of attached indicators, and deleting the one that matches the target name.
MQL4 Script to Delete an Indicator
Here is a complete script. You can run this on any chart, input the short name of the indicator, and it will remove it.
//+------------------------------------------------------------------+
//| DeleteSpecificIndicator.mq4 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
//--- Input parameters
input string InpIndicatorName = "MACD(12,26,9)"; // Exact Short Name of the indicator to delete
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// Call the function to delete the indicator on the current chart (0)
if(IndicatorDeleteByName(0, InpIndicatorName))
{
Print("Success: Indicator '", InpIndicatorName, "' was deleted.");
ChartRedraw(0); // Force the chart to update visually
}
else
{
Print("Failure: Indicator '", InpIndicatorName, "' was not found or could not be deleted.");
Print("Note: Ensure you are using the 'Short Name' (e.g., 'Moving Average(14,0,0)'), not just the file name.");
}
}
//+------------------------------------------------------------------+
//| Function to find and delete an indicator by its short name |
//+------------------------------------------------------------------+
bool IndicatorDeleteByName(long chartID, string targetShortName)
{
// Get the total number of windows (0 is main, 1+ are subwindows)
int windowsTotal = (int)ChartGetInteger(chartID, CHART_WINDOWS_TOTAL);
// Iterate through every window on the chart
for(int w = 0; w < windowsTotal; w++)
{
// Get the total number of indicators in this specific window
int indicatorsTotal = ChartIndicatorsTotal(chartID, w);
// Iterate through all indicators in this window
for(int i = 0; i < indicatorsTotal; i++)
{
// Get the Short Name of the indicator at index 'i'
string currentName = ChartIndicatorName(chartID, w, i);
// Check if it matches the target
if(currentName == targetShortName)
{
// Attempt to delete
if(ChartIndicatorDelete(chartID, w, targetShortName))
{
return(true); // Return true immediately after deletion
}
else
{
Print("Error deleting indicator. Error code: ", GetLastError());
return(false);
}
}
}
}
// If we loop through everything and don't find it
return(false);
}
Key Functions Used
-
ChartGetInteger(chart_id, CHART_WINDOWS_TOTAL):
Retrieves the total number of windows on the chart. Window0is the main price chart. Windows1and higher are subwindows (like RSI or MACD). -
ChartIndicatorsTotal(chart_id, sub_window):
Returns the number of indicators attached to a specific window index. -
ChartIndicatorName(chart_id, sub_window, index):
Returns the Short Name of the indicator.- Important: This is usually formatted with parameters. For example, the standard Moving Average is not just "Moving Average", it is often "Moving Average(14,0,0)". You must match this string exactly.
-
ChartIndicatorDelete(chart_id, sub_window, indicator_shortname):
Removes the indicator with the specified short name from the specified window. -
ChartRedraw(chart_id):
Forces the terminal to repaint the chart immediately so the removal is visible to the user instantly.
How to Find the Correct "Short Name"
If the script fails to delete the indicator, you are likely using the wrong name string. To find the exact name required by the code:
- Open the Data Window in MT4 (Ctrl+D).
- Hover your mouse over the indicator line on the chart.
- Look at the name displayed in the Data Window or the top-left corner of the indicator's subwindow.
- That exact text (including parentheses and numbers) is the
InpIndicatorName.
Q&A
Q: Can I delete all indicators from a chart at once?
A: Yes. You would iterate backwards through the windows and indicators (from total-1 down to 0) and call ChartIndicatorDelete on every name found.
Q: Does this work for custom indicators?
A: Yes, provided the custom indicator sets its IndicatorShortName correctly within its code. If the developer did not set a short name, MT4 usually defaults to the file name.
Q: Why does ChartIndicatorDelete require the window number?
A: An indicator with the same name can exist in multiple windows (e.g., a Moving Average on the main chart and another Moving Average applied to an RSI in a subwindow). The window index specifies exactly which instance to remove.