Back

Getting today's opening price from History Function

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();
}

 

Update Backtest








We could get the opening price using History:

public override void OnData(Slice data)
{
if (Time.Minute == 40)
{
var todaySlices = History(TimeSpan.FromMinutes(20), Resolution.Minute);
var openSlice = todaySlices.Where(x => x.Time.Date == Time.Date).FirstOrDefault();
var spyOpen = openSlice["SPY"].Open;
}
}

How/when are you calling OpenPositions method?

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thanks for the help. Looks like "todaySlices.Get(sec.Symbol, Field.Open);" was the wrong way to get the opening slices and what I needed to do was do was ".Where(x => x.Time.Date == Time.Date).FirstOrDefault();"

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed