Can't get if the visible timeframes from an object is the same as the current one correctly

 

Hello,

recently I starting coding my own indicator and I'm stuck so I decided to post here as this site is where I got all the information and answers before by looking to other users questions. But now I can't find one solution in this forum that works for me and I would like to understand why is not working. The problem is that when I try to see if an object is visible in the current timeframe my code only works if the visible timeframes are all but not if it's some of them. For testing I just create a line in a TF, for example M15, that will be shown only in M15, M5 and M1 and a label that will be shown in all TF. When I try to see in M15 if the objects are visible only the label says it does.
I've found https://www.mql5.com/en/forum/339009 and the other post linked in it but that solution wasn't working for me so I tried to understand the mechanism becuase I have never worked with flags by reading https://www.mql5.com/en/book/applications/objects/objects_timeframes and https://www.mql5.com/en/docs/basis/operations/bit .
As I've understood, maybe I'm wrong, when I do the & operation it should return me the same TF as the current one if that timeframe is visible on the object as it will be the only bits set to 1. Did i get it wrong?

long TFmask=ObjectGetInteger(0, objectID,OBJPROP_TIMEFRAMES);
int currentTF=Period();
long period=TFmask & currentTF;
if(period == currentTF)
   Print("YES");
else
   Print("NO"); 


Appart from that, I've printed some info to try to know what is happening and the line only visible in M15, M5 and M1 gets period=1, while currentTF=15. In the case of the lable period=15 so it's correct. What did I do wrong?

Thank you and sorry if I posted something in a wrong way, is my first time posting here but I tried to do it as correct as I've seen in other posts. Anything done wrong don't hesitate and tell me so I can improve in future posts!
How to check if element is visible on chart
How to check if element is visible on chart
  • 2020.04.29
  • Mamboki
  • www.mql5.com
Hi, can anyone help me how to write IF statement to check if object is visible on current time frame...
 

Forum on trading, automated trading systems and testing trading strategies

Question about OBJPROP_TIMEFRAMES

Alain Verleyen, 2015.01.24 13:53

Just for fun.

By the way I found 2 bugs in the process, see comments (first one already found by Gumrai)

input string   objname="test";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   long objTimeframes=0;
   ENUM_TIMEFRAMES period=(ENUM_TIMEFRAMES)Period();

//--- Get visibility property of the object
   if(ObjectGetInteger(0,"test",OBJPROP_TIMEFRAMES,0,objTimeframes))
     {
      long mask=-1;
      //--- set mask from period
      switch(period)
        {
         case PERIOD_M1 : mask=0x0001; break;         // The object is drawn in 1-minute chart
         case PERIOD_M5 : mask=0x0002; break;         // The object is drawn in 5-minute chart
         case PERIOD_M15: mask=0x0004; break;         // The object is drawn in 15-minute chart
         case PERIOD_M30: mask=0x0008; break;         // The object is drawn in 30-minute chart
         case PERIOD_H1 : mask=0x0010; break;         // The object is drawn in 1-hour chart
         case PERIOD_H4 : mask=0x0020; break;         // The object is drawn in 4-hour chart
         case PERIOD_D1 : mask=0x0040; break;         // The object is drawn in day charts
         case PERIOD_W1 : mask=0x0080; break;         // The object is drawn in week charts
         case PERIOD_MN1: mask=0x0100; break;         // The object is drawn in month charts     
         default:
            break;
        }
      //--- check mask. Special cases : 
      //---    1° BUG 1: if "Show on all timeframes" is enabled, objTimeframes=0 is returned and not '0x01ff' as stated in documentation.
      //---    2° BUG 2: it's not possible with MT4 to disable "Show on all timeframes" without enabled at least 1 period ;
      //---              but it's possible to set it to -1 with mql4. In this case, MT4 object properties window will display erroneously "Show on all timeframes" enabled.
      if(objTimeframes==0 || (objTimeframes!=-1 && (objTimeframes&mask)==mask))
        {
         printf("Object %s is visible on this chart %s",objname,EnumToString(period));
        }
      else
        {
         printf("Object %s exists but is not visible on this chart %s",objname,EnumToString(period));
        }
     }
//--- ObjectGetInteger error processing
   else
     {
      int err=GetLastError();
      if(err==ERR_OBJECT_DOES_NOT_EXIST)
        {
         printf("Object %s doesn't exist!",objname);
        }
      else
        {
         printf("Error(%i) whily getting properties of %s",err,objname);
        }
     }
  }
//+------------------------------------------------------------------+

 
Alain Verleyen #:

As I said in my post I already read that thread and tried it and didn't work. So I decided to understand how to do it and tried my own way. As it is shown, my way is not working so I wanted to know what I understood wrong to make it right by myself.
 
Victor #:
As I said in my post I already read that thread and tried it and didn't work. So I decided to understand how to do it and tried my own way. As it is shown my way is not working so I wanted to know what I understood wrong to make it right by myself.

The code I posted is from mql4, you need to adjust it to mql5. The logic is the same.

You are not using the mask correctly, as you can't mix "chart timeframe" and "object timeframe", you need to convert from one to the other, which is what my old post was showing for mql4.

 
Alain Verleyen #:

The code I posted is from mql4, you need to adjust it to mql5. The logic is the same.

You are not using the mask correctly, as you can't mix "chart timeframe" and "object timeframe", you need to convert from one to the other, which is what my old post was showing for mql4.

I am transforming it, maybe I am doing it wrong but I get the object_timeframe and doing and & with the current chart timeframe. It should give me all 1 wherein both variables is a 1, resulting in the same number if object_timefrates has the current chart timeframe. Or at least, this is what I am trying to do.
 
Victor #:

I am transforming it,

No you don't.

maybe I am doing it wrong

Yes you do it wrong.

but I get the object_timeframe and doing and & with the current chart timeframe. It should give me all 1 wherein both variables is a 1, resulting in the same number if object_timefrates has the current chart timeframe. Or at least, this is what I am trying to do.

No !

You don't read carefully what I tried to explain you. "object_timeframes" will NEVER "have" the current chart timeframe, because objects timeframe(s) and chart timeframe are 2 DIFFERENT things.

 
Alain Verleyen #:

You don't read carefully what I tried to explain you. "object_timeframes" will NEVER "have" the current chart timeframe, because objects timeframe(s) and chart timeframe are 2 DIFFERENT things.

You are right, I was not understanding you. Now I do, timeframes used in objects are not the same as timeframes used in charts.
If someone doesn't understand like I did before, here is an example. If you set the visible timeframe of an object, let's say to M1, you use OBJ_PERIOD_M1 https://www.mql5.com/en/book/applications/objects/objects_timeframes. But the timeframe of a M1 chart is PERIOD_M1 https://www.mql5.com/en/docs/constants/chartconstants/enum_timeframes. For me at least the problem was that I was refering to the same period but didn't notice the enums used were different.

To do it in MQL5 you can use the code provided by  Alain Verleyen in this post and just change the mask values to the ones given in the visibility of objects documentation https://www.mql5.com/en/book/applications/objects/objects_timeframes.


Thank you and sorry for not understanding the first time you explained it to me ^^

MQL5 Book: Creating application programs / Graphical objects / Visibility of objects in the context of timeframes
MQL5 Book: Creating application programs / Graphical objects / Visibility of objects in the context of timeframes
  • www.mql5.com
MetaTrader 5 users know about the Display tab in the object properties dialog where you can use the switches to select on which timeframes the...