I'm working on learning how to trade options and starting with this example from the datasets:
I can't get the actual code to work. Nothing seems to trade. Step debugging is not helping me either…but I believe this is the issue:
if self.contract and not self.portfolio[self.contract.symbol].invested and chain:Specifically the:
self.portfolio[self.contract.symbol].invested Any help is appreciated…
Mia Alissi
Let's review the code and identify the issues that might be causing it not to trade:
Undefined Variable
calls: In theon_datamethod, the variablecallsis used but not defined. You need to filter the option contracts from thechainto get only the call options. Here's how you can definecalls:Contract Selection Logic: The logic to select the contract is inside the condition
if self.contract and chain. However,self.contractis initiallyNone, and it seems like you want to select a contract whenself.contractis not set. You might want to adjust the condition to check ifself.contractisNoneinstead:Portfolio Check: The line
self.portfolio[self.contract.symbol].investedwill raise an error ifself.contractisNone. Ensure thatself.contractis assigned a valid contract before checking if it's invested.Contract Assignment: The assignment of
self.contractshould happen before you attempt to place an order. Ensure thatself.contractis assigned correctly after selecting the desired contract.Order Placement: Ensure that the order placement logic is correctly placed after the contract selection and assignment.
Here's the corrected version of the
on_datamethod:Make sure to test the updated code to ensure it behaves as expected.
Sources: Automating the Wheel Strategy, Greeks and IV Implementation, Handling Data - QuantConnect.com, Covered Call - QuantConnect.com, Individual Contracts - 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.
Stephen B
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!