More BUGs: Time[0] does not return correct time when testing in tester!

 
This code (which by the way resides in a library)

//+------------------------------------------------------------------+
// will return true when called within iSecondsAllowed seconds of the 
// beginning of the current candle. In cases when the first tick of the 
// new candle arrives later than iSecondsAllowed true is still returned
// to indicate that the new candle has started.
bool IsCandleStart(int iSecondsAllowed)
{
    static datetime iPeriodStartTime=0, iPeriodEndTime=0;
    static datetime iCurrTime = 0;
 
    iCurrTime = TimeCurrent();
    if( iCurrTime > iPeriodEndTime )
    {
        iPeriodStartTime = Time[0];
        iPeriodEndTime = Time[0]+Period()*60-1;
        Print("@CandleStart: ", TimeToStr( iPeriodStartTime, TIME_SECONDS ) );
 
        return (true); // crossing into new candle is always a candle start
    }
 
    if(iCurrTime <= (iPeriodStartTime+iSecondsAllowed))
    {
        Print("IsCandleStart returninig TRUE: ",
            "iPeriodStartTime(", iPeriodStartTime, ")=", TimeToStr( iPeriodStartTime, TIME_DATE|TIME_SECONDS ),
            " iCurrTime(",iCurrTime, ")=", TimeToStr( iCurrTime, TIME_DATE|TIME_SECONDS ), 
            " iSecondsAllowed=",iSecondsAllowed );
    }
    return(iCurrTime <= (iPeriodStartTime+iSecondsAllowed));
}
//+------------------------------------------------------------------+

, when tested in the tester, with from-to dates equal to 01.Oct to 01.Nov, generates output like this:


2007.02.17 04:33:13 2006.10.01 20:00 SeriesAnalysisLib GBPUSD, H4: IsCandleStart returninig TRUE: iPeriodStartTime(1171656000)=2007.02.16 20:00:00 iCurrTime(1159732800)=2006.10.01 20:00:00 iSecondsAllowed=1
2007.02.17 04:33:13 2006.10.01 20:00 SeriesAnalysisLib GBPUSD, H4: IsCandleStart returninig TRUE: iPeriodStartTime(1171656000)=2007.02.16 20:00:00 iCurrTime(1159732800)=2006. 10.01 20:00:00 iSecondsAllowed=1
2007.02.17 04:33:13 2006.10.01 20:00 SeriesAnalysisLib GBPUSD, H4: IsCandleStart returninig TRUE: iPeriodStartTime(1171656000)=2007.02.16 20:00:00 iCurrTime(1159732800)=2006. 10.01 20:00:00 iSecondsAllowed=1

How can this be correct? TimeCurrent() and Time[0] should be the same at the beginning of each candle (are at least very close) in any case 16.Feb.2006 has nothing to do with my Oct-Nov test. To get rid of this problem I have to recompile and then set the inputs again.

Here are some screenshots




 
This usually happens when time period (from-to) is changed in the tester. If I recompile after changing the dates - then it is fine again, but as soon as I change the from-to dates the reappears. Setting the values from the Inputs tab does not solve the problem.
 
source code...
 
the easiest way to check for new bar:

datetime bartime;
 
 
int start()
{
if(bartime!=Time[0])
{
   // time changed
   bartime=Time[0];
}
 

irusoh1 wrote:
 the easiest way to check for new bar:
...
if(bartime!=Time[0])
I agree that this is much simpler (although it doesn't let you check for new candle within the first X seconds of it like my code does). I will try using this as a workaround to see if it can solve my immediate problem, thank you.

However my main purpose here was to report a condition which very much looks like a MT4 bug to me. I was hoping to get some response from somebody from MetaQuotes Software telling me whether they agree that this is a bug and if yes when will it be fixed.

This is why I think it is a bug - look at the debug message printed in the Journal by my code:

2007.02.17 04:33:13 2006.10.01 20:00 SeriesAnalysisLib GBPUSD,H4: IsCandleStart returninig TRUE: iPeriodStartTime(1171656000)=2007.02.16 20:00:00 iCurrTime(1159732800)=2006.10.01 20:00:00 iSecondsAllowed=1

The tester knows that the current time (current simulated time) is 2006.10.01 20:00 - because of the date stamp in the beginning of the printed line. This is also confirmed by the call to TimeCurrent (printed in BLUE). However - and this is the bug! - Time[0] for the same period returns 2007.02.16 20:00:00?!?, which is bs.

Somebody from MetaQuotes please tell me you will fix this ASAP...
 
Put your code

bool IsCandleStart(int iSecondsAllowed)
{
    static datetime iPeriodStartTime=0, iPeriodEndTime=0;
    static datetime iCurrTime = 0;
 
    iCurrTime = TimeCurrent();
    if( iCurrTime > iPeriodEndTime )
    {
        iPeriodStartTime = Time[0];
        iPeriodEndTime = Time[0]+Period()*60-1;
        Print("@CandleStart: ", TimeToStr( iPeriodStartTime, TIME_SECONDS ) );
 
        return (true); // crossing into new candle is always a candle start
    }
 
    if(iCurrTime <= (iPeriodStartTime+iSecondsAllowed))
    {
        Print("IsCandleStart returninig TRUE: ",
            "iPeriodStartTime(", iPeriodStartTime, ")=", TimeToStr( iPeriodStartTime, TIME_DATE|TIME_SECONDS ),
            " iCurrTime(",iCurrTime, ")=", TimeToStr( iCurrTime, TIME_DATE|TIME_SECONDS ), 
            " iSecondsAllowed=",iSecondsAllowed );
    }
    return(iCurrTime <= (iPeriodStartTime+iSecondsAllowed));
}
//+------------------------------------------------------------------+


into include-file (i.e. /experts/my_include.mqh) , add line #include <my_include. mqh> and try again.