Getting the proper date with DateTime() and DateNum()


I am having a hard time with things that I expected to be normally simple. I guess I made an obvious mistake, but which one ?

On the database I have a single symbol with 1mn bars starting from date 28/09/2009 to a recent date.
I just cannot extract the date of the very first bar with the following code:

SetBarsRequired(sbrAll, sbrAll);

dn = DateNum();
currentDate1 = dn[0];
StringDate1 = DateTimeToStr(dn[0], 1);

dt = DateTime();
currentDate2 = dt[0];
StringDate2 = DateTimeToStr(dt[0], 1);

StringDate1 variable holds "04/06/2013"
StringDate2 variable holds "23/01/2012"

I expected "28/09/2009" in both cases. Removing line SetBarsRequired(sbrAll, sbrAll); does not change the result.

The string StringDate1 corresponds to the numerical value dn[0] when calculated with the appropriate formula (10000 * year + ....). This seems to imply that the string conversion is correct but the original numerical value is not.

Status("action") == actionCommentary code section displays the correct value with for example:
printf("DateNum: " + NumToStr(dn[selbar], 1)); when moving the cursor position on the chart display

I am aware of the fact that the "DateTimeToStr()" function only works with DateTime() arrays (old topic on Amibroker Forum).
However thats the numerical value (before string conversion) that is not correct.

And, as already said above, the numerical values as well as the corresponding string conversions seem to be correct for DateNum() when pointing on the chart display.

1 Like

DateNum and DateTime are different functions and return different things.

DateNum returns human-readable integer value representing DATE only (without time) in the format YYYMMDD, where YYY is (year - 1900). So 2024-01-13 (January 13, 2024) is 1240113

DateTime returns PACKED BINARY, NOT HUMAN READABLE date time that holds both date AND time but in packed numeric format that is NOT readable.

Since DateTime numbers are NOT HUMAN READABLE that is why DateTimeToStr exists. As its name says DateTimeToStr takes DateTime argument, NOT DateNum.

You MUST NOT use DateNums as input to DateTimeToStr.

Therefore these lines are wrong:

dn = DateNum();
currentDate1 = dn[0];
StringDate1 = DateTimeToStr(dn[0], 1); // WRONG! Never ever use DateTimeToStr with DateNum as input

I wrote many many times, that DateTime is NOT a plain number. It is packed binary to fit large amount of information (both date and time) into limited number of bits. Its "numerical value" has NO MEANING to human. The only way to get human readable date from DateTime is to use DateTimeToStr.

The documentation clearly says:

It is important to understand that DateTime is not a simple number but rather bitset

1 Like

Thanks Tomasz and sorry to waiste your time as it seems I have some difficulties writing clearly and concisely.
I did not even post relevant code, which does not help. Sorry.

The problem is not about converting to strings. I know how DateNum() and DateTime() work.
The problem is that the date returned by either function (numerical form) does not correspond to the very first bar.
The date of the first bar in the database corresponds to 28 september 2009 (I have not used a string here in order to avoid confusion). Whether or not I use "SetBarsRequired(sbrAll, sbrAll)", I still get the same date that is 23 april 2012 (not a string again, no string conversion done in AFL).

However after having been pulling my hair for some time, I have found that the code works properly when AFL execution is triggered from the Parameter window.
I already found that it also works when code execution is triggered when selecting a bar on the chart with the mouse (Status("action") == actionCommentary code block).

So what happens in debugging mode ? Did I make anything wrong here ? Or are there some limits in this mode that I am not aware of ?

@Quark, I guess you have to change the default "debugger" settings in the Preferences dialog.

See also this previous thread to better understand how the debugger works.

1 Like

Everything works 100% perfect.

You have WATCH window available in debugger for reason. You should use it.
If you used it as described in manual you would quickly find that debugger does NOT use million bars because it is nonsensical (like single stepping thru million bars).
For this reason debugger uses SMALL number of bars so you can actually debug. As @beppe wrote - there is a setting for that
In the WATCH WINDOW you would see the contents of variables and you would see the array contents in the "Arrays" tab.

But before anything else, YOU MUST READ THE MANUAL:

EVERYTHING is there.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.