Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
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)
        self.AddSecurity(SecurityType.Equity, 'SPY', Resolution.Daily)
        # Dictionary to hold SecurityData 
        self.data = {}
        self.atr = {}
        self.ts = {}
        self.profit_factor = 3
        self.Schedule.On(self.DateRules.EveryDay("SPY"),
                 self.TimeRules.AfterMarketOpen("SPY", -10),
                 self.on_market_opendata)
    

    def on_market_opendata(self):
    
             
          sortedByReturns = [k for k in self.data.keys() if self.data[k].returns is not None]
          sortedByReturns = sorted(sortedByReturns, key=lambda k : self.data[k].returns.Current.Value, reverse=True)
         # invested_symbols = [symbol for symbol in self.symbols if self.Portfolio[self.symbols[symbol].symbol].Invested ]
          current_holding = len(self.Portfolio)
        
        # Select top 10 symbols
          rankings = sortedByReturns[:10]
       
        

          for symbol in rankings:
            if symbol in self.data:
              self.data[symbol].update_previous_sma80()
            if current_holding < 10:
               self.Log("the value of previous_sma80 is ",self.data[symbol].previous_sma80)
               previous_sma80 = self.data[symbol].check_previous_sma80()
               if previous_sma80 is not None and (self.data[symbol].sma80.Current.Value > previous_sma80):
                  if not self.Portfolio[symbol].Invested:
                    self.SetHoldings(symbol, 1/len(self.Portfolio))
          for kvp in self.Portfolio:
            symbol = kvp.Key
            holding = kvp.Value
            previous_sma80 = self.data[symbol].check_previous_sma80()
            if previous_sma80 is not None and (self.data[symbol].sma80.Current.Value < previous_sma80):
                self.Liquidate(symbol)
                  
          
     

        
          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
        self.temp = 1
        self.returns = algorithm.KER(symbol, 60)
        self.sma80 = algorithm.SMA(symbol,80,Resolution.Daily)
        self.previous_sma80 = RollingWindow[decimal](2)
    def update_previous_sma80(self):
        self.previous_sma80.Add(self.sma80.Current.Value)

    def check_previous_sma80(self):
        return self.previous_sma80[1]

        if not history.empty:
            history = history.close.unstack(0)
            if not history.empty:
                df = history[symbol]
                for time, close in df.iteritems():
                    self.returns.Update(time, close)
                    self.sma80.Update(time,close)
                    self.previous_sma80.Update(time,close)
            self.returns = None
            self.sma80 = None
           # self.sma44 = None