Hi,
I am new to QC and hope to be continuing here for foreseeable future. While i am getting the hang of architecture, i am trying to fetch the price for ES.
In initialize, i have the following:
self.future = self.AddSecurity(
SecurityType.Future, 'ES', Resolution.Tick)
self.future.SetFilter(lambda x: x.FrontMonth())
self.future_ES = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
On onData block, i wrote something like;
if self.Time.microsecond == 0 and self.Time.second == 0:
self.barOpen = 0
self.barClose = 0
self.barVolume = 0
self.Debug('Calling new function {}'.format(float(self.Securities[self.future_ES].Price)))
self.OnDataGetFutureData(slice)
And a function:
def OnDataGetFutureData(self, slice):
"""Check for future open close volume for charting."""
try:
for chain in slice.FutureChains:
for contract in chain.Value:
self.barOpen = self.Securites[contract.Symbol].Open
self.barClose = self.Securites[contract.Symbol].Close
self.barVolume = self.Securites[contract.Symbol].Volume
self.Debug('Open: {} Close: {} Volume:{}'.format(self.barOpen,self.barClose,self.barVolume))
except:
return
Unfortunately, I am getting type error on self.Securities[self.future_ES].price and if i comment that, the function is not setting the values properly.
I did try looking into documentation with no avail. I started working on QC only few days ago. Any help would be appreciated.
Narayanan Doraiswamy
Do i need to write a consolidator in such scenarios? And if so, how can i get volume information?
Ian Larson
I think you're doing 2 futures initializations here. First you initialize:
self.future = self.AddSecurity(SecurityType.Future, 'ES', Resolution.Tick)
I haven't seen this syntax before. And you're not even using this anyhow, except to filter it later with: self.future.SetFilter(lambda x: x.FrontMonth())
Think it's best to delete it.
The problem then becomes, I believe, you're not actually filtering the futures contracts you're using. (you call self.future_ES not self.future! Need to add:
self.future_ES.SetFilter(lambda x: x.FrontMonth())
I am not familiar with using futures yet (trying to learn myself) so there might be something else going on, but if you have only one symbol instead of a futures chain maybe the type error will go away.
Maybe helpful resources;
https://www.quantconnect.com/forum/discussion/2745/futures-backtests-getting-data-and-specifying-contracts/p1https://www.quantconnect.com/docs/data-library/futuresAlethea Lin
Hi Narayanan,
Welcome to QuantConnect!
Ian is spot on with the answers. The link he provided is also very helpful for understanding how to get future contract’s price. Future contracts do not have the Price property, so you need to use AskPrice and BidPrice instead. See more about future’s properties here. I have attached a simple example for obtaining the Bid/Ask price and other attributes of a futures contract here.
Hope this helps to address your question, and please reach out if you have more questions. We are here to make your QC journey enjoyable!
Narayanan Doraiswamy
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!