Overall Statistics
Total Orders
17
Average Win
1.02%
Average Loss
-0.68%
Compounding Annual Return
1.329%
Drawdown
1.500%
Expectancy
0.501
Start Equity
100000
End Equity
100713.5
Net Profit
0.714%
Sharpe Ratio
0.29
Sortino Ratio
0.366
Probabilistic Sharpe Ratio
29.677%
Loss Rate
40%
Win Rate
60%
Profit-Loss Ratio
1.50
Alpha
-0.035
Beta
0.193
Annual Standard Deviation
0.024
Annual Variance
0.001
Information Ratio
-2.35
Tracking Error
0.089
Treynor Ratio
0.036
Total Fees
$11.00
Estimated Strategy Capacity
$550000.00
Lowest Capacity Asset
SPY XPFJZXMFFOME|SPY R735QTJ8XC9X
Portfolio Turnover
1.60%
# region imports
from AlgorithmImports import *
# endregion

class HipsterYellowGreenPanda(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2021, 1, 1)
        self.set_end_date(2021, 7, 17)
        self.set_cash(100000)
        self.equity_symbol = self.add_equity("SPY", data_normalization_mode=DataNormalizationMode.RAW).symbol
        self.option_symbol = self.add_option(self.equity_symbol).symbol

        self.contract = None

    def on_data(self, slice: Slice) -> None:
        if self.portfolio[self.equity_symbol].invested:
            self.liquidate(self.equity_symbol)

        if self.contract is not None and self.portfolio[self.contract.symbol].invested:
            return

        if slice.option_chains.contains_key(self.option_symbol):
            chain = slice.option_chains[self.option_symbol]

            # Select call contracts
            calls = [contract for contract in chain if contract.right == OptionRight.CALL]
            if len(calls) == 0:
                return
            
            # Select the call contracts with the furthest expiration
            furthest_expiry = sorted(calls, key = lambda x: x.expiry, reverse=True)[0].expiry
            furthest_expiry_calls = [contract for contract in calls if contract.expiry == furthest_expiry]
            
            # From the remaining contracts, select the one with its strike closest to the underlying price
            self.contract = sorted(furthest_expiry_calls, key = lambda x: abs(chain.underlying.price - x.strike))[0]
            self.market_order(self.contract.symbol, 1)