# Getting overnight OHLC values

Hello,
I am trying to get OHLC values for overnight trading using 5 minutes intraday bars.

I am able to plot the individuals values using SparseCompress:

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn <= 151000;
x = SparseCompress( DaySession == 0, High ); // compact sparse data
y = SparseExpand( DaySession == 0, x ); // expand sparse data
Plot( y, "Overnight High", colorRed );
``````

But cannot move further. How can I get OHLC for overnight trading that I can use in the strategy?

Thank you.

I am not quite sure whether I am getting you...

Do you want to connect each overnight session by last y (see ValueWhen below)?
Or what do you want to do with variable y?

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn <= 151000;

x = SparseCompress( ! DaySession, High ); // compact sparse data
y = SparseExpand( ! DaySession, x ); // expand sparse data
Plot( ValueWhen( ! DaySession , y), "Overnight High", colorRed );
``````

Or is this what you want to do:

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn <= 151000;

O = SparseExpand( ! DaySession, SparseCompress( ! DaySession, Open ) ); // sparse data
H = SparseExpand( ! DaySession, SparseCompress( ! DaySession, High ) ); // sparse data
L = SparseExpand( ! DaySession, SparseCompress( ! DaySession, Low ) ); // sparse data
C = SparseExpand( ! DaySession, SparseCompress( ! DaySession, Close ) ); // sparse data

Plot( C, "Overnight OHLC", colorDefault, StyleCandle );

//RestorePriceArrays(); // uncomment if required
// and further code using non-sparsed OHLC here
``````

2 Likes

BTW, the same plot can be achieved using just Iif.

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn <= 151000;

cond = ! DaySession;
C = IIf( cond, C, Null);

Plot( C, "Overnight OHLC", colorDefault, StyleCandle );

//RestorePriceArrays(); // uncomment if required
//and further code using non-sparsed OHLC here
``````
1 Like

Thank you for the inspiration.

I was actually looking for highest high during night session.

Thus this works for me:

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn < 151000;

HD = HighestSince(DaySession, H);
x = SparseCompress( ! DaySession, HD  ); // compact sparse data
y = SparseExpand( ! DaySession, x ); // expand sparse data

Plot( ValueWhen( ! DaySession , y), "Overnight High", colorRed );
``````
1 Like

I found out that I am still not sure how to accomplish the calculation.

I am trying to get â€śhighest overnight Highâ€ť that I will use in following day session.

This works fine:

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn < 151000;

OH = HighestSince(DaySession, H);
OHigh = ValueWhen( ! DaySession , OH);

Plot( OHigh, "Overnight High", colorRed );
``````

But how do I get the prior value of OHigh in current session?
Thus I need for example highest hight in previous overnight session.

Thank you for any tip.

1. I changed OH variable from
``````OH = HighestSince(DaySession, H);
``````

to

``````OH = HighestSince(Ref(DaySession, -1), H);
``````

1. created prevOHigh variable
``````prevOHigh = Valuewhen( Daysession, OHigh, 2 );
``````

Full code:

``````tn = TimeNum();
DaySession = tn >= 083000 AND tn < 151000;

OH = HighestSince(Ref(DaySession, -1), H);
OHigh = ValueWhen( ! DaySession, OH );
prevOHigh = Valuewhen( Daysession, OHigh, 1 );

Plot( prevOHigh, "Prev Overnight High", colorRed, styleStaircase );
Plot( C, "Price", colorDefault, styleCandle );
Plot( DaySession, "", colorLightYellow, styleArea | styleOwnScale, Null, Null, 0, -1 );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
``````
1 Like

Or maybe this one is what you look for....

``````lookback = 2;// days lookback

SetBarsRequired( Max(1, lookback) * 86400 / Max(60, Interval()) );

startsession= tn == 083000;

DaySession = tn >= 083000 AND tn < 151000;

OH = HighestSince(Ref(DaySession, -1), H);
OHigh = ValueWhen( ! DaySession, OH );

prevOHigh = ValueWhen( startsession, OH, lookback );

Plot( prevOHigh, "Prev Overnight High", colorRed, styleStaircase );
Plot( C, "Price", colorDefault, styleCandle );
Plot( DaySession, "", colorLightYellow, styleArea | styleOwnScale, Null, Null, 0, -1 );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
``````
1 Like

Thank you fxhrat, the last code with lookback looks like the way I am looking for.

However it counts as the high value also the first bar in DaySession (if its high is the highest one).

Is there a way to address "a bar before startsession" without the need to write exact time (as the bar can be missing, or the code would be too much depended on tiimeframe)?

It does not include high of a daysession.

Look here

``````// get highest high since last daysession's ended
// so only look for highest high of "current" night session
// "night" session starts at start of currently set time -> 15:10:00
// Ref( ..., -1) starts to check for HHV at that first bar after daysession
OH = HighestSince(Ref(DaySession, -1), H);

// store that highest high of night session to new variable
// to also make its value available in next day session
// till there is a next night session to reset to (new) highest high
OHigh = ValueWhen( ! DaySession, OH );

// get night session HHV of a n-day look back of daysessions
// and reset to new HHV if there is new daysession start
prevOHigh = ValueWhen( startsession, OH, lookback );
``````

So there is no inclusion of daysessions' bars highs but only night session high values.

`startsession = tn == 083000;`

is just an example and yes it can be coded so that it would handle daysession's where 08:30:00 would be missing. But that is another story. This above one is simplest form of a session start flag (not handling missing timenum values).

2 Likes

Thank you so much for clarification. You are right, there is no inclusion of daysessions high. I made an misstake observing the chart.