Overall Statistics
Total Trades
363
Average Win
0.01%
Average Loss
-0.01%
Compounding Annual Return
-0.310%
Drawdown
0.400%
Expectancy
-0.275
Net Profit
-0.330%
Sharpe Ratio
-0.584
Probabilistic Sharpe Ratio
2.470%
Loss Rate
74%
Win Rate
26%
Profit-Loss Ratio
1.78
Alpha
-0.002
Beta
0.007
Annual Standard Deviation
0.004
Annual Variance
0
Information Ratio
0.386
Tracking Error
0.197
Treynor Ratio
-0.307
Total Fees
$363.00
Estimated Strategy Capacity
$470000000.00
Lowest Capacity Asset
TGTX VGZQ8WW1GODH
from AlgorithmImports import *
from QuantConnect.Orders import *

class MyQC500UniverseSelectionModel(QC500UniverseSelectionModel):
    def SelectCoarse(self, algorithm, coarse):
        # Filter out securities that have a closing price less than $10
        filteredCoarse = [x for x in coarse if x.Price > 10]
        return filteredCoarse

class CalibratedOptimizedCompensator(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 1, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        # Set QC500 Universe Selection Model
        self.SetUniverseSelection(MyQC500UniverseSelectionModel())
        self.AddSecurity(SecurityType.Equity, 'SJI R735QTJ8XC9X', Resolution.Daily)
        # Dictionary to hold SecurityData 
        self.data = {}
        self.portfolio_count = 0

    def OnData(self, data):
        sortedByMOM = [k for k in self.data.keys() if self.data[k].mom is not None]
        sortedByMOM = sorted(sortedByMOM, key=lambda k : self.data[k].mom.Current.Value, reverse=True)
        # Select top 10 symbols
        rankings = sortedByMOM[:10]
        for symbol in rankings:
            if data.ContainsKey(symbol) and self.data[symbol].sma45.Current.Value > self.data[symbol].sma44:
                if not self.Portfolio[symbol].Invested and self.portfolio_count < 10:
                    self.portfolio_count += 1
                    self.SetHoldings(symbol, 1/len(self.Portfolio))
        for kvp in self.Portfolio:
            symbol = kvp.Key
            if symbol not in rankings or self.data[symbol].sma45.Current.Value < self.data[symbol].sma44:
                self.Liquidate(symbol)
                self.portfolio_count = self.portfolio_count - 1
                # if self.portfolio_count < 10:
                #     # Find the highest ranking symbol that meets the buying criteria and is not invested
                #     for new_symbol in rankings:
                #         if new_symbol not in self.Portfolio and data.ContainsKey(new_symbol) and self.data[new_symbol].sma45.Current.Value > self.data[new_symbol].sma44:
                #             self.SetHoldings(new_symbol, 1/len(self.Portfolio))
                #             self.portfolio_count += 1
                #             break


                
                  
                
    def OnSecuritiesChanged(self, changes):
        
        for security in changes.AddedSecurities:
            symbol = security.Symbol
            if symbol not in self.data:
                self.data[symbol] = SecurityData(self, symbol, security)



class SecurityData:
    
    def __init__(self, algorithm, symbol, security):
       
        self.algorithm = algorithm
        self.symbol = symbol
        self.security = security
        self.profitTarget = 0.0
        self.entry_price = 0
        # Retrieves ROIC fundamental
        self.mom = algorithm.LOGR(symbol, 60, Resolution.Daily)
        
        self.sma45 = algorithm.SMA(symbol,80,Resolution.Daily)
       # self.sma44 = algorithm.SMA(symbol,79,Resolution.Daily)
        # Initialize MOM indicator with historical data
        history = algorithm.History(symbol,81, Resolution.Daily)
              # Calculate the SMA for the previous day
        self.sma44 = history.close.rolling(window=80).mean()[-1]
        
        if not history.empty:
            history = history.close.unstack(0)
            if not history.empty:
                df = history[symbol]
                for time, close in df.iteritems():
                    self.mom.Update(time, close)
                    self.sma45.Update(time,close)
                   # self.sma44.Update(time,close)
        else:
            self.mom = None
            self.sma45 = None
            self.sma44 = None