Hi all, 

Not too sure why my live trading - after it runs the algo and fits the criteria, it bought the options, but it didnt subsequently submitted the profit taking limit order in the algo. Can someone please assist with this. 

The following is the code i use to check if it should buy the call options or not. 

  1. def BuyCall(self,chains):
  2. self.S = self.Securities[self.equity].Close
  3. expiry = sorted(chains,key = lambda x: x.Expiry, reverse=True)[0].Expiry
  4. calls = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Call]
  5. call_contracts = sorted(calls,key = lambda x: abs(x.Strike - x.UnderlyingLastPrice < 0))
  6. if len(call_contracts) == 0:
  7. self.Debug("No Contracts Found")
  8. return
  9. self.Log("Contracts Found!")
  10. self.call = call_contracts[0]
  11. self.K = self.call.Strike
  12. self.T = ((self.call.Expiry - self.Time).days)/(365)
  13. option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
  14. if (option_invested):
  15. for i in range(len(option_invested)):
  16. if (str(self.Portfolio[option_invested[i]].Symbol.Value) == str(self.call)):
  17. self.Log("Already bought the Contract")
  18. return
  19. if self.r is None:
  20. history = self.History(USTreasuryYieldCurveRate, self.yieldCurveTwo, 60, Resolution.Daily)
  21. twoyear = history['twoyear'][-1]
  22. self.r = twoyear / 100
  23. self.Debug((f"| Previous Rates Used -- Succesful {self.Time} -- {self.r}"))
  24. self.option_price = self.BlackScholesCall(self.S, self.K, self.T, self.r, self.sigma)
  25. self.Log((f"| Option Price Calculated - Succesful {self.Time} [+]-- Contracts {str(self.call)} || Stock @ {str(self.call.UnderlyingLastPrice)} Option Price BS @ {(self.option_price)} rates: {self.r} Option Price Market @ {self.call.AskPrice} DTE @ {(self.call.Expiry - self.Time).days}"))
  26. if self.call.AskPrice > self.option_price:
  27. self.Log((f"| Option too Expensive! {self.Time})"))
  28. return
  29. if self.call.AskPrice == 0: return
  30. self.SetHoldings(self.call.Symbol, 0.5)
  31. quantity = self.Portfolio[self.call.Symbol].Quantity # <-- quantity is zero and the following orders will be invalid
  32. if quantity != 0:
  33. self.LimitOrder(self.call.Symbol, -quantity, (self.call.AskPrice * self.Profit_Taking))
  34. self.Log ("\r+-------------")
  35. self.Log (f"| {self.Time} [+] Bought Contracts -- {str(self.call)}")
  36. self.Log(f"Order Quantity filled: {self.Portfolio[self.call.Symbol].Quantity}; Fill price: {self.Portfolio[self.call.Symbol].AveragePrice} Limit price: {self.call.AskPrice * self.Profit_Taking}")
  37. self.LastTime = self.Time.day
  38. self.Log ("\r+-------------")
  39. else: self.Log(f"| {self.Time} -- No Money")
+ Expand

Author

Muhammad Yusuf Bin Salleh

July 2023