Overall Statistics
Total Trades
573
Average Win
1.40%
Average Loss
-0.73%
Compounding Annual Return
16.454%
Drawdown
16.300%
Expectancy
0.719
Net Profit
293.094%
Sharpe Ratio
1.312
Probabilistic Sharpe Ratio
73.236%
Loss Rate
41%
Win Rate
59%
Profit-Loss Ratio
1.92
Alpha
0.159
Beta
0.066
Annual Standard Deviation
0.134
Annual Variance
0.018
Information Ratio
-0.314
Tracking Error
0.224
Treynor Ratio
2.675
Total Fees
$573.00
import pandas as pd
class LiquidUniverseSelection(QCAlgorithm):
    
    def Initialize(self):
        self.SetWarmup(10)
        self.SetBenchmark("QQQ")
        self.SetStartDate(2011, 12, 31)
        # self.SetEndDate(2012, 5, 8)
        self.SetCash(5000) 
        res1 = Resolution.Minute
        res = Resolution.Minute
        self.MRKT = self.AddEquity('SPY', res1).Symbol  # market
        self.TLT = self.AddEquity('TLT', res1).Symbol  # market
        self.S0=self.AddEquity('PM',res).Symbol
        self.S1=self.AddEquity('CL',res).Symbol
        self.S2=self.AddEquity('AZO',res).Symbol
        self.S3=self.AddEquity('HSY',res).Symbol
        self.S4=self.AddEquity('PAYX',res).Symbol
        self.S5=self.AddEquity('DLTR',res).Symbol
        self.S6=self.AddEquity('CLX',res).Symbol
        self.S7=self.AddEquity('ADP',res).Symbol
        self.S8=self.AddEquity('CPB',res).Symbol
        self.S9=self.AddEquity('CBOE',res).Symbol
        self.S10=self.AddEquity('ROL',res).Symbol
        self.S11=self.AddEquity('MKC',res).Symbol
        self.S12=self.AddEquity('GIS',res).Symbol
        self.S13=self.AddEquity('K',res).Symbol
        self.S14=self.AddEquity('CHH',res).Symbol
        self.S15=self.AddEquity('ABC',res).Symbol
        self.S16=self.AddEquity('OMC',res).Symbol
        self.S17=self.AddEquity('MCD',res).Symbol
        self.S18=self.AddEquity('MD',res).Symbol
        self.S19=self.AddEquity('LH',res).Symbol
        self.S20=self.AddEquity('TJX',res).Symbol
        self.S21=self.AddEquity('VRSK',res).Symbol
        self.S22=self.AddEquity('INTU',res).Symbol
        self.S23=self.AddEquity('SRCL',res).Symbol
        self.S24=self.AddEquity('KMB',res).Symbol
        self.S25=self.AddEquity('CACI',res).Symbol
        self.S26=self.AddEquity('FDS',res).Symbol
        self.S27=self.AddEquity('ANSS',res).Symbol
        self.S28=self.AddEquity('FISV',res).Symbol
        self.S29=self.AddEquity('TROW',res).Symbol
        self.S30=self.AddEquity('MORN',res).Symbol
        self.S31=self.AddEquity('IT',res).Symbol
        self.S32=self.AddEquity('GD',res).Symbol
        self.S33=self.AddEquity('MO',res).Symbol
        self.S34=self.AddEquity('NOC',res).Symbol
        self.S35=self.AddEquity('CHRW',res).Symbol
        self.S36=self.AddEquity('DGX',res).Symbol
        self.S37=self.AddEquity('SBUX',res).Symbol
        self.S38=self.AddEquity('HD',res).Symbol
        self.S39=self.AddEquity('ROST',res).Symbol
        self.S40=self.AddEquity('YUM',res).Symbol
        self.S41=self.AddEquity('MA',res).Symbol
        self.S42=self.AddEquity('SPGI',res).Symbol
        self.S43=self.AddEquity('JKHY',res).Symbol
        self.S44=self.AddEquity('BR',res).Symbol
        self.S45=self.AddEquity('MCO',res).Symbol
        self.S46=self.AddEquity('APH',res).Symbol
        self.S47=self.AddEquity('BRO',res).Symbol
        self.S48=self.AddEquity('MMC',res).Symbol
        self.S49=self.AddEquity('CDK',res).Symbol
        self.S50=self.AddEquity('MMS',res).Symbol
        self.S51=self.AddEquity('HLF',res).Symbol
        self.S52=self.AddEquity('CTXS',res).Symbol
        self.S53=self.AddEquity('TTC',res).Symbol
        self.S54=self.AddEquity('WU',res).Symbol
        self.S55=self.AddEquity('EQC',res).Symbol
        self.S56=self.AddEquity('CHE',res).Symbol
        self.S57=self.AddEquity('DOX',res).Symbol
        self.S58=self.AddEquity('SYY',res).Symbol
        self.S59=self.AddEquity('WDFC',res).Symbol
        self.S60=self.AddEquity('NEU',res).Symbol
        self.S61=self.AddEquity('JNJ',res).Symbol
        self.S62=self.AddEquity('EGOV',res).Symbol
        self.S63=self.AddEquity('NLOK',res).Symbol
        self.S64=self.AddEquity('MNST',res).Symbol
        self.S65=self.AddEquity('DPZ',res).Symbol
        self.S66=self.AddEquity('VRSN',res).Symbol
        self.S67=self.AddEquity('PG',res).Symbol
        self.S68=self.AddEquity('ORCL',res).Symbol

        self.signals = {'2012-01-03':{self.S0:0.2,self.S1:0.2,self.S2:0.2,self.S3:0.2,self.S4:0.2},'2012-02-01':{self.S3:0.2,self.S2:0.2,self.S1:0.2,self.S0:0.2,self.S4:0.2},'2012-03-01':{self.S1:0.2,self.S2:0.2,self.S0:0.2,self.S5:0.2,self.S4:0.2},'2012-04-02':{self.S1:0.2,self.S0:0.2,self.S4:0.2,self.S2:0.2,self.S3:0.2},'2012-05-01':{self.S1:0.2,self.S4:0.2,self.S2:0.2,self.S6:0.2,self.S7:0.2},'2012-06-01':{self.S1:0.2,self.S7:0.2,self.S4:0.2,self.S8:0.2,self.S6:0.2},'2012-07-02':{self.S1:0.2,self.S0:0.2,self.S7:0.2,self.S8:0.2,self.S4:0.2},'2012-08-01':{self.S1:0.2,self.S0:0.2,self.S9:0.2,self.S8:0.2,self.S7:0.2},'2012-09-04':{self.S9:0.2,self.S1:0.2,self.S7:0.2,self.S0:0.2,self.S6:0.2},'2012-10-01':{self.S9:0.2,self.S7:0.2,self.S1:0.2,self.S6:0.2,self.S10:0.2},'2012-10-09':{self.TLT:1.0},'2012-11-12':{self.S9:0.2,self.S8:0.2,self.S6:0.2,self.S3:0.2,self.S1:0.2},'2012-12-03':{self.S9:0.2,self.S6:0.2,self.S3:0.2,self.S11:0.2,self.S12:0.2},'2013-01-02':{self.S3:0.2,self.S6:0.2,self.S1:0.2,self.S12:0.2,self.S13:0.2},'2013-02-01':{self.S6:0.2,self.S14:0.2,self.S1:0.2,self.S15:0.2,self.S12:0.2},'2013-03-01':{self.S6:0.2,self.S14:0.2,self.S15:0.2,self.S1:0.2,self.S3:0.2},'2013-04-01':{self.S6:0.2,self.S14:0.2,self.S15:0.2,self.S3:0.2,self.S4:0.2},'2013-04-22':{self.TLT:1.0},'2013-05-24':{self.S6:0.2,self.S16:0.2,self.S9:0.2,self.S4:0.2,self.S17:0.2},'2013-06-03':{self.S9:0.2,self.S6:0.2,self.S0:0.2,self.S4:0.2,self.S16:0.2},'2013-06-07':{self.TLT:1.0},'2013-07-15':{self.S18:0.2,self.S4:0.2,self.S6:0.2,self.S0:0.2,self.S19:0.2},'2013-08-01':{self.S18:0.2,self.S4:0.2,self.S0:0.2,self.S6:0.2,self.S20:0.2},'2013-08-02':{self.TLT:1.0},'2013-08-22':{self.S18:0.2,self.S6:0.2,self.S4:0.2,self.S0:0.2,self.S21:0.2},'2013-09-03':{self.S18:0.2,self.S6:0.2,self.S21:0.2,self.S4:0.2,self.S0:0.2},'2013-10-01':{self.S18:0.2,self.S6:0.2,self.S4:0.2,self.S21:0.2,self.S0:0.2},'2013-11-01':{self.S18:0.2,self.S4:0.2,self.S6:0.2,self.S2:0.2,self.S21:0.2},'2013-12-02':{self.S22:0.2,self.S4:0.2,self.S18:0.2,self.S6:0.2,self.S19:0.2},'2014-01-02':{self.S22:0.2,self.S4:0.2,self.S18:0.2,self.S6:0.2,self.S17:0.2},'2014-02-03':{self.S22:0.2,self.S10:0.2,self.S4:0.2,self.S6:0.2,self.S2:0.2},'2014-02-05':{self.TLT:1.0},'2014-05-06':{self.S23:0.2,self.S6:0.2,self.S1:0.2,self.S0:0.2,self.S17:0.2},'2014-05-21':{self.TLT:1.0},'2014-06-12':{self.S23:0.2,self.S1:0.2,self.S0:0.2,self.S6:0.2,self.S17:0.2},'2014-07-01':{self.S7:0.2,self.S0:0.2,self.S6:0.2,self.S1:0.2,self.S23:0.2},'2014-08-01':{self.S0:0.2,self.S6:0.2,self.S23:0.2,self.S1:0.2,self.S24:0.2},'2014-08-04':{self.TLT:1.0},'2014-08-29':{self.S25:0.2,self.S0:0.2,self.S4:0.2,self.S17:0.2,self.S23:0.2},'2014-09-02':{self.S25:0.2,self.S0:0.2,self.S4:0.2,self.S17:0.2,self.S23:0.2},'2014-09-04':{self.TLT:1.0},'2015-02-23':{self.S0:0.2,self.S4:0.2,self.S21:0.2,self.S26:0.2,self.S6:0.2},'2015-03-02':{self.S4:0.2,self.S0:0.2,self.S21:0.2,self.S26:0.2,self.S16:0.2},'2015-03-12':{self.TLT:1.0},'2015-04-07':{self.S16:0.2,self.S26:0.2,self.S0:0.2,self.S4:0.2,self.S21:0.2},'2015-05-01':{self.S16:0.2,self.S26:0.2,self.S18:0.2,self.S27:0.2,self.S4:0.2},'2015-06-01':{self.S16:0.2,self.S26:0.2,self.S6:0.2,self.S4:0.2,self.S28:0.2},'2015-07-01':{self.S18:0.2,self.S1:0.2,self.S6:0.2,self.S16:0.2,self.S26:0.2},'2015-07-27':{self.TLT:1.0},'2015-09-17':{self.S9:0.2,self.S6:0.2,self.S0:0.2,self.S16:0.2,self.S29:0.2},'2015-10-01':{self.S9:0.2,self.S6:0.2,self.S0:0.2,self.S16:0.2,self.S30:0.2},'2015-11-02':{self.S9:0.2,self.S0:0.2,self.S6:0.2,self.S31:0.2,self.S30:0.2},'2015-12-01':{self.S9:0.2,self.S0:0.2,self.S6:0.2,self.S30:0.2,self.S4:0.2},'2015-12-30':{self.TLT:1.0},'2016-03-22':{self.S0:0.2,self.S30:0.2,self.S4:0.2,self.S6:0.2,self.S32:0.2},'2016-04-01':{self.S0:0.2,self.S30:0.2,self.S6:0.2,self.S4:0.2,self.S33:0.2},'2016-05-02':{self.S0:0.2,self.S30:0.2,self.S6:0.2,self.S16:0.2,self.S34:0.2},'2016-06-01':{self.S0:0.2,self.S16:0.2,self.S30:0.2,self.S4:0.2,self.S33:0.2},'2016-07-01':{self.S0:0.2,self.S35:0.2,self.S6:0.2,self.S34:0.2,self.S33:0.2},'2016-08-01':{self.S6:0.2,self.S34:0.2,self.S9:0.2,self.S33:0.2,self.S1:0.2},'2016-09-01':{self.S9:0.2,self.S34:0.2,self.S36:0.2,self.S0:0.2,self.S33:0.2},'2016-10-03':{self.S34:0.2,self.S35:0.2,self.S30:0.2,self.S32:0.2,self.S33:0.2},'2016-11-01':{self.S6:0.2,self.S37:0.2,self.S34:0.2,self.S38:0.2,self.S39:0.2},'2016-11-11':{self.TLT:1.0},'2017-01-11':{self.S22:0.2,self.S35:0.2,self.S4:0.2,self.S21:0.2,self.S16:0.2},'2017-02-01':{self.S22:0.2,self.S35:0.2,self.S4:0.2,self.S21:0.2,self.S16:0.2},'2017-03-01':{self.S4:0.2,self.S33:0.2,self.S26:0.2,self.S21:0.2,self.S35:0.2},'2017-03-06':{self.TLT:1.0},'2017-04-18':{self.S33:0.2,self.S27:0.2,self.S40:0.2,self.S28:0.2,self.S0:0.2},'2017-04-20':{self.TLT:1.0},'2017-06-12':{self.S33:0.2,self.S28:0.2,self.S38:0.2,self.S41:0.2,self.S42:0.2},'2017-07-03':{self.S33:0.2,self.S28:0.2,self.S42:0.2,self.S41:0.2,self.S34:0.2},'2017-07-07':{self.TLT:1.0},'2017-07-31':{self.S28:0.2,self.S40:0.2,self.S34:0.2,self.S42:0.2,self.S41:0.2},'2017-08-01':{self.S28:0.2,self.S40:0.2,self.S34:0.2,self.S43:0.2,self.S42:0.2},'2017-09-01':{self.S34:0.2,self.S41:0.2,self.S40:0.2,self.S42:0.2,self.S6:0.2},'2017-10-02':{self.S34:0.2,self.S0:0.2,self.S42:0.2,self.S40:0.2,self.S21:0.2},'2017-11-01':{self.S1:0.2,self.S22:0.2,self.S40:0.2,self.S44:0.2,self.S28:0.2},'2017-12-01':{self.S1:0.2,self.S42:0.2,self.S0:0.2,self.S43:0.2,self.S22:0.2},'2017-12-04':{self.TLT:1.0},'2017-12-27':{self.S28:0.2,self.S1:0.2,self.S17:0.2,self.S45:0.2,self.S46:0.2},'2018-01-02':{self.S28:0.2,self.S1:0.2,self.S17:0.2,self.S45:0.2,self.S42:0.2},'2018-02-01':{self.S28:0.2,self.S38:0.2,self.S43:0.2,self.S42:0.2,self.S22:0.2},'2018-03-01':{self.S47:0.2,self.S43:0.2,self.S37:0.2,self.S48:0.2,self.S17:0.2},'2018-03-26':{self.TLT:1.0},'2018-08-10':{self.S49:0.2,self.S28:0.2,self.S42:0.2,self.S4:0.2,self.S48:0.2},'2018-08-17':{self.TLT:1.0},'2018-10-01':{self.S40:0.2,self.S4:0.2,self.S28:0.2,self.S38:0.2,self.S1:0.2},'2018-10-25':{self.TLT:1.0},'2018-11-20':{self.S47:0.2,self.S40:0.2,self.S4:0.2,self.S26:0.2,self.S50:0.2},'2018-11-23':{self.TLT:1.0},'2019-01-17':{self.S47:0.2,self.S40:0.2,self.S4:0.2,self.S51:0.2,self.S49:0.2},'2019-02-01':{self.S40:0.2,self.S47:0.2,self.S4:0.2,self.S52:0.2,self.S53:0.2},'2019-03-01':{self.S54:0.2,self.S40:0.2,self.S4:0.2,self.S47:0.2,self.S26:0.2},'2019-04-01':{self.S40:0.2,self.S54:0.2,self.S1:0.2,self.S16:0.2,self.S30:0.2},'2019-05-01':{self.S40:0.2,self.S54:0.2,self.S1:0.2,self.S55:0.2,self.S4:0.2},'2019-06-03':{self.S40:0.2,self.S55:0.2,self.S1:0.2,self.S4:0.2,self.S42:0.2},'2019-06-26':{self.TLT:1.0},'2019-07-29':{self.S40:0.2,self.S55:0.2,self.S21:0.2,self.S4:0.2,self.S42:0.2},'2019-08-01':{self.S40:0.2,self.S55:0.2,self.S52:0.2,self.S21:0.2,self.S42:0.2},'2019-08-05':{self.TLT:1.0},'2019-08-27':{self.S55:0.2,self.S52:0.2,self.S40:0.2,self.S21:0.2,self.S56:0.2},'2019-09-03':{self.S52:0.2,self.S55:0.2,self.S40:0.2,self.S21:0.2,self.S56:0.2},'2019-09-17':{self.TLT:1.0},'2019-10-31':{self.S55:0.2,self.S52:0.2,self.S57:0.2,self.S58:0.2,self.S54:0.2},'2019-11-01':{self.S55:0.2,self.S52:0.2,self.S57:0.2,self.S54:0.2,self.S58:0.2},'2019-12-02':{self.S55:0.2,self.S52:0.2,self.S57:0.2,self.S59:0.2,self.S4:0.2},'2020-01-02':{self.S55:0.2,self.S52:0.2,self.S57:0.2,self.S59:0.2,self.S4:0.2},'2020-02-03':{self.S55:0.2,self.S57:0.2,self.S4:0.2,self.S58:0.2,self.S6:0.2},'2020-02-20':{self.TLT:1.0},'2020-04-24':{self.S55:0.2,self.S24:0.2,self.S52:0.2,self.S60:0.2,self.S61:0.2},'2020-05-01':{self.S55:0.2,self.S24:0.2,self.S52:0.2,self.S62:0.2,self.S35:0.2},'2020-06-01':{self.S55:0.2,self.S63:0.2,self.S64:0.2,self.S1:0.2,self.S52:0.2},'2020-07-01':{self.S55:0.2,self.S1:0.2,self.S63:0.2,self.S52:0.2,self.S64:0.2},'2020-08-03':{self.S55:0.2,self.S63:0.2,self.S1:0.2,self.S64:0.2,self.S65:0.2},'2020-09-01':{self.S55:0.2,self.S66:0.2,self.S1:0.2,self.S24:0.2,self.S65:0.2},'2020-10-01':{self.S55:0.2,self.S1:0.2,self.S24:0.2,self.S66:0.2,self.S0:0.2},'2020-11-02':{self.S55:0.2,self.S1:0.2,self.S6:0.2,self.S24:0.2,self.S67:0.2},'2020-12-01':{self.S55:0.2,self.S1:0.2,self.S4:0.2,self.S68:0.2,self.S67:0.2}}
        
        self.Schedule.On(
            self.DateRules.EveryDay(),
            self.TimeRules.AfterMarketOpen('SPY', 45),
            self.rebalance
        )
    
    def rebalance(self):
        currentDate = self.UtcTime.date()
        try:
            new_position = self.signals["{}".format(currentDate)]
        except:
            return
        # openOrders = self.Transactions.GetOpenOrders()
        # if len(openOrders) > 0:
        #     for x in openOrders:
        #          self.Transactions.CancelOrder(x.Id)
        invested = [ x.Symbol for x in self.Portfolio.Values if x.Invested ]
        invested = dict.fromkeys(invested, 0)
        invested.update(new_position)
        for sec, weight in invested.items():
            self.SetHoldings(sec, weight)
    
    # def OnOrderEvent(self, orderEvent):
    #     order = self.Transactions.GetOrderById(orderEvent.OrderId)
    #     if order.Type == OrderType.Market and orderEvent.Status == OrderStatus.Filled and order.Direction == OrderDirection.Buy: 
    #         price = order.Price
    #         quantity = order.Quantity
    #         self.StopMarketOrder(order.Symbol, quantity*-1, 0.92 * price)