How to know when any object is deleted from chart? [Solved]

 

I have a OBJ_FIBO on chart which is drawn

If i get its price value using ObjGetInteger, it returns price but when i delete the object, it does not show any value such as 0

if i use ObjectFind its also not returning any thing

so how can i know if the OBJ_FIBO is deleted from chart?

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Arpit T:

I have a OBJ_FIBO on chart which is drawn

If i get its price value using ObjGetInteger, it returns price but when i delete the object, it does not show any value such as 0

if i use ObjectFind its also not returning any thing

so how can i know if the OBJ_FIBO is deleted from chart?


ObjectDelete() returns true if the object is deleted so you could check for that at the time of deletion.

ObjectFind() - the documentation says "If the object is not found, the function returns a negative number." - is that what you are getting?


If you want to do another check, your could try to read a property, for example ObjectGetString()  - have a look at https://www.mql5.com/en/docs/objects/objectgetstring

Documentation on MQL5: Object Functions / ObjectGetString
Documentation on MQL5: Object Functions / ObjectGetString
  • www.mql5.com
ObjectGetString - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Arpit T:

I have a OBJ_FIBO on chart which is drawn

If i get its price value using ObjGetInteger, it returns price but when i delete the object, it does not show any value such as 0

if i use ObjectFind its also not returning any thing

so how can i know if the OBJ_FIBO is deleted from chart?

Check documentation. ObjectFind returns the index of the subwindow where it is found, if the return is less than 0 then there is no object with such name.

Documentation on MQL5: Object Functions / ObjectFind
Documentation on MQL5: Object Functions / ObjectFind
  • www.mql5.com
ObjectFind - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
There is an Event you can get triggered, if it's not disabled by the deletion function.



 
R4tna C #:


ObjectDelete() returns true if the object is deleted so you could check for that at the time of deletion.

ObjectFind() - the documentation says "If the object is not found, the function returns a negative number." - is that what you are getting?


If you want to do another check, your could try to read a property, for example ObjectGetString()  - have a look at https://www.mql5.com/en/docs/objects/objectgetstring

the problem is when object is deleted it does not return anything, such as negative number.

#property copyright "Copyright 2022"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
    for(int j=0; j<ObjectsTotal(0,-1,-1); j++) {
      string  n = ObjectName(0,j);

      if(
         ObjectGetInteger(0, n, OBJPROP_TYPE) == OBJ_FIBO
      ) {
     
         Print(n);
         
          if(ObjectFind(0,n)==-1)
          Print("deleted",n);

      }
  
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Arpit T #:

the problem is when object is deleted it does not return anything, such as negative number.

Looking at this code, if your first if condition evaluates to be true [ObjectGetInteger...] it suggests the object exists, so the 2nd if statement [ObjectFind...] can never return -1.

As there is no else clause, one can never know the other outcome, so I suggest you put one in.

Also I note there is no ObjectDelete() - is that intentional, or is it called elsewhere?

 
Arpit T #:

the problem is when object is deleted it does not return anything, such as negative number.

Why you need know if the object is deleted? Do you need know that immediately after it is deleted? Why do you need check all of the objects? If the object has a proper name you don't need check all of the objects.

 
R4tna C #:

Looking at this code, if your first if condition evaluates to be true [ObjectGetInteger...] it suggests the object exists, so the 2nd if statement [ObjectFind...] can never return -1.

As there is no else clause, one can never know the other outcome, so I suggest you put one in.

Also I note there is no ObjectDelete() - is that intentional, or is it called elsewhere?

i am adding and deleting OBJ_FIBO Manually

 
Samuel Manoel De Souza #:

Why you need know if the object is deleted? Do you need know that immediately after it is deleted? Why do you need check all of the objects? If the object has a proper name you don't need check all of the objects.

Yes i need to know immediately when i delete OBJ_FIBO from chart manually. Since i am using it for manually drawn fibonacci retracement it may have default name so need to check

 
Dominik Christian Egert #:
There is an Event you can get triggered, if it's not disabled by the deletion function.



any example of  CHART_EVENT_OBJECT_DELETE will really help if possible, Thanks

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Examples of Working with the Chart
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Examples of Working with the Chart
  • www.mql5.com
Examples of Working with the Chart - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Also, use the Styler in your Meta Editor before post the code. Just need click a button and that will organize your code.
Styler - Developing programs - MetaEditor Help
Styler - Developing programs - MetaEditor Help
  • www.metatrader5.com
The styler quickly brings a source code design in line with the recommended standard. This makes the code look professional and easy to read. A...