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);
- What do you expect it to do when you ask for a future time?
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.
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 ) ];
gooly: If you just want the last bar of a month you can simply use (not tested!!):
- Your code, as written, returns the last bar of last month.
- OP asked for a test of last bar of this month.
Ahh - ok, you are right, thanks, so he needs:
datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PeriodSeconds();
gooly: Ahh - ok, you are right, thanks, so he needs:
datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PeriodSeconds();
- Now you are calculating one bar after last month's last bar which is the first bar of this month.
- OP asked for a test of last bar of this month.
WHRoeder:
- Now you are calculating one bar after last month's last bar which is the first bar of this month.
- 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;
gooly: ohh- you're right again, I made it in haste - next try:
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."
datetime OpenTimeLastBarActMonth = Time[ iBarShift(_Symbol,_Period, iTime(_Symbol,PERIOD_MN1,0) - 1 ) ] + PERIOD_MN1*60;
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();
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 testbut even this seems to work on Dec, 5:
Yours | Mine |
---|---|
bool IsLastBarOfMonth(void){ Superfluous parentheses removed.datetime OpenTimeLastBarActMonth = StrToTime( Month() == 12 ? string(Year()+1) + ".1.1" : string(Year()) + "." + string(Month()+1) + ".1") -PeriodSeconds(); return Time[0] == OpenTimeLastBarActMonth; } | bool IsLastBarOfMonth(int iBar=0){ datetime currBar = Time[iBar]; datetime nextBar = currBar + PeriodSeconds(_Period); return TimeMonth(currBar) != TimeMonth(nextBar); } |
- You've finally got something that does (for bar zero) what mine does (any bar and efficiently :)
- Both are subject to the same limitation "works except over holidays and weekends."
- The other line may currently work but could stop with any release. StrToTime("YYYY.13.1") is definitely undefined behavior.
WHRoeder:
The other line may currently work but could stop with any release. StrToTime("YYYY.13.1") is definitely undefined behavior.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
I try to verify in the strategy tester if the actual bar
is the last bar in the actual month. I tried to write these two functions.
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!