Clean (erase) Arrays

 

I'm trying to make a function to clear all the informations in an array every new day.

I'm using the code below, but it's not working.

The informations keeps in the array after new day.


datetime new_day=0;
double pw[];
int Agr_Res=20;


if(iTime(_Symbol,PERIOD_D1,0)>new_day)
     {
      ArrayFree(pw);
      ArrayResize(pw,Agr_Res);
      new_day=iTime(_Symbol,PERIOD_D1,0);
     }


Anyone could help me?
If you have a better idea, feel free to share.

 
Guilherme Mendonca: I'm trying to make a function to clear all the informations in an array every new day. I'm using the code below, but it's not working.

Read up on "ArrayInitialize()" and "ArrayFill()".

When you allocate space from memory it does not initialize its contents and chances are that it might just reallocate the space the same space just freed before it. So, initialize the contents!

Documentation on MQL5: Array Functions / ArrayInitialize
Documentation on MQL5: Array Functions / ArrayInitialize
  • www.mql5.com
ArrayInitialize - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Read up on "ArrayInitialize()" and "ArrayFill()".

When you allocate space from memory it does not initialize its contents and chances are that it might just reallocate the space the same space just freed before it. So, initialize the contents!

 OK. thanks.
I'll try it.

 
  1. Always post all relevant code (using Code button) or attach the file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't know if your new_day is part of your function (which will never work) or globally declared (equivalent to static).

  2. You could use D1 timeframe, but then you have to deal with 4066 / synchronization issue.

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020.12.15)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

  3. Instead, just use the standard new bar code with just the date.
              New candle - MQL4 programming forum #3 (2014)

    static datetime cur_day=0;
    datetime pre_day=cur_day; cur_day = date();
    
    if(pre_day != cur_day)
         {
          ArrayFree(pw);
          ArrayResize(pw,Agr_Res);
         }
              date/time (2017)
              Find bar of the same time one day ago - MQL4 programming forum (2017)
Reason: