Experts: New bar event in EA

 

New bar event in EA:

This is a template EA with onBar() event.

Author: Robert Simon

 
  1. times[curIndex]-(MathMod(times[curIndex]/60,utils.periodIndexToPeriod(i)))*60
    for efficiency and readability, simplify
    times[curIndex]- MathMod(times[curIndex],utils.periodIndexToPeriod(i)*60);

  2. for efficiency and readability and to avoid unnecessary function calls, I'd replace the functions with an array and save results
    int utils.periodIndexToPeriod   = {
        PERIOD_M1,  PERIOD_M5,  PERIOD_M15, PERIOD_M30, PERIOD_H1,  PERIOD_H4,
        PERIOD_D1,  PERIOD_W1,  PERIOD_MN1, 20,         50  };
    for(int i=curIndex+1; i<MAX; i++)
        int period  = utils.periodIndexToPeriod[i];
            seconds = period*60,
            time0   = times[curIndex] - MathMod(times[curIndex],seconds);
        if (times[i] != time0) {
            times[i] = time0;
            onBar(period);
    }   }

  3. And avoid unnecessary function calls with the simpler
    times[curIndex] = Time[0]; // = iTime(NULL,0,0);

 

Hi.

thx for this ideas, ur right.

1. Accessing the arrays is more faster than use switch - case element. (in this example 4 times), some test:

  start = GetTickCount();
  for(test=0; test<10000000; test++)
   for(i=0; i<MAX; i++)
    int value1 = utils.periodIndexToPeriod[i];
  log("Elapsed : " + (GetTickCount()-start)); // 2100 ms
  start = GetTickCount();
  for(test=0; test<10000000; test++)
   for(i=0; i<MAX; i++)
    int value2 = utils2.periodIndexToPeriod(i);
  log("Elapsed : " + (GetTickCount()-start)); // 8000 ms

but how accsess this 10.000.000*MAX in a row? :D and i think much more readble switch - case options beacuse it is more similar the other rutins. Easily readable by someone who not a coder.

2. use local variable in loop save some times but not so much i think so test it: in half year backtest, where do some loop in onBar event, ur code done in 6006ms, my 5990ms on ~ 72Kbar event so its same

3. Time[0] / iTime(NULL,0,0) does not affect the result.

So i change the loop code for ur versions (more readable), but not change the array access rutin. btw thx a lot, i swear i will not publish a code, after 2 am, when i am too tired :P


 
WHRoeder:
  1. times[curIndex]-(MathMod(times[curIndex]/60,utils.periodIndexToPeriod(i)))*60
    for efficiency and readability, simplify
    times[curIndex]- MathMod(times[curIndex],utils.periodIndexToPeriod(i)*60);

  2. for efficiency and readability and to avoid unnecessary function calls, I'd replace the functions with an array and save results
    int utils.periodIndexToPeriod   = {
        PERIOD_M1,  PERIOD_M5,  PERIOD_M15, PERIOD_M30, PERIOD_H1,  PERIOD_H4,
        PERIOD_D1,  PERIOD_W1,  PERIOD_MN1, 20,         50  };
    for(int i=curIndex+1; i<MAX; i++)
        int period  = utils.periodIndexToPeriod[i];
            seconds = period*60,
            time0   = times[curIndex] - MathMod(times[curIndex],seconds);
        if (times[i] != time0) {
            times[i] = time0;
            onBar(period);
    }   }

  3. And avoid unnecessary function calls with the simpler
    times[curIndex] = Time[0]; // = iTime(NULL,0,0);

Thanks for this I am trying with custom timerames, if I set it up for a 3 hour candle for example eg 180 minutes. When does this time start to count? eg form midnight every 3 hours or does it depend when the indicatoe is placed on the chart?


thanks again

 

Why not use iTime("EURUSD",PERIOD_M1,0)

e.g.

if(New_Time_M1 != iTime("EURUSD",PERIOD_M1,0)) // Compare time --> new bar is born
{
New_Time_M1=iTime("EURUSD",PERIOD_M1,0); // New time set
New_Bar_M1=true; // A new bar detected

}

 
Why don't you just use the iTime function?

http://docs.mql4.com/series/itime