I’ve been working through the Futures Boot Camp on QuantConnect, and I’m currently stuck on Lesson #4 (Margin Call Warning).
Even though my code is identical to the official solution, the platform keeps showing the same error:
“Looks like you didn’t use SetHoldings() correctly.”
Here’s the relevant part of my code:
if not self.Portfolio.Invested: self.SetHoldings(self.liquid_contract.Symbol, 1)
Everything else (dates, filters, settings) matches the provided solution.
However, the submission still fails — even the “official answer” script gives the same error.
Mia Alissi
Here’s a comprehensive troubleshooting guide for the “didn’t use SetHoldings() correctly” error in the QuantConnect Futures Boot Camp, even if your code matches the official solution:
1. Method Name Case and Spelling
self.set_holdings(...)(all lowercase, underscore). WritingSetHoldings,setHoldings, or other variations will not be detected.self.set_holdings(...)? (Notself.SetHoldings(...))self.set_holdings(...).2. Called on the Correct Object
self.set_holdings(...). Do not usealgorithm.set_holdings(...)or other object names.self.only.self.set_holdings(...).3. Correct Method Placement (on_data Execution)
initialize, Lean can't run it since there's no price data yet, and the grader expects the order to be executed during live data handling, such as inon_dataor similar event handlers.on_data, not ininitializeor elsewhere?on_data(self, data).4. Proper Triggering of the Call
if not self.portfolio.invested:is never true, the method never runs.self.debug("trigger")statement before the call to check if it runs.5. Correct Argument Type
Symbolobject of your future contract, not a string, nor the contract object itself.self.set_holdings(self.liquid_contract.symbol, 1)self.liquid_contract.symbolis aSymbol. Do not use a string like"ES".6. No Multiple SetHoldings Calls
set_holdingsmultiple times, later calls may override your intended position, confusing the grader.set_holdingscalls.7. API Usage: Correct Integer Value
1(not 100, not 0.5, not -1) as amount.1.1.8. Add the Contract Properly
AddFutureContractbefore trading might break expectations.self.add_future_contract(self.liquid_contract.symbol, resolution), as required.9. No Syntax/Indentation Weirdness
10. No Variable Shadowing
SetHoldingsas a variable name elsewhere in your code.SetHoldingsis never assigned.11. Confirm OnData Signature
on_Data,ondata, wrong capitalization, or missing thedataparameter = handler won't run.def on_data(self, data):.12. Platform or Cache Issues
13. Possible QuantConnect Grader Bug
Quick Checklist
self.set_holdings(self.liquid_contract.symbol, 1)inon_data, on a single line, matching official style/case.Symbolobject) is passed.If you exhaust this checklist and both your code and the official answer fail, it’s not you—report it as a platform bug!
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.
Dndcreator
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!