Problem Description
Solution
To remove specific graphical objects like lines and arrows from your MetaTrader 4 chart, you need a script that iterates through all objects on the chart, checks their type, and deletes them if they match your criteria.
Here is the complete MQL4 code for a script that performs this task.
MQL4 Script: Remove Lines and Arrows
This script includes input parameters so you can choose exactly which types of objects to delete when you run it.
//+------------------------------------------------------------------+
//| CleanChartObjects.mq4 |
//| Copyright 2023, Quantitative AI |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative AI"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
//--- Input Parameters to filter what to delete
input bool InpDeleteTrendLines = true; // Delete Trendlines
input bool InpDeleteHorizLines = true; // Delete Horizontal Lines
input bool InpDeleteVertLines = true; // Delete Vertical Lines
input bool InpDeleteArrows = true; // Delete Arrows and Signs
input bool InpDeleteAll = false; // DANGER: Delete EVERYTHING (Overrides above)
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// Get the total number of objects on the chart
int totalObjects = ObjectsTotal();
int deletedCount = 0;
// We must loop BACKWARDS from the last object to the first.
// If we loop forwards (0 to total) and delete object 0,
// object 1 becomes the new object 0, causing us to skip objects.
for(int i = totalObjects - 1; i >= 0; i--)
{
string objName = ObjectName(i);
int objType = ObjectType(objName);
bool toDelete = false;
// 1. Check if "Delete All" is selected
if(InpDeleteAll)
{
toDelete = true;
}
// 2. Otherwise, check specific types
else
{
// Check Trendlines (Standard and By Angle)
if(InpDeleteTrendLines)
{
if(objType == OBJ_TREND || objType == OBJ_TRENDBYANGLE)
toDelete = true;
}
// Check Horizontal Lines
if(InpDeleteHorizLines)
{
if(objType == OBJ_HLINE)
toDelete = true;
}
// Check Vertical Lines
if(InpDeleteVertLines)
{
if(objType == OBJ_VLINE)
toDelete = true;
}
// Check Arrows (Standard arrows, thumbs up/down, checks, stops, etc.)
if(InpDeleteArrows)
{
if(objType == OBJ_ARROW ||
objType == OBJ_ARROW_THUMB_UP ||
objType == OBJ_ARROW_THUMB_DOWN ||
objType == OBJ_ARROW_UP ||
objType == OBJ_ARROW_DOWN ||
objType == OBJ_ARROW_STOP ||
objType == OBJ_ARROW_CHECK ||
objType == OBJ_ARROW_LEFT_PRICE ||
objType == OBJ_ARROW_RIGHT_PRICE)
{
toDelete = true;
}
}
}
// 3. Execute Deletion
if(toDelete)
{
if(ObjectDelete(objName))
{
deletedCount++;
}
else
{
Print("Failed to delete object: ", objName, " Error: ", GetLastError());
}
}
}
// Force the chart to redraw to reflect changes immediately
WindowRedraw();
Print("Cleanup complete. Removed ", deletedCount, " objects.");
}
//+------------------------------------------------------------------+
How to Install and Run
- Open MetaEditor: In MT4, press
F4or click the "MetaEditor" button. - Create New File: Click
New->Script-> Name itCleanChartObjects-> ClickFinish. - Paste Code: Delete everything in the new window and paste the code provided above.
- Compile: Press
F7or click the "Compile" button. Check the "Errors" tab at the bottom to ensure there are 0 errors. - Run: Go back to the main MT4 window. In the Navigator panel (Ctrl+N), expand the Scripts folder. Drag and drop
CleanChartObjectsonto your chart. - Configure: A window will pop up allowing you to select
trueorfalsefor the specific items you want to remove.
Key Logic Explained
#property script_show_inputs: This directive forces the input window to appear when you drag the script onto the chart. Without this, the script would run immediately with default settings.- Reverse Loop (
i--): When deleting items from a list (or objects from a chart), you must always iterate backwards. If you delete Object #0, the former Object #1 shifts down to become the new Object #0. A forward loop would skip this shifted object. ObjectType(): This function identifies what kind of object we are looking at (e.g.,OBJ_HLINEfor horizontal lines). This ensures we don't accidentally delete buttons, labels, or other objects created by Expert Advisors unlessInpDeleteAllis selected.WindowRedraw(): After deleting objects programmatically, the visual chart might not update instantly. This function forces a refresh so the lines disappear immediately.
Q&A: MQL4 Object Management
Q: Will this script delete lines drawn by my custom indicators?
A: It depends. If the indicator draws lines using "Object" functions (like ObjectCreate), this script will delete them if they match the selected type (e.g., Trendlines). If the indicator draws lines using "Indicator Buffers" (standard indicator lines), this script cannot touch them.
Q: Can I undo the deletion if I run this script by mistake?
A: No. In MetaTrader 4, objects deleted via script or code are permanently removed. There is no "Undo" function for script actions.
Q: How do I modify the script to delete Fibonacci tools as well?
A: You would add a check for OBJ_FIBO. Inside the loop, add:
if(objType == OBJ_FIBO) toDelete = true;
Q: Why does the script not delete rectangles or text labels?
A: The script is specifically filtered to target lines and arrows as requested. To delete text, you would need to check for OBJ_TEXT or OBJ_LABEL. To delete rectangles, check for OBJ_RECTANGLE. Alternatively, setting the input InpDeleteAll to true will remove absolutely every object on the chart.