I am using a 5 min consolidator. I noticed that the volume of 5 min consolidated bar is different than the sum of the volume of the last 5 1 min bars. The attached backtest gives a volume for the 5 min tsla (020-08-21 12:50:00) as 1563325, however the sum of the last 5 1 min bars is 857095 The open/high/low/close values are correct but the volume is off.
Why is this?
2020-08-21 12:55:00 :0:05:00 TSLA: O: 416.15 H: 416.78 L: 414.514 C: 416.6 V: 1563325 2020-08-21 12:55:002020-08-21 12:55:00 :857095.0
Fred Painchaud
Hi Keith,
Did you make sure that the same bars are considered in both cases? Like making sure the last one is not considered in both or is considered in both…
Thinking out loud trying to find a reason for your observation…
Fred
Keith Forward
Hi Fred,
Thank you for your response. I do not think that is the problem. I have logged the last 5 min bar and it does not seem to related to other bars. I believe it could be something about having the symbol data in a class and having the consolidator in this class. However, this approach appears to the typical way of dealing with consolidator with multiple symbols. I noticed this problem when I have been working on a more complex alpha model and I was able to reproduce this error with this simple backtest. Any help would be greatly appreciated.
Thanks,
Keith
Fred Painchaud
“I believe it could be something about having the symbol data in a class and having the consolidator in this class.”
I haven't done testing myself yet on this but it's not the first time I discuss this. With Mak, for instance, we've been kinda leaning towards that conclusion too. There's either a bug around that in LEAN or there's something we don't understand/see.
I don't have an answer right now. But I will eventually find the time to do some small test, designed to pinpoint only that and thus see what's wrong if anything is.
Fred
Keith Forward
Hi Fred,
I think I have found a major issue when calling history and consolidator. In the backtest below, I call the past history and populate a rolling window of 5 min bars in the symbol data class. I also used the consolidator during the backtest. I would think that 5 min bars in the symbol data class would be the same as the 5 min bars in the backtest with bars being indexed on the endtime of the bar. However, the history and consolidator appears to be indexed on the start time of the bar and not endtime. For example in the SymbolData class history, the 1 min bar for TSLA at 2020-08-18 with an endtime of 12:52 is the following:
1 min bar: TSLA: O: 378.532 H: 379.074 L: 378.524 C: 378.972 V: 78795 start of bar: 2020-08-18 12:51:00 EndTime of bar: 2020-08-18 12:52:00
However when history is called during the backtest, this bar is labeled as 12:51 and not 12:52 (where history data is indexed by endtime and not start)
askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open volume
time
2020-08-18 12:50:00 378.536 379.534 378.438 379.400 1500.0 378.400 379.390 378.210 379.084 500.0 378.490 379.520 378.256 379.300 133940.0
2020-08-18 12:51:00 378.972 379.090 378.536 378.536 500.0 378.800 378.980 378.400 378.400 1500.0 378.972 379.074 378.524 378.532 78795.0
2020-08-18 12:52:00 379.004 379.200 378.886 378.972 1000.0 378.700 379.012 378.520 378.800 500.0 378.716 379.180 378.710 378.900 35705.0
I had to run the backtest twice and change the start date to get the data above. However this does not resolve the volume issue identify above. How can the 5 min bars align so that 5 min bars are the same in both the symbolData class and backtest consolidator?
Thank you,
Keith
Fred Painchaud
Yep. I do think that the start time vs end time indexing is an issue. I don't know if this is only for Tesla - it can be.
Please report the Tesla issue here:
I will also reference this thread to the devs (in case they don't see it).
I still don't have an answer re the original issue with consolidators/universe as I did not do the test I was writing about. I'll do it asap and report back here.
Fred
Keith Forward
Hi Fred,
I think it is a systemic issue: It is the same for HES, for example 1 min bar with endtime of 12:52
bar from symbolData class:
2020-08-20 00:00:00 1 min bar: HES: O: 48.00951 H: 48.00951 L: 47.96554 C: 47.96554 V: 2572 start of bar: 2020-08-19 12:51:00 EndTime of bar: 2020-08-19 12:52:00
Called history during backtest:
askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open volume
time
2020-08-19 12:50:00 48.03882 48.077899 48.019280 48.04859 307.0 48.01928 48.058359 48.009510 48.03882 205.0 48.038820 48.058359 48.038820 48.043705 3903.0
2020-08-19 12:51:00 47.97043 48.038820 47.970430 48.03882 205.0 47.95089 48.019280 47.950890 48.01928 102.0 47.965545 48.009510 47.965545 48.009510 2572.0
For spy, for example 1 min with endtime 12:51:
2020-08-20 00:00:00 1 min bar: SPY: O: 331.8704 H: 331.9194 L: 331.8704 C: 331.9145 V: 57464 start of bar: 2020-08-19 12:50:00 EndTime of bar: 2020-08-19 12:51:00
askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open volume
time
2020-08-19 12:50:00 331.919384 331.919384 331.870447 331.880235 102.0 331.909597 331.909597 331.860660 331.860660 1635.0 331.914490 331.919384 331.870447 331.870447 57464.0
2020-08-19 12:51:00 331.909597 331.929171 331.899809 331.919384 102.0 331.899809 331.919384 331.890022 331.909597 2963.0 331.909597 331.919384 331.899809 331.919384 17486.0
2020-08-19 12:52:00 331.890022 331.919384 331.880235 331.909597 1124.0 331.880235 331.909597 331.870447 331.899809 409.0 331.880235 331.914490 331.880235 331.909597 32483.0
Thanks,
Keith
Keith Forward
Hi Fred,
I think I realized part of the problem with the indexing. The issue is associated with the for loop in the symbolData class (not the history call).
for time, row in self.df_min.iterrows():
tradebarbar = TradeBar(time, self.Symbol, row.open, row.high, row.low, row.close, row.volume)
self.rolling_windowMin.Add(tradebarbar)
self.consolidatorMins.Update(tradebarbar)
When the tradebarbar is constructed, it is considering the time as the start time of the bar instead of the endtime as it is in the DataFrame Index. Is there a way to have TrabeBar consider time as the end time and not the start?
Thank you,
Keith
Fred Painchaud
Hi Keith,
TradeBar's first param is the start time. And the last param is the period. TradeBar calculates EndTime by doing start time + period.
When you construct your TradeBar there, you do not pass the last param (period). So you are using some default value. Not sure what it is. Probably 1 minute as that is the default resolution pretty much everywhere.
Yeah, just looked at the code and it is 1 minute by default. So if the start time is given as first param, since you are at minute res, that in particular is fine.
Fred
Fred Painchaud
Hi again,
Not sure the implication was clear so here I am again: if you have the end time and you want to create a TradeBar, you need to substract the period to the end time to get the start time and pass that as first param…
Fred
Keith Forward
Hi Fred,
Great thank. Yes, I modified the loop to be:
for time, row in self.df_min.iterrows():
tradebarbar = TradeBar(time-timedelta(minutes= 1), self.Symbol, row.open, row.high, row.low, row.close, row.volume )
self.rolling_windowMin.Add(tradebarbar)
self.consolidatorMins.Update(tradebarbar)
This resolved the indexing issues. Unfortunately, this did not resolve the volume consolidator issues when running the backtest. Does the consolidator need to be updated when the backtest is running as it is the symbolData class?
Thank you,
Keith
Fred Painchaud
Hi Keith,
Once the consolidator is registered, it should be handled by LEAN no matter where in the code it was registered. So no, it should not need to be updated because it is in SymbolData. LEAN is not aware of SymbolData.
BUT
Still did not have time to do my testing. I do have my little finger telling me there's a problem with consolidators and universe selection. Either a bug OR some explanation on how to do it right.
Fred
Keith Forward
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!