Hello and apologies for this question if it is a super basic one. I don't understand why the code is being flagged with 10 errors like the following:
"GapUpBreakout" has no attribute "SetStartDate"
"GapUpBreakout" has no attribute "SetEndDate"
"GapUpBreakout" has no attribute "SetCash"
"GapUpBreakout" has no attribute "AddUniverse"
"GapUpBreakout" has no attribute "Log"
"GapUpBreakout" has no attribute "Liquidate"
"GapUpBreakout" has no attribute "Log"
"GapUpBreakout" has no attribute "Liquidate"
"GapUpBreakout" has no attribute "Time"
"GapUpBreakout" has no attribute "Time"
How do I fix the disconnect so that those attributes are defined properly. Thanks!
Mia Alissi
Here is a review of the core logic and algorithm workflow from your code, focusing on operational and functional issues (not naming, imports, or allocation percentage):
Findings:
Incorrect Stop Loss Calculation:
The stop loss level is set with
stop_loss_level = sd.open_price * 1.10
. This means the stop is 10% above the open price, so you only exit if the price drops below 10% above the open, which is not actually a loss. Normally, stop-loss should be something like0.90 * open_price
(10% below open), not higher. This impacts proper risk control.ActiveSecurities.Keys Might Not Work as Intended:
In your
OnData
method, you write:for symbol in self.ActiveSecurities.Keys:
.In Python, this should be
for symbol in self.ActiveSecurities.Keys:
(ifKeys
is a property), but Python dicts/lists use lowercase.keys()
. Make sure this does not cause a runtime error in your environment.Excessive History Call Frequency:
The daily history request is made every second for every symbol, which can use high bandwidth and run into rate-limits. Typically, history is called once per symbol per day, not every second.
Entry Logic Can Trigger Multiple Times Per Day:
The code uses
bought_today
to try to prevent multiple buys per symbol per day, but doesn't reliably reset this at market open. If you want to enter only once per day per symbol, ensurebought_today
is reset after liquidating at end of day, and carry the right state forward (which is done inOnEndOfDay
, so just confirm this reliably triggers).SymbolData Not Timestamped:
The
SymbolData
only has price, state, and entry, but not a record of the date. This could cause issues if you want to use data spanning multiple days or need to track which day's open you want.Potential OnEndOfDay Coverage:
If
OnEndOfDay
triggers only once globally rather than per symbol, your liquidation might miss symbols—ensure it's called correctly per symbol and that all positions are closed as expected.Copy of the improved code with those bugs addressed and additional robustness:
Summary of corrections for S:
SymbolData
to help manage trades that span across/multiple days.ActiveSecurities.Keys
should be checked for proper iteration in Python context.Let me know if you'd like these changes in your naming style or to address any other aspects!
Sources: Opening Range Breakout for Stocks in Play, Momentum Effect In Commodities Futures, Asset Growth Effect, Momentum And State Of Market Filters, Key Concepts - QuantConnect.com
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.
Digitall
Thanks so much! Your code is just getting flagged by the system for one problem now. I've copied that problem below. I'm trying to make sure my algorithm looks at all US stocks, including all levels of NYSE like NYSE American and all levels of Nasdaq like NasdaqGS, NasdaqGS, NasdaqCM, etc
[{
"resource": "/home/lean-user/workspace/project/main.py",
"owner": "QuantConnect",
"severity": 4,
"message": "Argument 1 to \"add_universe\" of \"QCAlgorithm\" has incompatible type \"Callable[[list[CoarseFundamental]], list[Symbol]]\"; expected \"Callable[[list[Fundamental]], list[Symbol]]\"",
"startLineNumber": 18,
"startColumn": 9,
"endLineNumber": 18,
"endColumn": 28
}]
Louis Szeto
Hi S O'Keeffe
This will not affect the functionality of the obsolete hinting code. You can just change the line
into
to avoid the problem pops up.
Best
Louis
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.
Digitall
Hi Louis,
Thanks so much! I switched that line as you recommended and then the ‘Ask Mia’ tool helped me fix another error about defining ‘course’ in the code.
I ran a backtest, but the algorithm didn't trade SBET on 5/27. It should have traded that stock since that stock opened above $2.50 price, it opened 10% higher than the previous day's close, and it hit the 30% breakout level to buy. Can you tell why the algorithm didn't trade SBET? Is one of my triggers or filters not working? Is the ‘f.symbol.id.market’ line not working?
Louis Szeto
Hi Digitall
There are a few issues to be addressed:
2. The historical data call is getting the price already aggregated, so you need the last but not second-last price row.
You may also consider optimizing the code practice, since the Python code might not be able to handle Second resolution in real-time trading:
Best
Louis
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.
Digitall
OK thanks so much Louis! I'll try to fix these issues
Digitall
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!