Separating Date and Time in Explorations

Quick question: How can we separate the "Date/Time" column in "Date" and "Time"?

We have this:

Filter = 1; 
SetOption( "NoDefaultColumns", True );
AddTextColumn(Name(),"Symbol");
AddColumn( DateTime(), "Date/Time", formatDateTime);
AddColumn(C,"Close");

Returning this:

|Symbol|Date/Time|Close||
|AAPL|9/4/2018 0:15|218.24||

And we want this output:

|Symbol|Date|Time|Close|
|AAPL|9/4/2018|0:15|218.24|

Since you are aware of

AddTextColumn()

You can pass the DateTime() Array to the function DateTimeFormat()
https://www.amibroker.com/guide/afl/datetimeformat.html

AddTextColumn( DateTimeFormat( "%x", LastValue( DateTime() )) , "Date");

You can customize the string in anyway you want.
I leave the Time part to you.

5 Likes

Thanks travic for the reply.
But if we use LastValue(DateTime()) it will be returning always the same (last) date.

Or am I missing something?

Well, that was a sample code highlighting about DateTimeFormat()

If you want Array, you can use the BarIndex() of a DateTime() variable

1 Like

@Nick

you may want to test this snippet:

dt = DateTime();
dn = DateNum();
dates = DateTimeConvert( 2, dn, Null );
hh = DateTimeConvert( 5, dt );
mm = DateTimeConvert( 4, dt );
ss = DateTimeConvert( 3, dt );
Filter = 1;
AddColumn( C, "Close" );
AddColumn( dates, "Date", formatDateTime );
AddColumn( hh, "HH", 1 );
AddColumn( mm, "MM", 1 );
AddColumn( ss, "SS", 1 );
// You may fake it as a decimal number (if you drops seconds)
hhmm = hh + ( mm / 100 );
AddColumn( hhmm, "HH.MM", 1.2 );

Unfortunately, as you see the time part is split into HH|MM|SS columns or you may "fake" it as I did if you do not need to display the seconds.
Probably there is a better way, but right now I'm missing it!

5 Likes

Thanks Beppe for your reply.
Doesn't seem to match the Date/Time column:
abdatetime

BarIndex? Built in? Regular expressions?
How can we do this?

@Nick, here it seems to work:

immagine

I suppose you have an offset in your database Intraday settings:

immagine

In any case, let's see if someone else with more experience will suggest a better way to do it!

2 Likes

That fixed it. Thank you very much..

I wonder if this can be done with regular expressions.

What you want to do (a single historical formatted time column) can only be done via AddRow

Version( 6.20 );

nm = Name();
dt = DateTime();
bir = Status( "barinrange" );

Filter = 0; 

SetOption( "NoDefaultColumns", True );

AddTextColumn( "", "Ticker", 1, -1, -1, 60 );
AddTextColumn( "", "WKD", 1, -1, -1, 40 );
AddColumn( Null, "Date", formatDateTimeISO, -1, -1, 80 );
AddColumn( Null, "Time", 1, -1, -1, 60 );
AddColumn( Null, "Price", 1, -1, -1, 70 );

for( i = 0; i < BarCount; i++ ) {
	if( bir[i] ) {
		dtstr = DateTimeFormat("\t%a\t%Y-%m-%d\t%H:%M:%S", dt[i]);
		valstr = StrFormat("\t%g", C[i]);
		AddRow( nm + dtstr + valstr );
	}
}

727


Other than that you may use TimeNum() if you do not care for formating.

Filter = 1; 
AddColumn( TimeNum(), "Time", 1, -1, -1, 60 );
AddColumn( C, "Price", 1.2, -1, -1, 70 );
5 Likes

@Nick, please note that if you had a TimeShift in your settings, changing it to zero will change your charts date/time axis references.

To add a string to explorations, you usually employ AddTextColumn(). Since this function takes a single string as a parameter, using it, you can only display a text that does NOT vary on bar-by-bar basis

On the other hand, AddMultiTextColumn() offers the possibility to display different text strings based on the underlying array.

If you like it, you can add a time string column ("HH:MM") adding the following code to my previous snippet (this will work if you use intraday exploration intervals >= in1minute):

// Add this code to the above exploration
interv = Interval(); // this is the interval for the exploration

if( interv >= in1Minute AND interv < inDaily )
{
    seconds = hh * 3600 + mm * 60 + ss;
    // AddColumn( seconds, "Seconds" );
    index =  seconds / interv;
    // AddColumn( index, "#List index", 1 );

	// create a textList for AddMultiTextColumn()
    textList = "";
    hhh = 0;
    mmm = 0;
    sss = 0;
    while( hhh < 24 )
    {
        hhhmmm = StrFormat( "%02.0f:%02.0f", hhh, mmm );
        textList += hhhmmm + "\n";
        sss += interv;
        mmm = sss / 60;

        if( sss >= 3600 )
        {
            hhh += 1;
            mmm = 0;
            sss = 0;
        }
    }

    AddMultiTextColumn( index, textList, "HH:MM" );
}

Since this code generates a very long "textList" string, IMO it is better to use it only for an interval above or equal to 5 minutes.

