bool always reinitialized to false.

 
Good day,
i am writting an indicator of simple levels display. i can change the value true or false by a button.
but when i change the time frame, the bool always return false.

How i can fix it please ?
Regards.

nb : i have declared my bool in global, then tried in local but no success.

//--- display levels
      bool display_levels;

  int OnInit()
  {
        display_levels=false;
  }
 
Email Temporaire: i am writting an indicator of simple levels display. i can change the value true or false by a button. but when i change the time frame, the bool always return false. How i can fix it please ?
nb : i have declared my bool in global, then tried in local but no success.

When the timeframe changes, the Indicator is reloaded and the the OnInit() is run again.

So, don't reinitialise the variable in OnInit() and only initialise it in the declaration.

//--- display levels
   bool display_levels = false;

int OnInit()
{
   // display_levels=false;
}
 
Fernando Carreiro #:

When the timeframe changes, the Indicator is reloaded and the the OnInit() is run again.

So, don't reinitialise the variable in OnInit() and only initialise it in the declaration.

Thank you.
I try it, but same result.
Maybe it's because I change the true/false value in OnChartEvent() function ?

if(       id==CHARTEVENT_OBJECT_CLICK &&
          sparam==my_btn &&
          display_levels==false)
      {
        Print("The mouse has been clicked on the object with name '"+sparam+"'");
        Print("display_levels = "+(string)display_levels);
        //---
        display_levels=true;
        //---
        Print("display_levels = "+(string)display_levels);
      }



In wich function have I to do it please? OnCalculate ?

 
Email Temporaire #:I try it, but same result. Maybe it's because I change the true/false value in OnChartEvent() function ? In wich function have I to do it please? OnCalculate ?

Then save the state to an actual graphical button object and read the state from it when the timeframe changes.

Alternatively save the variable to Global Terminal Variables and restore from it when the timeframe changes.

Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global Variables of the Terminal - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Then save the state to an actual graphical button object and read the state from it when the timeframe changes.

Alternatively save the variable to Global Terminal Variables and restore from it when the timeframe changes.

Already tried your 1st solution.
But my btn state is reinitialized too. I even tried with color graphical object, and the same.

I will try with your 2nd proposal.
I will let you know.
Thank you.

 
Email Temporaire #: Already tried your 1st solution. But my btn state is reinitialized too. I even tried with color graphical object, and the same.

The 1st solution (using a button state) is guaranteed to work. So you must be doing something wrong. Show your code if you need more help.

// Get button state
    bool bGetState = (bool) ObjectGetInteger( 0, idButtonObject, OBJPROP_STATE );

// Set button state
   ObjectSetInteger( 0, idButtonObject, OBJPROP_STATE, bSetState );
 
Fernando Carreiro #:

The 1st solution (using a button state) is guaranteed to work. So you must be doing something wrong. Show your code if you need more help.

I used GlobalVarialbes and it works fine for me.
Thank you VERY MUCH for your help.

 
Email Temporaire #: Thank you VERY MUCH for your help.
You are welcome!
 
Email Temporaire: i am writting an indicator … but when i change the time frame, the bool always return false.
Indicators are reloaded on chart change, EAs are not.
Reason: