Your code is theoretically correct, although better use priceClose(1); to get yesterday's close price.

The problem is that the number of bars is not always equal to the number of hours. There are no bars at the weekend, on holidays, or when the market is closed. For this reason, better use

DailyOpen = priceOpen(timeOffset(UTC,1,0,0));
DailyClose = priceClose(timeOffset(UTC,1,23,59));

Have a look at the source code of the dayOpen / dayClose functions in the indicators.c file for getting the idea.