AddRow(), as demonstrated in the previous post, is a good alternative if you want more flexibility in your output.

This code will work only with intervals that add to 60 (1, 5, 15, etc.)

2 Likes

Question is why would you want to do that? If you wanted this to re-import data, then there is no reason to do, as ASCII importer allows "Comma or space" as separator and that works perfectly for fields that have date and time separated by space.
image

More info:
http://www.amibroker.com/guide/d_ascii.html
http://www.amibroker.com/guide/w_impwizard.html

4 Likes

@Tomasz You read my mind. Is is exactly that.

I was wondering why to import data, the date had to be separated from the time (picked in different columns) and then to "export" (via exploration) there is only a datetime column.

As for the refunds, he may be crazy. :slight_smile:

Thanks @beppe and @fxshrat very much for your detailed solutions.

Using AddRow just for that is already slightly hilarious enough. But using AddMultiTextColumn being 200% slower than anything else on tens of thousands of bars is just ridiculous. Some people have too much time on their hand for wasting time.

What about this quote:

No need to reinvent the wheel!

1 Like

I agree 200%. Probably using it on thousand of bars makes no sense.
But maybe someone could use it just on the current stock for the last few days on a 15min interval. Who knows!

(Moving the "build textList" inside a condition where stocknum == 0, to create the textList only once, could improve the speed).

The nice thing about AmiBroker is that anyone can use it as they like!

1 Like

The 11th post of the thread:

@nick - This is a good example showing, that explaining in n-th post of the thread (not the first one) why do you actually need some solution is a bad idea. As you can see, mind reading abilities were needed to decipher what's inside your mind ... If you had described the true reason of your request in the initial post, instead of just writing:

... you would have probably received the proper solution in the second or third post and there would be no need for other users to spend their time on coding such complex examples.

Explaining in the initial post the true reason of the request is very important (or even crucial), because it usually turns out, that something can be achieved in another, simpler and better way, but no one can suggest a proper (or optimal) alternative without knowing the wider context.

4 Likes

Now dear @Milosz you read my Mind! :smiley:
Day before TJ has cited same issue with one of the posts under IBController as well.

I just saw how the whole thread unfolded, and there are many scripts for exporting data.
To start with @Nick had to use the most popular approach which would have been straight forward.
There is an article in the KB.

Here is a modified version of the old SCAN exporter.
We don't need to rely on the UI of AA window for bars.
Directly run this AFL from AB or (via APX) from OLE coupled with other code to Auto Export data, Delete the bars and import new format data in a flash.

// Convert EoD bars to IEoD
// or part of script to replace Intraday Data with one IEod bar
// Periodicty in APX/AA is set to daily

// Set the Dates: From and To Range for Export
dtStart = StrToDateTime("2018-Aug-31 00:00:00");
dtEnd  =  StrToDateTime("2018-Aug-31 23:59:59");

// Uncomment to Create Folder
// fmkdir( "C:\\DataExport\\" );

fh = fopen( "C:\\DataExport\\20Dump_" + Name() + ".txt", "w" );
// Dont use a single file, R/W issue and file Access issues will arise due fast multithread nature of AB.

// later Run Shell Commands like "COPY /b 20Dump_*.txt Master.txt" to concatenate all data into one file
// and "delete >DEL 20Dump_*.txt"
// or add them to your import script.

if ( fh )
{
    dt = DateTime();
	
	// Required only for HEADERS
	// fputs( "Ticker,Date_YMD,Open,High,Low,Close,Volume,Time\n", fh );
    
	for ( i = BarCount - 1; i > 0; i-- )
    {
		if(i >= 0 )             // Or Custom Conditions here to exclude/include Symbol etc
		{					
			if (DateTimeDiff(dt[i], dtStart ) > 0)
			{		
				if (DateTimeDiff(dt[i], dtEnd ) <= 1)                             // 1 here to include Last Day
				{
                                        // some variables for readability

  			                qa = DateTimeFormat( ",%Y%m%d,", dt[i] );
				        qs = StrFormat( "%g,%g,%g,%g,%.f", O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
			          	fputs(Name() + qa + qs + ",15:59:00\n", fh );
				}
			}
			else
			{
				break;
			}
		}
    }
	fclose( fh );
}
Buy = 0;	// line required by SCAN option

EDIT: For those not familiar with reading twice, the above script is only intended tp demonstrate what I do for my need. NOTE: I have hardcoded time component for my need to convert daily EoD bars to IEoD.

3 Likes

I want this type of codding for very my exploration. I explain you, what i want or what face problem in Exploration.

Every time when i run my any exploration on daily time frame, it's shows just 1 time on that particular day.

Actual I want, when i run any exploration, it's shows Actual Time (when I run that afl)
i'm attaching screenshot...

Exploration

Hello @beppe
Be my guest :smiley: :smiley:

Hi Hirenvyas,

I am also looking for something similar... Did you get any help from seniors? Can we both connect? pls let me know...

Thanks in advance

Hi All,

By default ,on explorer, Date/Time column appears as default column and occupies lengthy space, is it possible hide this Date/Time column or trim the column length to show only time especially for Intraday.

Thanks in advance