Object disappeares after changing TimeFrame on chart

 

Hello.


I draw a rectangle object on my Chart in H1.

ObjectCreate("Box", OBJ_RECTANGLE,0, ...)

If I change to another timeframe on chart, the object is not shown.
If I go back to H1 it's gone too.

Any ideas, why?

I delete this object in init(), deinit(). But neither shouold be executed, changing the timeframe on chart, right?


Thanks.
WorstCases

 
WorstCases:

I delete this object in init(), deinit(). But neither shouold be executed, changing the timeframe on chart, right?

init() is run when you change timeframes . . . you can trap this and make a decision to not delete if init() is being run as a result of changing timeframes . . .

Info here you can check if the reason is REASON_CHARTCHANGE and if it is don't delete your Objects.

 

Thank you for the info.

let's say the previous code example is my init():

How would I write it using your example that the code is never executed, unless the EA is newly attached to the chart or enabled with the "ExpertAdvisor" butto?


int init()
   {
   Code
   Code
   Code
   }
 

Another general question: Where can I see under which scenarios init() is always executed?


Thanks.
WorstCases

 

I know this is not the most professional approach, but for now I did do the following:

bool DidInitRunBefore = 0;

init()
   {
   if (!DidInitRunBefore)
      {
      Code
      }
   Code
   Code
   DidInitRunBefore = true;
   }

The "DidInitRunBefore" is not reset at change of chart, right.

 

Why not just do something like . . . .

init()
   {
   if ( UninitializeReason() != REASON_CHARTCHANGE)
      {  // code to delete Objects 
      }

   }

By the way . . . a bool is true or false . . .

 

Thank you!

I did assume I can use either 0/1 or false/true, because 0=false and 1=true in case of bool?

Am I wrong?

 
WorstCases:

Thank you!

I did assume I can use either 0/1 or false/true, because 0=false and 1=true in case of bool?

Am I wrong?

You aren't wrong . . . but 1 is an int, false is a bool . . . if you want your code to be clear and unambiguous use true/false for bools and use numbers for ints ;-)
 
Ah - I see, OK. thank you.
Reason: