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
Portfolio Turnover
0%
# region imports
from AlgorithmImports import *
from sectorcodes import sectorcodes
# endregion

class FatYellowGreenGoat(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 7, 26)
        self.SetEndDate(2021, 10, 26)
        self.SetCash(100000)  
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        self.Data = {}
        barPeriod = 15
        
        for etf in sectorcodes.keys():
            symbol = self.AddEquity(etf,Resolution.Daily).Symbol 
            self.rollingwindow = RollingWindow[TradeBar](barPeriod)

            self.EnableAutomaticIndicatorWarmUp = True 
            self.logr = self.LOGR(etf,barPeriod)
            self.logr.Updated += self.LogrUpdated
            self.logr_window = RollingWindow[IndicatorDataPoint](barPeriod)
            self.Data[etf] = self.logr_window


        
    def LogrUpdated(self,sender,updated):
        self.logr_window.Add(updated)
    
    def OnData(self, data: Slice):
        for etf in sectorcodes.keys(): 
            self.rollingwindow.Add(data[etf])

    def rank(self):
        if not self.logr_window.IsReady: return
        
        temp = {}
        codeswospy = sectorcodes.copy()
        codeswospy.pop('SPY')

        for etf in codeswospy.keys():
            temp[etf] = sum(self.Data[etf]) / sum(self.Data['SPY'])
        sorted_rank = (sorted(temp.items(), key=lambda item: item[1], reverse =True))    
        sorted_etfs = list(sorted_rank.keys())

        return sorted_etfs[:3]
        

    def CoarseSelectionFunction(self,coarse):
        sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
        byprice = [x for x in sortedByDollarVolume if x.DollarVolume > 1000000 and x.Price > 5 and x.HasFundamentalData]
        
        byprice = [x.Symbol for x in byprice][:1000]
        
        return byprice
    

    def FineSelectionFunction(self,fine):
        sorted_etfs = self.rank()
        sector1 = sorted_etfs[0]
        sector2 = sorted_etfs[1]
        sector3 = sorted_etfs[2]
        fineUniverse = [x for x in fine if x.AssetClassification.MorningstarIndustryGroupCode == MorningstarIndustryGroupCode.sector1
                        or x.AssetClassification.MorningstarIndustryGroupCode == MorningstarIndustryGroupCode.sector2
                        or x.AssetClassification.MorningstarIndustryGroupCode == MorningstarIndustryGroupCode.sector3 and x.MarketCap > 5e7]
        tickerSymbolValuesOnly = [c.Symbol for c in fineUniverse]

    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            self.MarketOrder(security,1)
        


       
  


    #def LogReturns(self, etf, barPeriod):
     #   self.window = RollingWindow[Tradebar](barPeriod)
      #  self.logr = self.LOGR(etf,barPeriod)
       # self.logr.Updated += self.LogrUpdated
        #self.logr_window = RollingWindow[IndicatorDataPoint](barPeriod)
#region imports
from AlgorithmImports import *
#endregion
#MorningstarIndustryGroupCode
sectorcodes = {
 "MOO":"Agriculture",
 "ITB":"BuildingMaterials",
 "XME":"MetalsAndMining",
 "SLX":"Steel",
 "EATZ":"Restaurants",
 "PEJ":'TravelAndLeisure',
 "KBWB":"Banks",
 "KIE":"Insurance",
 "VNQ":"REITs",
 "IBB":"Biotechnology",
 "PPH":"DrugManufacturers",
 "IHF":"HealthcareProvidersAndServices",
 "IHI":"MedicalDevicesAndInstruments",
 "XLU":"UtilitiesIndependentPowerProducers",
 "IXP":"TelecommunicationServices",
 "FDN":"MediaDiversified",
 "XLE":"OilAndGas",
 "XRT":"AerospaceAndDefense",
 "XHB":"Construction",
 "IYM":"IndustrialProducts",
 "JETS":"Transportation",
 "IGV":"Software", 
 "XTH":"Hardware", 
 "SMH":"Semiconductors",
 "SPY":"BenchMark" }