Hi,
I have a vertical spread strategy and it works fine until one of the short put options gets assigned. After the assignment no more trades happen, and the status for the assigned option is Invalid. And the unrealized proft is non-zero even after liquidity.
Looks like the option assignment breaks the algorithm in diferent ways. Should the option assignment be treated differently?
Thanks
Alethea Lin
Hi Nick,
Please post your algorithm by attaching the backtest. This will allow the community and us to help address your questions more efficiently.
Coder
Hi Alethea, I have provided a picture of the option getting assigned. After this assignment all other orders will be invalid. for some reason I can't attach the algo here. and her eis the message in the console: Backtest Handled Error: The security with symbol 'SPY 170818P00243000' is marked as non-tradable. Please let me know if I can provide more info. Thanks
Alethea Lin
Hi Nick,
We cannot figure out the exact issue without the backtest. You should be able to attach your backtest using the Attach Backtest button. If not, please copy and paste your code using the Insert Code Snippet button.
Coder
HI Alethea, I founda workaround which is exercising the option few minutes before expiration/assignmnet.
Alethea Lin
Hi Nick,
I am glad you found a workaround for the issue. Would you mind still sharing your backtest or an example code that reproduces the bug with us? We would love to look into the issue so you and the community members can avoid the workaround in the future!
Thanks for your support.
Coder
Hi Alethea,
I don't have the previous code anymore. but I think the main issue was using weeklyoption prices. After switching to setfliter most issues are gone.
Thanks
Coder
Hi,
Is there a way to find out if the option is assigned (on the sell leg), so that I can immediatley close the other position (buy leg)? Thanks
Yiyun Wu
Hi Nick,
Once an option is exercised, an order event will be created and passed to OnOrderEvent method where we can identified it by OrderType. Below is the example code you can try:
def OnOrderEvent(self, orderEvent): order = self.Transactions.GetOrderById(orderEvent.OrderId) if order.Type == OrderType.OptionExercise: pass # do stuff
Attached backtest is a simple example of how to use OnOrderEvent. In the Log page, it outputs excercised options information.
Hope this helps!
Coder
Hi Yiyun,
Thanks for your answer. Is there a way to be only informed of assignment.
OrderType.OptionExercise doesn't recognize between exercise and assignment. I am interested only when assignmnet happens.When looking at the Orders, here is how the Tag for the assigned option looks like: Simulated option assignment before expiration - Adjusting(or removing) the exercised/assigned option - Option Exercise/AssignmentBut using self.log in orderEvent, it removes:
Simulated option assignment before expirationAlexandre Catarino
Hi Nick M ,
Sorry about the wait.
The OrderEvent object has a boolean attribute, IsAssignment, that can be used to distinguish the two:
def OnOrderEvent(self, orderEvent): if orderEvent.IsAssignment: order = self.Transactions.GetOrderById(orderEvent.OrderId) if order.Type == OrderType.OptionExercise: self.Log(str(order))
Coder
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!