Overall Statistics
Total Trades
3912
Average Win
0.05%
Average Loss
-0.02%
Compounding Annual Return
7.774%
Drawdown
1.000%
Expectancy
0.766
Net Profit
86.506%
Sharpe Ratio
5.381
Probabilistic Sharpe Ratio
100%
Loss Rate
43%
Win Rate
57%
Profit-Loss Ratio
2.08
Alpha
0.073
Beta
0.028
Annual Standard Deviation
0.014
Annual Variance
0
Information Ratio
-0.519
Tracking Error
0.166
Treynor Ratio
2.744
Total Fees
$4449.50
##########################################
###Combinatorial Portfolio Optimization###
#Kamer Ali Yuksel, Principal AI Architect#
##########################################

import numpy as np

syms = ['ENSG', 'MLR', 'WTM', 'IAC', 'ROK', 'EGOV', 'PLCE', 'MSA', 'HRI', 'MKTX', 'PRU', 'ODFL', 'TER', 'PODD', 'VVI', 'CALM', 'IEX', 'TGT', 'PZZA', 'CLAR', 'WSO', 'LUV', 'ITW', 'RS', 'TDY', 'XEL', 'WFC', 'HST', 'PVH', 'LCII', 'NUS', 'GBCI', 'NOC', 'CASH', 'HIFS', 'RGR', 'CMI', 'GPI', 'DAR', 'AFG', 'COO', 'SIG', 'LLY', 'SO', 'PKG', 'AGX', 'CRVL', 'HRL', 'BBBY', 'ATRI', 'KO', 'EXR', 'ISBC', 'KDP', 'PRSC', 'PKBK', 'MLM', 'TROW', 'MCD', 'XPO', 'EVR', 'UBA', 'GENC', 'FICO', 'LDOS', 'WTS', 'EGBN', 'COP', 'TILE', 'MPWR', 'PLUS', 'DDS', 'CTO', 'SAIA', 'WAL', 'CDNS', 'DFS', 'NI', 'NEE', 'VGR', 'DPZ', 'MNR', 'HD', 'ABR', 'JBSS', 'SNX', 'AGNC', 'ABCB', 'MPW', 'GCBC', 'TRTN', 'CLCT', 'O', 'EEFT', 'THG', 'CLX', 'TKR', 'SUI', 'OKE', 'CVCY', 'GL', 'GSBC', 'AMZN', 'HOFT', 'SBNY', 'GS', 'AOS', 'STE', 'CMS', 'ETN', 'WINA', 'UTHR', 'NYMT', 'JKHY', 'GABC', 'CBZ', 'HSKA', 'CORE', 'FCX', 'TCX', 'RPT', 'SMG', 'SLM', 'IDXX', 'GRMN', 'BMY', 'PRIM', 'CSV', 'INTC', 'COHR', 'TCBK', 'FIZZ', 'CMCSA', 'EVI', 'PNC', 'LBC', 'TRN', 'CR', 'HAL', 'SFST', 'EIX', 'TYL', 'EMN', 'TJX', 'AME', 'HT', 'DLR', 'CI', 'LKFN', 'KALU', 'CNS', 'DISCA', 'ELS', 'KFRC', 'LDL', 'ROST', 'CVGW', 'CHH', 'EW', 'CMA', 'SR', 'NEU', 'F', 'HI', 'DVA', 'SPG', 'MLAB', 'SCI', 'HBNC', 'CAT', 'BFC', 'HSY', 'QRTEA', 'AIR', 'CTAS', 'RGCO', 'NYT', 'MYRG', 'DE', 'MNRO', 'WST', 'DHIL', 'AEP', 'CACC', 'AMT', 'STL', 'UHAL', 'QCRH', 'CPRT', 'CBRL', 'AIV', 'ABC', 'COKE', 'MCBC', 'HP', 'RGS', 'WEC', 'BRKL', 'FFIV', 'ALG', 'SLP', 'DTE', 'ORLY', 'NATH', 'AWK', 'RMD', 'ACNB', 'SBAC', 'NRIM', 'TT', 'PPG', 'ERIE', 'KMB', 'HCI', 'WLK', 'POL', 'FFIN', 'CPT', 'AAPL', 'TSN', 'SIVB', 'MSTR', 'TDG', 'GRIF', 'CCL', 'GHM', 'TDW', 'PBH', 'FCAP', 'GLRE', 'BECN', 'ADS', 'NTUS', 'ETR', 'CUZ', 'GIS', 'GPC', 'GHL', 'NUE', 'EXC', 'BDC', 'APA', 'CBT', 'REG', 'GATX', 'PBCT', 'OFC', 'ZIXI', 'Y', 'OVBC', 'SLGN', 'DGX', 'NWN', 'SCOR', 'BSX', 'DSPG', 'BXS', 'INVA', 'GD', 'SPR', 'J', 'HAFC', 'TRV', 'LMNR', 'C', 'FE', 'MCO', 'PCAR', 'PBI', 'HPQ', 'NCMI', 'ECOL', 'PCG', 'ATLO', 'MTW', 'ADP', 'TRST', 'HBI', 'MWA', 'EQIX', 'SRCL', 'EXP', 'JNPR', 'FISV', 'WERN', 'CXW', 'TPX', 'ATI', 'JNJ', 'JEF', 'FSTR', 'OXY', 'BELFB', 'CCRN', 'PGC', 'HOG', 'DVN', 'XRX', 'ASB', 'AVT', 'CPF', 'UMH', 'INGR', 'POWL', 'TAP', 'DHC', 'PBIP', 'L', 'CUB', 'LII', 'VMC', 'CPSI', 'LSI', 'LOW', 'CMCO', 'WHG', 'FIS', 'ECL', 'HUBG', 'NXGN', 'MDP', 'PNM', 'IRET', 'DEI', 'USD', 'BUSE', 'NEOG', 'MEI', 'FCBC', 'CYH', 'NWLI', 'STFC', 'MHK', 'GBL', 'FSP', 'RL', 'BWA', 'D', 'GPS', 'ALCO', 'CUBE', 'KRG', 'TOL', 'SYKE', 'VNO', 'ITT', 'EME', 'R', 'TXT', 'TCBI', 'NDAQ', 'WRI', 'AJG', 'MRO', 'MGEE', 'HOLX', 'SWK', 'YUM', 'WEYS', 'VMW', 'NJR', 'ES', 'KR', 'OC', 'LKQ', 'PSA', 'IBM', 'CL', 'MKC', 'TTEK', 'BFIN', 'WHR', 'SLB', 'K', 'EGLE', 'IDCC', 'AWR', 'HXL', 'CVS', 'GTS', 'LECO', 'WWD', 'CHS', 'PNR', 'HTZ', 'RWT', 'KNX', 'SIGI', 'BEN', 'FDP', 'COWN', 'TGTX', 'BFS', 'PTVCB', 'FORM', 'NUAN', 'SVRA', 'HRB', 'VLO', 'ACC', 'MANT', 'LMNX', 'CLI', 'RNST', 'MDT', 'CDZI', 'MSFT', 'IVZ', 'FARM', 'UNM', 'VRTX', 'JCOM', 'EPC', 'AMKR', 'TDS', 'HTLD', 'AAL', 'TRC', 'CNR', 'ATGE', 'UDR', 'ZION', 'MDLZ', 'CPB', 'CW', 'THS', 'DISH', 'MAT', 'TGI', 'FCN', 'TR', 'FRBK', 'HBMD', 'MGM', 'CSX']

        
class MultidimensionalModulatedRegulators(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2009, 1, 1)
        self.SetEndDate(2017, 4, 27)
        #self.SetStartDate(2017, 4, 29)
        self.SetCash(1000000)
        self.SetExecution(VolumeWeightedAveragePriceExecutionModel())
 
        self.symbols = []
        for i in range(len(syms)):
            self.symbols.append(Symbol.Create(syms[i], SecurityType.Equity, Market.USA))
            self.Debug(syms[i])
            
        self.SetUniverseSelection(ManualUniverseSelectionModel(self.symbols) )
        self.UniverseSettings.Resolution = Resolution.Hour
        
        self.AddEquity('SPY', Resolution.Hour)
        self.SetBenchmark('SPY')

        self.SetBrokerageModel(AlphaStreamsBrokerageModel())
        self.constant_weights = np.array([3.3982e-03, 4.0131e-03, 8.8476e-04, 2.4438e-03, 4.7452e-03, 4.6775e-04,
        8.7468e-04, 3.8617e-03, 2.4561e-03, 2.9398e-03, 3.0835e-03, 9.0018e-04,
        1.7305e-03, 1.2359e-03, 4.0575e-03, 1.4563e-03, 1.6322e-03, 3.0160e-03,
        1.2031e-03, 4.4845e-04, 1.6924e-03, 3.0365e-03, 1.1069e-03, 5.2772e-04,
        3.0231e-03, 2.1131e-03, 3.1062e-04, 1.8905e-03, 2.6216e-03, 2.0522e-03,
        2.1182e-03, 6.7722e-03, 3.3624e-03, 1.1989e-03, 2.7580e-03, 1.6098e-03,
        3.6809e-03, 1.2427e-03, 3.9181e-03, 4.2553e-03, 2.5029e-03, 1.5474e-03,
        2.7488e-03, 2.1876e-03, 2.9279e-03, 1.0024e-03, 1.3895e-03, 1.8311e-03,
        9.9601e-05, 4.7479e-03, 1.9526e-03, 4.1095e-03, 1.9882e-03, 3.3241e-03,
        1.5774e-03, 2.3187e-03, 3.7817e-04, 6.4654e-04, 9.1861e-04, 1.9457e-03,
        3.2280e-03, 6.4726e-04, 1.5960e-05, 3.8441e-04, 6.0117e-04, 5.7787e-04,
        1.4022e-03, 5.2363e-03, 4.3581e-04, 4.6993e-03, 9.1263e-04, 1.8786e-03,
        6.1021e-05, 4.1448e-03, 5.3642e-03, 3.3932e-03, 3.7011e-03, 6.0127e-03,
        9.3353e-03, 3.6589e-03, 1.8764e-03, 4.4968e-03, 2.4470e-03, 9.3047e-04,
        2.7044e-03, 3.6457e-03, 3.7984e-03, 2.2097e-03, 4.6226e-03, 1.0479e-03,
        6.8413e-04, 3.6535e-03, 1.2478e-03, 3.3776e-03, 6.8999e-04, 1.3600e-04,
        8.3674e-04, 5.0344e-03, 2.3911e-03, 6.7925e-04, 8.6788e-04, 1.8997e-03,
        5.0603e-04, 1.1128e-03, 1.8579e-03, 2.6279e-03, 8.8382e-04, 4.4225e-03,
        8.7548e-03, 7.8730e-04, 2.8393e-03, 7.9878e-05, 3.3917e-03, 7.7264e-04,
        4.2540e-03, 8.8076e-04, 1.0013e-03, 5.8392e-04, 3.7622e-04, 2.2637e-03,
        3.1360e-03, 2.0709e-03, 9.9822e-04, 9.8224e-04, 1.2244e-03, 1.2914e-03,
        3.2193e-03, 2.0359e-03, 4.7989e-03, 1.8859e-03, 1.6195e-03, 3.0714e-03,
        7.6656e-04, 1.1511e-03, 2.5067e-03, 1.0530e-03, 1.5999e-03, 2.8640e-03,
        1.1296e-03, 3.3264e-03, 6.6075e-03, 2.5281e-03, 1.3831e-03, 4.8883e-03,
        7.1290e-04, 2.1165e-03, 6.3633e-04, 1.6459e-03, 2.6854e-03, 8.1952e-04,
        2.0746e-03, 3.1961e-03, 3.8710e-03, 2.0082e-03, 1.6817e-03, 1.4095e-04,
        9.7681e-04, 4.1032e-03, 7.0415e-04, 4.3659e-03, 2.2255e-03, 4.2101e-03,
        3.2997e-03, 1.3460e-03, 1.1906e-03, 6.9374e-04, 8.5441e-04, 2.2671e-04,
        5.0444e-03, 3.2997e-03, 2.3097e-03, 1.1144e-03, 1.5167e-03, 7.3460e-04,
        2.1653e-04, 1.3961e-03, 1.6494e-03, 8.1710e-04, 2.1162e-03, 6.9748e-04,
        2.4283e-03, 1.0195e-03, 2.7334e-03, 2.2227e-03, 1.2141e-03, 2.2430e-03,
        2.9653e-03, 4.0808e-03, 2.3811e-03, 5.7922e-03, 2.9688e-03, 4.4592e-03,
        3.7906e-04, 9.3453e-04, 3.4414e-03, 3.7641e-04, 3.1678e-03, 2.9384e-03,
        1.1053e-04, 1.8011e-03, 1.2398e-03, 6.0225e-03, 2.4337e-03, 2.7341e-03,
        9.1213e-03, 9.8882e-04, 2.2261e-03, 1.2247e-03, 4.1184e-04, 4.3928e-03,
        1.7236e-04, 3.1327e-03, 1.0180e-03, 2.7616e-03, 3.2947e-03, 2.9200e-03,
        3.2411e-03, 4.2903e-03, 5.7166e-03, 1.5675e-03, 3.3748e-03, 2.3981e-03,
        6.5097e-03, -4.2571e-04, -2.1832e-03, -4.0805e-04, -3.3862e-03, -4.5834e-04,
        -3.2253e-04, -4.9479e-03, -7.6738e-06, -1.6149e-03, -1.8058e-03,
        -9.3083e-03, -5.2888e-03, -1.5225e-03, -1.8564e-03, -1.9197e-03,
        -3.1216e-03, -4.4995e-03, -2.1873e-03, -4.4611e-05, -7.1745e-04,
        -2.3479e-03, -5.6220e-04, -5.0790e-05, -1.8926e-03, -2.2377e-03,
        -5.5140e-03, -2.4081e-03, -2.8151e-03, -4.1414e-03, -5.6136e-03,
        -2.0077e-03, -2.5702e-03, -2.0467e-03, -3.3155e-03, -1.2586e-03,
        -3.7666e-03, -2.4434e-03, -4.6233e-04, -1.7369e-03, -9.5675e-05,
        -2.5175e-03, -3.9991e-03, -7.5867e-03, -1.7360e-03, -4.4203e-03,
        -1.2891e-03, -1.4918e-03, -1.6679e-03, -2.0608e-03, -6.0624e-03,
        -3.3795e-03, -3.3919e-03, -1.6373e-03, -5.7850e-03, -1.7513e-04,
        -1.6979e-03, -5.3128e-04, -4.3007e-03, -6.0927e-04, -2.5250e-04,
        -2.9573e-03, -1.6989e-03, -1.8562e-03, -3.8319e-04, -5.7742e-04,
        -4.9189e-04, -3.7982e-03, -1.0289e-03, -2.2973e-04, -1.2009e-03,
        -2.7979e-03, -3.3080e-03, -1.5908e-03, -1.1377e-03, -7.0216e-04,
        -3.8374e-03, -6.4523e-03, -6.7781e-03, -2.6616e-03, -3.9824e-03,
        -2.4865e-03, -3.1169e-03, -1.4913e-03, -5.7018e-05, -6.8845e-03,
        -3.0822e-03, -3.7436e-04, -1.9115e-03, -1.6261e-03, -5.8021e-04,
        -2.5004e-03, -1.2858e-03, -3.0620e-03, -6.0363e-04, -2.5375e-03,
        -2.5317e-03, -4.2836e-03, -2.8117e-04, -3.6767e-03, -3.4148e-03,
        -3.5441e-03, -3.6617e-03, -6.2274e-03, -8.5130e-04, -7.3523e-04,
        -2.7754e-03, -8.2459e-04, -6.8715e-03, -2.8323e-03, -1.0536e-03,
        -1.1868e-03, -1.2009e-03, -2.8987e-03, -1.2303e-03, -6.7284e-03,
        -2.7377e-03, -1.3598e-03, -9.1519e-04, -3.3482e-03, -1.2186e-03,
        -2.2549e-03, -1.1550e-03, -3.2078e-03, -2.0035e-03, -2.1355e-03,
        -2.0510e-03, -1.3050e-03, -2.4886e-03, -2.2676e-04, -1.3538e-03,
        -7.0719e-04, -1.3962e-03, -2.6120e-04, -3.1129e-03, -5.5568e-03,
        -2.5538e-03, -1.1429e-03, -5.5755e-03, -3.5150e-03, -1.8648e-04,
        -3.7977e-03, -3.0495e-03, -2.9051e-03, -2.0988e-03, -4.2588e-03,
        -1.3462e-03, -2.3541e-03, -2.1168e-03, -1.2916e-04, -3.6415e-03,
        -2.5912e-03, -2.2253e-03, -3.0152e-04, -3.2417e-03, -3.6338e-03,
        -2.8385e-03, -1.2504e-04, -2.2989e-03, -4.2426e-04, -1.0907e-03,
        -3.5349e-03, -2.8741e-03, -2.8913e-03, -6.9701e-04, -6.3451e-04,
        -2.9305e-03, -1.6677e-03, -9.5412e-04, -3.7681e-03, -3.9134e-04,
        -1.5209e-03, -1.4536e-03, -3.6273e-03, -2.1133e-03, -2.7022e-03,
        -1.4214e-03, -7.8475e-03, -6.8464e-04, -5.2469e-03, -1.5225e-03,
        -7.6332e-04, -2.0228e-03, -1.2330e-03, -1.0026e-03, -1.4351e-03,
        -2.7028e-03, -3.6341e-03, -3.4216e-04, -2.5141e-03, -1.1006e-03,
        -4.2435e-05, -2.1080e-03, -3.2233e-03, -1.2455e-03, -2.5704e-03,
        -2.5645e-03, -3.6839e-03, -5.3616e-04, -2.0797e-03, -1.5523e-04,
        -1.5702e-03, -4.6892e-04, -1.0742e-03, -6.0005e-04, -1.4292e-03,
        -1.6030e-03, -3.0869e-03, -4.0751e-04, -2.2157e-03, -2.1663e-03,
        -3.1137e-03, -4.5964e-04])
        #self.constant_weights[-279:] = -1.0
        self.constant_weights = self.constant_weights / np.sum(np.abs(self.constant_weights))

    def OnData(self, data):
                
        rebalance = False
        
        if self.Portfolio.TotalHoldingsValue > 0:
            total = 0.0
            for i, sym in enumerate(self.symbols):
                curr = (self.Securities[sym].Holdings.HoldingsValue/self.Portfolio.TotalPortfolioValue)
                diff = self.constant_weights[i] - curr
                total += np.abs(diff)
                
            if total > 0.25: 
                rebalance = True
                
            if rebalance:
                for i, sym in enumerate(self.symbols):
                    curr = (self.Securities[sym].Holdings.HoldingsValue/self.Portfolio.TotalPortfolioValue)
                    if self.constant_weights[i] < curr:
                        self.SetHoldings(sym, self.constant_weights[i])
                for i, sym in enumerate(self.symbols):
                    curr = (self.Securities[sym].Holdings.HoldingsValue/self.Portfolio.TotalPortfolioValue)                       
                    if self.constant_weights[i] > curr:
                        self.SetHoldings(sym, self.constant_weights[i])
        else:
            for i, sym in enumerate(self.symbols):
                    self.SetHoldings(sym, self.constant_weights[i])