Overall Statistics
Total Orders
478
Average Win
1.58%
Average Loss
-1.03%
Compounding Annual Return
15.882%
Drawdown
33.600%
Expectancy
0.699
Start Equity
1000000
End Equity
6055745.16
Net Profit
505.575%
Sharpe Ratio
0.721
Sortino Ratio
0.734
Probabilistic Sharpe Ratio
20.773%
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
1.53
Alpha
0.011
Beta
0.997
Annual Standard Deviation
0.139
Annual Variance
0.019
Information Ratio
0.689
Tracking Error
0.015
Treynor Ratio
0.1
Total Fees
$19099.23
Estimated Strategy Capacity
$1800000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
3.72%
# region imports
from AlgorithmImports import *
# endregion


class Topseven(QCAlgorithm):
    _winter = [
          datetime(2012, 2, 2),
          datetime(2013, 2, 2),
          datetime(2014, 2, 2),
          datetime(2015, 2, 2),
          datetime(2016, 2, 2),
          datetime(2017, 2, 2),
          datetime(2018, 2, 2),
          datetime(2019, 2, 2),
          datetime(2020, 2, 2),
          datetime(2021, 2, 2),
          datetime(2022, 2, 2),
          datetime(2023, 2, 2),
    # ...
         ]
    _spring = [
          datetime(2012, 4, 30),
          datetime(2013, 4, 30),
          datetime(2014, 4, 30),
          datetime(2015, 4, 30),
          datetime(2016, 4, 30),
          datetime(2017, 4, 30),
          datetime(2018, 4, 30),
          datetime(2019, 4, 30),
          datetime(2020, 4, 30),
          datetime(2021, 4, 30),
          datetime(2022, 4, 30),
          datetime(2023, 4, 30),
    # ...
         ]
    _summer = [
          datetime(2012, 7, 30),
          datetime(2013, 7, 30),
          datetime(2014, 7, 30),
          datetime(2015, 7, 30),
          datetime(2016, 7, 30),
          datetime(2017, 7, 30),
          datetime(2018, 7, 30),
          datetime(2019, 7, 30),
          datetime(2020, 7, 30),
          datetime(2021, 7, 30),
          datetime(2022, 7, 30),
          datetime(2023, 7, 30),
    # ...
         ]
    _autumn = [
          datetime(2012, 10, 31),
          datetime(2013, 10, 31),
          datetime(2014, 10, 31),
          datetime(2015, 10, 31),
          datetime(2016, 10, 31),
          datetime(2017, 10, 31),
          datetime(2018, 10, 31),
          datetime(2019, 10, 31),
          datetime(2020, 10, 31),
          datetime(2021, 10, 31),
          datetime(2022, 10, 31),
          datetime(2023, 10, 31),
    # ...
         ]

    def initialize(self):
        # Locally Lean installs free sample data, to download more data please visit https://www.quantconnect.com/docs/v2/lean-cli/datasets/downloading-data
        self.set_start_date(2012, 5, 18)  # Set Start Date
        self.set_end_date(2024, 10, 30)  # Set End Date
        self.set_cash(1000000)  # Set Strategy Cash
        self._spy = self.add_equity("SPY", Resolution.DAILY)
        self._amzn = self.add_equity("AMZN", Resolution.DAILY)
        self._google = self.add_equity("GOOGL", Resolution.DAILY)
        self._meta = self.add_equity("META", Resolution.DAILY)
        #self._spy = self.add_equity("SPY", Resolution.DAILY)
        self._appl = self.add_equity("APPL", Resolution.DAILY)
        self._msft = self.add_equity("MSFT", Resolution.DAILY)
        
        for holidays in [self._winter, self._spring, self._summer, self._autumn]:
            for holiday in holidays:
                self.schedule.on(
                self.date_rules.on(self._spy.exchange.hours.get_next_market_close(holiday - timedelta(14), False)),
                self.time_rules.before_market_close(self._spy.symbol, 1),
                lambda: self.set_holdings([PortfolioTarget(self._amzn.symbol, 0.2),PortfolioTarget(self._google.symbol, 0.2), PortfolioTarget(self._meta.symbol, 0.2), PortfolioTarget(self._appl.symbol, 0.2), PortfolioTarget(self._msft.symbol, 0.2)], True)
        )
        # Hold SPY after the holiday.
                self.schedule.on(
                self.date_rules.on(self._spy.exchange.hours.get_next_market_close(holiday + timedelta(1), False)),
                self.time_rules.before_market_close(self._spy.symbol, 1),
                lambda: self.set_holdings([PortfolioTarget(self._spy.symbol, 1)], True)
        )
       

    def on_data(self, data: Slice):
        """on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        """
        if not self._spy.holdings.invested:
            self.set_holdings("SPY", 1)
            self.debug("Purchased Stock")
        if self._amzn.holdings.invested:
            self.liquidate(self._amzn.symbol)
            self.liquidate(self._google.symbol)
            self.liquidate(self._msft.symbol)
            self.liquidate(self._appl.symbol)
            self.liquidate(self._meta.symbol)
            self._contract_symbol = None