Is there a way to refresh/reload a static variable?

 
Is there a way to refresh/reload a static variable? I tried RefreshRates and it doesn't seem to help.
 
Renz Carillo:
Is there a way to refresh/reload a static variable? I tried RefreshRates and it doesn't seem to help.

just assign a new value to the static variable

static simply means it keeps it's value between calls to the function

 
Renz Carillo:
Is there a way to refresh/reload a static variable? I tried RefreshRates and it doesn't seem to help.
// --- Standard Global Variable ---------------------------------------------------------
static int  s_id = 0;

void OnDeinit(const int reason) {
   s_id = 0; // reset the id OnDeinit() function
}

void ResetStaticID ()
   s_id = 0; // reset the id
}
 
Paul Anscombe:

just assign a new value to the static variable

static simply means it keeps it's value between calls to the function

I don't know if that would be possible in my code

   static double balance = AccountBalance();

What i'm trying to do is refresh this static variable after a certain day has passed.

 
Renz Carillo:

What i'm trying to do is refresh this static variable after a certain day has passed.

if(new day)
  balance = AccountBalance();
 
Keith Watford:

I've also tried that but still to no avail, what i specifically need is to retrieve the starting balance for each day

 

I solved it, probably not the best method but it still work nevertheless

   double balance = AccountBalance();
   static double newdaybalance;

   
   if (Hour()==0){ newdaybalance = balance ; } 
 
Renz Carillo:
   static double balance = AccountBalance();

What i'm trying to do is refresh this static variable after a certain day has passed.

  1. That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 2013.02.10

  2. Resetting static: Problem in detecting a new Bar - MQL5 programming forum #5.3 2020.04.24

Reason: