Hello everyone!
I hope someone can help me out on 2 questions.
My first question. I'm subscribed to tick data for a stock, and I've consolidated the tick data into minute bar data. The minute bar data will be received in the minute bar handler and I wish to run my buy/sell logic in the minute bar handler and I'll need to reference the most recent tick data in the buy logic. When tick data is received in OnData, I put the data into a global variable like the following snippet:
def OnData(self, data):
self.data = data
and I'd be referencing the current tick data twice in the consolidated bar handler like the following snippet:
def ConsolidatedBarHandler(self, sender, bar):
ticks_one = self.data.Ticks[self.spy]
last_price = ticks[0].LastPrice
ticks_two = self.data.Ticks[self.spy]
last_price = ticks[0].LastPrice
My question is, will ticks_one and ticks_two be the same tick? I guess my main question is, will Ondata be running on a separate thread, hence, updating self.data with the most recent ticks the moment ticks are received?
My second question. Why does self.data.ContainsKey for spy turn up to be false in the backtest?
Forget about the “update order” part.
Best regards
Jay Ma
Varad Kabade
Hi Li Cheng,
Moving forward, we recommend using self.CurrentSlice attribute to access the current tick data for the latest tick.
Best,
Varad Kabade
Li Cheng
Hi Varad, thanks for answering my question! You've given me an extremely valuable insight into how to access the latest tick, since I'm a novice at tick data algorithms, I was not aware that I can use self.CurrentSlice to reference the most recent tick at that point in time.
If I have a universe of 5 stocks in my algorithm, do I reference the latest tick for “SPY” as self.CurrentSlice["SPY"].LastPrice?
Alexandre Catarino
Hi Li Cheng ,
The algorithm manager saves the reference of the Slice object that will be used to call OnData as CurrentSlice before firing scheduled events or any other task (like data consolidation). You just need to replace `self.data` for `selt.CurrentSlice` in your code.
Li Cheng
Hi Alex, thanks so much for your addition to this thread!
Can I clarify with you guys about what I understood from your replies?
Assume universe resolution is tick and data is consolidated into minute bars. At 15:30:00, we receive a consolidated minute bar. In the consolidated bar handler, assume we have a ton of calculations with history calls, causing the algorithm to have to run a chunk of code that uses up 15 microseconds of time. After running the aforementioned chunk of code, the current time is 15:30:00:15, the algorithm attempts to reference the current tick prices.
What I'm understanding is, at this point self.data will contain the tick price at 15:29:59:99, whereas self.CurrentSlice will contain the tick price at 15:30:00:14. Am I correct?
And if I'm wrong, which means that I misunderstood Varad's reply as well, how do I get the tick price at 15:30:00:14?
Varad Kabade
Hi Li Cheng,
The above summary is incorrect. While the algorithm is busy in the consolidator handler, there will not be another slice object, so CurrentSlice is still the same object in OnData.
Best,
Varad Kabade
Li Cheng
Hi Varad,
Thanks for clarifying that. Is there any way that I can get the tick price at 15:30:00:14? (Based on the example that I gave)
Varad Kabade
Hi Li Cheng,Yes, we recommend adding the code snippet in the Update method of consolidator.
Before that we need to import:
Note that the above time need not be exact we can check for a given window too.Best,Varad Kabade
Li Cheng
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!