//+------------------------------------------------------------------+ //| AutoDel.mqh | //| Copyright 2026, Niquel Mendoza. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, Niquel Mendoza." #property link "https://www.mql5.com" #property strict #ifndef MQLARTICLES_UTILS_OBJECTS_AUTODEL_MQH #define MQLARTICLES_UTILS_OBJECTS_AUTODEL_MQH //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #include "Native\\Imp.mqh" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ namespace TSN { //+------------------------------------------------------------------+ //| Structure that stores a group of graphic objects to be deleted | //| at a specific time | //+------------------------------------------------------------------+ struct GraphicObjEliminations { // Array with the names of the objects that will be deleted when the current time is greater than "time_to_delete" string m_object_names[]; // Time at which the objects will be deleted datetime time_to_delete; }; //+------------------------------------------------------------------+ //| Class to manage the scheduled deletion of graphic objects | //| from a specified chart | //+------------------------------------------------------------------+ class CGraphicObjEliminations { private: //--- Internal variables GraphicObjEliminations m_eliminations[]; // Array of deletions int m_indexes_to_remove[]; // Indexes to remove from the deletions array int m_last_element_index; // Variable that stores the "position" of the last added element int m_current_object_index; // Variable that stores the "position" of the last string added to the last added element //--- Parameters long m_chart_id; // ID of the chart where the objects to be deleted should be found int m_reserve_delete_array; // Reserve for the deletions array 'm_indexes_to_remove' int m_reserve_main_array; // Reserve for the main array 'm_eliminations' int m_reserve_string_array; // Reserve for the string array of the last element added to the m_eliminations array public: CGraphicObjEliminations(void); //--- Initialization void Initilialize(long chart_id, int reserve_arr_delete, int reserve_main_arr, int reserve_str_arr); //--- Add a new set of names to delete void InitAddElement(datetime time_to_delete); inline void AddElement(const string& object_name); //--- General functions inline void DeleteAll(); inline void HideAll(); inline void ShowAll(); //--- General setters and getters // Chart id long ChartId() const { return m_chart_id; } void ChartId(long new_value) { m_chart_id = new_value; } // Reserve for the string "array" of the last element added to the deletions array int ElementStrArrayReserve() const { return m_reserve_string_array; } void ElementStrArrayReserve(int new_value) { m_reserve_string_array = new_value; } //--- Main function to check if graphic objects should be deleted void OnNewBar(datetime curr_time); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CGraphicObjEliminations::CGraphicObjEliminations(void) : m_reserve_delete_array(0), m_reserve_main_array(0), m_reserve_string_array(0), m_chart_id(0), m_last_element_index(0), m_current_object_index(0) { } //+------------------------------------------------------------------+ //| Initialization function | //+------------------------------------------------------------------+ void CGraphicObjEliminations::Initilialize(long chart_id, int reserve_arr_delete, int reserve_main_arr, int reserve_str_arr) { m_chart_id = chart_id; m_reserve_delete_array = reserve_arr_delete; m_reserve_main_array = reserve_main_arr; m_reserve_string_array = reserve_str_arr; } //+------------------------------------------------------------------+ //| Function to add a new group of graphic objects | //| that will be deleted at a specified time. | //+------------------------------------------------------------------+ void CGraphicObjEliminations::InitAddElement(datetime time_to_delete) { m_last_element_index = ArraySize(m_eliminations); m_current_object_index = 0; ArrayResize(m_eliminations, m_last_element_index + 1, m_reserve_main_array); m_eliminations[m_last_element_index].m_object_names.Reserve(m_reserve_string_array); m_eliminations[m_last_element_index].time_to_delete = time_to_delete; } //+----------------------------------------------------------------------+ //| Function to add a new object name to the last added set-element. | //+----------------------------------------------------------------------+ void CGraphicObjEliminations::AddElement(const string& object_name) { ArrayResize(m_eliminations[m_last_element_index].m_object_names, m_current_object_index + 1, m_reserve_string_array); m_eliminations[m_last_element_index].m_object_names[m_current_object_index] = object_name; m_current_object_index++; } //+------------------------------------------------------------------+ //| Deletes all stored graphic objects, and also | //| resizes the main array to 0 | //+------------------------------------------------------------------+ inline void CGraphicObjEliminations::DeleteAll(void) { for(int i = 0; i < ArraySize(m_eliminations); i++) for(int j = 0; j < ArraySize(m_eliminations[i].m_object_names); j++) ObjectDelete(m_chart_id, m_eliminations[i].m_object_names[j]); ArrayResize(m_eliminations, 0, m_reserve_main_array); } //+------------------------------------------------------------------+ //| Hides all stored graphic objects | //+------------------------------------------------------------------+ inline void CGraphicObjEliminations::HideAll(void) { for(int i = 0; i < ArraySize(m_eliminations); i++) for(int j = 0; j < ArraySize(m_eliminations[i].m_object_names); j++) HIDE_OBJECT(m_eliminations[i].m_object_names[j], m_chart_id); } //+------------------------------------------------------------------+ //| Shows all stored graphic objects | //+------------------------------------------------------------------+ inline void CGraphicObjEliminations::ShowAll(void) { for(int i = 0; i < ArraySize(m_eliminations); i++) for(int j = 0; j < ArraySize(m_eliminations[i].m_object_names); j++) SHOW_OBJECT(m_eliminations[i].m_object_names[j], m_chart_id); } //+----------------------------------------------------------------------+ //| Main function in which we check which elements of the deletions | //| array are ready to be deleted both from the array and graphically | //| from the chart with ObjectDelete | //+----------------------------------------------------------------------+ void CGraphicObjEliminations::OnNewBar(datetime curr_time) { //--- Initial const int total_eliminations = ArraySize(m_eliminations); ArrayResize(m_indexes_to_remove, 0, m_reserve_delete_array); int remove_index_count = 0; //--- Review for(int i = 0; i < total_eliminations; i++) // From oldest to newest { if(m_eliminations[i].time_to_delete < curr_time) { //--- Delete for(int j = 0; j < ArraySize(m_eliminations[i].m_object_names); j++) ObjectDelete(m_chart_id, m_eliminations[i].m_object_names[j]); //--- Mark ArrayResize(m_indexes_to_remove, remove_index_count + 1, m_reserve_delete_array); m_indexes_to_remove[remove_index_count] = i; remove_index_count++; } } //--- Delete RemoveMultipleIndexes(m_eliminations, m_indexes_to_remove, m_reserve_main_array); } //--- } #endif // MQLARTICLES_UTILS_OBJECTS_AUTODEL_MQH