Weird exporting data results

I have code below to write EOD data to a file. Two weird results:

  1. Sometimes writes only to correctly named file. But other times adds a second, shorter file, with different first date.
  2. Correctly dated file has dates wothout leading zeros which make confuses my exel type software. Tried FormatDateTime, but that creates wrong dates. Could use a "FormatDate" only.
    Thanks in advance for any help.
    210223 Export files dates_002
    210223 Export files dates
// General Fluff
#pragma nocache
_TRACE("!CLEAR!"); // this clears the internal log window
Filter = 1;  // rely on Analysis Filter
Buy = Sell = 0;
Short = Cover = False; // long only strategy for now
SetTradeDelays(0,0,0,0);  // day to make trades
dn = DateNum();
tn = TimeNum();

fileName = NumToStr(dn[0]%1000000, 1.0, False) + " - "
 + NumToStr(dn[BarCount -1]%1000000, 1.0, False) + " " + Name() + ".csv";
filedeleted = fdelete(fileName);
if(Status("stocknum") == 0){
	fh = fopen(fileName,"w");
		fputs("Symbol,Date,Open,High,Low,Close,Aux1,Volume\n", fh);
fh = fopen(fileName,"a");
	for(i = 0; (i < BarCount); i++){
		if( dn[i] <= 1210122){
			Line = Name() + "," + NumToStr(dn[i]%1000000,  6.0, False) + 
			fputs(Line, fh);

I don't think those are weird results. DateNum() is working just as it should.
The leading Zero's will not be in the number.

There was a reason ISO date formatting became popular too.
So instead of "wrestling" with DateNum() as Tomasz says :slight_smile:
use DateTimeFormat( ''formatstr'', datetime ) and replace DateNum() with DateTime()

The DateTimeFormat function in manual allows you to get a variety of formatting that you need.

Even within Excel, DateNum may become an issue. If you use %s and a standard format, Excel can interpret date properly.


Your suggestion was right on. Using DateTimeFormat() and DateTime in place of DateNum() in all instances did the trick. I previously spent hours searching for other Date functions using the Search feature in Help. Problem with that is you have to know exactly what you are looking for and how to spell it.

You need just this link and the corresponding page in the manual.

1 Like

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