Unable to fetch the correct Previous Day close Price from 5 min Timeframe

OK thanks. Filtering through the view menu would help. (Better than changing the database settings all the time).
It would be cool though if in the future this could be done both at the same time: Full day view, and at the same time have daily values calculated according to trading hours.

You could also use SparseCompress() and SparseExpand() to filter out non-RTH bars while in 24hr mode.

For the missing closing bars, you can get a little more sophisticated with ValueWhen() so that you don't pinpoint a certain time, but look for a time that is greater than the time before the scheduled close and less than the time after the scheduled close. This way, if the market closes early like on a holiday the code would return the last price during RTH. This is obviously more complicated than simply using RTH filtering. It also adds complexity and CPU time to the charts.

The bar alignment issue is harder to tackle so I just avoid it.

Personally I use an exploration using daily bars and Tomasz's way of using day filtering and I save the prior day data, along with some other stats, as persistent static variables, so that my charts can look them up quickly. This way I calculate all my daily data once in the morning and my charts just look up the data all day, regardless of bar alignment, etc.

Just look at old threads .
It has been posted several times already.

daysback = 2;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

tn = TimeNum();
time_window = tn >= 93000 AND tn < 160000;
session_start = time_window AND NOT Ref(time_window,-1);

prev_high = Ref(HighestSince(session_start, H, 1), -1);
prev_high = ValueWhen(session_start, prev_high);

prev_low = Ref(LowestSince(session_start, L, 1), -1);
prev_low = ValueWhen(session_start, prev_low);

Plot( C, "Price", colorDefault, styleBar );

Plot(prev_low, "prev session LL", colorRed, styleStaircase);
Plot(prev_high, "prev session HH", colorGreen, styleStaircase);
1 Like

Thank you both for your help, I will look into it.

@fxshrat
It gives strange results. Partly because the 'switch' (when the line changes) happens at the opening bell, whereas it should switch at the end of the trading session (as soon as the High is a fact), not at the beginning.
After the opening, it gives as PDH the highest value of the preceding 24 hours, not the high of the previous RTH trading session. So at times this is a high occurring in the overnight (European day) session, not the high of the RTH trading hours.
At times, during the European day, it looks back two days, not one. (e.g.MESH4 feb 1 before opening), or it gives the high of the previous overnight session.
I tried changing a few things in your code, but so far I did not succeed.

@PeterD
So far, I could not construct anything workable with SparseCompress.

The best thing I can do is use Tomasz' way, using View>Filter in the morning, draw my lines, and then switch back to 24 hrs.

It is not strange. The code is supposed to draw the previous session's high and low but not high and low of current session! So it does not look into future.

So of course at the start of current session it starts to plot extremes of previous day's session.

But there was code missing in previous post:

for ( i = 0; i < 4; i++ ) {  
  field = StrExtract("O,H,L,C", i);
  VarSet(field, IIf(time_window, VarGet(field), Null));
}

Of another threads code

So updated code

daysback = 2;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

tn = TimeNum();
time_window = tn >= 93000 AND tn < 160000;
session_start = time_window AND NOT Ref(time_window,-1);

for ( i = 0; i < 4; i++ ) {  
  field = StrExtract("O,H,L,C", i);
  VarSet(field, IIf(time_window, VarGet(field), Null));
}

prev_high = Ref(HighestSince(session_start, H, 1), -1);
prev_high = ValueWhen(session_start, prev_high);

prev_low = Ref(LowestSince(session_start,Nz(L,1e9), 1), -1);
prev_low = ValueWhen(session_start, prev_low);

// PLOT SESSION PRICE ONLY
Plot( C, "Price", colorRed, styleBar );

RestorePriceArrays();// Restore Price arrays #############################################################

// PLOT ALL PRICE
Plot( C, "Price", colorDefault, styleBar );

Plot(prev_low, "LL", colorRed, styleStaircase);
Plot(prev_high, "HH", colorGreen, styleStaircase);

And here is picture where you can see that it plots previous dession high low at current session.

If you want to draw from previous session's end then you have to add variable session_end.

daysback = 2;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

tn = TimeNum();
time_window = tn >= 93000 AND tn < 160000;
session_start = time_window AND NOT Ref(time_window,-1);
session_end = NOT time_window AND Ref(time_window,-1);

for ( i = 0; i < 4; i++ ) {  
  field = StrExtract("O,H,L,C", i);
  VarSet(field, IIf(time_window, VarGet(field), Null));
}

prev_high = Ref(HighestSince(session_start, H, 1), -1);
prev_high = ValueWhen(session_end, prev_high);

prev_low = Ref(LowestSince(session_start,Nz(L,1e9), 1), -1);
prev_low = ValueWhen(session_end, prev_low);

// PLOT SESSION PRICE ONLY
Plot( C, "Price", colorRed, styleBar );

RestorePriceArrays();// Restore Price arrays #############################################################

// PLOT ALL PRICE
Plot( C, "Price", colorDefault, styleBar );

Plot(prev_low, "LL", colorRed, styleStaircase);
Plot(prev_high, "HH", colorGreen, styleStaircase);

Then it would look like this

2 Likes

This does the trick! Thank you very much! You made my day!
Your last code is indeed what I had intended.

I (experimentally, as a guess to be honest) added the PDC, in an attempt to solve the alignment problem with my own PDC code (see above).
I added this to your last code:

prev_close = Ref(C, -1);
prev_close = ValueWhen(session_end, prev_close);

Plus the according Plot line:

Plot(prev_close, "CC", colorBrightGreen, styleStaircase | styleNoRescale);

It worked, and it partially solved the alignment problem on 4min and 60min charts. Partially, as although the CC (=PDC) line does appear in the 4 and 60min charts, it just 'rounds' it to the nearest bar end. On a 60 min chart this can mean a 30min deviation from the real Close of that day.
Do you see a solution to this?