Overall Statistics
Total Trades
13900
Average Win
0.03%
Average Loss
-0.02%
Compounding Annual Return
7.755%
Drawdown
1.200%
Expectancy
0.605
Net Profit
147.338%
Sharpe Ratio
6.23
Probabilistic Sharpe Ratio
100%
Loss Rate
43%
Win Rate
57%
Profit-Loss Ratio
1.84
Alpha
0.077
Beta
0.007
Annual Standard Deviation
0.012
Annual Variance
0
Information Ratio
-0.515
Tracking Error
0.181
Treynor Ratio
10.867
Total Fees
$14537.83
##########################################
#Kamer Ali Yuksel linkedin.com/in/kyuksel#
##########################################

import numpy as np

syms = ['ANDE', 'GRIF', 'ENSG', 'MLR', 'WTM', 'CBB', 'IAC', 'ROK', 'CNP', 'SLG', 'MKTX', 'MCK', 'VRTS', 'ODFL', 'AES', 'JWN', 'SNPS', 'MFA', 'VVI', 'IEX', 'SCCO', 'CLAR', 'SBUX', 'ASTE', 'DX', 'ADC', 'AVAV', 'LUV', 'IART', 'TDY', 'TSBK', 'ESXB', 'EXAS', 'LCII', 'OLP', 'WSBF', 'LORL', 'GBCI', 'EXPO', 'NOC', 'CF', 'CASH', 'HIFS', 'RGR', 'CMI', 'PGR', 'SPGI', 'DAR', 'COO', 'MU', 'ASH', 'LLY', 'ROLL', 'HBCP', 'PKG', 'CNA', 'CRVL', 'HRL', 'FITB', 'UNP', 'SONA', 'AXP', 'ATRI', 'EXR', 'KDP', 'PRSC', 'PKBK', 'ATO', 'NVDA', 'SGEN', 'EVR', 'CLFD', 'FHN', 'CNMD', 'EGBN', 'COP', 'MPWR', 'NP', 'HTLF', 'DDS', 'PFE', 'HMN', 'UFPT', 'SAIA', 'WAL', 'CDNS', 'JNJ', 'NEE', 'IRM', 'VGR', 'TTWO', 'MED', 'WYND', 'DPZ', 'AJRD', 'XLNX', 'EVBN', 'MNR', 'HD', 'JBSS', 'CVLY', 'PFC', 'SNX', 'BK', 'AGNC', 'ABCB', 'PEBO', 'UNH', 'MPW', 'GCBC', 'PRK', 'ATVI', 'CCF', 'DORM', 'LAD', 'SBSI', 'EEFT', 'TKR', 'SUI', 'OKE', 'WPC', 'GSBC', 'ROCK', 'UVE', 'AMZN', 'CVI', 'CHE', 'TRS', 'SBNY', 'MTSC', 'ICFI', 'AOS', 'RYN', 'MSCI', 'CMS', 'DHI', 'LXP', 'TCBI', 'WAB', 'LSCC', 'HTH', 'WINA', 'TTC', 'NYMT', 'GABC', 'MRO', 'SAFT', 'KWR', 'FCX', 'TCX', 'HRC', 'ALXN', 'NVR', 'SMG', 'ZBRA', 'IHC', 'IOSP', 'CCBG', 'NPO', 'GRMN', 'SMBC', 'DIS', 'PRIM', 'CSV', 'INTC', 'FIZZ', 'LYV', 'CSGS', 'LBC', 'TREX', 'NTAP', 'ARGO', 'CR', 'SMTC', 'BLK', 'SFST', 'EIX', 'TXN', 'TYL', 'BRC', 'NLY', 'TJX', 'CB', 'IMKTA', 'CI', 'LKFN', 'KALU', 'CNS', 'DISCA', 'LDL', 'ALGN', 'MGPI', 'CVGW', 'SSB', 'EW', 'CMA', 'SR', 'WBS', 'EBSB', 'PDFS', 'F', 'HI', 'URI', 'SNEX', 'EBAY', 'OFG', 'HBNC', 'HWKN', 'CAT', 'CTB', 'BFC', 'MBWM', 'AZO', 'AGM', 'CTAS', 'NYT', 'NNI', 'DE', 'WST', 'DHIL', 'CACC', 'STL', 'ESS', 'STLD', 'QCRH', 'AYI', 'CGNX', 'CPRT', 'VTR', 'FDX', 'CBRL', 'VAR', 'ABC', 'FFIV', 'ALG', 'SLP', 'JOUT', 'LULU', 'NATH', 'AWK', 'MRCY', 'SHEN', 'NNN', 'CASY', 'JCOM', 'ONTO', 'SBAC', 'NRIM', 'BKNG', 'GTY', 'TT', 'ERIE', 'ECPG', 'HEI', 'HCI', 'RCII', 'WLK', 'POL', 'UDR', 'TREE', 'AAPL', 'TSN', 'WEX', 'BCML', 'STZ', 'CLR', 'LMAT', 'PKE', 'MSTR', 'TDG', 'KMX',
'EZPW', 'AVB', 'TDW', 'PBH', 'TDC', 'ACGL', 'ODC', 'GLRE', 'YORW', 'ADS', 'FNB', 'NTUS', 'ETR', 'GRC', 'CUZ', 'DIOD', 'MOS', 'GHL', 'A', 'NUE', 'EXC', 'NOV', 'BDC', 'FMBH', 'APA', 'FARO', 'SBCF', 'PBCT', 'SCSC', 'NPK', 'WFC', 'Y', 'CIA', 'HLF', 'DGX', 'POWI', 'PVH', 'NWN', 'SCOR', 'BSX', 'IBOC', 'FBP', 'KBR', 'DSPG', 'BXS', 'SLAB', 'WY', 'HES', 'INVA', 'FBC', 'POR', 'CRAI', 'WABC', 'GD', 'PLPC', 'CRL', 'AGCO', 'MKSI', 'J', 'HAFC', 'PAYX', 'FDS', 'LMNR', 'C', 'FE', 'MCO', 'KMT', 'ARE', 'TISI', 'HPQ', 'BLKB', 'NCMI', 'MD', 'PRAA', 'PCG', 'MTW', 'AAP', 'RGP', 'LDOS', 'TRST', 'WTS', 'BSRR', 'MDC', 'MWA', 'DHT', 'EQIX', 'SRCL', 'UBSI', 'JNPR', 'CHRW', 'FISV', 'CXW', 'HWM', 'ATI', 'JEF', 'HMSY', 'BELFB', 'CCRN', 'PGC', 'AKR', 'DVN', 'XRX', 'LAMR', 'AVT', 'NKSH', 'CPF', 'FAST', 'INFN', 'EML', 'INGR', 'JCI', 'OLED', 'PDCE', 'CATO', 'AMG', 'SEE', 'L', 'CUB', 'MCY', 'CPSI', 'SJW', 'LOW', 'WHG', 'HUBG', 'VIVO', 'SEIC', 'ELY', 'O', 'USD', 'SHBI', 'NWLI', 'STFC', 'GBL', 'ALBO', 'FSP', 'RL', 'BWA', 'CEVA', 'URBN', 'ARR', 'HIG', 'GPS', 'ALCO', 'BDGE', 'KRG', 'LAZ', 'TOL', 'UHT', 'VNO', 'ATNI', 'PICO', 'ANAT', 'GPN', 'R', 'TXT', 'NDAQ', 'NWPX', 'HNI', 'MAS', 'SNV', 'UCBI', 'RTX', 'YUM', 'WEYS', 'ORRF', 'SPTN', 'VMW', 'DAKT', 'BKD', 'OC', 'PPL', 'CLGX', 'DCI', 'PFBC', 'CL', 'BH', 'BFIN', 'NATI', 'SLB', 'K', 'STT', 'EGLE', 'DHX', 'MRVL', 'EA', 'AWR', 'NUVA', 'INTU', 'EQC', 'SCHN', 'WWD', 'PNR', 'GFF', 'CFFI', 'FFIC', 'LHCG', 'BBY', 'HTZ', 'ESSA', 'TPR', 'KW', 'CWCO', 'RMBS', 'RWT', 'KNX', 'ENZ', 'SIGI', 'RIGL', 'BEN', 'CTL', 'SCU', 'BMI', 'COWN', 'GEF', 'FORR', 'DD', 'MLP', 'PTVCB', 'FORM', 'KELYA', 'IMMR', 'VALU', 'HRB', 'TFSL', 'TNC', 'VLO', 'ACC', 'MANT', 'LEG', 'LMNX', 'MDT', 'CDZI', 'MSFT', 'IVZ', 'HE', 'FARM', 'NTCT', 'HUM', 'VRTX', 'FUL', 'CRY', 'EPR', 'TDS', 'SHW', 'ALKS', 'OI', 'HTLD', 'KVHI', 'AAL', 'TRC', 'CNR', 'ATGE', 'CVLT', 'CWT', 'HCCI', 'AVID', 'DISH', 'GOOGL', 'MGLN', 'IFF', 'LQDT', 'MAT', 'LHX', 'WYNN', 'FCN', 'TR', 'FRBK', 'IRBT', 'HBMD']

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([8.8633e-04, 1.5941e-04, 3.0562e-03, 2.0499e-03, 3.3373e-03, 2.1871e-04,
        2.8273e-03, 1.0512e-03, 1.0885e-03, 2.5332e-03, 1.9938e-03, 2.5125e-03,
        2.2269e-03, 2.4496e-03, 8.6828e-04, 1.0082e-03, 4.5930e-04, 5.5439e-04,
        2.7551e-03, 2.4599e-03, 2.0200e-03, 1.2336e-03, 2.0759e-03, 1.1889e-03,
        2.9976e-03, 5.5755e-04, 4.2591e-04, 1.5798e-03, 6.6894e-04, 1.7825e-03,
        1.5673e-03, 2.7448e-04, 1.1754e-03, 1.1472e-03, 8.0700e-04, 5.2089e-04,
        3.2873e-03, 3.6518e-03, 2.8897e-03, 2.9017e-03, 7.4109e-05, 1.5630e-03,
        3.2916e-03, 1.1254e-03, 2.5275e-03, 2.6473e-03, 6.1998e-04, 3.0128e-03,
        3.0782e-03, 1.2433e-03, 2.8614e-03, 3.5734e-03, 1.5976e-03, 1.8196e-03,
        3.2821e-03, 1.5701e-04, 2.1741e-04, 2.7332e-03, 1.3114e-03, 2.3037e-03,
        1.3464e-03, 1.8784e-03, 2.1307e-03, 2.9026e-03, 2.1776e-03, 1.9203e-03,
        1.8122e-03, 3.6501e-03, 2.6772e-03, 8.6106e-04, 3.4548e-03, 2.3881e-03,
        2.0669e-03, 2.1925e-03, 2.0392e-04, 3.6180e-03, 2.4907e-03, 2.9435e-03,
        1.3568e-04, 2.4363e-03, 1.8329e-03, 1.3755e-03, 6.8516e-04, 2.7448e-03,
        3.6473e-03, 3.4942e-03, 1.6137e-03, 3.6006e-03, 1.7335e-03, 3.2699e-03,
        7.8575e-06, 1.4741e-03, 3.0508e-03, 1.9711e-03, 8.9850e-04, 2.3724e-04,
        8.1599e-04, 2.3947e-03, 2.9694e-03, 2.6842e-03, 1.5689e-03, 1.4786e-03,
        2.4375e-03, 9.8674e-04, 1.0561e-03, 2.2180e-03, 1.8081e-03, 1.4037e-03,
        9.6816e-04, 1.1428e-03, 4.2894e-04, 1.2847e-04, 1.1526e-03, 9.0601e-04,
        1.8286e-03, 2.4699e-03, 1.5577e-03, 1.0858e-03, 2.7824e-03, 3.5764e-03,
        2.0699e-03, 2.9642e-03, 1.4524e-03, 3.5270e-04, 1.2258e-03, 2.5536e-03,
        9.9499e-04, 2.5686e-03, 3.3781e-03, 1.9114e-03, 3.0453e-04, 2.4108e-03,
        2.0258e-03, 2.3552e-03, 3.5002e-03, 2.6903e-03, 1.6961e-03, 2.3963e-03,
        3.2944e-03, 1.9545e-03, 2.0880e-03, 3.0073e-03, 4.0716e-04, 2.9329e-03,
        3.0306e-03, 1.9524e-04, 2.9929e-03, 9.9227e-04, 1.4702e-03, 2.9922e-03,
        1.7090e-03, 4.1578e-04, 2.7120e-03, 2.1966e-03, 2.6230e-03, 1.9538e-03,
        2.3091e-03, 1.6047e-03, 1.7600e-03, 1.3000e-03, 1.2895e-03, 1.6950e-03,
        3.0570e-03, 1.7082e-03, 3.0294e-03, 2.4639e-03, 7.1026e-04, 1.2728e-03,
        1.4403e-03, 1.2290e-03, 1.8657e-03, 2.0635e-03, 2.5235e-03, 6.5336e-04,
        2.7304e-04, 2.8982e-03, 2.1908e-03, 2.9412e-03, 2.4825e-03, 5.4047e-05,
        8.2131e-04, 3.0149e-03, 2.6296e-03, 1.7219e-03, 2.7369e-03, 3.1376e-03,
        2.3736e-03, 1.1320e-03, 3.4138e-03, 1.6816e-03, 1.4901e-03, 2.5945e-03,
        9.7275e-04, 3.3560e-03, 8.5842e-04, 3.5790e-03, 2.7980e-03, 2.9044e-03,
        1.4500e-03, 2.0194e-03, 2.7680e-03, 1.6691e-03, 2.8459e-03, 1.9700e-03,
        2.0019e-03, 1.2660e-03, 3.8171e-03, 5.7981e-05, 1.7612e-03, 7.4821e-04,
        1.4547e-03, 1.5506e-03, 2.6186e-03, 1.5768e-03, 2.3853e-03, 1.2078e-03,
        2.0235e-03, 2.7557e-03, 3.1674e-03, 2.5263e-03, 2.3857e-03, 2.6495e-03,
        5.2270e-04, 1.4843e-03, 1.3841e-03, 2.4072e-03, 2.3513e-03, 2.5673e-03,
        1.9322e-03, 6.1396e-04, 3.2578e-03, 1.3611e-03, 3.2852e-03, 1.6797e-03,
        2.3024e-03, 1.0129e-03, 1.6497e-03, 2.1615e-03, 3.3924e-03, 3.7590e-03,
        1.1984e-03, 5.8926e-04, 2.3057e-03, 7.9830e-04, 1.5626e-03, 1.0982e-03,
        1.0830e-03, 2.1149e-03, 3.1508e-04, 2.2406e-03, 1.9694e-03, 3.2360e-03,
        9.5002e-04, 1.0707e-03, 1.8348e-03, 1.3422e-03, 3.1250e-03, 3.3841e-03,
        5.2982e-04, 1.9424e-03, 3.2840e-03, 1.8004e-03, 1.2651e-04, 6.1386e-04,
        2.4143e-03, 2.6865e-03, 1.7207e-03, 3.7359e-04, 2.1075e-03, 3.4171e-03,
        2.2562e-03, -1.1474e-03, -1.2791e-03, -1.8742e-03, -1.2098e-03, -6.2826e-04,
        -7.1401e-04, -9.9885e-04, -2.7099e-03, -1.5852e-03, -7.4220e-04,
        -1.7949e-03, -2.2599e-03, -2.9382e-03, -3.5168e-03, -2.8925e-03,
        -2.9285e-04, -7.7803e-04, -1.1414e-03, -1.9144e-03, -3.0744e-03,
        -2.9239e-03, -3.4909e-04, -2.2485e-03, -3.0394e-03, -2.5675e-03,
        -1.4130e-03, -2.7800e-03, -1.2424e-03, -1.2364e-03, -1.5549e-03,
        -1.6069e-03, -3.3396e-03, -2.6497e-03, -6.7721e-05, -2.0371e-03,
        -1.2133e-05, -1.3940e-05, -2.5760e-03, -2.0047e-03, -2.7467e-03,
        -1.2958e-04, -2.6254e-03, -1.0998e-03, -3.3949e-03, -3.0714e-03,
        -2.9306e-03, -1.0435e-03, -1.6119e-03, -9.9581e-04, -2.5757e-03,
        -5.3342e-04, -1.0272e-03, -2.3878e-03, -3.2655e-03, -1.5540e-03,
        -2.1135e-03, -1.4261e-03, -6.5257e-04, -3.1808e-03, -2.6260e-03,
        -3.1493e-03, -2.2988e-03, -2.5035e-03, -3.2246e-03, -2.9013e-03,
        -2.0954e-03, -3.0106e-03, -2.4332e-03, -1.2553e-03, -1.1877e-03,
        -2.2871e-03, -2.0219e-03, -1.4155e-03, -6.1655e-04, -3.7149e-03,
        -2.6832e-03, -1.7423e-04, -2.3371e-03, -1.9432e-03, -2.8835e-03,
        -4.4489e-04, -2.3293e-03, -1.0055e-03, -1.1453e-03, -1.6781e-03,
        -2.3886e-03, -3.2054e-03, -2.0252e-03, -1.4854e-03, -2.9244e-03,
        -2.1683e-03, -1.7124e-03, -3.6245e-04, -7.7742e-04, -8.6574e-04,
        -9.5752e-04, -1.2747e-03, -2.1046e-03, -2.3227e-03, -2.1107e-03,
        -1.4829e-03, -2.7389e-03, -5.9421e-04, -2.8059e-03, -1.1542e-03,
        -3.4263e-03, -1.5033e-03, -1.3022e-03, -1.8389e-03, -2.7964e-03,
        -3.9535e-04, -1.3175e-03, -1.5396e-03, -1.8523e-03, -9.5944e-04,
        -5.0657e-04, -3.5599e-03, -2.2674e-03, -1.5791e-03, -2.3200e-03,
        -2.3836e-03, -2.0196e-03, -2.4826e-03, -2.2726e-03, -1.7219e-03,
        -1.4033e-03, -1.8741e-03, -2.7485e-03, -3.2247e-03, -1.5409e-03,
        -3.4669e-03, -1.9113e-03, -1.5412e-03, -7.5730e-04, -3.0373e-03,
        -1.8401e-03, -2.9416e-03, -9.9595e-04, -1.8222e-03, -3.6105e-03,
        -1.9000e-03, -1.8862e-03, -2.4009e-03, -2.2862e-03, -2.4599e-03,
        -2.3460e-03, -3.1454e-03, -3.7791e-04, -2.6747e-03, -7.3944e-04,
        -3.0823e-03, -1.8647e-03, -5.3439e-04, -2.2904e-03, -2.5001e-03,
        -1.6939e-03, -1.6245e-03, -1.4672e-03, -1.9419e-03, -3.0187e-03,
        -1.4510e-03, -1.8224e-03, -3.5012e-03, -2.9870e-03, -2.5257e-03,
        -1.4612e-03, -1.6570e-03, -5.6029e-04, -1.7654e-03, -2.5972e-03,
        -1.6271e-03, -1.4522e-03, -2.6552e-03, -1.5966e-03, -3.0016e-03,
        -1.1898e-03, -3.0570e-03, -2.0793e-03, -2.1308e-03, -3.8084e-03,
        -2.0031e-04, -1.9931e-03, -2.0033e-03, -3.6186e-05, -4.4565e-05,
        -6.3998e-04, -9.4627e-04, -1.8612e-03, -1.2888e-06, -1.8941e-03,
        -1.5128e-03, -2.4917e-03, -2.1499e-03, -6.9622e-04, -3.8293e-04,
        -1.0310e-03, -1.2976e-03, -1.6476e-03, -1.7337e-03, -7.1605e-04,
        -1.7639e-03, -1.6798e-03, -8.9892e-04, -9.7798e-04, -1.6174e-03,
        -1.9730e-03, -2.1281e-03, -1.4403e-03, -2.6871e-03, -5.7612e-04,
        -1.0047e-03, -1.3189e-03, -8.9359e-04, -1.2680e-03, -9.3961e-04,
        -2.7916e-03, -1.4754e-03, -1.8072e-03, -1.6793e-03, -1.9750e-03,
        -1.0353e-03, -1.2324e-03, -2.9538e-03, -1.0378e-04, -2.3865e-03,
        -3.0039e-03, -2.3864e-03, -1.9977e-03, -1.6797e-03, -2.6004e-03,
        -3.4529e-03, -1.0501e-03, -1.6043e-03, -1.5226e-03, -2.1554e-03,
        -2.3892e-03, -1.8279e-03, -2.2375e-04, -2.5927e-03, -1.8375e-03,
        -8.7979e-04, -3.9993e-05, -2.9841e-03, -3.4868e-04, -2.0359e-03,
        -3.1305e-03, -2.2479e-03, -4.2427e-04, -1.2092e-03, -2.3396e-03,
        -2.7198e-03, -1.5953e-03, -1.1286e-03, -1.1545e-03, -3.1308e-05,
        -9.1548e-04, -1.5911e-03, -1.4591e-03, -2.3605e-03, -2.2888e-04,
        -1.5030e-03, -1.4845e-03, -5.1100e-04, -1.2105e-03, -1.7870e-03,
        -1.5835e-03, -1.9926e-03, -8.7517e-04, -1.5396e-03])
        
        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.2: 
                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])