Download MetaTrader 5

Runs once new day starts

To add comments, please log in or register
Dua Yong Rew
5536
Dua Yong Rew  

Hi all,

I have a function to store some daily data and this need to be refresh once a new day starts.

My EA is put on 1H chart

How to go about doing so?

Thanks

brewmanz
486
brewmanz  

use something like

static datetime currentDay = 0
...
  bool newDayStarted = false;
  if(currentDay != iTime(NULL, PERIOD_D1, 0))
  {
    newDayStarted = true;
    currentDay != iTime(NULL, PERIOD_D1, 0)
  }

code just typed; not checked or compiled, etc

Dua Yong Rew
5536
Dua Yong Rew  
thanks, will KIV and test the code
Dua Yong Rew
5536
Dua Yong Rew  
bool NewDay()
{
   static datetime PrevDay;

   if(PrevDay < iTime(NULL, PERIOD_D1, 0))
   {
      PrevDay = iTime(NULL, PERIOD_D1, 0);
      return(true);
   }
   else
   {
      return(false);
   }
}
correct code should be this?
brewmanz
486
brewmanz  
if(PrevDay < iTime(NULL, PERIOD_D1, 0))

will fail to recalc for indicator on refresh that restarts from oldest bar. That why I used '!=' not equal

Dua Yong Rew
5536
Dua Yong Rew  
brewmanz:

will fail to recalc for indicator on refresh that restarts from oldest bar. That why I used '!=' not equal


Sorry, I don't get what you mean. Please explain more.
brewmanz
486
brewmanz  

(the following is my understanding anyway)

Let's say that you do a Refresh to update some missing bars.

Then deinit(), init(), and start (with IndicatorCounted() of 0) will happen.

Any variables set up/initialised in init() will be just fine.

However, static variables will NOT be initialised again, so a check of PrevDay against iTime will not trigger recalcs, as Prevday will be never be lower than any bars, until a new latest bar is created

Dua Yong Rew
5536
Dua Yong Rew  
brewmanz:

(the following is my understanding anyway)

Let's say that you do a Refresh to update some missing bars.

Then deinit(), init(), and start (with IndicatorCounted() of 0) will happen.

Any variables set up/initialised in init() will be just fine.

However, static variables will NOT be initialised again, so a check of PrevDay against iTime will not trigger recalcs, as Prevday will be never be lower than any bars, until a new latest bar is created

I understand you now.

Thanks for the information.

selnomeria
254
selnomeria  
brewmanz:

use something like

code just typed; not checked or compiled, etc

currentDay != iTime(NULL, PERIOD_D1, 0)

here is type error. fix it. there should be = instead o f !=
To add comments, please log in or register