Hello,
I don't analyse options very often, but wanted to implemented one 0DTe optoin strategy I have found on one blog post.
In nutshell, the strategy have following rules:
Connect to TWS
- Using SPX open prices as our strike prices, get SPXW call and put open and last traded prices on expiry day for the prior four days
- Calculate the ATM 0DTE straddle price and implied expected move for each of the four days
- Calculate the actual move from the open and close prices of SPX for each of the four days
- Calculate the mean expected and actual moves over the four days
- Calculate the trade signal
- Place the appropriate straddle trades
Attached, you can find strategy and one backtest. I am confused, becuase strategy doesn't trade at all. Like there is no 1 day expiration options data on any bar. But if I import options data on weekly SPX, there should be data at least aevery week (as I said, I do't trade options, so I am not sure if this is true). Could someone please help with this strategy ?
Mia Alissi
Reviewing 'Mislav's' Python code for the 0DTE option strategy, a few potential issues stand out that could prevent the strategy from executing trades as expected:
Resolution Mismatch for Index and Options: The algorithm is set to use
Resolution.Hour
for both the index (SPX
) and its options (SPXW
). However, for a 0DTE strategy focusing on the very short term, a finer resolution (e.g.,Resolution.Minute
) might be more appropriate to capture the necessary price movements and option expirations within a single trading day.Option Chain Handling: The algorithm attempts to process option chains directly within the
OnData
method without checking if the option chain belongs to theSPXW
index. This could potentially lead to issues if the algorithm subscribes to other symbols in the future. A more robust approach would be to filter the option chains by the target symbol explicitly.RollingWindow Initialization: The
RollingWindow
objects forexpected_moves
andactual_moves
are initialized but not instantiated correctly. They should be instantiated with a specific size, e.g.,self.expected_moves = RollingWindow[float](5)
should beself.expected_moves = RollingWindow[float](5, 0.0)
to specify the window size and initial value.Trade Execution Logic: The logic for executing trades based on the comparison of average expected and actual moves seems sound, but there's no explicit check to ensure that trades are only placed on the appropriate days (i.e., days with 0DTE options available). This could be refined by adding a condition to verify that the options being traded indeed expire on the same day.
Missing Portfolio Check Before Trading: Before placing new trades, it's a good practice to check if there are any existing positions that need to be closed or managed. The current implementation jumps directly to trading without such checks, which could lead to unintended exposures.
Lack of Error Handling: The code does not include any error handling or logging, which could help diagnose issues when the strategy does not behave as expected. Adding logging statements, especially around key decision points like trade execution, could provide valuable insights.
OptionStrategies Method Usage: The usage of
OptionStrategies.ShortStraddle
andOptionStrategies.Straddle
might not be correctly implemented as these methods are not standard in the QuantConnect API. Instead, you would typically manually select the call and put options to create a straddle and place orders for them individually.Incorrect Use of Slice Indexing: The code uses
slice[atmPut.Symbol].Open
and similar expressions, which might not work as expected becauseslice
objects do not directly support indexing by symbol for option contracts. Instead, you should access the price data through theOptionChain
object obtained fromslice.OptionChains
.To address these issues, consider revising the resolution for more granular data, ensuring proper instantiation and usage of
RollingWindow
, refining the trade execution logic with additional checks, and incorporating error handling and logging for better diagnostics.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.
David Eldringhoff
Have you coded for options before? do you have a working template to pull an option and buy in some other code? When I have a hard time with something I start by just trying to find a way to pull an options contract and store it. Do a lot of logging to figure out where the problems are. Do you know if your option chain is even finding a contract? Have you logged it? Here is my example of a working way to pull same day DTE0 QQQ contracts for a covered call strategy I ran before
.ekz.
It's not clear what your code is trying to do on these steps:
“Calculate the trade signal” and “Place the appropriate straddle trades”
Can you share the blog post where you learned about the strategy? I'd like to try coding it.
Mislav Sagovac
.ekz. , Here is the detailed description of the strategy:
Trading 0DTE Options with the IBKR Native API - Robot Wealth
.ekz.
Got it. Will take a look. Thanks.
Mislav Sagovac
.ekz. , Have you looked at blog post :)
I am planning to give it another try next week, so I am interesed have you developed anything ?
Mislav Sagovac
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!