I am trying to figure out how to liquidate my options as a limit order. The issue I am running into is I can't figure out how to select the options I previously bought and sell that exact quantity and those specific options. I'm trying to figure out if their is a parameter or variable thats available to select all owned options and quantity amounts of owned contracts. Any help is appreciated.
Halldor Andersen
Hi, Sa.
The documentation section on QuantConnect Options API is a good reference for algorithm development, using options.
In the attached backtest I demonstrate how to retrieve quantity for each option contract in the portfolio, and use it as an input for self.LimitOrder(), in order to liquidate the position. The algorithm places one limit buy order when the value of the indicator RSI is less than 20 and liquidates the options holdings using limit sell order when RSI is higher than 40.
Sa Py
Hello Halldor,
Â
Thank you for that example is helped me get on the right track, but I needed it to be able to continue purchasing contracts continously(past just one time and liquidating). I tried to remove the self.activeOrder function to allow it to not have to wait for liquidating contracts but it now seems to sell contracts even though the if statement is > 0 quantity. Any ideas on why?
Â
Â
Halldor Andersen
Hi Sa.
I'm glad I could help. If activeOrder flag is removed, then a limit buy order is placed every minute, as long as the indicator value of RSI is less than 20, this might result in an open position in multiple options contracts.
Liquidating multiple options contracts in the portfolio, without the activeOrder flag, calls for a different input for LimitOrder() in the liquidation section of the code. Also, we need to add Transactions.CancelOpenOrders(contract) to cancel existing orders that did not get filled.
In the attached backtest, I've made adjustments to your code so that the algorithm buys option contracts, using LimitOrder(), as long as the value of the indicator RSI is less than 20. The algorithm emits a limit sell order for the options contracts in the portfolio using LimitOrder() if the value of RSI is larger than 40. Keep in mind, a fill is not guaranteed if LimitOrder() is used to liquidate security.
Sa Py
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!