Last bar in the month
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 ) ];
- 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();
- 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.
- 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;
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();
but 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.
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!