I tried this...
public override void Initialize()
{
SetCash(_cash);
SetStartDate(new DateTime(2020, 3, 1);
SetEndDate(new DateTime(2020, 3, 9));
AddEquity(security, Resolution.Minute);
_tradeBarHistory = History<TradeBar>("MSFT", _closingPeroids, Resolution.Minute);
_tradeBars = new RollingWindow<TradeBar>(_closingPeroids);
_warmUpInitializeComplete = false;
SetWarmup(50);
}
public override void OnData(Slice data)
{
if (IsWarmingUp) return;
if(!_warmUpInitializeComplete) {
foreach(var tradeBar in tradeBarHistory) {
tradeBars.Add(tradeBar);
}
_warmUpInitializeComplete = true;
}
if(!data.ContainsKey(security) || data[security] == null) {
Debug(string.Format("Security {0} not found @ {1} and ContainsKey {2}.", security, Time, data.ContainsKey(security)));
continue;
}
if(tradeBars.IsReady && mom.IsReady) {
var firstBar = tradeBars.First();
var lastBar = tradeBars.Last();
var currentTrade = data[security].Bars.Last();
Debug(string.Format("firstBar.Time: {0}, lastBar.Time {1}, currentTrade", firstBar.Time, lastBar.Time, currentTrade.Time));
}
}
When I run the above code I get an error:
Runtime Error: 'QuantConnect.Data.Market.TradeBar' does not contain a definition for 'Bars' (Open Stacktrace)
However, when I look at the documenation here, it appears to have a property called Bars?
Can anyone tell me what i'm doing wrong?
Thanks,
Michael
Rahul Chowdhury
Hey Michael,
The error is coming from
var currentTrade = data[security].Bars.Last();
The Slice data has a property called Bars, which contains all the trade bar data keyed by symbol. data is the slice data from the current time stamp. data[symbol] returns the trade bar data for that symbol and TradeBar does not have any Bars property. So when you attempt to access Bars from the TradeBar, it throws an error.
Instead to properly access the trade bar for a specific security, you must access data.Bars[symbol]. This returns the TradeBar for that symbol at the current time stamp. Here is a modified version of your code which demonstrates this.
Best
Rahul
Michael A Astrauckas
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!