I expect June data but output shows March data for one day only
QUANTCONNECT COMMUNITY
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.
Sanjay K Singh
# region imports
from AlgorithmImports import *
from analyse import *
from datetime import datetime
# endregion
class NqTrend(QCAlgorithm):
def initialize(self):
self.set_start_date(2025, 6, 5)
self.set_cash(100000)
self.i = 0
self.future = self.add_future(
Futures.Indices.NASDAQ_100_E_MINI,
Resolution.MINUTE,
extended_market_hours=True,
data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO,
data_mapping_mode = DataMappingMode.LAST_TRADING_DAY,
contract_depth_offset = 0
)
self.future.set_filter(lambda future_filter_universe: future_filter_universe.front_month())
self.df = pd.DataFrame()
def OnEndOfAlgorithm(self):
self.debug(self.i)
self.debug(self.df)
def on_data(self, data: Slice):
self.i = self.i + 1
bar = data.bars.get(self.future.symbol)
if bar:
if bar.end_time.hour == 8 and bar.end_time.minute >= 31 and bar.end_time.minute <= 59:
self.df = pd.concat([self.df, pd.DataFrame({'time': bar.end_time, 'open': bar.open, 'high': bar.high, 'low': bar.low, 'close': bar.close, 'volume': bar.volume}, index=[1])], ignore_index=True)
elif bar.end_time.hour == 9 and bar.end_time.minute <= 30:
self.df = pd.concat([self.df, pd.DataFrame({'time': bar.end_time, 'open': bar.open, 'high': bar.high, 'low': bar.low, 'close': bar.close, 'volume': bar.volume}, index=[1])], ignore_index=True)
elif bar.end_time.hour == 9 and bar.end_time.minute >= 31 and bar.end_time.minute <= 59:
self.df = pd.concat([self.df, pd.DataFrame({'time': bar.end_time, 'open': bar.open, 'high': bar.high, 'low': bar.low, 'close': bar.close, 'volume': bar.volume}, index=[1])], ignore_index=True)
# Append rows Generate signals
elif (bar.end_time.hour >= 10 and bar.end_time.hour <= 12) or (bar.end_time.hour >= 13 and bar.end_time.hour <= 16):
self.df = pd.concat([self.df, pd.DataFrame({'time': bar.end_time, 'open': bar.open, 'high': bar.high, 'low': bar.low, 'close': bar.close, 'volume': bar.volume}, index=[1])], ignore_index=True)
# Close out if any positions (Assuming it crosses vwap or other stop loss); append rows
Mia Alissi
Here are the critical logic and configuration bugs in Sanjay's code that could cause only March data for a single day (not June) to appear, rather than the expected June slice:
1. Missing
set_end_date
ininitialize
set_start_date(2025, 6, 5)
but does not set an explicit end date. This means the backtest may by default only run for a single day (June 5) or potentially pick up a default or previously set global date (which could be March in some QC environments).set_end_date(2025, 6, 7)
, the algorithm will not collect a range of June data for the week as expected.2. Time Zone Mismatch Risk
research.ipynb
usesqb.set_time_zone(TimeZones.HONG_KONG)
, which is a +12 hour difference.on_data
assumes a particular hour/minute that may not match the incoming bar timestamps if those are delivered in a different time zone than you expect.3. Data Mapping Mode & Symbol Key
main.py
, you useLAST_TRADING_DAY
fordata_mapping_mode
, but in your research notebook, you useOPEN_INTEREST
. This could result in the underlying contract being different for the same symbol identifier at a given time, depending on roll dates, which will produce subtly different minute data (especially if the front month changed between March and June).data.bars.get(self.future.symbol)
will only fetch data if the symbol exactly matches. If the filteredfuture.symbol
corresponds to a contract that expired or didn't have data in June (but did in March), you will only see that March contract's bars.4. Filtering on Bar Times
on_data
filtering logic is very restrictive, checking specific hour/minute combinations. If for any day, there is only a single bar that fits that criteria (and most are skipped due to an off time or timezone issue), your dataframe will look like only one day's worth of data, potentially for March if that's where a contract's last (or only) bar fell.5. DataFrame Appending
pd.concat([...], ignore_index=True)
is correct and should append, but if bars almost never match your time criteria, you'll see just isolated rows—even if more data is available.6. No Output Or Overwriting Bug, but Only Data Matches Filter
self.df
is not overwritten except with new rows, but since your filter is so narrow and potentially mismatched by timezone, only one matching row might appear across the whole backtest.Summary of Bugs to Fix
self.set_end_date(2025, 6, 7)
ininitialize
to ensure the June range is covered.data_mapping_mode
between research and algorithm to avoid rolling contract mismatches.self.debug(bar.end_time)
) to confirm what times are actually available versus expected.With these bugs, your algorithm is highly likely to only receive or keep the single bar which passes all filters on a previous contract's data, possibly leading to the "March data for one day only" issue instead of the intended June slice.
Sources: docs writing algorithms key concepts time modeling time zones, Stock Selection Strategy Based On Fundamental Factors, Future Option Multiple Contracts In Different Contract Months With Same Underlying Future Regression Algorithm, Seasonality Effect Based On Same Calendar Month Returns, Opening Range Breakout for Stocks in Play
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.
Sanjay K Singh
After incorporating all the suggestions, I still do not see data from June. I only get 1-day of march data
class NqTrend(QCAlgorithm):
def initialize(self):
self.set_start_date(2025, 6, 1)
self.set_end_date(2025, 6, 6)
self.set_cash(100000)
self.i = 0
self.future = self.add_future(
Futures.Indices.SP_500_E_MINI,
Resolution.MINUTE,
extended_market_hours=True,
data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO,
data_mapping_mode = DataMappingMode.OPEN_INTEREST,
contract_depth_offset = 0
)
self.future.set_filter(lambda future_filter_universe: future_filter_universe.front_month())
self.df = pd.DataFrame()
def OnEndOfAlgorithm(self):
self.debug(self.i)
self.debug(self.df.iloc[-10:])
def on_data(self, data: Slice):
self.i = self.i + 1
bar = data.bars.get(self.future.symbol)
self.df = pd.concat([self.df, pd.DataFrame({'time': bar.end_time, 'open': bar.open, 'high': bar.high, 'low': bar.low, 'close': bar.close, 'volume': bar.volume}, index=[1])], ignore_index=True)
Alexandre Catarino
The out of sample period for free tier users is fixed to 3 months, this holds the recent data as unknown for you to test before going to live trading.
Sanjay K Singh
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!