Hello,
I'm new here and with python coding.
I'm trying to figure out how to choose options contract base on time and not price, e.g buy an AAPL CALL contract at 10:00 (whatever the underlaying price is) and sell the next day at 16:00.
can someone help out?
Soumya sen
Please check out this tutorial on Options and you should be able to place the orders.
https://www.quantconnect.com/tutorials/introduction-to-options/quantconnect-options-apiTal Shoham
Hi Sen,
Whan I run the code from the tutorial I'm getting this runtime error:
Runtime Error: UnboundLocalError : local variable 'i' referenced before assignment
at OnData in main.py:line 82
:: optionchain = i.Value
UnboundLocalError : local variable 'i' referenced before assignment (Open Stacktrace).
What is wrong?
def OnData(self,slice): for i in slice.OptionChains: if i.Key != self.symbol: continue optionchain = i.Value self.Log("underlying price:" + str(optionchain.Underlying.Price)) df = pd.DataFrame([[x.Right,float(x.Strike),x.Expiry,float(x.BidPrice),float(x.AskPrice)] for x in optionchain], index=[x.Symbol.Value for x in optionchain], columns=['type(call 0, put 1)', 'strike', 'expiry', 'ask price', 'bid price']) self.Log(str(df))
Alexandre Catarino
Hi Tal Shoham ,
I cloned the backtest from the tutorial soumya sen proposed and it doesn't reproduce the runtime error.
Alexandre Catarino
Hi Tal Shoham ,
I have modified the example above to use Schedule Events to buy the call at 10 am. It is a starting point for the strategy you want to implement.
Please note that since the schedule event method is parameterless, therefore we need to use self.CurrentSlice instead of the parameter slice in OnData method.
Tal Shoham
Hi Alexandre,
That's great.
Thanks.
Tal Shoham
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!