Hey Akash,1.
We can find the profit for a security by accessing
self.Portfolio[security.Symbol].Profit
and the total profit of our portfolio with
self.Portfolio.TotalProfit
Let's calculate the profit when an option exercise order is submitted by the algorithm. We can detect this in the OnOrderEvent method, which fires each time there is a new order or an order is updated.
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Type == OrderType.OptionExercise:
self.Debug(f"{orderEvent.Symbol} Profit: {self.Portfolio[orderEvent.Symbol].Profit}, Total Profit: {self.Portfolio.TotalProfit}")
I've attached a backtest which demonstrates this using the iron condor template from the tutorial.
2. You can use the last close price to calculate the premium. Lean assumes all orders are completely filled at the current market price. You can learn more about fill models in the documentation.