How to reference previous value from same time but different day?

Hello everyone. I'm trying to reference an indicator value from the same timestamp but previous business day. For example, for 2023-01-05 11:35:03 I would like to pull the data from 2023-01-04 11:35:03.

I've searched the forums for this, but the previous posts seem to be about referencing the value from a specific time, by setting timenum() = 11:35:05 and then using ValueWhen() to reference that value. But I need the value from the same time (which would roll and change) and not a specific hardcoded time. Here are some things I've tried:

vol = StDev( C - Ref(C, -1) , 60);
dt = DateTime();

yesterday = DateTimeAdd(dt, -1, inDaily);
yesterdaysVolSameTime = ValueWhen(dt == yesterday, vol );

Plot( yesterdaysVolSameTime, "yesterdaysVolSameTime", ParamColor( "Color yesterdaysVolSameTime", colorWhite ), ParamStyle("Style yesterdaysVolSameTime", styleThick));

^ unfortunately it's coming up blank

vol = StDev( C - Ref(C, -1) , 60);
tn = TimeNum();
notSameDay = Now(6) != Day();
sameTime = Now(4) == tn;

yesterdaysVolSameTime = ValueWhen(sameTime AND notSameDay, vol );

Plot( yesterdaysVolSameTime, "yesterdaysVolSameTime", ParamColor( "Color yesterdaysVolSameTime", colorWhite ), ParamStyle("Style yesterdaysVolSameTime", styleThick));

^ I tried the Now() function but it seems to be referencing the current time as in the real time today and not the "current time" of the bar. It's also coming up empty as a result.

If it were a larger time interval like 15m or 30m I realize I could just use Ref(vol, -26) or something, but because of the granular timeframe (1s to 5s) there are a lot of data holes and so the number of bars would vary.

I'm sure it's very simple but I can't quite seem to figure it out so thanks in advance for any help and guidance.

here is an example using the SelectedValue function

ft = "Arial Bold";
sz = 12;
bi = BarIndex();
dt = DateTime();
selectedvalue_dt = SelectedValue( dt );
previousday_selectedvalue_dt = DateTimeAdd( selectedvalue_dt, -1, inDaily );
location_today = dt == selectedvalue_dt;
location_yesterday = dt == previousday_selectedvalue_dt;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( Volume, _DEFAULT_NAME(), IIf( C > O, ParamColor( "Up Color", colorGreen ), ParamColor( "Down Color", colorRed ) ), ParamStyle( "Style", styleHistogram | styleThick, maskHistogram ) );
Plot( location_today, "", colorAqua,  styleDashed | styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 1, 2 );
Plot( location_yesterday, "", colorPink,  styleDashed | styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 1, 2 );

Volume_at_selectedvalue_dt = LastValue( ValueWhen( location_today, V ) );
Volume_at_previousday_selectedvalue_dt = LastValue( ValueWhen( location_yesterday, V ) );

maxY = HighestVisibleValue( V );
str1 = "Volume today: \n" + NumToStr( Volume_at_selectedvalue_dt, 0 );
str2 = "Volume yesterday: \n" + NumToStr( Volume_at_previousday_selectedvalue_dt, 0 );
idx1 = LastValue( ValueWhen( location_today, bi ) );
idx2 = LastValue( ValueWhen( location_yesterday, bi ) );
PlotTextSetFont( str1, ft, sz, idx1 + 1, maxY, colorWhite, colorDefault, 0 );
PlotTextSetFont( str2, ft, sz, idx2 + 1, maxY, colorWhite, colorDefault, 0 );
2 Likes

Thanks for the response and the elegant code! I just tried it out and it did plot the previous day's value as promised, and I also learned about SelectedValue. Now when I tried to use it to generate buy/sell signals, it doesn't seem to want to use the value. Here is an example code:

bi = BarIndex();
dt = DateTime();

R1 = RSIa(C, 14);

//Plot( R1, "R1", IIf( R1 > 50 , ParamColor( "Color R1 Up", colorWhite), ParamColor( "Color R1 Down", colorYellow)), ParamStyle("Style R1", styleThick));

selectedvalue_dt = SelectedValue( dt );
previousday_selectedvalue_dt = DateTimeAdd( selectedvalue_dt, -1, inDaily );

location_today = dt == selectedvalue_dt;
location_yesterday = dt == previousday_selectedvalue_dt;

RSI_at_selectedvalue_dt = LastValue( ValueWhen( location_today, R1 ) );
RSI_at_previousday_selectedvalue_dt = LastValue( ValueWhen( location_yesterday, R1 ) );

Buy = Cross(RSI_at_previousday_selectedvalue_dt, 40);
Sell = Cross(50, RSI_at_previousday_selectedvalue_dt);

//Buy = Cross(R1, 40);
//Sell = Cross(50, R1);

buy = ExRem( buy, sell );
sell = ExRem( sell, buy );

//Plot Buy and Sell Arrows
PlotShapes(IIf(Ref(Buy,-1), shapeUpArrow, shapeNone), ParamColor("BuyUp",colorWhite), 0, Open, Offset = -6);
PlotShapes(IIf(Ref(Sell,-1), shapeDownArrow, shapeNone), ParamColor("SellDown",colorWhite), 0, Open, Offset = -6);

//Determine buy & sell prices
BuyPrice = Open;
SellPrice = Open;

//Bet sizing / Money Management
maxpos = 1000; // maximum number of open positions
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 20 , spsPercentOfEquity );

//Trade Delays
SetTradeDelays(1,1,1,1);

(obviously this would be a very silly system, it's just an example)

When I tried this the arrows did not plot and backtest was showing 0 trades. Replacing the RSI_at_previousday_selectedvalue_dt with R1 and the plot and backtest work as expected. I was looking through the user guide and was wondering if SelectedValue could be used for this purpose?

I should further clarify that I don't necessarily need the exact value from that exact timestamp of the previous trading day, but just the ballpark figure from around that time of day. I suppose I could separate the periods out into chunks but was holding out for a more elegant solution.

i'm not sure what you want to do. I thought you wanted to compare 2 values. What do you want to backtest?

I need the previous day's value around the same time period in order to use in a calculation for an indicator.

i will leave that question for someone else

All right thanks for your help!