Overall Statistics
Total Trades
7548
Average Win
0.03%
Average Loss
-0.01%
Compounding Annual Return
7.476%
Drawdown
1.200%
Expectancy
0.681
Net Profit
82.245%
Sharpe Ratio
5.483
Probabilistic Sharpe Ratio
100%
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
1.98
Alpha
0.07
Beta
0.03
Annual Standard Deviation
0.014
Annual Variance
0
Information Ratio
-0.538
Tracking Error
0.166
Treynor Ratio
2.537
Total Fees
$7955.24
##########################################
#Kamer Ali Yuksel linkedin.com/in/kyuksel#
##########################################

import numpy as np

syms = ['FR', 'ENSG', 'ARTNA', 'MLR', 'WTM', 'IAC', 'ROK', 'ACGL', 'DUK', 'EGOV', 'PLCE', 'BECN', 'SLG', 'MSA', 'WSM', 'HRI', 'MKTX', 'MCK', 'PRU', 'VRTS', 'ODFL', 'TER', 'WDFC', 'PODD', 'JWN', 'MAA', 'MFA', 'PPBI', 'VVI', 'CALM', 'NOV', 'LMT', 'IEX', 'SYK', 'TGT', 'PZZA', 'CLAR', 'SBUX', 'ASTE', 'AVY', 'NHI', 'DX', 'WSO', 'WCC', 'LUV', 'GE', 'TVTY', 'WBA', 'HOPE', 'GATX', 'ITW', 'RS', 'LRCX', 'TDY', 'XEL', 'HLF', 'HST', 'PVH', 'UTL', 'EXAS', 'LCII', 'LORL', 'NUS', 'GBCI', 'EXPO', 'NOC', 'CF', 'CASH', 'HIFS', 'RGR', 'EQT', 'KMPR', 'CMI', 'GPI', 'PGR', 'KLAC', 'DAR', 'AFG', 'COO', 'SIG', 'MU', 'GILD', 'LLY', 'CINF', 'FCNCA', 'PKG', 'AGX', 'CRVL', 'HRL', 'BKE', 'UNP', 'BBBY', 'SJM', 'ATRI', 'KO', 'EXR', 'ISBC', 'KDP', 'PRSC', 'PKBK', 'ATO', 'BLL', 'PRAA', 'MTD', 'SGEN', 'ESGR', 'XPO', 'EVR', 'GENC', 'FICO', 'AMSWA', 'EGBN', 'HBI', 'COP', 'MPWR', 'PLUS', 'DDS', 'NBN', 'HWM', 'RCL', 'SAIA', 'WAL', 'CDNS', 'ATR', 'JNJ', 'ITIC', 'DFS', 'NI', 'FIX', 'NEE', 'V', 'IRM', 'REGN', 'VGR', 'WYND', 'DLX', 'COST', 'LAMR', 'DPZ', 'MNR', 'HD', 'ABR', 'JBSS', 'JCI', 'PFC', 'SNX', 'PIPR', 'AGNC', 'LII', 'ABCB', 'LSI', 'MPW', 'MXIM', 'GCBC', 'PXD', 'FIS', 'ECL', 'CCF', 'BOOM', 'MASI', 'BANF', 'EEFT', 'LANC', 'THG', 'CLX', 'TKR', 'SUI', 'WOR', 'OKE', 'CVCY', 'GL', 'WPC', 'CNSL', 'GSBC', 'RIG', 'CUBE', 'CSGP', 'AMZN', 'CHE', 'HOFT', 'SBNY', 'GS', 'AOS', 'STE', 'MSCI', 'LCNB', 'CMS', 'LXP', 'TCBI', 'BRKR', 'WAB', 'LSCC', 'ETN', 'HTH', 'WINA', 'AJG', 'WDC', 'UTHR', 'NYMT', 'GABC', 'PHM', 'SAFT', 'HSKA', 'KWR', 'CORE', 'ORA', 'FCX', 'TCX', 'RPT', 'NVR', 'RHI', 'SLM', 'IHC', 'BOKF', 'BGS', 'AIZ', 'ES', 'IDXX', 'NPO', 'GRMN', 'NFG', 'BMY', 'SMBC', 'PRIM', 'CSV', 'RGEN', 'INTC', 'COHR', 'FIZZ', 'BRT', 'CMCSA', 'EVI', 'PNC', 'LBC', 'RHP', 'GNTX', 'TRN', 'ARGO', 'CR', 'HAL', 'SMTC', 'MKC', 'SFST', 'EIX', 'TXN', 'TYL', 'EMN', 'TJX', 'HT', 'OFLX', 'DLR', 'CI', 'LKFN', 'DOX', 'KALU', 'EQC', 'GWW', 'CNS', 'DISCA', 'ELS', 'PDCO', 'KFRC', 'LDL', 'ROST', 'JOE', 'RGLD', 'ALGN', 'TXRH', 'SWX', 'CVGW', 'CHH', 'EW', 'CMA', 'SR', 'WBS', 'NEU', 'COG', 'BA', 'PDFS', 'F', 'HI', 'DVA', 'SPG', 'OGE', 'OFG', 'OZK', 'MLAB', 'SCI', 'HBNC', 'SGC', 'CAT', 'AON', 'CTB', 'TAST', 'BFC', 'POOL', 'MBWM', 'QRTEA', 'DKS', 'BFS', 'APH', 'CTAS', 'RGCO', 'NYT', 'MYRG', 'DE', 'MNRO', 'WST', 'DHIL', 'AEP', 'CACC', 'KFY', 'AMT', 'STL', 'NYCB', 'UHAL', 'STLD', 'QCRH', 'CPRT', 'MS', 'CBRL', 'VAR', 'ABC', 'COKE', 'MCBC', 'HP', 'RNST', 'WEC', 'BRKL', 'FFIV', 'ALG', 'SLP', 'CHD', 'DTE', 'ORLY', 'NATH', 'AWK', 'RMD', 'ACNB', 'CASY', 'SBAC', 'GTY', 'TT', 'PPG', 'ERIE', 'PH', 'CCOI', 'EGP', 'HEI', 'HCI', 'CHDN', 'MIDD', 'WLK', 'POL', 'UDR', 'IRDM', 'FFIN', 'CPT', 'JBT', 'AAPL', 'ISRG', 'TSN', 'BCML', 'BC', 'STZ', 'VMI', 'LHX', 'CBU', 'SIVB', 'HOMB', 'UHS', 'EQR', 'SM', 'HSIC', 'BDX', 'MSTR', 'FBNC', 'CSX', 'TDG', 'GRIF', 'CVX', 'CCL', 'ARW', 'GHM', 'TDW', 'PBH', 'FCAP', 'GLRE', 'YORW', 'ADS', 'NTUS', 'ETR', 'GRC', 'CUZ', 'GIS', 'GPC', 'PRA', 'MOS', 'WMT', 'GHL', 'NUE', 'EXC', 'BDC', 'OMC', 'APA', 'CBT', 'SSD', 'REG', 'SBCF', 'MTX', 'PBCT', 'SCSC', 'OFC', 'ZIXI', 'WFC', 'Y', 'CIA', 'OVBC', 'PG', 'SLGN', 'ITRI', 'DGX', 'NWN', 'SCOR', 'BSX', 'FBP', 'DSPG', 'BXS', 'OII', 'WY', 'INVA', 'POR', 'CRAI', 'GD', 'SPGI', 'SPR', 'CRL', 'J', 'HAFC', 'SO', 'PAYX', 'TEX', 'TRV', 'LMNR', 'C', 'CVCO', 'FE', 'MCO', 'TMP', 'PCAR', 'PBI', 'HPQ', 'BLKB', 'MLM', 'NCMI', 'TROW', 'MD', 'CMO', 'MCD', 'ECOL', 'PCG', 'KNL', 'ATLO', 'MTW', 'UBA', 'ADP', 'GT', 'GES', 'IIVI', 'PCH', 'LDOS', 'TRST', 'WTS', 'BRO', 'MWA', 'TILE', 'CZNC', 'EQIX', 'SRCL', 'EXP', 'JNPR', 'FISV', 'CTO', 'ACN', 'WERN', 'CXW', 'TPX', 'ATI', 'JEF', 'FSTR', 'HMSY', 'OXY', 'BELFB', 'CCRN', 'WNEB', 'PGC', 'HOG', 'DVN', 
'ABM', 'XRX', 'ASB', 'AVT', 'CPF', 'EVBN', 'FAST', 'INFN', 'UMH', 'FSS', 'WWW', 'INGR', 'POWL', 'ED', 'TAP', 'DHC', 'PBIP', 'L', 'MET', 'CUB', 'VMC', 'NAT', 'CPSI', 'LOW', 'TPC', 'CMCO', 'WHG', 'TRTN', 'HURN', 'HUBG', 'DORM', 'NXGN', 'VIVO', 'CMTL', 'MDP', 'PRGS', 'O', 'PNM', 'IRET', 'DEI', 'USD', 'BUSE', 'MTH', 'SHBI', 'NEOG', 'MEI', 'FCBC', 'CVA', 'CYH', 'NWLI', 'ADSK', 'STFC', 'MHK', 'EXPD', 'GBL', 'FSP', 'RL', 'UEIC', 'BWA', 'VFC', 'URBN', 'D', 'ARR', 'GPS', 'ALCO', 'KRG', 'LAZ', 'TOL', 'CAH', 'SYKE', 'MYGN', 'ICFI', 'VNO', 'ITT', 'EME', 'R', 'TXT', 'ETH', 'NDAQ', 'WRI', 'TTC', 'FNF', 'MAS', 'SNV', 'JKHY', 'MRO', 'MGEE', 'CBZ', 'UCBI', 'HOLX', 'SWK', 'YUM', 'WEYS', 'SMG', 'IVC', 'BIIB', 'VMW', 'NJR', 'KR', 'CIR', 'BXP', 'BKD', 'OC', 'PPL', 'MORN', 'TCBK', 'DCI', 'MTB', 'LKQ', 'PSA', 'IBM', 'CL', 'JACK', 'UGI', 'TTEK', 'BFIN', 'EPAC', 'WHR', 'SLB', 'NWFL', 'K', 'NLY', 'AME', 'GBLI', 'STT', 'EGLE', 'IDCC', 'AWR', 'NUVA', 'HXL', 'CVS', 'SCHN', 'GTS', 'WTRG', 'LECO', 'WWD', 'CHS', 'PNR', 'NCR', 'UMPQ', 'HTZ', 'KW', 'CWCO', 'WTBA', 'RWT', 'KNX', 'CFFN', 'FC', 'SIGI', 'RIGL', 'BEN', 'EMR', 'FDP', 'PEAK', 'COWN', 'DD', 'TGTX', 'HSY', 'AIR', 'PTVCB', 'FORM', 'WEN', 'NUAN', 'KELYA', 'IMMR', 'HAYN', 'HAE', 'SVRA', 'ADBE', 'ESS', 'HRB', 'PCYO', 'TNC', 'VLO', 'ACC', 'AIV', 'MANT', 'LMNX', 'CLI', 'MPX', 'MDT', 'RGS', 'CDZI', 'MSFT', 'ALL', 'IVZ', 'HE', 'FARM', 'UNM', 'VRTX', 'FUL', 'MA', 'JCOM', 'EPC', 'AMKR', 'TDS', 'RNR', 'NRIM', 'WMK', 'OI', 'BAX', 'HTLD', 'HWC', 'KMB', 'AAL', 'TRC', 'FULT', 'CNR', 'ATGE', 'ZION', 'MDLZ', 'CPB', 'CW', 'THS', 'DISH', 'MAT', 'TGI', 'FCN', 'PKE', 'TR', 'FRBK', 'IRBT', 'HBMD', 'MGM', 'ANH', 'FHI']

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([1.1681e-03, 2.1538e-03, 9.6941e-05, 2.1016e-03, 2.1257e-03, 2.1175e-03,
        1.7404e-03, 5.1447e-04, 2.1295e-03, 1.1398e-03, 1.7158e-03, 1.1927e-03,
        4.6085e-04, 1.6319e-03, 1.8285e-03, 1.5960e-03, 1.6513e-03, 2.1693e-03,
        2.3698e-04, 2.1109e-03, 2.0379e-03, 1.6657e-03, 1.7519e-03, 1.0481e-03,
        9.1470e-04, 1.9097e-03, 1.6363e-03, 9.9969e-04, 2.1162e-03, 2.4857e-05,
        1.5257e-04, 9.2471e-04, 1.2081e-03, 6.5077e-04, 1.7057e-03, 1.3786e-03,
        7.4173e-04, 7.9177e-04, 1.8611e-03, 1.5428e-03, 2.0640e-03, 2.1286e-03,
        1.8977e-03, 3.0439e-04, 1.8783e-03, 5.2937e-04, 5.5576e-04, 9.8823e-05,
        3.6876e-04, 3.7239e-04, 8.4689e-04, 6.8328e-04, 1.8652e-05, 2.0301e-03,
        1.2669e-03, 9.5110e-04, 1.1690e-03, 5.7136e-04, 9.9343e-05, 1.0384e-03,
        1.9584e-03, 2.1888e-03, 1.7438e-03, 1.9845e-03, 1.4255e-03, 1.6500e-03,
        1.4129e-03, 4.6995e-04, 2.1361e-03, 1.9986e-03, 1.3687e-03, 2.0583e-03,
        2.1404e-03, 1.7464e-03, 1.3179e-03, 2.7827e-04, 2.1168e-03, 1.9666e-03,
        2.1365e-03, 2.1612e-03, 1.7473e-03, 1.6428e-03, 2.1689e-03, 2.0211e-04,
        2.0800e-03, 2.0688e-03, 5.9387e-04, 3.0409e-04, 1.0787e-03, 1.3412e-03,
        1.3654e-04, 2.0410e-04, 1.6973e-03, 1.7808e-03, 5.3335e-04, 1.8252e-03,
        1.6247e-03, 2.0649e-03, 2.0695e-03, 2.1173e-03, 2.1612e-03, 1.8334e-03,
        4.3180e-04, 1.1468e-05, 1.3725e-03, 4.0170e-05, 1.6455e-03, 2.0362e-03,
        5.6648e-04, 3.0828e-04, 1.1063e-03, 2.1430e-03, 2.4766e-04, 1.9411e-03,
        1.9291e-03, 2.0564e-03, 1.5514e-03, 4.7279e-04, 4.1406e-04, 1.9068e-03,
        2.1576e-03, 2.0812e-03, 1.9382e-03, 5.4867e-04, 1.4439e-03, 2.0853e-03,
        2.1704e-03, 2.1390e-03, 2.0459e-03, 2.1227e-03, 1.9899e-03, 8.6902e-04,
        5.2609e-04, 2.1653e-03, 2.1132e-03, 1.8902e-03, 1.8478e-03, 5.5037e-05,
        2.0442e-03, 2.0970e-03, 2.0963e-03, 5.9666e-04, 2.0598e-03, 2.0337e-03,
        1.9769e-03, 2.1269e-03, 1.6483e-04, 2.1551e-03, 1.8213e-03, 1.9452e-03,
        1.0154e-03, 1.7823e-03, 4.3314e-04, 7.4539e-04, 3.3252e-04, 8.4960e-05,
        1.3144e-04, 2.1092e-03, 7.7659e-04, 1.2032e-03, 6.3261e-04, 2.0026e-03,
        1.9128e-03, 1.0335e-03, 1.2009e-03, 1.5149e-03, 2.1384e-03, 1.9876e-03,
        2.1571e-03, 2.2066e-04, 2.0619e-03, 1.7476e-03, 2.7092e-04, 1.7185e-03,
        1.8087e-04, 3.0862e-04, 2.4617e-04, 1.4254e-03, 1.4691e-03, 1.6363e-03,
        1.3856e-03, 2.0543e-03, 2.0211e-03, 1.9685e-03, 1.1176e-03, 1.1668e-03,
        2.0815e-03, 5.4708e-05, 8.6125e-04, 1.2424e-04, 1.1207e-03, 7.9389e-04,
        8.9805e-04, 3.1466e-04, 2.1437e-03, 7.3783e-04, 3.1100e-05, 1.1984e-03,
        2.1837e-03, 1.9525e-03, 3.7698e-04, 2.0207e-03, 1.2380e-03, 1.8308e-03,
        3.4184e-04, 1.8152e-03, 2.1406e-03, 2.1225e-03, 1.6994e-03, 1.9616e-03,
        3.7795e-04, 8.7483e-04, 1.0566e-03, 1.5057e-03, 2.0435e-03, 1.4086e-05,
        6.0437e-04, 5.3727e-04, 2.0187e-03, 1.7281e-03, 1.4585e-03, 7.1504e-04,
        1.1171e-03, 2.0794e-03, 2.0655e-03, 8.0067e-04, 1.5435e-03, 1.6989e-03,
        2.0838e-03, 4.8866e-04, 1.8817e-03, 1.1074e-03, 1.8462e-03, 1.8403e-03,
        2.1516e-03, 9.3750e-04, 1.6186e-04, 2.1130e-03, 1.9988e-03, 1.0111e-05,
        3.4585e-04, 7.6446e-04, 2.1631e-03, 1.7070e-03, 1.9398e-03, 1.8843e-03,
        2.0576e-03, 2.0030e-03, 2.6855e-04, 1.3715e-03, 5.5354e-05, 2.0555e-03,
        2.1481e-03, 1.0825e-03, 2.0298e-03, 7.8175e-04, 1.7236e-03, 8.2339e-04,
        2.1667e-03, 1.9505e-03, 2.9474e-04, 1.3754e-03, 1.9767e-03, 1.2084e-03,
        1.8948e-03, 1.1871e-04, 1.5896e-03, 1.1155e-03, 3.1716e-05, 2.0456e-03,
        6.4347e-04, 1.2178e-03, 2.1404e-03, 4.3453e-05, 2.1764e-03, 1.9378e-03,
        5.7613e-05, 1.7606e-03, 1.7344e-03, 2.0977e-03, 2.0975e-03, 7.5979e-04,
        2.1439e-03, 1.2266e-03, 2.1617e-03, 1.9578e-03, 2.0897e-03, 1.4936e-03,
        2.1890e-03, 3.9925e-04, 2.0701e-03, 9.9985e-04, 1.2965e-03, 5.2062e-04,
        2.1266e-03, 5.0892e-04, 1.9722e-03, 1.1171e-03, 3.7539e-05, 5.6858e-05,
        2.0527e-03, 2.1137e-03, 1.2639e-03, 1.0950e-03, 2.0690e-04, 2.1302e-03,
        7.3852e-04, 2.1068e-03, 2.0421e-03, 1.4289e-03, 2.1330e-03, 1.5501e-03,
        2.4360e-05, 1.9013e-03, 7.8792e-04, 2.1419e-03, 1.2198e-03, 1.9709e-03,
        2.0776e-03, 2.1091e-03, 2.1619e-03, 2.1264e-03, 2.0989e-03, 6.6190e-04,
        5.0409e-04, 1.8299e-03, 3.8260e-04, 1.3865e-03, 1.0847e-03, 1.6061e-03,
        1.7401e-03, 1.6430e-03, 1.1649e-03, 2.0359e-03, 1.9515e-04, 1.8440e-03,
        2.1263e-03, 1.7357e-03, 1.2385e-03, 7.4183e-04, 1.3258e-03, 1.7848e-03,
        2.1335e-03, 5.1856e-04, 1.9882e-03, 5.2495e-04, 1.8882e-03, 1.7821e-04,
        1.8139e-03, 1.8122e-03, 2.0659e-03, 1.9090e-03, 2.0651e-03, 1.9789e-03,
        7.4155e-04, 7.9284e-07, 2.1711e-03, 6.9812e-04, 2.1455e-03, 2.0199e-03,
        5.1596e-04, 2.0782e-03, 6.0650e-04, 1.0429e-03, 2.1148e-03, 9.1814e-04,
        8.4396e-04, 1.8966e-03, 2.1366e-03, 4.3654e-04, 1.6093e-03, 2.1990e-05,
        2.0446e-03, 1.3172e-03, 7.7351e-04, 2.1139e-03, 2.2516e-06, 1.7411e-03,
        2.1283e-03, -2.5068e-04, -1.5771e-03, -2.0248e-03, -2.0424e-03, -1.1501e-03,
        -1.8964e-03, -1.2118e-03, -9.4964e-05, -2.1016e-03, -6.0692e-05,
        -3.3509e-04, -1.8794e-03, -2.1351e-03, -1.9831e-03, -2.0978e-03,
        -1.7903e-03, -1.5894e-03, -6.7344e-04, -1.3929e-03, -1.8382e-03,
        -2.0530e-03, -2.0746e-03, -1.9745e-03, -2.0013e-03, -1.0611e-03,
        -1.5152e-03, -1.5993e-03, -1.9876e-03, -1.7266e-03, -1.7009e-03,
        -1.3192e-03, -1.8071e-03, -1.4258e-03, -1.7877e-03, -1.0831e-04,
        -1.9013e-03, -2.0517e-03, -2.0323e-03, -1.4841e-03, -1.8860e-03,
        -1.5501e-03, -1.1064e-03, -2.1361e-03, -1.8901e-03, -2.1262e-03,
        -2.1717e-03, -2.0312e-03, -2.1542e-03, -2.0539e-03, -4.4147e-05,
        -6.4704e-04, -1.2991e-03, -6.5256e-04, -3.7845e-04, -2.1175e-03,
        -4.4684e-04, -2.4704e-04, -1.8909e-03, -2.0703e-03, -2.1351e-03,
        -1.0786e-03, -1.7663e-03, -6.2781e-05, -3.5393e-04, -1.8083e-03,
        -2.0800e-03, -1.7325e-03, -1.9048e-03, -1.3800e-03, -2.0349e-03,
        -1.9559e-03, -8.8384e-04, -4.4659e-05, -1.9317e-03, -8.2234e-05,
        -2.0616e-03, -6.1431e-04, -1.4230e-03, -1.4246e-03, -7.1659e-04,
        -1.1188e-03, -2.1893e-03, -1.4354e-04, -2.1110e-03, -1.9815e-03,
        -1.3827e-03, -2.8018e-04, -1.5899e-03, -6.7545e-04, -2.9712e-04,
        -3.6035e-04, -1.8612e-03, -2.1214e-03, -7.6091e-04, -4.3491e-04,
        -1.8217e-03, -6.5166e-04, -1.4032e-03, -6.6690e-04, -2.1628e-03,
        -8.0722e-05, -1.1212e-03, -1.5947e-03, -3.9721e-04, -1.7785e-05,
        -2.5709e-04, -1.4685e-03, -1.9274e-03, -1.1130e-03, -1.9108e-03,
        -3.5662e-04, -5.6057e-04, -9.0255e-04, -1.6742e-03, -1.9959e-03,
        -2.0245e-04, -2.0560e-03, -1.7234e-03, -1.7923e-03, -9.1364e-04,
        -2.1189e-03, -2.0343e-03, -2.0471e-03, -2.0794e-03, -3.4419e-04,
        -1.7360e-03, -7.9402e-04, -1.1958e-04, -1.0129e-03, -1.6754e-03,
        -1.9063e-03, -1.9485e-03, -5.1980e-04, -2.0145e-03, -8.1104e-04,
        -1.9790e-04, -1.9021e-03, -8.4164e-04, -1.8922e-03, -1.4722e-03,
        -5.9822e-04, -1.5026e-03, -1.6458e-03, -5.2989e-04, -1.6572e-03,
        -1.4225e-03, -2.6569e-05, -1.3181e-03, -2.0391e-03, -1.5870e-04,
        -2.1366e-03, -2.1120e-03, -1.2625e-03, -5.8778e-04, -1.9557e-03,
        -8.1913e-04, -1.4499e-03, -1.9799e-03, -1.9507e-03, -2.0609e-03,
        -2.1212e-03, -2.1008e-04, -1.3896e-03, -7.5707e-04, -1.2448e-03,
        -2.0303e-03, -1.6134e-03, -1.4136e-03, -2.1304e-03, -1.0094e-03,
        -1.7904e-03, -1.8235e-03, -2.0920e-03, -8.7914e-04, -1.3426e-03,
        -1.9281e-03, -1.5040e-04, -2.1038e-03, -5.4868e-05, -2.0398e-03,
        -1.5297e-03, -1.9916e-03, -1.8563e-03, -2.0189e-03, -1.5557e-03,
        -2.1302e-03, -2.0092e-03, -1.5711e-03, -1.5169e-03, -1.2353e-03,
        -3.3474e-04, -2.0192e-03, -2.1237e-03, -1.5321e-03, -1.9694e-03,
        -1.8490e-03, -8.3999e-04, -1.3935e-03, -1.5531e-03, -3.1193e-04,
        -1.8157e-03, -1.3339e-03, -1.5297e-03, -2.8002e-04, -1.0167e-03,
        -1.0404e-03, -1.6264e-03, -1.7675e-03, -1.5232e-03, -1.4994e-03,
        -1.9943e-03, -2.1740e-03, -1.3302e-04, -1.8676e-04, -7.3384e-04,
        -1.7656e-03, -1.7402e-03, -1.7571e-03, -2.9289e-04, -1.4719e-05,
        -1.3815e-03, -2.0421e-03, -1.6351e-03, -9.4577e-04, -2.5230e-05,
        -2.0891e-03, -9.9598e-06, -1.6563e-03, -1.4983e-03, -2.0546e-03,
        -2.1293e-03, -1.7918e-03, -1.1980e-03, -8.4801e-04, -2.0666e-03,
        -1.7343e-03, -5.0809e-05, -1.9292e-03, -4.3772e-04, -2.1608e-03,
        -5.3666e-04, -1.0157e-03, -1.5658e-03, -1.4956e-03, -1.7981e-03,
        -7.7700e-04, -1.3815e-03, -2.0561e-03, -9.2779e-04, -1.2855e-03,
        -1.2949e-03, -1.9744e-04, -1.7307e-03, -1.9760e-03, -1.7358e-03,
        -5.9414e-04, -2.0121e-03, -5.5778e-04, -2.0432e-03, -2.1076e-03,
        -1.3497e-03, -7.1829e-04, -1.8147e-03, -1.2972e-03, -1.9785e-03,
        -9.7268e-04, -6.0275e-04, -1.9347e-03, -1.8023e-03, -2.0970e-03,
        -1.0684e-03, -1.9682e-03, -4.9862e-04, -1.9663e-03, -2.1244e-03,
        -2.0830e-03, -5.2259e-04, -6.1887e-04, -1.7227e-03, -7.8921e-04,
        -1.8375e-03, -2.0554e-03, -1.6617e-03, -1.8853e-03, -1.3044e-03,
        -1.4174e-03, -1.9006e-03, -1.6330e-03, -1.1725e-04, -2.1044e-03,
        -9.7622e-04, -1.9957e-03, -2.0582e-03, -2.0585e-03, -2.0325e-03,
        -2.1182e-03, -2.0004e-03, -2.9365e-04, -1.2033e-03, -1.5143e-03,
        -1.2445e-03, -1.2087e-03, -1.3591e-03, -1.4547e-03, -2.1083e-03,
        -1.6327e-03, -2.0554e-03, -1.9503e-03, -2.1508e-03, -1.4906e-03,
        -1.6678e-03, -3.1225e-04, -2.1612e-03, -8.1187e-04, -2.0713e-03,
        -1.3449e-03, -3.3828e-04, -3.3827e-04, -1.4563e-03, -2.1508e-03,
        -1.6012e-03, -1.4414e-03, -1.9754e-03, -9.8982e-04, -2.0057e-03,
        -8.4609e-04, -2.1426e-03, -2.0687e-03, -9.8828e-04, -2.1048e-03,
        -2.0949e-03, -1.1353e-03, -1.5319e-03, -1.9293e-03, -1.1846e-04,
        -1.7781e-03, -1.7343e-03, -1.9568e-04, -2.1059e-03, -1.7944e-03,
        -4.3215e-04, -6.4227e-04, -2.1226e-03, -1.3714e-03, -6.4521e-04])
        
        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])