Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
10000
End Equity
10000
Net Profit
0%
Sharpe Ratio
0
Sortino 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
-2.533
Tracking Error
0.091
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
#region imports
from AlgorithmImports import *
#endregion
class WellDressedSkyBlueSardine(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2024, 1, 1)
        #self.SetEndDate(2021, 1, 1)
        self.SetCash(10000)
        self.rebalanceTime = datetime.min
        self.activeStocks = set()

        #self.universe_selection(alt_coarse)= True
        
        self.AddUniverse(self._fundamental_filter_function)
        self.AddUniverse(BrainSentimentIndicatorUniverse, self.universe_selection)

        self.UniverseSettings.Resolution = Resolution.Daily
        
        self.portfolioTargets = []
        self.fundamental =[]


    def _fundamental_filter_function(self, fundamental: List[Fundamental]) -> List[Symbol]:
        sorted_by_dollar_volume = sorted(fundamental, key=lambda x: x.dollar_volume, reverse=True) 
        self.fundamental = [c.symbol for c in sorted_by_dollar_volume if c.Price>10][:50]
        #self.Debug("{} self.fundamental: {}".format(self.Time, self.fundamental))    
        return Universe.UNCHANGED

    def universe_selection(self, alt_coarse: List[BrainSentimentIndicatorUniverse]) -> List[Symbol]:
        # Rebalancing monthly
        if self.Time <= self.rebalanceTime:
            return self.Universe.Unchanged
        self.Log("{} Entered fine selection".format(self.Time))    
        for x in self.fundamental:
            self.Log("{}".format(x))
        self.rebalanceTime = self.Time + timedelta(30)
        sortedby30daySentiment = sorted([x for x in alt_coarse if x.sentiment_30_days is not None], key=lambda x: x.sentiment_30_days, reverse=True)
        self.list =list (set(self.fundamental) & set(sortedby30daySentiment))
        self.Log("{} self.list".format(self.Time, self.list))    
        for x in self.list:
            self.Log("{}".format(x))
        #sortedby30daySentiment = sorted (alt_coarse, key=lambda x: x.sentiment_30_days, reverse=True)
        #self.alt_coarse = [s.symbol for s in sortedby30daySentiment[:10]]
        return list (set(self.fundamental) & set(sortedby30daySentiment))
        #return [d.symbol for d in sorted([x for x in alt_coarse if x.SentimentalArticleMentions30Days], 
        #            key=lambda x: x.SentimentalArticleMentions30Days, reverse=True)[:10]]

        #sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
        #return [x.Symbol for x in sortedByDollarVolume if x.Price > 10
        #                                        and x.HasFundamentalData][:10]


    def OnSecuritiesChanged(self, changes: List[Fundamental]) -> List[Symbol]:
        # close positions in removed securities
        for x in changes.RemovedSecurities:
            self.Liquidate(x.Symbol)
            self.Log("{} Removed {}".format(self.Time,x.Symbol))
            self.activeStocks.remove(x.Symbol)
        
        # can't open positions here since data might not be added correctly yet
        for x in changes.AddedSecurities:
            self.activeStocks.add(x.Symbol)  
            self.Log("{} Added {}".format(self.Time,x)) 

        # adjust targets if universe has changed
        self.portfolioTargets = [PortfolioTarget(symbol, 1/len(self.activeStocks)) 
                            for symbol in self.activeStocks]
        self.Log("{} Portfolio Targets: {}".format(self.Time, self.portfolioTargets))                     
        self.Log("{} Portfolio: ".format(self.Time)) 
        for symbol in self.activeStocks:
            self.Log(f"{symbol} ")


    def OnData(self, data):

        if self.portfolioTargets == []:
            return
        
        for symbol in self.activeStocks:
            if symbol not in data:
                return
        
        self.SetHoldings(self.portfolioTargets)
        
        self.portfolioTargets = []