# Accessing Previous Days data of Particular Time Interval

Hi,

I am writing an AFL where i want to access previous day’s first 5 minutes OHLC values. I will be running this AFL in 5 minutes timeframe. Any help related to this would be appreciated.

Thanks,
Vinay

@vinaykumar perhaps try this,

``````dn = Day();
newDay = dn != Ref( dn, -1 );

FirstBarOpen = ValueWhen( newday, Open );
FirstBarHigh = ValueWhen( newday, High);
FirstBarLow = ValueWhen( newday, Low );
FirstBarClose = ValueWhen( newday, Close);
``````

I think the solution from @portfoliobuilder will get you the OHLC values from the first bar of the current day. To get the values from the first bar of the previous day, I would add something like this untested snippet to his code:

``````dailyFirstBarOpen = TimeFrameCompress(FirstBarOpen, inDaily);
prevDayFirstBarOpen = TimeFrameExpand(Ref(dailyFirstBarOpen, -1), inDaily, expandFirst);
``````

Of course, you would need to repeat that for each value of interest, i.e. you would have a total of four sets of lines like the ones above.

2 Likes

@mradtke thanks! I missed the part where he was asking for yesterday's value, so I think my solution could be slightly modified. He mentions working in 5 minute bars intraday so I did not use any MultiTimeFrame functions in the solution.

``````dn = Day();
newDay = dn != Ref( dn, -1 );
// today's first bar values
FirstBarOpen = ValueWhen( newday, Open );
FirstBarHigh = ValueWhen( newday, High);
FirstBarLow = ValueWhen( newday, Low );
FirstBarClose = ValueWhen( newday, Close);

// yesterday's first bar values
YesterdayFBO = ValueWhen( newday, Open, 2 );
YesterdayFBH = ValueWhen( newday, High, 2);
YesterdayFBL = ValueWhen( newday, Low, 2 );
YesterdayFBC = ValueWhen( newday, Close, 2);
``````
5 Likes

Thanks @portfoliobuilder. The trick was passing 3rd argument to ValueWhen function. That worked.

i have a similar query. i want to read the value of yesterdays volume for the same candle time as now for the same time frame. So say i am exploring in the 5 min tf, and thus the value of the current bar of 5 mins is
`AddColumn(V,"TV",1.2); AddColumn(TimeNum()/100,"T",1);`

Now i want to read yest volume for the same candle time.
further i want to read the same value for the past 3 days, make an average of them and find the relative volume strength of today. i can do the rest but am unable to initiate it.

Only users with "Verified Badge" are allowed to post on this forum.

Done that. Thanks for informing.

2 Likes

You could take a look at the SparseCompress() function. However, you'd have to call it for each unique TimeNum() value if you wanted to end up with an array of average values.

@maxis if you are looking for the same bar intraday, instead of using "time" you could use the bar number for that day.

So for example if you want to look at the 3rd bar everyday,

``````dn = DateNum();
newday = dn != Ref(dn,-1);
id_bar = BarsSince( newday ) + 1; // Intra-Day bar number

Bar3 = id_bar==3;
``````

A quick method of finding volume on "bar 3"

``````Bar3Vol_a = ValueWhen( Bar3, V );
Bar3Vol_b = ValueWhen( Bar3, V, 2 );
Bar3Vol_c = ValueWhen( Bar3, V, 3 );
``````

But if you want more flexibility then pehaps use @mradtke idea of SparseCompress. I'm not too familiar with SparseCompress so I suggest you double check this usage.

``````///@link https://www.amibroker.com/guide/afl/sparsecompress.html
x = SparseCompress( Bar3, Volume ); // compact sparse data
y = MA( x, 3 );  // 3 bar ave of Vol on bar 3
y = SparseExpand( Bar3, y );
``````

He wrote same time. Not same bar.

So using bars since start of day is flawed (especially on very short intervals) since there may be timenums missing -> and as we know assuming that all bars exist all the time is bad.

Also (once again) reminder... if there is repetitive code then we iterate.
Done this years ago already, BTW. 