Overall Statistics
Total Trades
14
Average Win
0.70%
Average Loss
-0.22%
Compounding Annual Return
3.088%
Drawdown
4.700%
Expectancy
2.013
Net Profit
3.088%
Sharpe Ratio
0.367
Probabilistic Sharpe Ratio
22.263%
Loss Rate
29%
Win Rate
71%
Profit-Loss Ratio
3.22
Alpha
0.028
Beta
0.19
Annual Standard Deviation
0.064
Annual Variance
0.004
Information Ratio
0.354
Tracking Error
0.128
Treynor Ratio
0.123
Total Fees
$14.00
Estimated Strategy Capacity
$21000000.00
Lowest Capacity Asset
PEP R735QTJ8XC9X
Portfolio Turnover
0.56%
#region imports
from AlgorithmImports import *
#endregion


from clr import AddReference

AddReference("System")

AddReference("QuantConnect.Common")

AddReference("QuantConnect.Algorithm")

AddReference("QuantConnect.Algorithm.Framework")



from QuantConnect import *

from QuantConnect.Algorithm import *

from QuantConnect.Algorithm.Framework import *

from QuantConnect.Algorithm.Framework.Portfolio import PortfolioTarget

from QuantConnect.Algorithm.Framework.Risk import RiskManagementModel



class TrailingStopRiskManagementModel(RiskManagementModel):

    '''Provides an implementation of IRiskManagementModel that limits the maximum possible loss

    measured from the highest unrealized profit'''

    def __init__(self, maximumDrawdownPercent = 0.08):

        '''Initializes a new instance of the TrailingStopRiskManagementModel class

        Args:

            maximumDrawdownPercent: The maximum percentage drawdown allowed for algorithm portfolio compared with the highest unrealized profit, defaults to 5% drawdown'''

        self.maximumDrawdownPercent = -abs(maximumDrawdownPercent)

        self.trailingHighs = dict()

        self.lastDay = -1

        

        self.percentGain = 0.005



    def ManageRisk(self, algorithm, targets):

        '''Manages the algorithm's risk at each time step

        Args:

            algorithm: The algorithm instance

            targets: The current portfolio targets to be assessed for risk'''

            

        if algorithm.Time.day == self.lastDay:

            return []

        self.lastDay = algorithm.Time.day

        

        riskAdjustedTargets = list()



        for kvp in algorithm.Securities:

            symbol = kvp.Key

            security = kvp.Value

            percentChange = algorithm.Securities[symbol].Holdings.UnrealizedProfitPercent / 0.01



            # Add newly invested securities

            if symbol not in self.trailingHighs:

                self.trailingHighs[symbol] = security.Close    # Set to average holding cost

                continue

            

            # Remove if not invested

            if not security.Invested and symbol in self.trailingHighs:

                try:

                    self.trailingHighs.pop(symbol, None)

                except:

                    continue

                continue

            

            if percentChange.is_integer() and percentChange > 0:

                self.trailingHighs[symbol] = security.Close

            



            # Check for new highs and update - set to tradebar high

            # if self.trailingHighs[symbol] < security.High:

            #     self.trailingHighs[symbol] = security.High

            #     continue



            # Check for securities past the drawdown limit

            securityHigh = self.trailingHighs[symbol]

            if securityHigh == 0:

                riskAdjustedTargets.append(PortfolioTarget(symbol, 0))

                continue

            

            drawdown = (security.Low / securityHigh) - 1



            if drawdown < self.maximumDrawdownPercent:

                # liquidate

                riskAdjustedTargets.append(PortfolioTarget(symbol, 0))



        return riskAdjustedTargets
#region imports
from AlgorithmImports import *
#endregion
## SIMON LesFlex June 2021 ##
## Modified by Vladimir

from QuantConnect.Python import PythonQuandl

