Hello,
I'd like help fixing the code used in the attached backtest. I received 4 suggested changes (copied below) to fix the code used in the attached backtest, but I'm struggling to implement them. Could you please help me implement these 4 suggested changes to the code used in the attached backtest? This is the thread where the suggestions originally appeared:
Move the gap universe build schedule: Run it at 9:31am (not 9:30:10) to ensure minute bar data for 9:30 is available.
self.schedule.on(self.date_rules.every_day(), self.time_rules.at(9, 31), self._build_gap_universe)
Loosen the coarse filter: In _coarse_select, keep only the price-check (not volume). This keeps the candidate list feasible but doesn't drop stocks for weak prior-day volume.
selected = [c for c in coarse if c.price >= 0.5]
Add the true premarket volume filter in the universe build: For each symbol in your candidate set:
- pre_hist = self.history(symbol, 570, Resolution.MINUTE, extended_market_hours=True)
- pre_volume = pre_hist['volume'].sum() # sum from 00:00 → 09:30
- if pre_volume < 1_000_000:
- continue
Fix the gap calculation:
- today_open = pre_hist.iloc[-1]['open'] # This is the 9:30am bar, i.e., today's session open
- prev_close = self.history(symbol, 1, Resolution.DAILY).iloc[0]['close']
- gap_pct = (today_open - prev_close) / prev_close
With these changes, your actual premarket volume filter will work as you intend—and SBET (5/27) and KNW (6/6) will both pass the premarket volume test, rank by actual gap, and be eligible for trading based on true opening conditions.
Mia Alissi
Here is the corrected version of your algorithm, implementing all four requested changes:
Summary of main changes:
(9:30 open - prev close) / prev closeand top 50 are selected for the watchlist.Let me know if you want to further discuss or clarify any part of the code!
Sources: Opening Range Breakout for Stocks in Play, Momentum And State Of Market Filters, Asset Growth Effect, Momentum Effect In Commodities Futures, docs writing algorithms datasets quantconnect us etf constituents
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.
Retrospective Green Ocelot
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!