I'm trying to handle data at different resolutions (like 15 minutes, hourly, daily, and weekly) for the same stock. So, what I did was add minute resolution data for the stock and then consolidate it to get the resolution I want.
self.symbol = self.AddEquity(
"QQQ",
Resolution.Minute,
extendedMarketHours=True,
dataNormalizationMode=DataNormalizationMode.Raw,
).Symbol
# Custom Daily consolidator
customDailyConsolidator = TradeBarConsolidator(self.CustomDaily)
customDailyConsolidator.DataConsolidated += self.OnDailyData
self.SubscriptionManager.AddConsolidator(self.symbol, customDailyConsolidator)
self.Consolidate(self.symbol, Resolution.Hour, self.OnHourData)
self.Consolidate(self.symbol, timedelta(minutes=15), self.OnFifteenMinuteData)
It's been smooth consolidating 15-minute and hourly data, but daily and weekly data are giving me a bit of trouble. For instance, daily bars' open and close are aligned with regular market hours, but consolidating extended market hour data results in daily bars with their open matching the pre-market open price. Although I successfully use a custom daily method to address this, defining a custom weekly method proves to be more difficult.
def CustomDaily(self, dt):
'''Custom Daily Func with regular market hours'''
if dt.time() >= datetime.time(9, 30, 0):
start = dt.replace(hour=9, minute=30, second=0, microsecond=0)
end = dt.replace(hour=16, minute=0, second=0, microsecond=0)
else:
prior_day = dt - timedelta(days=1)
start = prior_day.replace(hour=9, minute=30, second=0, microsecond=0)
end = prior_day.replace(hour=15, minute=0, second=0, microsecond=0)
return CalendarInfo(start, end-start)
I think the easiest way to get daily or weekly data is to read it from the database, just like minute data. However, I believe QuantConnect does not support that functionality. Any advice to define a custom weekly method, and consolidate minute data into correct weekly bars?
The weekly open, high, low, and close prices should encompass the period from the opening of the first trading day at 9:30 AM to the closing of the last trading day at 4:00 PM.
def CustomWeekly(self, dt):
???
Mia Alissi
To define a custom weekly method, you can use the
CalendarInfo
object which contains the start time of the bar in the data time zone and the duration of the consolidation period. Here is an example of how you can define a custom weekly method:In this method, we first define the period as 7 days. Then we set the start time to 9:30 AM on the first trading day of the week. The
weekday()
function returns the day of the week as an integer (Monday is 0 and Sunday is 6), so we subtract this from the current date to get the start of the week. We then return aCalendarInfo
object with the start time and period.You can then use this method to create a weekly consolidator:
This will create weekly bars that start at 9:30 AM on the first trading day of the week and end at 4:00 PM on the last trading day of the week.
Please note that this code is based on the examples provided in the QuantConnect documentation.
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.
Shuai Liu
And actually, there's another problem when I use minute data to consolidate custom daily data. I noticed that the 3:59 PM close price is not equal to the daily close data. It's slightly different from the daily data you get when you directly add equity with daily resolution.
Mia Alissi
I understand your concern. The issue you're facing is due to the fact that the market doesn't always close at 4:00 PM, and some weeks have an irregular number of trading days. Unfortunately, the consolidator doesn't have the ability to adjust the end time of the bar based on the actual market close time.
However, you can create a workaround by using the daily consolidator and then aggregating the daily bars into weekly bars in your algorithm. Here's an example of how you can do this:
In this example,
OnDailyData
is the event handler for the daily consolidator. It creates a new bar at the start of each week (Monday) and updates the bar for the rest of the week.OnEndOfDay
is called at the end of each trading day. If it's Friday, it emits the weekly bar and clears the daily bars.Please note that this is a workaround and may not be perfect. For example, it doesn't handle holidays or early market closes. You may need to adjust it based on your specific needs.
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.
Shuai Liu
I got it. I can use the consolidated daily data to create the weekly bars.
However, there's another problem when I use minute data to consolidate custom daily data. I noticed that the 3:59 PM close price is not equal to the daily close data. It's slightly different from the daily data you get when you directly add equity with daily resolution.
I've been working on this problem for two days now, and it's quite confusing. It's frustrating to have daily and weekly data available, yet I have to put in so much effort to generate them from minute data...
Mia Alissi
The difference you're seeing between the 3:59 PM close price from minute data and the daily close price from daily data could be due to the fact that the daily close price is typically the last traded price before the market closes at 4:00 PM, while the 3:59 PM price is the last traded price at or before 3:59 PM.
In live trading, bars have a slight delay, so you may receive them milliseconds after the top of each minute. This means that the 3:59 PM bar could potentially include trades that occurred slightly after 3:59 PM, but before 4:00 PM. This could explain the discrepancy you're seeing.
In backtesting, your algorithm receives data at perfect timing. If you request minute resolution data, your algorithm receives the bars at the top of each minute. However, in live trading, bars have a slight delay, so you may receive them milliseconds after the top of each minute. This could explain the discrepancy you're seeing between the 3:59 PM close price and the daily close price.
If you need the exact daily close price, you might consider using the daily data directly. However, please note that custom data is often timestamped to midnight, but the data point may not be available in reality until several days after that point. If your custom dataset is prone to this delay, your backtest may not fetch the same data at the same time or frequency that your live trading algorithm receives the data, leading to deviations between backtesting and live trading. To avoid issues, ensure the timestamps of your custom dataset are the times when the data points would be available in reality.
For more information, you can refer to the QuantConnect documentation.
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.
Shuai Liu
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!