### Simon LesFlex June 2021 ###
### Key Short—Term Economic Indicators. The Key Economic Indicators (KEI) database contains monthly and quarterly statistics 
### (and associated statistical methodological information) for the 33 OECD member and for a selection of non—member countries 
### on a wide variety of economic indicators, namely: quarterly national accounts, industrial production, composite leading indicators, 
### business tendency and consumer opinion surveys, retail trade, consumer and producer prices, hourly earnings, employment/unemployment,
### interest rates, monetary aggregates, exchange rates, international trade and balance of payments. Indicators have been prepared by national statistical 
### agencies primarily to meet the requirements of users within their own country. In most instances, the indicators are compiled in accordance with 
### international statistical guidelines and recommendations. However, national practices may depart from these guidelines, and these departures may 
### impact on comparability between countries. There is an on—going process of review and revision of the contents of the database in order to maximise 
### the relevance of the database for short—term economic analysis.
### For more information see: http://stats.oecd.org/OECDStat_Metadata/ShowMetadata.ashx?Dataset=KEI&Lang=en
### Reference Data Set: https://www.quandl.com/data/OECD/KEI_LOLITOAA_OECDE_ST_M-Leading-indicator-amplitude-adjusted-OECD-Europe-Level-ratio-or-index-Monthly
## keihist = 1400

import numpy as np

class QuandlImporterAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.quandlCode = "OECD/KEI_LOLITOAA_OECDE_ST_M"
        ## Optional argument - personal token necessary for restricted dataset
        #Quandl.SetAuthCode("PrzwuZR28Wqegvv1sdJ7")
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
        
        self.SetStartDate(2018, 1, 1)
        self.SetEndDate(2019, 1, 1)
        self.SetCash(25000)                                         #Set Strategy Cash
        self.SetWarmup(100)
        self.SetBenchmark("SPY")
        self.init = True
        self.kei = self.AddData(QuandlCustomColumns, self.quandlCode, Resolution.Daily, TimeZones.NewYork).Symbol
        self.sma = self.SMA(self.kei, 1)
        self.mom = self.MOMP(self.kei, 2)
        self.XLFsector_symbolDataBySymbol = {}
        self.XLEsector_symbolDataBySymbol = {}
        self.XLBsector_symbolDataBySymbol = {}
        self.XLIsector_symbolDataBySymbol = {}
        self.XLYsector_symbolDataBySymbol = {}
        self.XLPsector_symbolDataBySymbol = {}
        self.XLUsector_symbolDataBySymbol = {}
        self.XLKsector_symbolDataBySymbol = {}
        self.XLVsector_symbolDataBySymbol = {}
        self.XLCsector_symbolDataBySymbol = {}
        
        #self.AddRiskManagement(TrailingStopRiskManagementModel(0.05))
        
        #self.SPY = self.AddEquity('SPY', Resolution.Daily).Symbol
        self.stock = self.AddEquity('QQQ', Resolution.Hour).Symbol
        self.bond = self.AddEquity('TLT', Resolution.Hour).Symbol
        self.vix = self.AddEquity('VIX', Resolution.Minute).Symbol
        
        self.XLF = self.AddEquity('XLF', Resolution.Hour).Symbol
        self.XLE = self.AddEquity('XLE', Resolution.Hour).Symbol
        self.XLB = self.AddEquity('XLB', Resolution.Hour).Symbol
        self.XLI = self.AddEquity('XLI', Resolution.Hour).Symbol
        self.XLY = self.AddEquity('XLY', Resolution.Hour).Symbol
        self.XLP = self.AddEquity('XLP', Resolution.Hour).Symbol
        self.XLU = self.AddEquity('XLU', Resolution.Hour).Symbol
        self.XLK = self.AddEquity('XLK', Resolution.Hour).Symbol
        self.XLV = self.AddEquity('XLV', Resolution.Hour).Symbol
        self.XLC = self.AddEquity('XLC', Resolution.Hour).Symbol
        
        #Stocks in Sectors
        self.XLFsector = ["JPM","BAC","BRK.B"]
        self.XLEsector = ["XOM","CVX"]
        self.XLBsector = ["LIN","SHW","APD"]
        self.XLIsector = ["HON","UNP","UPS"]
        self.XLYsector = ["AMZN","TSLA","HD"]
        self.XLPsector = ["PG","KO","PEP","WMT"]
        self.XLUsector = ["NEE","DUK","SO"]
        self.XLKsector = ["APPL","MSFT","NVDA"]
        self.XLVsector = ["JNJ","PFE","UNH"]
        self.XLCsector = ["FB", "GOOG", "DIS"]
        self.XLREsector = ["AMT","PLD","CCI","EQIX"]
        
        #Strategy
        strat = "self.Securities[symbol].Close < symbolData.high.Current.Value"
                
       # symbol_list = ['XLC', 'XLE', 'XLU', 'XLI', 'XLB', 'XLK', 'XLP', 'XLY', 'XLF', 'XLV']
        #self.symbols = [self.AddEquity(symbol, Resolution.Minute).Symbol for symbol in symbol_list]
        
        for symbol in self.XLFsector:
            self.AddEquity(symbol, Resolution.Hour)
            
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLFsector_symbolDataBySymbol[symbol] = symbolData
        
        for symbol in self.XLEsector:

            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLEsector_symbolDataBySymbol[symbol] = symbolData
            
        for symbol in self.XLBsector:
            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLBsector_symbolDataBySymbol[symbol] = symbolData
        
        for symbol in self.XLIsector:
            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLIsector_symbolDataBySymbol[symbol] = symbolData
            
        for symbol in self.XLYsector:
            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLYsector_symbolDataBySymbol[symbol] = symbolData
            
        for symbol in self.XLPsector:
            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLPsector_symbolDataBySymbol[symbol] = symbolData
        
        for symbol in self.XLUsector:
            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLUsector_symbolDataBySymbol[symbol] = symbolData
            
        for symbol in self.XLKsector:
            self.AddEquity(symbol, Resolution.Hour)
            ema10 = self.EMA(symbol, 10, Resolution.Hour, Field.Close)
            sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
            sma7 = self.SMA(symbol, 7, Resolution.Hour, Field.Close)
            sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
            sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
            ema20 = self.EMA(symbol, 20, Resolution.Hour, Field.Close)
            ema50 = self.EMA(symbol, 50, Resolution.Hour, Field.Close)
            rsi = self.RSI(symbol, 14, Resolution.Daily)
            wilr = self.WILR(symbol, 14, Resolution.Daily)
            wilr_fast = self.WILR(symbol, 10, Resolution.Daily)
            high = self.MAX(symbol, 5, Resolution.Daily, Field.High)
            midhigh = self.MAX(symbol, 3, Resolution.Daily, Field.High)
            low = self.MIN(symbol, 5, Resolution.Daily, Field.Low)
            stoplow = self.MIN(symbol, 20, Resolution.Daily, Field.Low)
            
            symbolData = SymbolData(symbol, sma7, ema10, sma20, sma200, sma50, ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow)
            self.XLKsector_symbolDataBySymbol[symbol] = symbolData
        
        
        
        self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.AfterMarketOpen(self.stock, 1), 
            self.Rebalance)
            
        #self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(30), self.EveryDayAfterMarketOpen)
        

    def Rebalance(self):
        if self.IsWarmingUp or not self.mom.IsReady or not self.sma.IsReady: return
        initial_asset = self.stock if self.mom.Current.Value > 0 else self.bond
        
        if self.init:
            #self.SetHoldings(initial_asset, 1)
            self.init = False
            

        keihist = self.History([self.kei], (int(self.GetParameter("keihist"))))
        #keihist = keihist['Value'].unstack(level=0).dropna()
        keihistlowt = np.nanpercentile(keihist, 15)
        keihistmidt = np.nanpercentile(keihist, 50)
        keihisthight = np.nanpercentile(keihist, 90)
        kei = self.sma.Current.Value
        keimom = self.mom.Current.Value
            
        if (keimom < 0 and kei < keihistmidt and  kei > keihistlowt) and not (self.Securities[self.XLP].Invested):
            # DECLINE
            self.Liquidate()
            self.SetHoldings(self.XLP, .01)
            
            for symbol, symbolData in self.XLPsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
            for symbol, symbolData in self.XLVsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .2, False, "Buy Signal")                    
            
            #self.SetHoldings(self.bond, .5)
            self.Debug("STAPLES {0} >> {1}".format(self.XLP, self.Time))
        
        elif (keimom > 0 and kei < keihistlowt) and not (self.Securities[self.XLB].Invested):
            # RECOVERY
            self.Liquidate()
            self.SetHoldings(self.XLB, .01)
            
            
            #XLB
            for symbol, symbolData in self.XLBsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
            #XLY
            for symbol, symbolData in self.XLYsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
           
            self.Debug("MATERIALS {0} >> {1}".format(self.XLB, self.Time))
            
            
        elif (keimom > 0 and kei > keihistlowt and kei < keihistmidt) and not (self.Securities[self.XLE].Invested):
            # EARLY
            self.Liquidate()
            self.SetHoldings(self.XLE, .01)

            #XLF
            for symbol, symbolData in self.XLFsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .15, False, "Buy Signal")
            
            #XLI
            for symbol, symbolData in self.XLIsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .15, False, "Buy Signal")
            
            #XLE 
            for symbol, symbolData in self.XLEsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .15, False, "Buy Signal")
           
            
            self.Debug("ENERGY {0} >> {1}".format(self.XLE, self.Time))
            
        elif (keimom > 0 and kei > keihistmidt and kei < keihisthight) and not (self.Securities[self.XLU].Invested):
            # REBOUND
            self.Liquidate()
            #self.SetHoldings(self.XLK, .5)
            self.SetHoldings(self.XLU, .01)
            
            #XLU
            for symbol, symbolData in self.XLUsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
            
            #XLK
            for symbol, symbolData in self.XLKsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value):
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
            
            
            
            
            self.Debug("UTILITIES {0} >> {1}".format(self.XLU, self.Time))
        
        elif (keimom < 0 and kei < keihisthight and kei > keihistmidt) and not (self.Securities[self.XLK].Invested):
            # LATE
            self.Liquidate()
            self.SetHoldings(self.XLK, .01)
            
            for symbol, symbolData in self.XLKsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value) :
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
            #self.SetHoldings(self.XLV, .5)
            
            for symbol, symbolData in self.XLCsector_symbolDataBySymbol.items():
                if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma50.Current.Value) :
                    self.SetHoldings(symbol, .2, False, "Buy Signal")
           
            self.Debug("INFO TECH {0} >> {1}".format(self.XLK, self.Time))
        
        elif (keimom < 0 and kei < 100 and not self.Securities[self.bond].Invested):
            self.Liquidate()
            #self.SetHoldings(self.bond, 1)
        

        self.Plot("LeadInd", "SMA(LeadInd)", self.sma.Current.Value)
        self.Plot("LeadInd", "THRESHOLD", 100)
        self.Plot("MOMP", "MOMP(LeadInd)", self.mom.Current.Value)
        self.Plot("MOMP", "THRESHOLD", 0)

        
# Quandl often doesn't use close columns so need to tell LEAN which is the "value" column.
class QuandlCustomColumns(PythonQuandl):
    def __init__(self):
        # Define ValueColumnName: cannot be None, Empty or non-existant column name
        self.ValueColumnName = "Value"
        
class SymbolData:
    def __init__(self, symbol, sma7, ema10, sma20, sma50, sma200,  ema20, rsi, wilr, wilr_fast, high, midhigh, low, stoplow):
        self.Symbol = symbol
        self.sma7 = sma7
        self.ema10 = ema10
        self.sma20 = sma20
        self.sma50 = sma50
        self.sma200 = sma200
        self.ema20 = ema20
        self.rsi = rsi
        self.wilr = wilr
        self.wilr_fast = wilr_fast
        self.high = high
        self.midhigh = midhigh
        self.low = low
        self.stoplow = stoplow