Hey QC Community! I'm new to the platform and absolute love it ❤️
I was trying to use custom data to run an Options Backtest, although I was successfully able to import the custom data, I'm not able to send Buy orders. I'm assuming there is some issue with my BuyCall() and BuyPut() methods.
Would be great if you could help me out!
OnData() method:
def OnData(self, data):
if data.ContainsKey(self.symbol):
value = data[self.symbol].Value
self.Log("Data Added: " + str(value))
data_list.append(value)
# To check if portfolio is already invested
option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==Se curityType.Option]
if option_invested:
if self.Time + timedelta(1) > option_invested[0].ID.Date: # If only one day is left until expiration, we liquidate the position
self.Liquidate(option_invested[0], "Too close to expiration")
return
# code logic not already invested
try:
if not self.Portfolio.Invested:
if data_list[-1] > data_list[-2]: # If current score is more than the previous one, Buy Call
self.Log("Buy Call Option")
for i in data.OptionChains:
chains = i.Value
self.BuyCall(chains)
elif data_list[-1] < data_list[-2]: # If current score is less than the previous one, Buy Put
self.Log("Buy Put Option")
for i in data.OptionChains:
chains = i.Value
self.BuyPut(chains)
else:
self.Log("NO NEW TRADE: PORTFOLIO ALREADY INVESTED")
except:
pass
The custom BuyCall() method:
def BuyCall(self, chains): # To find the right option and buy it
expiry = sorted(chains, key = lambda x: x.Expiry, reverse = True)[0].Expiry # Picking the furthest away expiration date
calls = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Call]
call_contracts = sorted(calls, key = lambda x: abs(x.Strike - x.UndetlyingLastPrice))
if len(call_contracts) == 0:
return
self.call = call_contract[0]
quantity = self.Portfolio.TotalPortfolioValue / self.call.AskPrice
quantity = int(0.05 * (quantity / 100))
self.Buy(self.call.Symbol, quantity)
The custom BuyPut() method:
def BuyPut(self, chains): # To find the right option and buy it
expiry = sorted(chains, key = lambda x: x.Expiry, reverse = True)[0].Expiry # Picking the furthest away expiration date
puts = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Put]
put_contracts = sorted(calls, key = lambda x: abs(x.Strike - x.UndetlyingLastPrice))
if len(call_contracts) == 0:
return
self.call = call_contract[0]
quantity = self.Portfolio.TotalPortfolioValue / self.call.AskPrice
quantity = int(0.05 * (quantity / 100))
self.Buy(self.call.Symbol, -quantity)
Nico Xenox
Hey Sankalp Bhatia,
not sure if it was intentionally or not but on the custom BuyPut you're still buying calls.
Hope it helps ;)
Sankalp Bhatia
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!