So I am able to get yesterday's close but am having a lot of difficulty getting today's opening price from the history function. I've tried to ways: Daily resolution of one day and getting the first element, minutely resolution and getting 5 minutes ago (which I assume is also the first element). When I researched this topic on previous posts, I noticed that people were just manually storing the opening price in OnData. Is this the only way or can we use the history function for getting today's open price?

Below are my attempts:

// Trying using one day history
public void OpenPositions()
{
IEnumerable<Slice> dailySlices = History(TimeSpan.FromDays(1), Resolution.Daily);
decimal weighting = 0;
if (toOpen.Count > 0) weighting = 1m / toOpen.Count;

foreach (Security sec in toOpen)
{
IEnumerable<decimal> closePrices = dailySlices.Get(sec.Symbol, Field.Close);
IEnumerable<decimal> openPrices = dailySlices.Get(sec.Symbol, Field.Open);
Debug(String.Format("{0} - Yest Close: {1} Today Open: {2}", sec.Symbol, closePrices.First().ToString(), openPrices.First().ToString()));
SetHoldings(sec.Symbol, weighting);
}
toOpen.Clear();
}

// Trying to use 5 minute and getting the first minute's open
public void OpenPositions()
{
IEnumerable<Slice> dailySlices = History(TimeSpan.FromDays(1), Resolution.Daily);
IEnumerable<Slice> todaySlices = History(TimeSpan.FromMinutes(5), Resolution.Minute);

decimal weighting = 0;
if (toOpen.Count > 0) weighting = 1m / toOpen.Count;

foreach (Security sec in toOpen)
{
IEnumerable<decimal> closePrices = dailySlices.Get(sec.Symbol, Field.Close);
IEnumerable<decimal> openPrices = todaySlices.Get(sec.Symbol, Field.Open);
Debug(String.Format("{0} - Yest Close: {1} Today Open: {2}", sec.Symbol, closePrices.First().ToString(), openPrices.First().ToString()));
SetHoldings(sec.Symbol, weighting);
}
toOpen.Clear();
}