Horizontal Line Object doesn't get removed when the indicator's visualization is unchecked

xtemer
49
Anyone know why the horizontal line in this code doesn't get removed when the indicator's visualization value is unchecked from the property dialog?

Here is the sample code (Test1.mq4) that shows a blue horizontal line on chart:

#property strict
#property indicator_chart_window

int OnInit()
{
   return(INIT_SUCCEEDED);
}

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[])
{
   string obj_name="Test1";
   
   double bid_price = MarketInfo(Symbol(), MODE_BID);

   if(ObjectFind(0,obj_name)==0 || ObjectCreate(0,obj_name,OBJ_HLINE,0,0,bid_price))
   {
      ObjectSetInteger(0,obj_name,OBJPROP_STYLE,STYLE_DOT);
      ObjectSetInteger(0,obj_name,OBJPROP_COLOR,clrBlue);
      ObjectSetInteger(0,obj_name,OBJPROP_WIDTH,3);
      ObjectSetInteger(0,obj_name,OBJPROP_SELECTABLE,False);
   }

   return(rates_total);
}

void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0,"Test1");
}


Let's say the indicator is on H1 chart, then uncheck the "H1" visualization from the property dialog (see the attached image file). I expect the horizontal line should be hidden/removed from the H1 chart, but it doesn't. I have to switch to another time frame and then switch back to H1 chart to remove the horizontal line.

So anyone know what is the code solution to remove the horizontal line when the indicator's visualization is unchecked?


Edit:

Here are the steps to reproduce the issue with the Test1.mq4:

1) Open a H1 chart on any currencies.

2) Put the Test1 indicator on the H1 chart (use default indicator settings, don't change anything).

3) Go to indicators list (CTRL+I)

4) Select the Test1 indicator and click Edit.

5) Go to Visualization tab and uncheck the "H1", you can leave the rests checked. Don't switch to other tab after this step!

6) Click OK. Don't switch to other time frame after this step!

7) You should see blue horizontal line is still there on the H1 chart while the expected result is the horizontal line should be removed.


Edit 2:

Attached the Test1.ex4 file.

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
Files:
test1.png  12 kb
Paul Anscombe
7333
xtemer:
Anyone know why the horizontal line in this code doesn't get removed when the indicator's visualization value is unchecked from the property dialog?

Here is the sample code that shows a blue horizontal line on chart:


Let's say the indicator is on H1 chart, then uncheck the "H1" visualization from the property dialog (see the attached image file). I expect the horizontal line should be hidden/removed from the H1 chart, but it doesn't. I have to switch to another time frame and then switch back to H1 chart to remove the horizontal line.

So anyone know what is the code solution to remove the horizontal line when the visualization is unchecked?

you are probably waiting for a new tick, try redrawing the chart after you have altered the object.

xtemer
49
xtemer  
Paul Anscombe #:

you are probably waiting for a new tick, try redrawing the chart after you have altered the object.

Can you show me how to do that in the code?

I checked the OnCalculate method doesn't get called at all after the H1 visualization is unchecked, same for the OnInit and OnDeinit method.

Paul Anscombe
7333
xtemer #:

Can you show me how to do that in the code?

I checked the OnCalculate method doesn't get called at all after the H1 visualization is unchecked, same for the OnInit and OnDeinit method.

Not sure why you want to be updating the object attributes every tick in OnCalculate  but that aside nowhere in your code are you setting the visible timeframes..

You code does not even compile where is the variable p declared.. 

How have you checked OnCalculate does not get called, I find that unlikely ?

xtemer
49
xtemer  
Paul Anscombe #:

Not sure why you want to be updating the object attributes every tick in OnCalculate  but that aside nowhere in your code are you setting the visible timeframes..

No, what I want is the horizontal line to be removed or hidden when the corresponding indicator's visualization is unchecked. Note that I'm talking about the whole indicator's visualization, not the horizontal line object's visualization.

On Moving Average indicator, this has no issue at all, the MA line is automatically removed when the corresponding indicator's visualization is unchecked, not sure why horizontal line object doesn't behave same, could it be because the horizontal line object isn't buffered object like MA?


> You code does not even compile where is the variable p declared.. 

My bad, that line shouldn't be there. Please check the code again, I have edited it.


> How have you checked OnCalculate does not get called, I find that unlikely ?

By putting Print function call in the method. The print message doesn't show up in the MT4's Experts tab after the visualization is unchecked.

Paul Anscombe
7333
xtemer #:

No, what I want is the horizontal line to be removed or hidden when the corresponding indicator's visualization is unchecked. Note that I'm talking about the whole indicator's visualization, not the horizontal line object's visualization.

On Moving Average indicator, this has no issue at all, the MA line is automatically removed when the corresponding indicator's visualization is unchecked, not sure why horizontal line object doesn't behave same, could it be because the horizontal line object isn't buffered object like MA?


> You code does not even compile where is the variable p declared.. 

My bad, that line shouldn't be there. Please check the code again, I have edited it.

you must have something else going on because it works fine on mine, if I select M5 Daily say then it only appears on those timeframes.

what version are you on,  mine is build 1353 

is that the actualy code you are running exactly

xtemer
49
xtemer  
Paul Anscombe #:

you must have something else going on because it works fine on mine, if I select M5 Daily say then it only appears on those timeframes.

are you on the latest version?

Yes, I'm on the latest version. I have two different MT4 apps from different brokers, same issue on both.


Here are the steps to reproduce the issue to make sure there is no misunderstanding:

1) Open a H1 chart on any currencies.

2) Put the Test1 indicator on the H1 chart.

3) Go to indicators list (CTRL+I)

4) Select the Test1 indicator and click Edit.

5) Uncheck the "H1" on visualization tab, you can leave the rests checked. 

6) Click OK. Don't switch to other time frame after this step!

7) You should see blue horizontal line is still there on the H1 chart while the expected result is the horizontal line should be removed.

Paul Anscombe
7333
xtemer #:

Yes, I'm on the latest version. I have two different MT4 apps from different brokers, same issue on both.


Here are the steps to reproduce the issue to make sure there is no misunderstanding:

1) Open a H1 chart on any currencies.

2) Put the Test1 indicator on the H1 chart.

3) Go to indicators list (CTRL+I)

4) Select the Test1 indicator and click Edit.

5) Uncheck the "H1" on visualization tab, you can leave the rests checked. 

6) Click OK. Don't switch to other time frame after this step!

7) You should see blue horizontal line is still there on the H1 chart while the expected result is the horizontal line should be removed.

no it does not create the object on mine, no line following your instructions  until I change TF

you don't already have the object created do you?

xtemer
49
xtemer  
Paul Anscombe #:

no it does not create the object on mine, no line following your instructions  until I change TF

you don't already have the object created do you?

The line object was created when I put the indicator on chart. But then it didn't get removed/hidden when the visualization is unchecked.

I even tested this on another PC to make sure it isn't my PC environment that caused this, but same issue happened.

Please try the compiled ex4 file on the attachment, I wonder if it's the compiler issue.


For others who did the test too, let me know your result.

Paul Anscombe
7333
xtemer #:

The line object was created when I put the indicator on chart. But then it didn't get removed/hidden when the visualization is unchecked.

I even tested this on another PC to make sure it isn't my PC environment that caused this, but same issue happened.

Please try the compiled ex4 file on the attachment, I wonder if it's the compiler issue.


For others who did the test too, let me know your result.

works fine on mine,  what is your version number

xtemer
49
xtemer  
Paul Anscombe #:

works fine on mine,  what is your version number

1353 and 1356 on different brokers.