Getting Close Value on Specific Date and Time

I’m trying to get close value from specific date and time.

Let’s say close value on 2017-05-31 at 09:00:59AM. I’m using below syntax but the output is nothing.
myVal1 = ValueWhen( DateNum() == 1170531 AND TimeNum() == 090059, Close );

It only works when I switch the back tester periodicity to Minute (instead of Day).

But if I switch the periodicity to Minute, my exploration result will have multiple lines for (each minute), which not that I expect to output. I expect the output to be single line only, with the column showing the close value on specific date and time.

Any guidance will be appreciated.


Could check what you get with the following snippet and different TF :


The above code is not a good test for time. Maybe for the date.


Hi Reinsley,
Sorry if I didnt explain clearly. I’m doing exploration, not charting.


Like this...

If periodicity set to Minute, I can get the output, but with multiple lines - thing that I dont expect.

The idea was to do an easy check on a chart then see what happen in the BT. IMHO.
Anyway my code was not the best for a test. I’ll remove it.
You may try this one.

// Date array formatted yyyymmdd hh:mm:ss
// Array of Date in "yyyymmdd" format, then array converted to String
// select a range with clubs
// open interpretation windows

SetChartBkGradientFill( colortan, colortan ) ;

function DateNumToStr( dn )
    return WriteIf( dn >= 1000000, "20", "19" ) + StrFormat( "%06.0f", dn % 1000000 );

_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} \nOp %g, \nHi %g, \nLo %g, \nCl %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorWhite ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
dn = DateNum();
tn = TimeNum();

bdn = BeginValue( dn );
edn = EndValue( dn );
btn = BeginValue( tn );
etn = EndValue( tn );

if( edn == LastValue( dn ) && bdn == dn[ 0 ] )
    printf( "no range selected" );
    begstrd = DateNumToStr( bdn );
    endstrd = DateNumToStr( edn );
    begstrt = DateNumToStr( btn );
    endstrt = DateNumToStr( etn );

    printf( begstrd + "\n" );
    printf( endstrd + "\n" );
    printf( begstrt + "\n" );
    printf( endstrt + "\n" );
    printf("Start ="+WriteVal(BeginValue(DateTime()),formatDateTime)+"\n");

Would mind to post the concernig part of your code ?

Hi Reinsley,
Like I mentioned on my first post, below is the code. And it only works if I set periodicity to minute, which I dont want (because it will generate multiple minute lines). I want to have single line (means periodically to Daily, not Minute) but still can output Close value on specific date and time specified by DateNum and TimeNum.

myVal1 = ValueWhen( DateNum() == 1170531 AND TimeNum() == 090059, Close );


Of course you can get one line only on 1-minute.
Problem is: your exploration code is wrong (in regards to expectation). That’s why you get what you don’t expect.

And for the record: you can’t output minute data on interval higher than 1-minute.
So yes, you have to use 1-minute TF if you search for 1-minute bars.

BTW, AFAICS in your picture you seem to be looking for price of each hour of day (hourly Close?). So based on your presented picture you wouldn’t even need to use 1-min interval but just hourly interval.

Set periodicity to 1minute and insert


he seems to be looking for a table similar to below one (but just with prices) showing one row per day and columns being each hour of the day.

See Getting Close Value on Specific Date and Time

1 Like

It becomes pretty obvious why this is so, when you realize that in your code you are referring to minutes (in TimeNum() == 090059 condition ). Daily bar does not have “minutes” so TimeNum() returns zero and this equality check is not true.

If you want exploration to show you the close at specific date and time, you should use 1-minute periodicity and the following code:

// want single line for 2017-05-31 9:00:59
Filter = DateNum() == 1170531 AND TimeNum() == 090059; 
AddColumn( Close, "Close");

alternatively you can use

Filter = DateTime() == _DT("2017-05-31 9:00:59");
AddColumn( Close, "Close");

The _DT() function is shortcut for StrToDateTime().