Price at specific date and time problem. (ValueWhen() and DateTimeDiff())

Hey guys,
I want to record prices at specific time, for example 9:00 am. And on a specific date.
So I've created date and time parameters. Also checking if day starting time is matching the desired parameter with DateTimeDiff().

FirstDayOfTheMonth = ParamDate ("First Day", "2019-10-30");
StartTime = ParamTime( "Start Time",	"09:00:00",		format = 0 		);
DayStartCheck = DateTimeDiff( TimeNum(), StartTime );
printf("\nDebug1 " + ValueWhen((DateNum() == FirstDayOfTheMonth) , Open, 0));
printf("\nDebug2 " + ValueWhen((DayStartCheck == 0) , Open, 0));
printf("\nDebug3 " + ValueWhen((DayStartCheck == 0) AND (DateNum() == FirstDayOfTheMonth) , Open, 0));

As you can see I am printing 3 debug values. first two give me expected result with a price, but third one is 0.

Debug1 26472
Debug2 26472
Debug3 0

Why is this happening?
Thank you

DatetimeDiff expects DateTime number/array as function arguments but not TimeNum!
Carefully read docs here. It clearly says DateTime.

FirstDayOfTheMonth = ParamDate ("First Day", "2019-10-30");// DateNum
StartTime = ParamTime( "Start Time", "09:00:00", format = 0 );// TimeNum

dt_converted = DateTimeConvert(2, FirstDayOfTheMonth, StartTime);// returns DateTime
dt_diff = DateTimeDiff(DateTime(), dt_converted);// returns seconds!
5 Likes

Oh. Clearly I have missed that and blindly couldn't figure out what's wrong ^^' Have read the docs but missed the most obvious part.

Thank you!

For what it is worth, AmiBroker uses No-Nonsense naming convention for functions. If it did not occur to you yet, ALL functions that have "DateTime" prefix in a function name expect and work only on DateTime values. The same TimeFrame functions have TimeFrame prefix, string functions have Str prefix, Internet functions have Internet prefix and so on and so on. Without reading the docs it should be absolutely obvious that DateTimeConvert expects DateTime input.

3 Likes

To me date is date and time is time )))
DateTime actually was not that straightforward to understand (for me) just based on the name.
And there is no TimeNumDiff() function (while for DateTime there is for some reason). For experienced Amibroker coders everything in the manual makes perfect sense, but for me all this is not that easy =)

And I am super grateful for all the help and explanations you guys provide. So thank you

1 Like

How would you know the diference in time without knowing/informing the date ? DateTimeDiff returns seconds

My thinking was to compare times only within same day (didn't even occur to me that times will be compared on different days). For example in my case I wanted to check if current time 9:15 is bigger than 3:00.
The problem is solved and I got everything working thanks to the solution above =)