Overall Statistics
Total Orders
89
Average Win
1.04%
Average Loss
0%
Compounding Annual Return
12.720%
Drawdown
7.500%
Expectancy
-0.087
Start Equity
1000000
End Equity
1656016.95
Net Profit
65.602%
Sharpe Ratio
1.083
Sortino Ratio
0.849
Probabilistic Sharpe Ratio
87.680%
Loss Rate
9%
Win Rate
91%
Profit-Loss Ratio
0
Alpha
0.033
Beta
0.285
Annual Standard Deviation
0.057
Annual Variance
0.003
Information Ratio
-0.349
Tracking Error
0.109
Treynor Ratio
0.216
Total Fees
$1161.55
Estimated Strategy Capacity
$6000.00
Lowest Capacity Asset
SPY 32JR182FXC546|SPY R735QTJ8XC9X
Portfolio Turnover
0.33%
# region imports
from AlgorithmImports import *
# endregion


class WheelStrategyAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2020, 6, 1)
        self.set_cash(1_000_000)
        self.set_security_initializer(BrokerageModelSecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices)))
        self._equity = self.add_equity("SPY", data_normalization_mode=DataNormalizationMode.Raw)
        self._otm_threshold = 0.05 # 5%
        
    def _get_target_contract(self, right, target_price):
        contract_symbols = self.option_chain_provider.get_option_contract_list(self._equity.symbol, self.time)
        expiry = min([s.id.date for s in contract_symbols if s.id.date.date() > self.time.date() + timedelta(30)])
        filtered_symbols = [
            s for s in contract_symbols 
            if (s.id.date == expiry and s.id.option_right == right and 
                (s.id.strike_price <= target_price if right == OptionRight.PUT else s.id.strike_price >= target_price))
        ]
        symbol = sorted(filtered_symbols, key=lambda s: s.id.strike_price, reverse=right == OptionRight.PUT)[0]
        self.add_option_contract(symbol)
        return symbol

    def on_data(self, data):
        if not self.portfolio.invested and self.is_market_open(self._equity.symbol):
            symbol = self._get_target_contract(OptionRight.PUT, self._equity.price * (1-self._otm_threshold))
            self.set_holdings(symbol, -0.2)
        elif [self._equity.symbol] == [symbol for symbol, holding in self.portfolio.items() if holding.invested]:
            symbol = self._get_target_contract(OptionRight.CALL, self._equity.price * (1+self._otm_threshold))
            self.market_order(symbol, -self._equity.holdings.quantity / 100)