Overall Statistics
Total Orders
272
Average Win
14.82%
Average Loss
-10.83%
Compounding Annual Return
-41.649%
Drawdown
90.700%
Expectancy
-0.042
Start Equity
100000
End Equity
9760
Net Profit
-90.240%
Sharpe Ratio
-1.279
Sortino Ratio
-0.623
Probabilistic Sharpe Ratio
0.000%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
1.37
Alpha
-0.321
Beta
0.103
Annual Standard Deviation
0.244
Annual Variance
0.06
Information Ratio
-1.341
Tracking Error
0.294
Treynor Ratio
-3.033
Total Fees
$9594.00
Estimated Strategy Capacity
$29000.00
Lowest Capacity Asset
SPY YHQNONJRKIVA|SPY R735QTJ8XC9X
Portfolio Turnover
8.53%
# region imports
from AlgorithmImports import *
from datetime import timedelta
import pandas as pd
# endregion

class SimpleStraddleBacktest(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2020, 1, 1)
        self.set_end_date(2024, 6, 1)
        self.set_cash(100_000)

        # Setting up asset and equity
        self._option = self.add_option("SPY", Resolution.MINUTE)        
        self._symbol = self._option.symbol
        self._option.set_filter(timedelta(7), timedelta(7))

        # Schedule liquidate
        self.schedule.on(self.date_rules.every_day(self._symbol), self.time_rules.at(9,33), self.liquidate_positions)
        
    def on_data(self, data:Slice):

        # Start the strategy at 3:58 PM
        if self.time.hour == 15 and self.time.minute == 38:
            
            # Find the list of contracts
            chain = data.option_chains.get(self._symbol)

            if chain is None:
                return
            
            calls = [x for x in chain if x.right == OptionRight.CALL]
            puts = [x for x in chain if x.right == OptionRight.PUT]
            
            straddles = [[call, put] for call in calls for put in puts if call.strike == put.strike]
            self.cheapest_straddle = sorted(straddles, key=lambda straddle: straddle[0].last_price + straddle[1].last_price)[0]
            self.log(f"Cheapest straddle contracts: {self.cheapest_straddle[0].symbol.value, self.cheapest_straddle[1].symbol.value}")

            # Set leverage
            self.securities[self.cheapest_straddle[0].symbol].set_leverage(1)
            self.securities[self.cheapest_straddle[1].symbol].set_leverage(1)


            if not self.portfolio.invested:
                # Buying straddle
                self.set_holdings(self.cheapest_straddle[0].symbol, .5)
                self.set_holdings(self.cheapest_straddle[1].symbol, .5)

    def liquidate_positions(self):
        self.liquidate()