Overall Statistics
Total Orders
10018
Average Win
3.48%
Average Loss
-1.48%
Compounding Annual Return
114.871%
Drawdown
66.000%
Expectancy
0.261
Start Equity
100000
End Equity
214421.59
Net Profit
114.422%
Sharpe Ratio
1.702
Sortino Ratio
1.565
Probabilistic Sharpe Ratio
56.567%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
2.36
Alpha
0.775
Beta
2.245
Annual Standard Deviation
0.668
Annual Variance
0.446
Information Ratio
2.331
Tracking Error
0.418
Treynor Ratio
0.506
Total Fees
$670.95
Estimated Strategy Capacity
$5300000.00
Lowest Capacity Asset
BA R735QTJ8XC9X
Portfolio Turnover
22.91%
# region imports
from AlgorithmImports import *
# endregion

class AlertFluorescentYellowChinchilla(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2020, 1, 3)
        self.set_end_date(2021, 1, 1)
        self.set_cash(100000)
        
        self.rebalanceTime = self.Time
        self.AddUniverse(self.CoarseFilter, self.FineFilter)
        self.UniverseSettings.Resolution = Resolution.Minute
        self.final_universe_size = 10
        self.activeStocks = set()
        self.SetWarmUp(10*390)
        self.AddEquity("AAPL", Resolution.Minute)
        self.neverInvested = True

        # set trailing stop risk management
        self.SetRiskManagement(TrailingStopRiskManagementModel(0.1))

    def CoarseFilter(self, coarse):
            # Rebalancing monthly
            if self.Time <= self.rebalanceTime:
                return self.Universe.Unchanged
            self.rebalanceTime = self.Time + timedelta(days=360)
            
            sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
            return [x.Symbol for x in sortedByDollarVolume if x.HasFundamentalData][:1000]
        
    def FineFilter(self, fine):
        sortedbyVolume = sorted(fine, key=lambda x: x.DollarVolume, reverse=True ) 
        fine_output = [x.Symbol for x in sortedbyVolume if x.MarketCap > 0][:self.final_universe_size]

        return fine_output

    def OnSecuritiesChanged(self, changes):
        # region removed securities
        for x in changes.RemovedSecurities:
            self.Liquidate(x.Symbol)
            if x.Symbol in self.activeStocks:
                self.activeStocks.remove(x.Symbol)

        for x in changes.AddedSecurities:
            self.activeStocks.add(x.Symbol)

    def on_data(self, data: Slice):

        if len(self.activeStocks) == 1 and not self.Portfolio["AAPL"].Invested:
            self.SetHoldings("AAPL", 2)
            self.neverInvested = True

        for x in self.activeStocks:
            if data.ContainsKey(x) and data[x] != None:
                if self.neverInvested and str(x).split(" ")[0] != "AAPL":
                    self.Liquidate("AAPL")
                    self.neverInvested = False
                if not self.Portfolio[x].Invested and self.Portfolio.MarginRemaining > 1000:
                    self.SetHoldings(x, .2)