Download MetaTrader 5
To add comments, please log in or register
Track all payments in your profile. It is easy!
user_123
172
user_123 2014.12.03 18:43 

Hello,

I try to verify in the strategy tester if the actual bar

bool LastBarInThisMonth = false;
LastBarInThisMonth = CheckLastBarInMonth(Time[0])

is the last bar in the actual month. I tried to write these two functions.

bool CheckLastBarInMonth(datetime MyTime){
   datetime MyTimeArray[];
   Print("Actual Bar: ", TimeToString(MyTime,TIME_DATE|TIME_MINUTES));
   datetime NextMonth = GetTimeOfFirstBarOfNextMonth(MyTime);
   Print("NextMonth Bar: ", TimeToString(NextMonth,TIME_DATE|TIME_MINUTES));
   ArrayInitialize(MyTimeArray,0);
   int Check = CopyTime(Symbol(),PERIOD_M1,NextMonth,2,MyTimeArray);
   Print("Check: ",Check);
   Print("MyTimeArray[1]: ", TimeToString(MyTimeArray[1],TIME_DATE|TIME_MINUTES));
   if(MyTime>=MyTimeArray[1]){
      return(true);
   }else{
      return(false);
   }
}


datetime GetTimeOfFirstBarOfNextMonth(datetime MyTime){
   int MyMonth = TimeMonth(MyTime)+1;
   int MyYear  = TimeYear(MyTime);
   
   if(MyMonth==13){
      MyMonth = 1;
      MyYear++;
   }
   
   string   sTimeOfNextMonth  = StringConcatenate(MyYear,".",MyMonth,".01");
   datetime dtTimeOfNextMonth = StringToTime(sTimeOfNextMonth);
   return(dtTimeOfNextMonth);
}


But it seems, like the CopyTime()-function does not work in the strategy tester? It copy always zero values.

Do somebody know if there is an another option to determine if the actual bar is the last bar in the month.

Thank you very much for your help!

whroeder1
13653
whroeder1 2014.12.03 19:10  
user_123: But it seems, like the CopyTime()-function does not work in the strategy tester? It copy always zero values.
  int Check = CopyTime(Symbol(),PERIOD_M1,NextMonth,2,MyTimeArray);
  1. What do you expect it to do when you ask for a future time?
  2. bool IsLastBarOfMonth(int iBar=0){
       datetime currBar = Time[iBar];
       datetime nextBar = currBar + PeriodSeconds(_Period);
       return TimeMonth(currBar) != TimeMonth(nextBar);
    }
    works except over holidays and weekends.
Carl Schreiber
5965
Carl Schreiber 2014.12.03 20:28  

If you just want the last bar of a month you can simply use (not tested!!):

datetime OpenTimeOfLastBarPrevMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ];
whroeder1
13653
whroeder1 2014.12.04 00:05  
gooly: If you just want the last bar of a month you can simply use (not tested!!):
  1. Your code, as written, returns the last bar of last month.
  2. OP asked for a test of last bar of this month.
Carl Schreiber
5965
Carl Schreiber 2014.12.04 08:14  

Ahh - ok, you are right, thanks, so he needs:

datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PeriodSeconds();
whroeder1
13653
whroeder1 2014.12.04 14:12  
gooly: Ahh - ok, you are right, thanks, so he needs:
datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PeriodSeconds();
  1. Now you are calculating one bar after last month's last bar which is the first bar of this month.
  2. OP asked for a test of last bar of this month.
Carl Schreiber
5965
Carl Schreiber 2014.12.04 14:44  
WHRoeder:
  1. Now you are calculating one bar after last month's last bar which is the first bar of this month.
  2. OP asked for a test of last bar of this month.

ohh- you're right again, I made it in haste - next try:

datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PERIOD_MN1*60;
whroeder1
13653
whroeder1 2014.12.05 13:06  
gooly: ohh- you're right again, I made it in haste - next try:
datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PERIOD_MN1*60;
Now you are computing the time the last bar of the month - assuming that every month is the same length (to the second.) There is a reason why Testing Features and Limits in MetaTrader 4 - MQL4 Articles (except for Bar zero) states "Weekly, monthly, and irregular timeframes are not tested."
Carl Schreiber
5965
Carl Schreiber 2014.12.05 13:46  

ok - again you're right - I'll try another one-liner  ;)

datetime OpenTimeLastBarActMonth = StrToTime(((Month()==12)?((string)(Year()+1)+".1.1"):((string)Year()+"."+(string)(Month()+1)+".1")) )-PeriodSeconds();

but even this seems to work on Dec, 5:

datetime OpenTimeLastBarActMonth = StrToTime((string)Year()+"."+(string)(Month()+1)+".1")-PeriodSeconds();
whroeder1
13653
whroeder1 2014.12.05 14:16  
gooly: ok - again you're right - I'll try another one-liner  ;)
but even this seems to work on Dec, 5:
Not a one liner since you still have to do the test
Yours
Mine
bool IsLastBarOfMonth(void){
   datetime OpenTimeLastBarActMonth = StrToTime( Month() == 12 ? string(Year()+1) + ".1.1" : string(Year()) + "." + string(Month()+1) + ".1") -PeriodSeconds();
   return Time[0] == OpenTimeLastBarActMonth;
}
Superfluous parentheses removed.
bool IsLastBarOfMonth(int iBar=0){
   datetime currBar = Time[iBar];
   datetime nextBar = currBar + PeriodSeconds(_Period);
   return TimeMonth(currBar) != TimeMonth(nextBar);
}
  1. You've finally got something that does (for bar zero) what mine does (any bar and efficiently :)
  2. Both are subject to the same limitation "works except over holidays and weekends."
  3. The other line may currently work but could stop with any release. StrToTime("YYYY.13.1") is definitely undefined behavior.
Carl Schreiber
5965
Carl Schreiber 2014.12.05 15:34  

WHRoeder:

The other line may currently work but could stop with any release. StrToTime("YYYY.13.1") is definitely undefined behavior.

Yes I realized that and I was surprised and amused that this is working - kind of fail saved behaviour?
/
To add comments, please log in or register