Question: MT4 ArrayCopySeries MODE_TIME

 
https://docs.mql4.com/array/ArrayCopySeries
There is no real memory allocation for data array and nothing is copied. When such an array is accessed, the access is redirected.

I have this NewBar() function

Code:
bool NewBar() 
    { 
    static datetime dt = 0; 
    if (Time[0] != dt) 
        { 
        dt = Time[0]; 
        return(true); 
    } 
    return(false); 
}
Now my Question: I might need such for all possible TimeFrames.

is it better to do it like this: e.g. M15

Code:
//in the light of:
//There is no real memory allocation for 
//data array and nothing is  copied. 
//When such an array is accessed,        
// the access is  redirected. 

datetime dtArTimeM15[];
ArrayCopySeries(dtArTimeM15, MODE_TIME, sPairName, PERIOD_M15); 

         
  
bool NewBarM15() 
    { 
    static datetime dt = 0; 
    if (dtArTimeM15[0] != dt) 
        { 
        dt =dtArTimeM15[0];
        return(true); 
    } 
    return(false); 
}
OR like this:

Code:
 bool NewBarM15() 
     { 
     static datetime dt = 0; 
     if (iTime(sPairName, PERIOD_M15, 0) != dt) 
         { 
         dt = iTime(sPairName, PERIOD_M15, 0);
         return(true); 
     } 
     return(false); 
 }
 
Define better. They both do the same exact thing.
int start(){ static datetime Time0;
    if (Time0 != Time[0]){  Time0 = Time[0]; // New bar this TF
        bool newM15 = (Time0 %  900) == 0,   // 15*60
             newH1  = (Time0 % 3600) == 0;   // 60*60
        ...
}   }
 
WHRoeder:
Define better. They both do the same exact thing.

Thanks WHRoeder.

I know they do the same but I'm not really too familiar with MT4 or C (coming more from 'python' background.).

Basically I wanted to know if

ArrayCopySeries(dtArTimeM15, MODE_TIME, sPairName, PERIOD_M15);

dtArTimeM15[0]

OR

iTime(sPairName, PERIOD_M15, 0)

are different in speed.

I'm unsure what about the redirection stuff and also how iTime does it.: //ArrayCopySeries: There is no real memory allocation for data array and nothing is copied. When such an array is accessed, the access is redirected.


Thanks also for your example: I guess I will use something similar. Very kind of you.

MJ

 

For anyone reading this later on.

The above suggested code

int start(){ static datetime Time0;
    if (Time0 != Time[0]){  Time0 = Time[0]; // New bar this TF
        bool newM15 = (Time0 %  900) == 0,   // 15*60
             newH1  = (Time0 % 3600) == 0;   // 60*60
        ...
}   }


does not work similar on higher TF (e.g. W1, MN1)

 

How can you compare 2 dates if 1 of them is uninitialized? Well in this case it'll work because the inequality will be confirmed and Time0 set, but it's bad practice to not initialize a variable before using it :P but yea the problem is you're using it as static... you may want to use a global variable here instead. But as said static datetime works in this case so no prob right now :P

And of course it won't work on anything above H1 chart as your using the Time[i] array. This array is always filled with current chart data. So if you're on weekly chart this array will be:

[monday1 00.00, monday2 00.00, monday3 00.00......] or if you're using CET broker [sunday1 2300, sunday2 2300, sunday3 2300...]

As you want to use a lower timeframe no matter what chart timeframe you're using:

int start()
{

int start(){ static datetime Time0;


datetime t1 = iTime(NULL, PERIOD_M15,0);
if (Time0 != t1){
Time0 = t1; // New bar this TF
bool newM15 = (Time0 % 900) == 0, // 15*60
newH1 = (Time0 % 3600) == 0; // 60*60
...
} }


Switch the period_m15 for anything lower if you want more accurate data but only as low as the lowest Time % xyz calculation.

 

Thanks 'forexCoder'.


There is a other problem too I just saw. by using something similar to: bool newM15 = (Time0 % 900) == 0, // 15*60

One gets a TRUE return even if the TF has wholes: https://www.mql5.com/en/articles/1407


So I will be doing something more like I had in the beginning.


ArrayCopySeries(dtArTimeM1, MODE_TIME, sPairName, PERIOD_M1); 
...
ArrayCopySeries(dtArTimeMN1, MODE_TIME, sPairName, PERIOD_MN1);

void NewMTFBarBool(datetime _CheckTime)
    {
    static datetime dt;
    
    if (dt < _CheckTime)
        {
        dt = _CheckTime; 
        
        bNewBarChartInterval = (_CheckTime == dtArTimeChartInterval[iBarShift(sPairName, iChartInterval, _CheckTime)]);
        bNewBarM1 = (_CheckTime == dtArTimeM1[iBarShift(sPairName, PERIOD_M1, _CheckTime)]);        
        bNewBarM5 = (_CheckTime == dtArTimeM5[iBarShift(sPairName, PERIOD_M5, _CheckTime)]);  
        bNewBarM15 = (_CheckTime == dtArTimeM15[iBarShift(sPairName, PERIOD_M15, _CheckTime)]);  
        bNewBarM30 = (_CheckTime == dtArTimeM30[iBarShift(sPairName, PERIOD_M30, _CheckTime)]);        
        bNewBarH1 = (_CheckTime == dtArTimeH1[iBarShift(sPairName, PERIOD_H1, _CheckTime)]);  
        bNewBarH4 = (_CheckTime == dtArTimeH4[iBarShift(sPairName, PERIOD_H4, _CheckTime)]);  
        bNewBarD1 = (_CheckTime == dtArTimeD1[iBarShift(sPairName, PERIOD_D1, _CheckTime)]);        
        bNewBarW1 = (_CheckTime == dtArTimeW1[iBarShift(sPairName, PERIOD_W1, _CheckTime)]);  
        bNewBarMN1 = (_CheckTime == dtArTimeMN1[iBarShift(sPairName, PERIOD_MN1, _CheckTime)]);  
        }
    else
        {
        // RESET
        bNewBarChartInterval = false;
        bNewBarM1 = false;
        bNewBarM5 = false;
        bNewBarM15 = false;
        bNewBarM30 = false;
        bNewBarH1 = false;
        bNewBarH4 = false;
        bNewBarD1 = false;
        bNewBarW1 = false;
        bNewBarMN1 = false;
        }
}  
 

In case someone reads that later on: I just came across a problem:

Now I came across a 1min chart where there are holes: e.g. 2011.05.30 01:54, the next one is 2011.05.30 01:56

The code works ok but if one loops through the chart one bar at a time one misses 1 5Min New Bar notification - which in my case was a problem.

Reason: