I am creating a strategy for futures (eg Futures.Indices.NASDAQ100EMini) that trades on 5 minute bars (M5). Apparently to do that I must use a QuoteBarConsolidator as in the example below. But I have some questions.
1. In the Initialize method, I set the root symbol to Futures.Indices.NASDAQ100EMini and then the next line uses Symbol.Create(self.rootSymbol, SecurityType.Future, Market.USA). I found this in an example and I have no idea why I must call the Symbol.Create() method. Is there a reason to call this?
2. The M5 consolidator code is at the end of the Initialize method, and it sets up the callback method OnFiveMinuteBar(). The parameters sent in are "sender" and "bar". What is "sender" and do I need to use it? As for "bar" is that a single M5 trade bar with open,high,low,close data? Is it already just the data for "NQ" or must I index it to geth the data (like bar["NQ"])? Is it just the current bar or can I get prior bars from it?
3. When I get a new bar of data, of course I want to have access to past bars for indicators and such. For example, for a simple moving average 50 bars long, I need 50 bars of data. How exactly do I access this data? Must I create my own array of bars in Initialize and every time OnFiveMinuteBar is called I must append that bar to my array? Or is there some other way to get the M5 bar data for some number of bars in the past as well as the current bar?
Here is my Initialize() code:
def Initialize(self):
self.SetStartDate(2018,1, 1) #Set Start Date
self.SetEndDate(2019,2,28) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.rootSymbol = Futures.Indices.NASDAQ100EMini
self.symbol_NQ = Symbol.Create(self.rootSymbol, SecurityType.Future, Market.USA) . # what does this do?
self.AddFuture(self.rootSymbol)
#
# Do i have to keep track of my own bars?
#
self.mybars = []
#
# Use consolidator
# https://www.quantconnect.com/tutorials/api-tutorials/consolidating-data-to-build-bars
#
fiveMinuteConsolidator = QuoteBarConsolidator(TimeSpan.FromMinutes(5))
fiveMinuteConsolidator.DataConsolidated += self.OnFiveMinuteBar
def OnFiveMinuteBar(self, sender, bar):
self.mybars.append(bar) . # do I have to keep track of bars myself?
# TODO: code goes here, but I don't know what is in sender or bar
Jack Simonson
Hi Marc,
To address your questions in order:
1) You do not need to do this but it allows you to build a variable that is a list of Symbol objects if you want to use it later. However, the below is sufficient (with an arbitrary example of a filter)
future = self.AddFuture(Futures.Indices.NASDAQ100EMini) future.SetFilter(timedelta(0), timedelta(182))
2) The parameter 'sender' is the Consolidator object and 'bar' is the data bar being passed into the handler, and both are necessary. The bar being passed in is dependent upon the type of consolidator you are using. In your case, a QuoteBar Consolidator will pass through a Quote Bar, which has the properties OHLC, LastAskSize, and Bid/Ask data objects, each of which has OHLC attributes. You can find an in-depth explanation of each type of bar and their properties here and here.
3) Consolidator handlers, i.e. OnDataConsolidated, only pass through one bar at a time. If you want to access a history of QuoteBars for a given Symbol, you would want to employ a Rolling Window, which can be defined to hold Decimal, TradeBar, QuoteBar, or Indicator Point objects. There is documentation of this here, as well as demonstration algorithms that will show you how to put them to use. However, if you want to use an indicator that requires a consolidator, i.e. a 5-min Simple Moving Average, then you don't need to use a Rolling Window. Instead, in Initialize() you need to add one line of code that 'registers' the indicator with the consolidator, such as in the code posted below or found in the consolidator documentation here. If you want to build consolidators and indicators for each futures contract that you plan to work with, you can add and register indicators where you would add consolidators, such as in this demonstration algorithm.
def Initialize(self): # ...other initialization... self.AddEquity("SPY", Resolution.Minute) consolidator = TradeBarConsolidator(30) self._sma = SimpleMovingAverage(10) self.RegisterIndicator("SPY", self._sma, consolidator) self.SubscriptionManager.AddConsolidator("SPY", consolidator)
Marc Ilgen
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!