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_data
method, the variablecalls
is used but not defined. You need to filter the option contracts from thechain
to 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.contract
is initiallyNone
, and it seems like you want to select a contract whenself.contract
is not set. You might want to adjust the condition to check ifself.contract
isNone
instead:Portfolio Check: The line
self.portfolio[self.contract.symbol].invested
will raise an error ifself.contract
isNone
. Ensure thatself.contract
is assigned a valid contract before checking if it's invested.Contract Assignment: The assignment of
self.contract
should happen before you attempt to place an order. Ensure thatself.contract
is 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_data
method: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!