Previous Day Close is different from Daily TimeFrame to 3 min TimeFrame

Hi,
I am getting different Previous Day Close values in Daily Time Frame (25388.90) and 3 minute TimeFrame(25313.85). The correct previous day close is 25388.90 i.e the value i am getting in Daily TimeFrame. How to get this value in 3 minute timeframe also. Below is the code

newday = Day() != Ref(Day(),-1) ;
pdc = ValueWhen(newday,TimeFrameGetPrice("C",inDaily,-1)) ;
pdc = IIf(TimeNum()<starttime,Null,pdc); //redefining the PDC
Plot(pdc,"PDC",colorBlack,styleThick);

Please help me regarding the correct previous day close value.

If you are detecting new day the way you do, you don't need TimeFrameGetPrice. ValueWhen already gets what you need and now you are essentially doing it twice. So

lastinday = Day() != Ref( Day(), 1 );
pdc = ValueWhen(lastinday, Close) ; // this is already Close price at day boundary

Isn't this returning the close of the first 3m candle of the new day? (from the candle that starts at minute 0).

Assuming the base interval is 3m.

Yes. its returning close of the first 3 min candle of the new day.

The below is the complete code, Previous Day High, Previous Day Low giving exact value only previous Day Close

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));



bartime = Status( "lastbartimeleft" ); 
mts  = int(bartime / 60);
sds = int(frac(bartime / 60) * 60);
if ( bartime > 0 ) {
  txt = StrFormat( "%g:%02.0f", mts, sds);
  x = Status("pxchartright");
} else {
  txt = "0.00";
  x = Status("pxchartright") - GfxGetTextWidth(txt);
}

Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle());
GfxSetCoordsMode(2);
GfxSetBkColor(ColorBlue);
GfxSetTextColor(ColorWhite);
GfxSelectFont("Tahoma", 14, 800 );
GfxTextOut(txt, x, LastValue(C));



printf("Bismillaahi Avvalahu Wa Akhirahu\n");

//Common variables

Starttime = TimeNum() > 093000 ;
endtime = TimeNum() < 150000 ;
EODsquareoff = TimeNum() == 150000 ;
newday = Day() != Ref(Day(),-1) ;





orbperiod15=15;
orbperiod30=30;
orbh15 = ValueWhen(newday,TimeFrameGetPrice("H",in1Minute*orbperiod15,0)) ;
orbl15 = ValueWhen(newday,TimeFrameGetPrice("L",in1Minute*orbperiod15,0)) ;
orbl5 = ValueWhen(newday,TimeFrameGetPrice("L",in1Minute*5,0)) ;
orbh5 = ValueWhen(newday,TimeFrameGetPrice("H",in1Minute*5,0)) ;

orbh30 = ValueWhen(newday,TimeFrameGetPrice("H",in1Minute*orbperiod30,0)) ;
orbl30 = ValueWhen(newday,TimeFrameGetPrice("L",in1Minute*orbperiod30,0)) ;


//orbl15 = ValueWhen(newday,TimeFrameGetPrice("L",in1Minute*15,0)) ;

pdh = ValueWhen(newday,TimeFrameGetPrice("H",inDaily,-1)) ;
pdl = ValueWhen(newday,TimeFrameGetPrice("L",inDaily,-1)) ;

pdc = ValueWhen(newday,TimeFrameGetPrice("C",inDaily,-1)) ;

printf("  pdc  %g\n",pdc);


orbh15 = IIf(TimeNum()<starttime,Null,orbh15); //redefining the ORBH value
orbl15 = IIf(TimeNum()<starttime,Null,orbl15); //redefining the ORBL value
orbl5= IIf(TimeNum()<starttime,Null,orbl5); //redefining the ORBL value
orbh30 = IIf(TimeNum()<starttime,Null,orbh30); //redefining the ORBH value
orbl30 = IIf(TimeNum()<starttime,Null,orbl30); //redefining the ORBL value

pdh = IIf(TimeNum()<starttime,Null,pdh); //redefining the PDH
pdl = IIf(TimeNum()<starttime,Null,pdl); //redefining the PDL
pdc = IIf(TimeNum()<starttime,Null,pdc); //redefining the PDC
LVBI = LastVisibleValue(BarIndex());


