Object Text Delete

 

I have written my first Indicator. It simply draws text on the chart in the upper right corner of main window. However, when I delete the Indicator the text does not delete. How can I fix this? The following is my code: 

//+------------------------------------------------------------------+
//|                                                TestIndicator.mq4 |
//|                                     Copyright 2022, Alan Northam |
//|                                             https://www.mql4.com |
//+------------------------------------------------------------------+
//--------------------------------------------------------------------

int start()                         // Special function start()
  {

   
   //Define 1M EMA
   double EMA_M1_TF2 = iMA(0,PERIOD_M1,2,0,MODE_EMA,PRICE_CLOSE,0);
   double EMA_M1_TF1 = iMA(0,PERIOD_M1,1,0,MODE_EMA,PRICE_CLOSE,0);
   
   
   ///////////////////////////////////////////////////////////////
   
   //Draw on Chart
   if(EMA_M1_TF1 > EMA_M1_TF2)
   {
   ObjectDelete(0,"ObjNameM1");
   ObjectCreate("ObjNameM1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("ObjNameM1","UP",10, "Verdana", ForestGreen);
   ObjectSet("ObjNameM1", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
   ObjectSet("ObjNameM1", OBJPROP_XDISTANCE, 30);
   ObjectSet("ObjNameM1", OBJPROP_YDISTANCE, 20); 
   }
   else
   {   
   ObjectDelete(0,"ObjNameM1");
   ObjectCreate("ObjNameM1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("ObjNameM1","DOWN",10, "Verdana", Red);
   ObjectSet("ObjNameM1", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
   ObjectSet("ObjNameM1", OBJPROP_XDISTANCE, 30);
   ObjectSet("ObjNameM1", OBJPROP_YDISTANCE, 20); 
   }
   
  return;                          // Exit the special function start()
  }
  
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  4. Alan Ray Northam: However, when I delete the Indicator the text does not delete. How can I fix this?

    Code it to do so, in the OnDeinit.

  5. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  4. Code it to do so, in the OnDeinit.

  5. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

1.  I edited my original post and used the Code button as suggested.

2.  I didn't realize there were two different places to enter posts.  I will use MT4 forum in the future.

3.  I added the OnDeinit function and now the objects are deleted from the main window when I close the Indicator.

4.  I didn't mean to double post.  I thought the first post did not get properly posted as I didn't see it in the forum after I posted it.

Question:  Does the OnDeint go at the bottom of the code after the Onint return?  Note:  This question has been answered!

 
Alan Ray Northam #: I didn't mean to double post.  I thought the first post did not get properly posted as I didn't see it in the forum after I posted it. Does the OnDeint go at the bottom of the code after the Onint return?

You can place the OnDeinit() before or after the OnInit(), where ever you like, as long as it's globally scoped (i.e. not inside another function).

It should also be placed after inputs and declarations of global variables, as it may depend on them.

 
Fernando Carreiro #:

You can place the OnDeinit() before or after the OnInit(), where ever you like, as long as it's globally scoped (i.e. not inside another function).

It should also be placed after inputs and declarations of global variables, as it may depend on them.

Fernando - Thank you for your response and for clearing up where to put the OnDeinit() function.

 
Alan Ray Northam #:

1.  I edited my original post and used the Code button as suggested.

2.  I didn't realize there were two different places to enter posts.  I will use MT4 forum in the future.

3.  I added the OnDeinit function and now the objects are deleted from the main window when I close the Indicator.

4.  I didn't mean to double post.  I thought the first post did not get properly posted as I didn't see it in the forum after I posted it.

Question:  Does the OnDeint go at the bottom of the code after the Onint return?  Note:  This question has been answered!

William Roeder #:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  4. Code it to do so, in the OnDeinit.

  5. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

When I changed the event handler from start to OnInit I got several errors so I guess I cannot just change the event handler must must then change a lot of the code.  :o(

 
Alan Ray Northam #: When I changed the event handler from start to OnInit I got several errors so I guess I cannot just change the event handler must must then change a lot of the code.  :o(

The correct event handler for the Indicator data is OnCalculate(), which is the equivalent to the old "start()".

OnInit() is only used for the initialisation of the indicator.

 
Fernando Carreiro #:

The correct event handler for the Indicator data is OnCalculate(), which is the equivalent to the old "start()".

OnInit() is only used for the initialisation of the indicator.

I changed to OnCalculate() and I do not get any errors.  However, the indicator does not run.

 
Alan Ray Northam #: I changed to OnCalculate() and I do not get any errors.  However, the indicator does not run.

Just changing the event handler is not enough. Your code has to be done according to HOW the OnCalculate needs to be used.

The handler is called on every new tick, so as you have your code in your first post, it will be deleting and creating on every tick.

Instead detect the new bar event and use the previous bar instead of the current one. Only create the object once, then update it ONLY when necessary, by checking for a new bar event.

Post the update code so that we can see what you have done.

 

This is what I came up with.  I used the Wizard to create the Indicator format and then added my code.  It works, but I don't understand why I have to have all these of the following:

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[] 

If I delete anyone of these then I get errors.

//+------------------------------------------------------------------+
//|                                                TestIndicator.mq4 |
//|                                     Copyright 2022, Alan Northam |
//|                                             https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Alan Northam"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#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[])
  {

//Define 1M EMA
   double EMA_M1_TF2 = iMA(0,PERIOD_M1,2,0,MODE_EMA,PRICE_CLOSE,0);
   double EMA_M1_TF1 = iMA(0,PERIOD_M1,1,0,MODE_EMA,PRICE_CLOSE,0);
   
   
   ///////////////////////////////////////////////////////////////
   
   //Draw on Chart
   if(EMA_M1_TF1 > EMA_M1_TF2)
   {
   ObjectDelete(0,"ObjNameM1");
   ObjectCreate("ObjNameM1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("ObjNameM1","UP",10, "Verdana", ForestGreen);
   ObjectSet("ObjNameM1", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
   ObjectSet("ObjNameM1", OBJPROP_XDISTANCE, 30);
   ObjectSet("ObjNameM1", OBJPROP_YDISTANCE, 20); 
   }
   else
   {   
   ObjectDelete(0,"ObjNameM1");
   ObjectCreate("ObjNameM1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("ObjNameM1","DOWN",10, "Verdana", Red);
   ObjectSet("ObjNameM1", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
   ObjectSet("ObjNameM1", OBJPROP_XDISTANCE, 30);
   ObjectSet("ObjNameM1", OBJPROP_YDISTANCE, 20); 
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Alan Ray Northam #: This is what I came up with.  I used the Wizard to create the Indicator format and then added my code.  It works, but I don't understand why I have to have all these of the following:

If I delete anyone of these then I get errors.

You can't delete arguments/parameters from a defined function. Just as there are rules to the English language, there are rules to programming languages too.

You should take the time to learn the MQL language and its rules, as well as the its environment. You need to code things according to those rules and the working environment.

Look up some examples in the CodeBase and study them, while referencing the documentation.

Reason: