Back

Get consolidated bars for every stock in universe

Day 1 newb here.  I am just playing around with creating my first trading algorithm.  I am working in python, and I followed the tutorial about how to set up consolidated trade bars.  I have set up a five minute trade bar which works when just using a single stock as argument, like in the tutorial.

I wish to get 5 minute bars for multiple stocks.  I was hoping the following would work, but it doesn't.  

self.SubscriptionManager.AddConsolidator(["AAPL","SPY"], fiveMinuteConsolidator)

So then I tried this:

self.SubscriptionManager.AddConsolidator("AAPL", fiveMinuteConsolidator)
self.SubscriptionManager.AddConsolidator("SPY", fiveMinuteConsolidator)

Which will backtest without error but only seems to trigger callback for "SPY" data.  I would love to get data for both symbols at the same time in the fiveMinuteBarHandler function.  

It seems bulky to have to re-define a different callback for each symbol.

ANy help is greatly appreciated

Update Backtest








It is not possible to get both symbols at the same time at fiveMinuteBarHandler.
If you want to use the same data consolidated event for every symbol, you can use the following code snippet;

for ticker in [ "SPY", "AAPL" ]:
consolidator = TradeBarConsolidator(timedelta(minutes=5))
consolidator.DataConsolidated += self.fiveMinuteBarHandler
self.SubscriptionManager.AddConsolidator(ticker, consolidator)

Data from symbols will arrive at fiveMinuteBarHandler one at a time. Since you need the same at the same time, you need to put some logic in place check whether you are receiving the bar of the second symbol with the same time.

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 reply.  I realized this was the only way to do it, and as it was my first day I have also realized there are other functions in place that make the job easier than using the consolidated trade bars.  I am using the `self.History` function now to retreive data for multiple stocks at once.  I can get a 5 mintue history and construct the bar myself.

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