Plot(orbh15,"15 Min HIGH",colorRed,styleThick);
Plot(orbl15,"ORBL15",colorGreen,styleLine);
Plot(orbl5,"ORBL5",colorBlue,styleDashed);
Plot(orbh5,"ORBH5",colorBlue,styleDashed);
Plot(orbh30,"ORBH30",colorRed,styleThick);
Plot(orbl30,"ORBL30",colorGreen,styleThick);
Plot(pdc,"PDC",colorBlack,styleThick);
//Plot(pdh,"PDH",colorRED,styleThick);
//Plot(pdl,"PDL",colorGreen,styleThick);

//Plot(pdh,"PDH",colorOrange,styleDashed);
//Plot(pdl,"PDL",colorBrown,styleDashed);

//Plotting Text on HIGH LOWS

PlotTextSetFont("   15 Min HIGH   ", "Tahoma", 10,  LVBI+5 , SelectedValue(orbh15), colorWhite, colorRed, -10);
PlotTextSetFont("   15 Min LOW   ", "Tahoma", 10,  LVBI+5 , SelectedValue(orbl15), colorWhite, colorGreen, 10);
PlotTextSetFont("   5 Min LOW   ", "Tahoma", 10,  LVBI+5 , SelectedValue(orbl5), colorWhite, colorGreen, 10);
PlotTextSetFont("   5 Min HIGH   ", "Tahoma", 10,  LVBI+5 , SelectedValue(orbh5), colorWhite, colorRed, 10);

PlotTextSetFont("   30 Min HIGH   ", "Tahoma", 10,  LVBI+5 , SelectedValue(orbh30), colorWhite, colorRed, 5);
PlotTextSetFont("   30 Min LOW   ", "Tahoma", 10,  LVBI+5 , SelectedValue(orbl30), colorWhite, colorGreen, -10);
PlotTextSetFont("   Prev Day CLOSE   ", "Tahoma", 10,  LVBI+5 , SelectedValue(pdc), colorWhite, colorBlue, 0);
//PlotTextSetFont("   Prev Day HIGH   ", "Tahoma", 10,  LVBI+5 , SelectedValue(pdh), colorWhite, colorBlue, 0);
//PlotTextSetFont("   Prev Day LOW   ", "Tahoma", 10,  LVBI+5 , SelectedValue(pdl), colorWhite, colorBlue, 0);

Plot(EMA(C,21),"EMA-21",colorBlue,styleLine|styleThick);
Plot(EMA(C,200),"EMA-200",colorBrown,styleLine|styleThick);




_SECTION_END();

Please find attached screen shots for your reference


1 Like

Thanks @firozstar !

@Tomasz kindly help. only previous day close is differing on multiple time frames. (Daily time frame PDC value is correct). how to get correct PDC in 3 min time frame?

It depends what you consider "daily close".
Because it can mean different things to different people especially when you do / do not have after hours quotations
and when you do / do not do the filtering.
Depending on filtering and presence/absence of after hours data

// last bar of previous day
X = ValueWhen( Day() != Ref( Day(), 1 ), Close );

it may mean:

  • actual previous day close
  • the close of last after hours close of the previous day
  • any other intraday close bar of previous day depending on filtering

You really need to read the docs:

and as to this:

This returns last bar of previous day. The parameter if Ref should be PLUS one, not minus.

X = ValueWhen( Day() != Ref( Day(), 1 ), Close );
1 Like

@firozstar, maybe I don't quite understand what your problem is, but I wonder why you are using a different "shift" parameter in the different lines where you use the TimeFrameGetPrice() function?

Maybe for the 3 minute Close, you should try:

orbC3 = ValueWhen( newday, TimeFrameGetPrice( "C", in1Minute * 3, -1 ) ) ;

My point was that to do what you are after you should use TimeFrameGetPrice OR ValueWhen, NOT both.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

1 Like

Please find attached screen shots, why Previous Day close is different for Daily Time Frame and 3 Min Time Frame. It should be same right?


Thanks! I did miss the 1!


Changing End time to 15.35 from 15:30 in "Database - Intraday Settings" has given the correct previous day close.

Not sure this is the right solution.

Thanks @Tomasz , @beppe and @DBV for your suggestions.