Trying to close an option based on the underlyinh price. I have a code here, but I don't think that this is the right way to do it:
if (self.Securities[self.equity_symbol].Price < self.option_symbol.UnderlyingLastPrice):
self._buyTicket = self.Buy(self.option_symbol.Symbol, 10)
I want the code above to be always true, not only in one day. It is similar to creating an order event, but the condition is on the underlying price instead of the option price itself.
Thanks
Jack Simonson
Hi Nick,
If by 'to always be true' you mean that this condition will be evaluated for every contract, then I think the best way to do this would be to write a function which takes an option contract and a Trade Bar as arguments and then compares the underlying last price to the current price of the equity. Then, if the condition is matched as the algorithm loops through the current contracts in the Universe, you can place the Buy order inside the function. I've attached a backtest that implements a simple version of this.
However, self.Securities[self.equity_symbol].Price and self.option_symbol.UnderlyingLastPrice will return the same value, so the condition to check for inequality will always return False. If you attach your code or elaborate more of what exactly you're trying to accomplish, that would help me to make suggestions or provide solutions to any issues you are encountering.
Coder
Hi Jack,
Thanks for your code. I have modified it to illustrate my question better. I have bought a put contract and want to sell it at some point in future when the underlying_price is less than the underlying_price when the option was originally bought.
I also have another concern. In this example I have simplified the condition, but what I really want to do is to sell the option after some profit or loss, but I am not sure how to calculate profit of an option strategy.
Jack Simonson
Hi Nick,
Depending on your options strategy, you can calculate profit in different ways.
If you want to sell the option before expiration, then you can calculate profit using the UnrealizedProfit attribute of a security.
self.Portfolio[optionContract.Symbol].UnrealizedProfit
If you plan to exercise your options/let them expire, then you would want to calculate the exercise value using something like the following:
def CalculateExerciseProfit(self, optionContract, dataTime): if optionContract.Expiry == dataTime: strike = optionContract.Strike underlying = optionContract.UnderlyingLastPrice quantity = self.Portfolio[optionContract.Symbol].Quantity ## Options contracts are for 100 shares, so multiply by 100 ## and the number of contracts that will be exercised if (optionContract.Right == OptionRight.Call): exerciseProfit = (underlying - strike) * 100 * quantity if (optionContract.Right == OptionRight.Put): exerciseProfit = (strike - underlying) * 100 * quantity return exerciseProfit
This value could be calculated anytime but is only relevant when the options expire. It is sub-optimal to exercise an option before expiration unless it is an American option whose underlying pays a dividend.
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!