Using Datetime functions in strategy stester

 
Thanks for great tool in MT4.
One of the strategies I wrote uses detetime functions (like CurDate()), eg. I want to open new positions only between noon and 2 PM. Unfortunately it seems it does not work fine during backtesting. As I can see in Journal, CurDate() function returns real current datetime (date and time of running strategy tester) - not the one in the past. Is there a way I can test this kind of strategy ?

Simon
 
// Uses MetaTrader's demo server timeclock:

if ( (Hour()>=12) && (Hour()<=14) ) {
// code
}
 
// Uses MetaTrader's demo server timeclock:

if ( (Hour()>=12) && (Hour()<=14) ) {
// code
}



Thanks for the tip, however it is not so simple ... The starting and ending time the trade should be done is in expert's parameters in string format, eg. "13:38". During Init(), I set starting and ending time variables using conversion - "13:38" string converts into current date and given time, eg. "2005-03-01 13:38'. In backtesintg, converting gives me always real current date and given time, eg. "2005-07-03 13:38". Is this by design or a bug ?

Simon
 
During Init(), I set starting and ending time variables using conversion - "13:38" string converts into current date and given time, eg. "2005-03-01 13:38'. In backtesintg, converting gives me always real current date and given time, eg. "2005-07-03 13:38". Is this by design or a bug ?

there is bug. thanx
 
 
During Init(), I set starting and ending time variables using conversion - "13:38" string converts into current date and given time, eg. "2005-03-01 13:38'. In backtesintg, converting gives me always real current date and given time, eg. "2005-07-03 13:38". Is this by design or a bug ?

there is bug. thanx

one remark.
if "13:38" is written in the source code then compiler adds absent parts of the date/time.
so there is no bug. sorry for misunderstanding
 
During Init(), I set starting and ending time variables using conversion - "13:38" string converts into current date and given time, eg. "2005-03-01 13:38'. In backtesintg, converting gives me always real current date and given time, eg. "2005-07-03 13:38". Is this by design or a bug ?

there is bug. thanx

one remark.
if "13:38" is written in the source code then compiler adds absent parts of the date/time.
so there is no bug. sorry for misunderstanding


Slawa, I think there is a bug :-(
- suppose I want to open orders between 12:15 and 13:30, I run my expert with two extern properties: strStart with the value of "12:15" and strEnd = "13:30";
- when I run my expert in real mode (not backtesting), my init function calls intStart = StrToTime(strStart), so that "12:15" converts into current date and 12:15 PM, which is what I expect (later in the code I check if intStart >= CurTime() in order to find out if the expert should open position or not).
- now the clue, if I run the same code in strategy tester mode, the call intStart = StrToTime(strStart) converts strStart exacly the same way as it does in normal mode, so intStart holds the value of current date and 12:15 PM. For example, If I run strategy tester today with starting date 01-JAN-2004, intStart becomes integer representation of "04-07-2005 12:15" instead of "01-01-2004 12:15". Later on, the condition intStart > CurTime() always evaluates into false, because CurTime() works fine in backtesting and returns the right value (01-01-2004 00:00, 01-01-2004 00:01, 01-01-2004 00:02 etc)

This is why I think there must be a bug. Could you please comment on that ?



Simon
 
- when I run my expert in real mode (not backtesting), my init function calls intStart = StrToTime(strStart), so that "12:15" converts into current date and 12:15 PM, which is what I expect (later in the code I check if intStart >= CurTime() in order to find out if the expert should open position or not).
- now the clue, if I run the same code in strategy tester mode, the call intStart = StrToTime(strStart) converts strStart exacly the same way as it does in normal mode, so intStart holds the value of current date and 12:15 PM. For example, If I run strategy tester today with starting date 01-JAN-2004, intStart becomes integer representation of "04-07-2005 12:15" instead of "01-01-2004 12:15". Later on, the condition intStart > CurTime() always evaluates into false, because CurTime() works fine in backtesting and returns the right value (01-01-2004 00:00, 01-01-2004 00:01, 01-01-2004 00:02 etc)

no no no. today compiler makes "04-07-2005 12:15".
tomorrow compiled string will be "04-07-2005 12:15".
and in 3 weeks time it will be "04-07-2005 12:15"
use another way for time string compose
 
it cannot be compilation time only, as this string (strStart and strEnd) is passed as expert's parameter, during compilation the compiler does not know the string yet ...
What is more, it works FINE for normal operation, even if I continuosly run the expert for a week, it chages time in a right way.
Simon-
 
please expose your source. misunderstanding suspected
 
Theryou are:
//+------------------------------------------------------------------+
//|                                                     System.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005"
#property link      "http://www.com"
#include <stdlib.mqh>


//----- constants

//---- input parameters
extern double    dblLots=0.1;
extern string    strTimeStartAnalyzer="12:00";
extern string    strTimeEndAnalyzerFinish="13:30";

extern int       intDebugLevel=3;   




datetime intTimeStartAnalyzer;
datetime intTimeEndAnalyzerFinish;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- TODO: Add your code here.

  
intTimeStartAnalyzer = StrToTime(strTimeStartAnalyzer);
intTimeEndAnalyzerFinish = StrToTime(strTimeEndAnalyzerFinish);


// testy
   ErrorLog("Init: TimeStartAnalyzer= " + TimeToStr(intTimeStartAnalyzer), 3);   
   ErrorLog("Init: TimeEndAnalyzerFinish= " + TimeToStr(intTimeEndAnalyzerFinish), 3);
// koniec testow 
  
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
  ErrorLog("Start: CurTime()= " + TimeToStr(CurTime()), 3);
  ErrorLog("Start: intTimeStartAnalyzer= " + TimeToStr(intTimeStartAnalyzer), 3);
  ErrorLog("Start: intTimeEndAnalyzerFinish= " + TimeToStr(intTimeEndAnalyzerFinish), 3);


  if(CurTime() > intTimeStartAnalyzer && CurTime() < intTimeEndAnalyzerFinish) 
  {
  // Open transactions if other conditions are

  // BUG shows HERE: never runs this code in backtesting ..... :-(
  }
   return(0);
}
//+------------------------------------------------------------------+


int ErrorLog(string strTresc, int intLevel )
{
   if (intLevel <= intDebugLevel)
   {
      Print(strTresc);
   }  
}
Reason: