Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from QuantConnect.Securities.Option import OptionStrategies

class BasicTemplateOptionStrategyAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetCash(1000000)
        self.SetStartDate(2015,12,24)
        self.SetEndDate(2015,12,24)
        
        self.option = self.AddOption("GOOG")
        self.option_symbol = self.option.Symbol
        self.option.SetFilter(-4, +4, 0, 180)
        
        self.strategy = None

    def OnData(self, data):
        if self.strategy is None:
            for kvp in data.OptionChains:
                chain = kvp.Value
                
                # Select puts with the furthest expiry
                puts = [c for c in chain if c.Right == OptionRight.Put]
                if len(puts) == 0:
                    return
                latest_expiry = sorted(puts, key=lambda f: f.Expiry)[-1].Expiry
                puts = [p for p in puts if p.Expiry == latest_expiry]
                
                # Build strategy
                sorted_by_strike = sorted(set([p.Strike for p in puts]))
                sell_put = sorted_by_strike[-1]
                buy_put = sorted_by_strike[-2]
                
                self.strategy = OptionStrategies.BullPutSpread(self.option_symbol, sell_put, buy_put, latest_expiry)
        
        else:
            
            fee_amount = 0
            for leg in self.strategy.OptionLegs:
                contracts = [c for c in data.OptionChains[self.option.Symbol]]
                contract = [c for c in contracts if c.Strike == leg.Strike and c.Expiry == leg.Expiration and c.Right == leg.Right]
                fee_amount += self.get_fee_amount(contract[0], self.option, leg.Quantity)
                
            self.Quit(f"Fees: {fee_amount}")
            

    def get_fee_amount(self, contract, security, quantity):
        order = MarketOrder(contract, quantity)
        parameters = OrderFeeParameters(security, order)
        fee = security.FeeModel.GetOrderFee(parameters).Value
        return fee.Amount