wake up upon candle update

 

In MQL4 how do I register for 1 minute candle updates?

When I create a new expert adviser script, I see the following methods:


init()

deinit()

start()


I notice the init() and start() functions get called only once and then never again; however, the documentation claims the start() function is called every "tick". I have placed "Print()" statements inside the start() function and they only appear once. Am I doing something wrong?


Meanwhile the EURUSD chart continues to draw new candle updates. How do I let the script know every time a new candle have been completely drawn?

Specifically, how can the EURUSD chart tell MQL4 when high[1], low[1], open[1], close[1] have changed? Is such type of "observer" design pattern available in MQL4? If not, is there sample code I can see that observes for candle update events?

 
  1. mathurpratik:
    In MQL4 how do I register for 1 minute candle updates?
    Add a chart/change to 1 minute (hour icon)
  2. I notice the init() and start() functions get called only once and then never again; however, the documentation claims the start() function is called every "tick". I have placed "Print()" statements inside the start() function and they only appear once. Am I doing something wrong?
    Market is closed on weekends
  3. Meanwhile the EURUSD chart continues to draw new candle updates. How do I let the script know every time a new candle have been completely drawn?
    int start(){
       static datetime Time0;
       bool newBar = Time0 != Time[0];
       if (newBar){  Time0 =  Time[0]; ... }
    if you don't want to do anything until a new bar
    int start(){
       static datetime Time0; if (Time0 == Time[0]) return(0); Time0 =  Time[0];
       ...
 
if u want to new bar event u can put the upward times into array and check the array times every tick, if it reached execute the right new bar event. Ofc u can create special period e.g: 20min/40min....

int curIndex;
datetime times[7];

int init () {

  curIndex = utils.periodToPeriodIndex(Period());
  times[curIndex] = iTime(NULL,0,0);
  for(int i=curIndex+1; i<7; i++) {
    times[i] = times[curIndex]-(MathMod(times[curIndex]/60,utils.periodIndexToPeriod(i)))*60;
  }
  
  return(0);
}

int start() {

  if (times[curIndex] != iTime(NULL,0,0)) {
    times[curIndex] = iTime(NULL,0,0);
    onBar(Period());
    for(int i=curIndex+1; i<7; i++)
      if (times[i] != times[curIndex]-(MathMod(times[curIndex]/60,utils.periodIndexToPeriod(i)))*60) {
        times[i] = times[curIndex]-(MathMod(times[curIndex]/60,utils.periodIndexToPeriod(i)))*60;
        onBar(utils.periodIndexToPeriod(i));
      }
  }
  
  onTick();

  return(0);
}

void onTick() {

}

void onBar(int period) {
  if (period = Period()) { /* current period */ }
  if (period = PERIOD_H1) { /* new H1 bar present */ }  
}

int utils.periodToPeriodIndex(int period) {
  switch(period) {
    case PERIOD_M1  : return(0); break;
    case PERIOD_M5  : return(1); break;
    case PERIOD_M15 : return(2); break;
    case PERIOD_M30 : return(3); break;
    case PERIOD_H1  : return(4); break;
    case PERIOD_H4  : return(5); break;
    case PERIOD_D1  : return(6); break;
    case PERIOD_W1  : return(7); break;
    case PERIOD_MN1 : return(8); break;
  }
}

int utils.periodIndexToPeriod(int index) {
  switch(index) {
    case 0: return(PERIOD_M1); break;
    case 1: return(PERIOD_M5); break;
    case 2: return(PERIOD_M15); break;
    case 3: return(PERIOD_M30); break;
    case 4: return(PERIOD_H1); break;
    case 5: return(PERIOD_H4); break;
    case 6: return(PERIOD_D1); break;
    case 7: return(PERIOD_W1); break;
    case 8: return(PERIOD_MN1); break;
  }
}
 
symr:
if u want to new bar event u can put the upward times into array and check the array times every tick, if it reached execute the right new bar event. Ofc u can create special period e.g: 20min/40min....



Thanks. This was exactly what I was looking for. Both of your responses were extremely helpful and I was able to do what I wanted.

 
symr:
if u want to new bar event u can put the upward times into array and check the array times every tick, if it reached execute the right new bar event. Ofc u can create special period e.g: 20min/40min....



Hey thanks this is very good. How would I create the special period eg 50 mins?

 
manuel_fx:


Hey thanks this is very good. How would I create the special period eg 50 mins?




manuel_fx -


Most likely you will have to create a "datetime" variable and check the difference between your last candle stick and the current time. If the difference is 50 minutes you have a new candle.

Meaning while, during the elapsing 50 minutes, you will likely have to compute the high,low,open, close yourself every tick. So basically just create 4 "double" variables that update every tick.

 
manuel_fx:

Hey thanks this is very good. How would I create the special period eg 50 mins?

Iam published a template ea, just waiting a moderator approval. So I hope you dont need to wait long to catch it...
 
Already released
Reason: