Overall Statistics
Total Trades
3841
Average Win
0.05%
Average Loss
-0.02%
Compounding Annual Return
7.833%
Drawdown
1.000%
Expectancy
0.777
Net Profit
87.357%
Sharpe Ratio
5.473
Probabilistic Sharpe Ratio
100%
Loss Rate
42%
Win Rate
58%
Profit-Loss Ratio
2.08
Alpha
0.074
Beta
0.026
Annual Standard Deviation
0.014
Annual Variance
0
Information Ratio
-0.514
Tracking Error
0.166
Treynor Ratio
3.068
Total Fees
$4357.60
##########################################
###Combinatorial Portfolio Optimization###
#Kamer Ali Yuksel, Principal AI Architect#
##########################################

import numpy as np

syms = ['ENSG', 'MLR', 'WTM', 'IAC', 'ROK', 'FCAP', 'EGOV', 'PLCE', 'BECN', 'MSA', 'HRI', 'MKTX', 'PRU', 'ODFL', 'TER', 'PODD', 'VVI', 'CALM', 'IEX', 'TGT', 'APA', 'PZZA', 'CLAR', 'WSO', 'LUV', 'ITW', 'PBCT', 'RS', 'TDY', 'XEL', 'WFC', 'HST', 'PVH', 'LCII', 'NUS', 'GBCI', 'NOC', 'CASH', 'HIFS', 'RGR', 'CMI', 'GPI', 'DAR', 'AFG', 'COO', 'SIG', 'LLY', 'PKG', 'AGX', 'CRVL', 'TRV', 'HRL', 'ATRI', 'KO', 'EXR', 'ISBC', 'KDP', 'PRSC', 'PKBK', 'MLM', 'TROW', 'MCD', 'XPO', 'EVR', 'UBA', 'GENC', 'FICO', 'LDOS', 'EGBN', 'COP', 'TILE', 'MPWR', 'PLUS', 'DDS', 'CTO', 'SAIA', 'WAL', 'CDNS', 'DFS', 'NI', 'NEE', 'VGR', 'DPZ', 'MNR', 'HD', 'ABR', 'JBSS', 'SNX', 'PBIP', 'AGNC', 'LII', 'ABCB', 'LSI', 'MPW', 'GCBC', 'TRTN', 'CLCT', 'EEFT', 'THG', 'CLX', 'TKR', 'SUI', 'OKE', 'GL', 'GSBC', 'AMZN', 'HOFT', 'SBNY', 'GS', 'AOS', 'STE', 'CMS', 'WRI', 'ETN', 'WINA', 'UTHR', 'NYMT', 'JKHY', 'GABC', 'CBZ', 'HSKA', 'CORE', 'FCX', 'TCX', 'RPT', 'SMG', 'SLM', 'IDXX', 'KR', 'GRMN', 'BMY', 'PRIM', 'CSV', 'INTC', 'COHR', 'TCBK', 'FIZZ', 'CMCSA', 'EVI', 'PNC', 'LBC', 'TRN', 'CR', 'HAL', 'SFST', 'EIX', 'WHR', 'TYL', 'EMN', 'TJX', 'HT', 'IDCC', 'DLR', 'CI', 'LKFN', 'KALU', 'CNS', 'DISCA', 'ELS', 'KFRC', 'LDL', 'ROST', 'CVGW', 'CHH', 'EW', 'CMA', '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', 'ALG', 'SLP', 'DTE', 'ORLY', 'NATH', 'AWK', 'RMD', 'ACNB', 'SBAC', 'AMKR', 'NRIM', 'TT', 'ERIE', 'HCI', 'WLK', 'POL', 'UDR', 'FFIN', 'CPT', 'AAPL', 'TSN', 'SIVB', 'MSTR', 'TDG', 'GRIF', 'CCL', 'GHM', 'TDW', 'PBH', 'GLRE', 'ADS', 'NTUS', 'ETR', 'CUZ', 'GIS', 'GPC', 'GHL', 'NUE', 'EXC', 'BDC', 'CBT', 'REG', 'GATX', 'OFC', 'ZIXI', 'Y', 'OVBC', 'SLGN', 'DGX', 'NWN', 'SCOR', 'BSX', 'DSPG', 'BXS', 'INVA', 'GD', 'SPR', 'J', 'HAFC', 'SO', 'LMNR', 'C', 'FE', 'MCO', 'BBBY', 'PCAR', 'PBI', 'HPQ', 'NCMI', 'ECOL', 'PCG', 'ATLO', 'MTW', 'ADP', 'TRST', 'WTS', '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', 'L', 'CUB', 'VMC', 'CPSI', 'LOW', 'CMCO', 'WHG', 'FIS', 'ECL', 'HUBG', 'NXGN', 'MDP', 'O', 'PNM', 'IRET', 'DEI', 'USD', 'BUSE', 'NEOG', 'MEI', 'FCBC', 'CYH', 'NWLI', 'STFC', 'MHK', 'GBL', 'CVCY', 'FSP', 'RL', 'BWA', 'D', 'GPS', 'ALCO', 'CUBE', 'KRG', 'TOL', 'SYKE', 'VNO', 'ITT', 'EME', 'R', 'TXT', 'TCBI', 'NDAQ', 'AJG', 'MRO', 'MGEE', 'HOLX', 'SWK', 'YUM', 'WEYS', 'VMW', 'NJR', 'ES', 'OC', 'LKQ', 'PSA', 'IBM', 'CL', 'MKC', 'TTEK', 'BFIN', 'SLB', 'K', 'AME', 'EGLE', 'AWR', 'HXL', 'CVS', 'GTS', 'LECO', 'WWD', 'CHS', 'PNR', 'SR', 'HTZ', 'RWT', 'KNX', 'SIGI', 'BEN', 'FDP', 'COWN', 'TGTX', 'BFS', 'PTVCB', 'FORM', 'NUAN', 'SVRA', 'HRB', 'VLO', 'ACC', 'MANT', 'LMNX', 'CLI', 'RNST', 'MDT', 'CDZI', 'MSFT', 'FFIV', 'IVZ', 'FARM', 'UNM', 'VRTX', 'JCOM', 'EPC', 'TDS', 'HTLD', 'PPG', 'KMB', 'AAL', 'TRC', 'CNR', 'ATGE', '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.0812e-03, 3.4426e-03, 1.7433e-03, 3.6108e-03, 4.0322e-03, 7.0092e-06,
        1.2160e-03, 7.8393e-04, 3.7572e-04, 3.2829e-03, 2.5510e-03, 3.2848e-03,
        1.9795e-03, 1.5948e-03, 1.9035e-03, 1.3232e-03, 3.7253e-03, 7.4393e-04,
        1.5663e-03, 3.2139e-03, 4.6774e-05, 1.3675e-03, 3.4900e-04, 1.1123e-03,
        2.7507e-03, 1.4062e-03, 1.1141e-04, 1.1193e-04, 1.8557e-03, 1.7124e-03,
        6.0188e-06, 1.3272e-03, 2.4017e-03, 2.7332e-03, 2.1359e-03, 5.9817e-03,
        3.5862e-03, 1.3894e-03, 3.0948e-03, 2.4452e-03, 3.2357e-03, 8.9207e-04,
        3.6493e-03, 6.5325e-03, 3.3698e-03, 1.9270e-03, 3.7521e-03, 2.8557e-03,
        1.0810e-03, 1.2786e-03, 1.9549e-04, 2.0035e-03, 5.0473e-03, 2.7927e-03,
        4.8174e-03, 2.6787e-03, 4.2066e-03, 1.6921e-03, 2.1634e-03, 1.2452e-04,
        1.9115e-03, 9.2756e-04, 1.8498e-03, 2.9666e-03, 5.1065e-04, 4.6565e-04,
        3.4908e-04, 1.1353e-05, 1.8789e-03, 5.6508e-03, 4.6657e-04, 3.8387e-03,
        7.9659e-04, 1.6680e-03, 1.9629e-04, 4.4046e-03, 5.1263e-03, 3.9773e-03,
        3.8184e-03, 7.5832e-03, 1.2118e-02, 2.9799e-03, 1.6087e-03, 4.4517e-03,
        1.8452e-03, 9.6206e-04, 2.8359e-03, 3.8410e-03, 4.9400e-04, 4.2248e-03,
        3.3712e-05, 2.1255e-03, 3.2796e-05, 4.3989e-03, 7.2285e-04, 3.5648e-04,
        3.4012e-03, 2.8880e-03, 6.3377e-04, 7.4969e-05, 6.8576e-04, 4.6577e-03,
        2.3115e-03, 2.2105e-03, 1.7792e-03, 1.6767e-04, 1.1168e-03, 2.1482e-03,
        2.1598e-03, 2.4872e-03, 3.7199e-03, 9.0039e-03, 1.4193e-04, 1.0609e-03,
        3.1350e-03, 2.0927e-04, 2.8903e-03, 3.5818e-04, 4.2612e-03, 1.8885e-05,
        8.9447e-04, 5.8242e-04, 4.0506e-04, 2.3715e-03, 2.6440e-03, 2.4477e-03,
        1.1949e-03, 6.0162e-04, 1.0234e-04, 1.3217e-03, 3.6697e-04, 3.1432e-03,
        1.9221e-03, 5.0628e-03, 2.1085e-03, 1.7069e-03, 2.8746e-03, 9.5599e-04,
        1.2216e-03, 2.7630e-03, 1.2873e-03, 2.1237e-03, 3.3068e-03, 1.3178e-03,
        3.4939e-03, 6.4403e-03, 6.1295e-04, 1.8296e-03, 2.1725e-03, 4.3890e-03,
        1.4346e-03, 3.7612e-05, 8.2120e-04, 1.4911e-03, 2.9822e-03, 1.5637e-03,
        1.5622e-03, 3.5142e-03, 2.6454e-03, 1.6082e-03, 1.7575e-03, 4.0609e-04,
        1.1752e-03, 3.7422e-03, 8.9565e-04, 4.4488e-03, 3.5427e-03, 3.8896e-03,
        2.0392e-03, 1.0185e-03, 7.3144e-04, 8.0330e-04, 8.1357e-04, 5.3866e-03,
        2.9938e-03, 2.1376e-03, 1.2438e-03, 1.5013e-03, 2.0954e-04, 6.8106e-04,
        6.9764e-04, 1.6981e-03, 6.2301e-04, 2.7551e-03, 3.9351e-04, 2.1341e-03,
        2.0144e-03, 5.0168e-03, 2.7626e-03, 1.1589e-03, 2.5006e-03, 3.3998e-03,
        3.9099e-03, 2.5529e-03, 5.3904e-03, 1.2018e-03, 3.8020e-03, 5.1012e-04,
        8.9739e-04, 3.4253e-03, 6.3658e-04, 1.2989e-03, 2.8561e-03, 2.0098e-03,
        1.5707e-03, 4.7658e-03, 2.0442e-03, 2.8097e-03, 9.7839e-03, 3.8099e-04,
        2.2462e-03, 1.0648e-03, 6.0067e-04, 1.9540e-04, 4.1107e-03, 4.4543e-03,
        2.2453e-03, 2.7645e-03, 2.7337e-03, 1.1943e-03, 3.4082e-03, 3.8013e-03,
        5.2040e-03, 1.6178e-03, 3.6956e-03, 3.9613e-03, 6.0612e-03, -3.0833e-04, -1.7685e-03, -5.0172e-04, -3.5029e-03, -7.6534e-05,
        -5.0322e-03, -2.1010e-03, -2.1918e-03, -1.0431e-02, -5.8618e-03,
        -9.2345e-04, -9.3589e-04, -1.7536e-03, -2.9623e-03, -5.3568e-03,
        -1.7905e-03, -1.0442e-03, -3.7690e-03, -1.9517e-04, -8.8258e-04,
        -1.8383e-03, -5.6296e-03, -2.5100e-03, -1.2085e-03, -4.5833e-03,
        -4.3625e-03, -1.7192e-03, -2.7646e-03, -2.0123e-03, -4.0401e-03,
        -1.2201e-03, -4.6392e-03, -2.4430e-03, -9.6991e-04, -3.0179e-03,
        -4.5902e-04, -1.9736e-03, -4.6463e-03, -6.6985e-03, -2.1671e-03,
        -7.6569e-05, -3.6137e-03, -1.4898e-03, -1.5091e-03, -1.8153e-03,
        -1.9130e-03, -5.8642e-03, -3.3251e-03, -3.4644e-03, -7.1299e-04,
        -6.1533e-03, -2.0687e-04, -1.8525e-04, -1.4695e-03, -6.1661e-04,
        -4.7249e-03, -5.4904e-04, -6.0655e-04, -2.8183e-03, -2.9298e-03,
        -1.8217e-03, -8.1402e-04, -5.3374e-04, -5.8665e-04, -3.5001e-03,
        -1.1578e-03, -9.2614e-04, -1.0293e-03, -3.0622e-03, -3.5639e-03,
        -1.8783e-03, -1.1904e-03, -4.9781e-04, -3.6557e-03, -6.2437e-03,
        -5.6544e-03, -2.1769e-03, -3.0934e-03, -2.6686e-03, -3.6881e-03,
        -1.8700e-03, -5.8881e-03, -2.4586e-03, -1.7734e-03, -1.6829e-03,
        -2.4595e-03, -1.2113e-03, -2.4570e-03, -9.1200e-04, -1.9290e-03,
        -2.6772e-03, -3.9571e-03, -4.2508e-05, -2.3009e-04, -3.5109e-03,
        -2.4278e-03, -1.9000e-03, -4.3200e-03, -5.8902e-03, -1.0054e-03,
        -8.2142e-04, -2.8263e-03, -7.5293e-04, -6.4670e-03, -2.6639e-03,
        -1.1489e-03, -1.6579e-03, -5.5390e-06, -1.6274e-03, -2.8532e-03,
        -1.1683e-03, -6.4400e-03, -2.0720e-03, -1.1866e-03, -8.6814e-04,
        -3.0422e-03, -1.2175e-03, -2.0891e-03, -1.2634e-03, -3.6210e-03,
        -1.7595e-03, -1.6745e-03, -2.1489e-03, -1.7446e-03, -2.6618e-03,
        -1.8318e-03, -2.9775e-04, -1.6222e-03, -5.2451e-04, -3.4424e-03,
        -5.4129e-03, -2.2546e-03, -1.3959e-03, -5.5762e-03, -2.4897e-03,
        -3.5982e-03, -3.1468e-03, -3.1834e-03, -1.9934e-03, -3.9418e-03,
        -4.7953e-04, -2.3479e-03, -2.2775e-03, -3.7142e-03, -2.5517e-03,
        -1.0139e-04, -2.1603e-03, -3.0052e-03, -3.1659e-03, -2.5281e-03,
        -2.9228e-04, -2.8460e-03, -6.5325e-04, -1.0752e-03, -4.0974e-03,
        -8.0081e-04, -3.0192e-03, -3.2552e-03, -7.5923e-04, -2.8146e-03,
        -4.3165e-03, -1.4082e-03, -4.4317e-04, -3.4952e-03, -9.0324e-05,
        -1.4403e-03, -1.2932e-03, -3.3981e-03, -2.0553e-03, -2.2774e-03,
        -1.6604e-03, -5.3150e-03, -9.2082e-04, -4.9261e-03, -2.0094e-03,
        -3.4309e-05, -1.8210e-03, -1.2215e-03, -4.3078e-04, -5.0803e-05,
        -2.4071e-03, -2.6293e-03, -4.6063e-03, -7.7817e-04, -2.2126e-03,
        -1.3942e-03, -2.4246e-03, -2.6961e-03, -8.6638e-04, -5.7645e-04,
        -1.0188e-03, -2.7431e-03, -2.6436e-03, -3.5517e-03, -1.0206e-03,
        -1.7100e-03, -1.7927e-03, -1.3184e-04, -6.9105e-04, -7.9016e-04,
        -2.0683e-03, -1.7558e-03, -3.0516e-03, -3.1357e-03, -2.1114e-03,
        -2.3530e-03, -3.2453e-03, -1.2705e-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])