Overall Statistics
Total Trades
1820
Average Win
1.12%
Average Loss
-0.68%
Compounding Annual Return
38.940%
Drawdown
20.300%
Expectancy
0.668
Net Profit
7166.433%
Sharpe Ratio
1.897
Probabilistic Sharpe Ratio
98.270%
Loss Rate
37%
Win Rate
63%
Profit-Loss Ratio
1.65
Alpha
0.399
Beta
0.139
Annual Standard Deviation
0.224
Annual Variance
0.05
Information Ratio
0.797
Tracking Error
0.294
Treynor Ratio
3.063
Total Fees
$5029.81
import pandas as pd
class LiquidUniverseSelection(QCAlgorithm):
    
    def Initialize(self):
        self.SetBenchmark("QQQ")
        self.SetStartDate(2008, 1, 1)
        self.SetEndDate(2021,1,10)
        self.SetCash(10000)
        res = Resolution.Hour
        self.MRKT = self.AddEquity('SPY', res).Symbol
        self.TLT = self.AddEquity('TLT', res).Symbol
        self.S0=self.AddEquity('SJT',res).Symbol
        self.S1=self.AddEquity('CI',res).Symbol
        self.S2=self.AddEquity('MTRX',res).Symbol
        self.S3=self.AddEquity('MOH',res).Symbol
        self.S4=self.AddEquity('CMP',res).Symbol
        self.S5=self.AddEquity('CLGX',res).Symbol
        self.S6=self.AddEquity('RMBS',res).Symbol
        self.S7=self.AddEquity('IMMR',res).Symbol
        self.S8=self.AddEquity('NEU',res).Symbol
        self.S9=self.AddEquity('MOS',res).Symbol
        self.S10=self.AddEquity('LNN',res).Symbol
        self.S11=self.AddEquity('JAKK',res).Symbol
        self.S12=self.AddEquity('FELE',res).Symbol
        self.S13=self.AddEquity('CALM',res).Symbol
        self.S14=self.AddEquity('ENS',res).Symbol
        self.S15=self.AddEquity('WGO',res).Symbol
        self.S16=self.AddEquity('MTN',res).Symbol
        self.S17=self.AddEquity('AXR',res).Symbol
        self.S18=self.AddEquity('MTW',res).Symbol
        self.S19=self.AddEquity('TPC',res).Symbol
        self.S20=self.AddEquity('UGI',res).Symbol
        self.S21=self.AddEquity('APH',res).Symbol
        self.S22=self.AddEquity('MD',res).Symbol
        self.S23=self.AddEquity('UNFI',res).Symbol
        self.S24=self.AddEquity('WCN',res).Symbol
        self.S25=self.AddEquity('IT',res).Symbol
        self.S26=self.AddEquity('CF',res).Symbol
        self.S27=self.AddEquity('USNA',res).Symbol
        self.S28=self.AddEquity('NTGR',res).Symbol
        self.S29=self.AddEquity('HUM',res).Symbol
        self.S30=self.AddEquity('GRA',res).Symbol
        self.S31=self.AddEquity('BR',res).Symbol
        self.S32=self.AddEquity('INT',res).Symbol
        self.S33=self.AddEquity('OMC',res).Symbol
        self.S34=self.AddEquity('MMS',res).Symbol
        self.S35=self.AddEquity('NCMI',res).Symbol
        self.S36=self.AddEquity('HMSY',res).Symbol
        self.S37=self.AddEquity('CTRN',res).Symbol
        self.S38=self.AddEquity('SNX',res).Symbol
        self.S39=self.AddEquity('ORLY',res).Symbol
        self.S40=self.AddEquity('NUVA',res).Symbol
        self.S41=self.AddEquity('RAD',res).Symbol
        self.S42=self.AddEquity('OC',res).Symbol
        self.S43=self.AddEquity('SHYF',res).Symbol
        self.S44=self.AddEquity('CDE',res).Symbol
        self.S45=self.AddEquity('THC',res).Symbol
        self.S46=self.AddEquity('XPER',res).Symbol
        self.S47=self.AddEquity('CHS',res).Symbol
        self.S48=self.AddEquity('WEX',res).Symbol
        self.S49=self.AddEquity('BMI',res).Symbol
        self.S50=self.AddEquity('GPI',res).Symbol
        self.S51=self.AddEquity('SBH',res).Symbol
        self.S52=self.AddEquity('WDC',res).Symbol
        self.S53=self.AddEquity('TEN',res).Symbol
        self.S54=self.AddEquity('CAR',res).Symbol
        self.S55=self.AddEquity('WYNN',res).Symbol
        self.S56=self.AddEquity('VECO',res).Symbol
        self.S57=self.AddEquity('AEIS',res).Symbol
        self.S58=self.AddEquity('KFY',res).Symbol
        self.S59=self.AddEquity('INCY',res).Symbol
        self.S60=self.AddEquity('HURN',res).Symbol
        self.S61=self.AddEquity('MTOR',res).Symbol
        self.S62=self.AddEquity('LMNX',res).Symbol
        self.S63=self.AddEquity('BZH',res).Symbol
        self.S64=self.AddEquity('ANDE',res).Symbol
        self.S65=self.AddEquity('VNDA',res).Symbol
        self.S66=self.AddEquity('SAH',res).Symbol
        self.S67=self.AddEquity('XRX',res).Symbol
        self.S68=self.AddEquity('MWA',res).Symbol
        self.S69=self.AddEquity('CTIC',res).Symbol
        self.S70=self.AddEquity('DAN',res).Symbol
        self.S71=self.AddEquity('HOG',res).Symbol
        self.S72=self.AddEquity('AXL',res).Symbol
        self.S73=self.AddEquity('MSTR',res).Symbol
        self.S74=self.AddEquity('AMSC',res).Symbol
        self.S75=self.AddEquity('IO',res).Symbol
        self.S76=self.AddEquity('AWK',res).Symbol
        self.S77=self.AddEquity('UFS',res).Symbol
        self.S78=self.AddEquity('DDS',res).Symbol
        self.S79=self.AddEquity('BMY',res).Symbol
        self.S80=self.AddEquity('HL',res).Symbol
        self.S81=self.AddEquity('LDOS',res).Symbol
        self.S82=self.AddEquity('ROP',res).Symbol
        self.S83=self.AddEquity('SANM',res).Symbol
        self.S84=self.AddEquity('ITT',res).Symbol
        self.S85=self.AddEquity('KMX',res).Symbol
        self.S86=self.AddEquity('LYV',res).Symbol
        self.S87=self.AddEquity('UAL',res).Symbol
        self.S88=self.AddEquity('AAXN',res).Symbol
        self.S89=self.AddEquity('CSGS',res).Symbol
        self.S90=self.AddEquity('SWM',res).Symbol
        self.S91=self.AddEquity('KALU',res).Symbol
        self.S92=self.AddEquity('FOE',res).Symbol
        self.S93=self.AddEquity('VSAT',res).Symbol
        self.S94=self.AddEquity('F',res).Symbol
        self.S95=self.AddEquity('CNC',res).Symbol
        self.S96=self.AddEquity('NFLX',res).Symbol
        self.S97=self.AddEquity('PDLI',res).Symbol
        self.S98=self.AddEquity('DPZ',res).Symbol
        self.S99=self.AddEquity('CRUS',res).Symbol
        self.S100=self.AddEquity('THO',res).Symbol
        self.S101=self.AddEquity('SOL',res).Symbol
        self.S102=self.AddEquity('INFN',res).Symbol
        self.S103=self.AddEquity('GHC',res).Symbol
        self.S104=self.AddEquity('CRM',res).Symbol
        self.S105=self.AddEquity('MKSI',res).Symbol
        self.S106=self.AddEquity('FORM',res).Symbol
        self.S107=self.AddEquity('MDRX',res).Symbol
        self.S108=self.AddEquity('SGEN',res).Symbol
        self.S109=self.AddEquity('EBIX',res).Symbol
        self.S110=self.AddEquity('EW',res).Symbol
        self.S111=self.AddEquity('BHE',res).Symbol
        self.S112=self.AddEquity('PLXS',res).Symbol
        self.S113=self.AddEquity('ALGN',res).Symbol
        self.S114=self.AddEquity('UIS',res).Symbol
        self.S115=self.AddEquity('AMD',res).Symbol
        self.S116=self.AddEquity('PDCE',res).Symbol
        self.S117=self.AddEquity('GTLS',res).Symbol
        self.S118=self.AddEquity('CKH',res).Symbol
        self.S119=self.AddEquity('MCF',res).Symbol
        self.S120=self.AddEquity('CHE',res).Symbol
        self.S121=self.AddEquity('GCO',res).Symbol
        self.S122=self.AddEquity('WTI',res).Symbol
        self.S123=self.AddEquity('IDCC',res).Symbol
        self.S124=self.AddEquity('MSB',res).Symbol
        self.S125=self.AddEquity('VHC',res).Symbol
        self.S126=self.AddEquity('QMCO',res).Symbol
        self.S127=self.AddEquity('NVR',res).Symbol
        self.S128=self.AddEquity('ATI',res).Symbol
        self.S129=self.AddEquity('CMTL',res).Symbol
        self.S130=self.AddEquity('VIAV',res).Symbol
        self.S131=self.AddEquity('CVI',res).Symbol
        self.S132=self.AddEquity('NOG',res).Symbol
        self.S133=self.AddEquity('ACM',res).Symbol
        self.S134=self.AddEquity('LNG',res).Symbol
        self.S135=self.AddEquity('WW',res).Symbol
        self.S136=self.AddEquity('TZOO',res).Symbol
        self.S137=self.AddEquity('SNBR',res).Symbol
        self.S138=self.AddEquity('IPGP',res).Symbol
        self.S139=self.AddEquity('STRA',res).Symbol
        self.S140=self.AddEquity('RRGB',res).Symbol
        self.S141=self.AddEquity('CSGP',res).Symbol
        self.S142=self.AddEquity('DXC',res).Symbol
        self.S143=self.AddEquity('WLK',res).Symbol
        self.S144=self.AddEquity('DHI',res).Symbol
        self.S145=self.AddEquity('SPPI',res).Symbol
        self.S146=self.AddEquity('PRDO',res).Symbol
        self.S147=self.AddEquity('BMRN',res).Symbol
        self.S148=self.AddEquity('JOE',res).Symbol
        self.S149=self.AddEquity('ANTM',res).Symbol
        self.S150=self.AddEquity('TTC',res).Symbol
        self.S151=self.AddEquity('TWI',res).Symbol
        self.S152=self.AddEquity('UAA',res).Symbol
        self.S153=self.AddEquity('SCHL',res).Symbol
        self.S154=self.AddEquity('ET',res).Symbol
        self.S155=self.AddEquity('TEX',res).Symbol
        self.S156=self.AddEquity('VRSN',res).Symbol
        self.S157=self.AddEquity('SSYS',res).Symbol
        self.S158=self.AddEquity('RGR',res).Symbol
        self.S159=self.AddEquity('MTZ',res).Symbol
        self.S160=self.AddEquity('LL',res).Symbol
        self.S161=self.AddEquity('LYB',res).Symbol
        self.S162=self.AddEquity('DDD',res).Symbol
        self.S163=self.AddEquity('SWBI',res).Symbol
        self.S164=self.AddEquity('NS',res).Symbol
        self.S165=self.AddEquity('CAL',res).Symbol
        self.S166=self.AddEquity('CTB',res).Symbol
        self.S167=self.AddEquity('TGNA',res).Symbol
        self.S168=self.AddEquity('CSOD',res).Symbol
        self.S169=self.AddEquity('COHR',res).Symbol
        self.S170=self.AddEquity('DK',res).Symbol
        self.S171=self.AddEquity('LORL',res).Symbol
        self.S172=self.AddEquity('GNRC',res).Symbol
        self.S173=self.AddEquity('HOV',res).Symbol
        self.S174=self.AddEquity('PENN',res).Symbol
        self.S175=self.AddEquity('CFX',res).Symbol
        self.S176=self.AddEquity('MDP',res).Symbol
        self.S177=self.AddEquity('CEQP',res).Symbol
        self.S178=self.AddEquity('DLX',res).Symbol
        self.S179=self.AddEquity('PPG',res).Symbol
        self.S180=self.AddEquity('MANH',res).Symbol
        self.S181=self.AddEquity('DIN',res).Symbol
        self.S182=self.AddEquity('STZ',res).Symbol
        self.S183=self.AddEquity('IONS',res).Symbol
        self.S184=self.AddEquity('GPK',res).Symbol
        self.S185=self.AddEquity('ABC',res).Symbol
        self.S186=self.AddEquity('CAH',res).Symbol
        self.S187=self.AddEquity('SR',res).Symbol
        self.S188=self.AddEquity('NYT',res).Symbol
        self.S189=self.AddEquity('UAN',res).Symbol
        self.S190=self.AddEquity('HPQ',res).Symbol
        self.S191=self.AddEquity('SPWR',res).Symbol
        self.S192=self.AddEquity('IRBT',res).Symbol
        self.S193=self.AddEquity('HLF',res).Symbol
        self.S194=self.AddEquity('CYH',res).Symbol
        self.S195=self.AddEquity('SAVE',res).Symbol
        self.S196=self.AddEquity('UI',res).Symbol
        self.S197=self.AddEquity('ZG',res).Symbol
        self.S198=self.AddEquity('TSLA',res).Symbol
        self.S199=self.AddEquity('PPC',res).Symbol
        self.S200=self.AddEquity('AME',res).Symbol
        self.S201=self.AddEquity('NR',res).Symbol
        self.S202=self.AddEquity('AIR',res).Symbol
        self.S203=self.AddEquity('YELP',res).Symbol
        self.S204=self.AddEquity('NUS',res).Symbol
        self.S205=self.AddEquity('GME',res).Symbol
        self.S206=self.AddEquity('LGND',res).Symbol
        self.S207=self.AddEquity('TMUS',res).Symbol
        self.S208=self.AddEquity('CATM',res).Symbol
        self.S209=self.AddEquity('BAH',res).Symbol
        self.S210=self.AddEquity('SGMS',res).Symbol
        self.S211=self.AddEquity('GIII',res).Symbol
        self.S212=self.AddEquity('IEP',res).Symbol
        self.S213=self.AddEquity('UTHR',res).Symbol
        self.S214=self.AddEquity('CW',res).Symbol
        self.S215=self.AddEquity('RXN',res).Symbol
        self.S216=self.AddEquity('TSCO',res).Symbol
        self.S217=self.AddEquity('VGR',res).Symbol
        self.S218=self.AddEquity('ILMN',res).Symbol
        self.S219=self.AddEquity('DD',res).Symbol
        self.S220=self.AddEquity('VZ',res).Symbol
        self.S221=self.AddEquity('PODD',res).Symbol
        self.S222=self.AddEquity('TTWO',res).Symbol
        self.S223=self.AddEquity('CCOI',res).Symbol
        self.S224=self.AddEquity('ALSN',res).Symbol
        self.S225=self.AddEquity('DBI',res).Symbol
        self.S226=self.AddEquity('DXCM',res).Symbol
        self.S227=self.AddEquity('NGL',res).Symbol
        self.S228=self.AddEquity('REGI',res).Symbol
        self.S229=self.AddEquity('TRGP',res).Symbol
        self.S230=self.AddEquity('TCP',res).Symbol
        self.S231=self.AddEquity('PEIX',res).Symbol
        self.S232=self.AddEquity('LAD',res).Symbol
        self.S233=self.AddEquity('GLUU',res).Symbol
        self.S234=self.AddEquity('DLTR',res).Symbol
        self.S235=self.AddEquity('CENX',res).Symbol
        self.S236=self.AddEquity('TDW',res).Symbol
        self.S237=self.AddEquity('ACCO',res).Symbol
        self.S238=self.AddEquity('SAFM',res).Symbol
        self.S239=self.AddEquity('WSM',res).Symbol
        self.S240=self.AddEquity('ZIOP',res).Symbol
        self.S241=self.AddEquity('GPN',res).Symbol
        self.S242=self.AddEquity('CQP',res).Symbol
        self.S243=self.AddEquity('OSPN',res).Symbol
        self.S244=self.AddEquity('COLM',res).Symbol
        self.S245=self.AddEquity('AZPN',res).Symbol
        self.S246=self.AddEquity('REX',res).Symbol
        self.S247=self.AddEquity('GTN',res).Symbol
        self.S248=self.AddEquity('ABMD',res).Symbol
        self.S249=self.AddEquity('SMCI',res).Symbol
        self.S250=self.AddEquity('FARO',res).Symbol
        self.S251=self.AddEquity('IMGN',res).Symbol
        self.S252=self.AddEquity('LAKE',res).Symbol
        self.S253=self.AddEquity('BLUE',res).Symbol
        self.S254=self.AddEquity('ASRT',res).Symbol
        self.S255=self.AddEquity('CROX',res).Symbol
        self.S256=self.AddEquity('DY',res).Symbol
        self.S257=self.AddEquity('CHTR',res).Symbol
        self.S258=self.AddEquity('ESI',res).Symbol
        self.S259=self.AddEquity('AMBA',res).Symbol
        self.S260=self.AddEquity('SSNC',res).Symbol
        self.S261=self.AddEquity('MEI',res).Symbol
        self.S262=self.AddEquity('VMW',res).Symbol
        self.S263=self.AddEquity('ENTA',res).Symbol
        self.S264=self.AddEquity('KLAC',res).Symbol
        self.S265=self.AddEquity('PINC',res).Symbol
        self.S266=self.AddEquity('BKH',res).Symbol
        self.S267=self.AddEquity('MSGN',res).Symbol
        self.S268=self.AddEquity('OSIS',res).Symbol
        self.S269=self.AddEquity('GE',res).Symbol
        self.S270=self.AddEquity('CWT',res).Symbol
        self.S271=self.AddEquity('XRAY',res).Symbol
        self.S272=self.AddEquity('NJR',res).Symbol
        self.S273=self.AddEquity('ANIK',res).Symbol
        self.S274=self.AddEquity('PDCO',res).Symbol
        self.S275=self.AddEquity('FPRX',res).Symbol
        self.S276=self.AddEquity('TSE',res).Symbol
        self.S277=self.AddEquity('MIK',res).Symbol
        self.S278=self.AddEquity('SLCA',res).Symbol
        self.S279=self.AddEquity('INVA',res).Symbol
        self.S280=self.AddEquity('CLF',res).Symbol
        self.S281=self.AddEquity('ZEN',res).Symbol
        self.S282=self.AddEquity('BAX',res).Symbol
        self.S283=self.AddEquity('MDCA',res).Symbol
        self.S284=self.AddEquity('ADVM',res).Symbol
        self.S285=self.AddEquity('HUBS',res).Symbol
        self.S286=self.AddEquity('LKQ',res).Symbol
        self.S287=self.AddEquity('NSP',res).Symbol
        self.S288=self.AddEquity('EVRG',res).Symbol
        self.S289=self.AddEquity('SPXC',res).Symbol
        self.S290=self.AddEquity('LBRDK',res).Symbol
        self.S291=self.AddEquity('SWX',res).Symbol
        self.S292=self.AddEquity('LBRDA',res).Symbol
        self.S293=self.AddEquity('LNT',res).Symbol
        self.S294=self.AddEquity('EME',res).Symbol
        self.S295=self.AddEquity('BDC',res).Symbol
        self.S296=self.AddEquity('TVTY',res).Symbol
        self.S297=self.AddEquity('NEWR',res).Symbol
        self.S298=self.AddEquity('PG',res).Symbol
        self.S299=self.AddEquity('SPSC',res).Symbol
        self.S300=self.AddEquity('EXEL',res).Symbol
        self.S301=self.AddEquity('COO',res).Symbol
        self.S302=self.AddEquity('FIZZ',res).Symbol
        self.S303=self.AddEquity('TTMI',res).Symbol
        self.S304=self.AddEquity('GBX',res).Symbol
        self.S305=self.AddEquity('AGX',res).Symbol
        self.S306=self.AddEquity('VAR',res).Symbol
        self.S307=self.AddEquity('IART',res).Symbol
        self.S308=self.AddEquity('VRTX',res).Symbol
        self.S309=self.AddEquity('LUMO',res).Symbol
        self.S310=self.AddEquity('CLVS',res).Symbol
        self.S311=self.AddEquity('LFUS',res).Symbol
        self.S312=self.AddEquity('RAMP',res).Symbol
        self.S313=self.AddEquity('ARLP',res).Symbol
        self.S314=self.AddEquity('MLND',res).Symbol
        self.S315=self.AddEquity('NVDA',res).Symbol
        self.S316=self.AddEquity('NAV',res).Symbol
        self.S317=self.AddEquity('RCKT',res).Symbol
        self.S318=self.AddEquity('CSX',res).Symbol
        self.S319=self.AddEquity('UVV',res).Symbol
        self.S320=self.AddEquity('TRUE',res).Symbol
        self.S321=self.AddEquity('AAOI',res).Symbol
        self.S322=self.AddEquity('BW',res).Symbol
        self.S323=self.AddEquity('QTWO',res).Symbol
        self.S324=self.AddEquity('Z',res).Symbol
        self.S325=self.AddEquity('OSUR',res).Symbol
        self.S326=self.AddEquity('JBSS',res).Symbol
        self.S327=self.AddEquity('ITGR',res).Symbol
        self.S328=self.AddEquity('WNC',res).Symbol
        self.S329=self.AddEquity('MDXG',res).Symbol
        self.S330=self.AddEquity('ERII',res).Symbol
        self.S331=self.AddEquity('FWONK',res).Symbol
        self.S332=self.AddEquity('HUN',res).Symbol
        self.S333=self.AddEquity('JKHY',res).Symbol
        self.S334=self.AddEquity('BLKB',res).Symbol
        self.S335=self.AddEquity('FOR',res).Symbol
        self.S336=self.AddEquity('AVA',res).Symbol
        self.S337=self.AddEquity('PETS',res).Symbol
        self.S338=self.AddEquity('RMD',res).Symbol
        self.S339=self.AddEquity('AOS',res).Symbol
        self.S340=self.AddEquity('PKG',res).Symbol
        self.S341=self.AddEquity('IQV',res).Symbol
        self.S342=self.AddEquity('AVAV',res).Symbol
        self.S343=self.AddEquity('CONN',res).Symbol
        self.S344=self.AddEquity('RGEN',res).Symbol
        self.S345=self.AddEquity('BBY',res).Symbol
        self.S346=self.AddEquity('CATO',res).Symbol
        self.S347=self.AddEquity('QDEL',res).Symbol
        self.S348=self.AddEquity('EVTC',res).Symbol
        self.S349=self.AddEquity('EFX',res).Symbol
        self.S350=self.AddEquity('AJRD',res).Symbol
        self.S351=self.AddEquity('HCSG',res).Symbol
        self.S352=self.AddEquity('PEN',res).Symbol
        self.S353=self.AddEquity('NSIT',res).Symbol
        self.S354=self.AddEquity('CREE',res).Symbol
        self.S355=self.AddEquity('SGMO',res).Symbol
        self.S356=self.AddEquity('SKX',res).Symbol
        self.S357=self.AddEquity('OSTK',res).Symbol
        self.S358=self.AddEquity('ZAGG',res).Symbol
        self.S359=self.AddEquity('EDIT',res).Symbol
        self.S360=self.AddEquity('ABM',res).Symbol
        self.S361=self.AddEquity('CORT',res).Symbol
        self.S362=self.AddEquity('AWR',res).Symbol
        self.S363=self.AddEquity('KDP',res).Symbol
        self.S364=self.AddEquity('NTLA',res).Symbol
        self.S365=self.AddEquity('QUAD',res).Symbol
        self.S366=self.AddEquity('WATT',res).Symbol
        self.S367=self.AddEquity('TWOU',res).Symbol
        self.S368=self.AddEquity('ANET',res).Symbol
        self.S369=self.AddEquity('SUN',res).Symbol
        self.S370=self.AddEquity('NKTR',res).Symbol
        self.S371=self.AddEquity('SRNE',res).Symbol
        self.S372=self.AddEquity('CMD',res).Symbol
        self.S373=self.AddEquity('MGPI',res).Symbol
        self.S374=self.AddEquity('MDGL',res).Symbol
        self.S375=self.AddEquity('PTCT',res).Symbol
        self.S376=self.AddEquity('TWLO',res).Symbol
        self.S377=self.AddEquity('MAT',res).Symbol
        self.S378=self.AddEquity('PVAC',res).Symbol
        self.S379=self.AddEquity('TRXC',res).Symbol
        self.S380=self.AddEquity('TUSK',res).Symbol
        self.S381=self.AddEquity('FLT',res).Symbol
        self.S382=self.AddEquity('TCMD',res).Symbol
        self.S383=self.AddEquity('ATO',res).Symbol
        self.S384=self.AddEquity('LSCC',res).Symbol
        self.S385=self.AddEquity('VCRA',res).Symbol
        self.S386=self.AddEquity('RPD',res).Symbol
        self.S387=self.AddEquity('OKTA',res).Symbol
        self.S388=self.AddEquity('LLY',res).Symbol
        self.S389=self.AddEquity('STMP',res).Symbol
        self.S390=self.AddEquity('LNTH',res).Symbol
        self.S391=self.AddEquity('QCOM',res).Symbol
        self.S392=self.AddEquity('CRBP',res).Symbol
        self.S393=self.AddEquity('CHGG',res).Symbol
        self.S394=self.AddEquity('AM',res).Symbol
        self.S395=self.AddEquity('FIS',res).Symbol
        self.S396=self.AddEquity('CHRS',res).Symbol
        self.S397=self.AddEquity('PRO',res).Symbol
        self.S398=self.AddEquity('DBD',res).Symbol
        self.S399=self.AddEquity('XLNX',res).Symbol
        self.S400=self.AddEquity('KW',res).Symbol
        self.S401=self.AddEquity('EVBG',res).Symbol
        self.S402=self.AddEquity('LCI',res).Symbol
        self.S403=self.AddEquity('ENPH',res).Symbol
        self.S404=self.AddEquity('ROKU',res).Symbol
        self.S405=self.AddEquity('GMS',res).Symbol
        self.S406=self.AddEquity('PLUS',res).Symbol
        self.S407=self.AddEquity('CDW',res).Symbol
        self.S408=self.AddEquity('HEP',res).Symbol
        self.S409=self.AddEquity('HCC',res).Symbol
        self.S410=self.AddEquity('ARNA',res).Symbol
        self.S411=self.AddEquity('FCN',res).Symbol
        self.S412=self.AddEquity('BIIB',res).Symbol
        self.S413=self.AddEquity('AMKR',res).Symbol
        self.S414=self.AddEquity('FIT',res).Symbol
        self.S415=self.AddEquity('ROCK',res).Symbol
        self.S416=self.AddEquity('ANAB',res).Symbol
        self.S417=self.AddEquity('RETA',res).Symbol
        self.S418=self.AddEquity('CDLX',res).Symbol
        self.S419=self.AddEquity('ARWR',res).Symbol
        self.S420=self.AddEquity('DRNA',res).Symbol
        self.S421=self.AddEquity('EAF',res).Symbol
        self.S422=self.AddEquity('ZBRA',res).Symbol
        self.S423=self.AddEquity('CCXI',res).Symbol
        self.S424=self.AddEquity('MRSN',res).Symbol
        self.S425=self.AddEquity('INO',res).Symbol
        self.S426=self.AddEquity('AVLR',res).Symbol
        self.S427=self.AddEquity('TBIO',res).Symbol
        self.S428=self.AddEquity('ESPR',res).Symbol
        self.S429=self.AddEquity('ALT',res).Symbol
        self.S430=self.AddEquity('ANGI',res).Symbol
        self.S431=self.AddEquity('BLNK',res).Symbol
        self.S432=self.AddEquity('OMI',res).Symbol
        self.S433=self.AddEquity('TUP',res).Symbol
        self.S434=self.AddEquity('BAND',res).Symbol
        self.S435=self.AddEquity('WKHS',res).Symbol
        self.S436=self.AddEquity('HTBX',res).Symbol
        self.S437=self.AddEquity('APPS',res).Symbol
        self.S438=self.AddEquity('KODK',res).Symbol
        self.S439=self.AddEquity('BMCH',res).Symbol
        self.S440=self.AddEquity('CWH',res).Symbol
        self.S441=self.AddEquity('FLGT',res).Symbol
        self.S442=self.AddEquity('CPK',res).Symbol
        self.S443=self.AddEquity('RCM',res).Symbol
        self.S444=self.AddEquity('EXPI',res).Symbol
        self.S445=self.AddEquity('HOME',res).Symbol
        self.S446=self.AddEquity('GOGO',res).Symbol
        self.S447=self.AddEquity('HDS',res).Symbol
        self.S448=self.AddEquity('GRWG',res).Symbol
        self.S449=self.AddEquity('RIOT',res).Symbol
        
        self.signals = {'2008-01-02':{self.TLT:1},'2008-02-19':{self.S0:0.1,self.S1:0.1,self.S2:0.1,self.S3:0.1,self.S4:0.1,self.S5:0.1,self.S6:0.1,self.S7:0.1,self.S8:0.1,self.S9:0.1},'2008-03-03':{self.S0:0.1,self.S3:0.1,self.S6:0.1,self.S2:0.1,self.S7:0.1,self.S9:0.1,self.S10:0.1,self.S11:0.1,self.S4:0.1,self.S12:0.1},'2008-04-01':{self.S6:0.1,self.S0:0.1,self.S10:0.1,self.S2:0.1,self.S7:0.1,self.S13:0.1,self.S14:0.1,self.S15:0.1,self.S4:0.1,self.S9:0.1},'2008-05-01':{self.S3:0.1,self.S0:0.1,self.S10:0.1,self.S14:0.1,self.S2:0.1,self.S4:0.1,self.S16:0.1,self.S8:0.1,self.S13:0.1,self.S17:0.1},'2008-06-02':{self.TLT:1},'2008-10-07':{self.S18:0.1,self.S19:0.1,self.S20:0.1,self.S21:0.1,self.S22:0.1,self.S23:0.1,self.S24:0.1,self.S25:0.1,self.S26:0.1,self.S27:0.1},'2008-10-15':{self.TLT:1},'2009-03-23':{self.S28:0.1,self.S29:0.1,self.S30:0.1,self.S31:0.1,self.S22:0.1,self.S32:0.1,self.S33:0.1,self.S34:0.1,self.S35:0.1,self.S36:0.1},'2009-04-01':{self.S37:0.1,self.S31:0.1,self.S28:0.1,self.S32:0.1,self.S38:0.1,self.S39:0.1,self.S29:0.1,self.S33:0.1,self.S34:0.1,self.S22:0.1},'2009-05-01':{self.S40:0.1,self.S1:0.1,self.S41:0.1,self.S22:0.1,self.S29:0.1,self.S42:0.1,self.S43:0.1,self.S3:0.1,self.S32:0.1,self.S31:0.1},'2009-06-01':{self.S44:0.1,self.S33:0.1,self.S45:0.1,self.S43:0.1,self.S40:0.1,self.S0:0.1,self.S46:0.1,self.S47:0.1,self.S8:0.1,self.S48:0.1},'2009-07-01':{self.S33:0.1,self.S0:0.1,self.S41:0.1,self.S47:0.1,self.S49:0.1,self.S50:0.1,self.S51:0.1,self.S42:0.1,self.S52:0.1,self.S32:0.1},'2009-08-03':{self.S44:0.1,self.S53:0.1,self.S33:0.1,self.S54:0.1,self.S0:0.1,self.S55:0.1,self.S56:0.1,self.S57:0.1,self.S41:0.1,self.S58:0.1},'2009-09-01':{self.S59:0.1,self.S53:0.1,self.S3:0.1,self.S0:0.1,self.S60:0.1,self.S61:0.1,self.S33:0.1,self.S62:0.1,self.S63:0.1,self.S64:0.1},'2009-10-01':{self.S59:0.1,self.S65:0.1,self.S66:0.1,self.S57:0.1,self.S62:0.1,self.S0:0.1,self.S3:0.1,self.S67:0.1,self.S33:0.1,self.S68:0.1},'2009-11-02':{self.S59:0.1,self.S3:0.1,self.S69:0.1,self.S56:0.1,self.S70:0.1,self.S71:0.1,self.S72:0.1,self.S0:0.1,self.S73:0.1,self.S74:0.1},'2009-12-01':{self.S0:0.1,self.S3:0.1,self.S75:0.1,self.S71:0.1,self.S76:0.1,self.S53:0.1,self.S77:0.1,self.S78:0.1,self.S79:0.1,self.S80:0.1},'2010-01-04':{self.S0:0.1,self.S81:0.1,self.S82:0.1,self.S75:0.1,self.S79:0.1,self.S71:0.1,self.S3:0.1,self.S83:0.1,self.S84:0.1,self.S80:0.1},'2010-02-01':{self.S85:0.1,self.S0:0.1,self.S83:0.1,self.S3:0.1,self.S86:0.1,self.S29:0.1,self.S75:0.1,self.S87:0.1,self.S77:0.1,self.S22:0.1},'2010-03-01':{self.S3:0.1,self.S83:0.1,self.S85:0.1,self.S86:0.1,self.S88:0.1,self.S22:0.1,self.S89:0.1,self.S84:0.1,self.S29:0.1,self.S90:0.1},'2010-04-01':{self.S91:0.1,self.S85:0.1,self.S92:0.1,self.S84:0.1,self.S3:0.1,self.S93:0.1,self.S29:0.1,self.S22:0.1,self.S94:0.1,self.S95:0.1},'2010-05-03':{self.S29:0.1,self.S22:0.1,self.S87:0.1,self.S96:0.1,self.S97:0.1,self.S3:0.1,self.S84:0.1,self.S5:0.1,self.S59:0.1,self.S98:0.1},'2010-06-01':{self.S99:0.1,self.S32:0.1,self.S29:0.1,self.S84:0.1,self.S5:0.1,self.S96:0.1,self.S87:0.1,self.S100:0.1,self.S101:0.1,self.S97:0.1},'2010-06-09':{self.TLT:1},'2010-08-31':{self.S99:0.1,self.S29:0.1,self.S0:0.1,self.S102:0.1,self.S103:0.1,self.S97:0.1,self.S84:0.1,self.S104:0.1,self.S105:0.1,self.S96:0.1},'2010-09-01':{self.S106:0.1,self.S99:0.1,self.S0:0.1,self.S102:0.1,self.S29:0.1,self.S103:0.1,self.S97:0.1,self.S107:0.1,self.S84:0.1,self.S104:0.1},'2010-10-01':{self.S102:0.1,self.S108:0.1,self.S29:0.1,self.S97:0.1,self.S96:0.1,self.S84:0.1,self.S109:0.1,self.S22:0.1,self.S110:0.1,self.S105:0.1},'2010-11-01':{self.S83:0.1,self.S111:0.1,self.S97:0.1,self.S112:0.1,self.S29:0.1,self.S113:0.1,self.S22:0.1,self.S114:0.1,self.S115:0.1,self.S96:0.1},'2010-12-01':{self.S111:0.1,self.S29:0.1,self.S112:0.1,self.S69:0.1,self.S116:0.1,self.S117:0.1,self.S118:0.1,self.S101:0.1,self.S84:0.1,self.S119:0.1},'2011-01-03':{self.S111:0.1,self.S29:0.1,self.S112:0.1,self.S69:0.1,self.S10:0.1,self.S120:0.1,self.S121:0.1,self.S122:0.1,self.S105:0.1,self.S123:0.1},'2011-02-01':{self.S124:0.1,self.S29:0.1,self.S125:0.1,self.S126:0.1,self.S127:0.1,self.S22:0.1,self.S78:0.1,self.S128:0.1,self.S46:0.1,self.S129:0.1},'2011-03-01':{self.S124:0.1,self.S75:0.1,self.S84:0.1,self.S130:0.1,self.S131:0.1,self.S31:0.1,self.S132:0.1,self.S29:0.1,self.S133:0.1,self.S125:0.1},'2011-04-01':{self.S124:0.1,self.S109:0.1,self.S84:0.1,self.S110:0.1,self.S134:0.1,self.S31:0.1,self.S125:0.1,self.S135:0.1,self.S22:0.1,self.S29:0.1},'2011-05-02':{self.S136:0.1,self.S137:0.1,self.S138:0.1,self.S3:0.1,self.S84:0.1,self.S31:0.1,self.S125:0.1,self.S139:0.1,self.S22:0.1,self.S135:0.1},'2011-06-01':{self.S125:0.1,self.S135:0.1,self.S138:0.1,self.S139:0.1,self.S3:0.1,self.S140:0.1,self.S84:0.1,self.S141:0.1,self.S142:0.1,self.S143:0.1},'2011-06-14':{self.TLT:1},'2012-01-04':{self.S124:0.1,self.S97:0.1,self.S106:0.1,self.S3:0.1,self.S144:0.1,self.S29:0.1,self.S145:0.1,self.S59:0.1,self.S146:0.1,self.S92:0.1},'2012-02-01':{self.S124:0.1,self.S147:0.1,self.S106:0.1,self.S97:0.1,self.S148:0.1,self.S59:0.1,self.S29:0.1,self.S149:0.1,self.S38:0.1,self.S3:0.1},'2012-03-01':{self.S97:0.1,self.S106:0.1,self.S3:0.1,self.S38:0.1,self.S139:0.1,self.S114:0.1,self.S150:0.1,self.S151:0.1,self.S152:0.1,self.S28:0.1},'2012-04-02':{self.S97:0.1,self.S48:0.1,self.S38:0.1,self.S153:0.1,self.S139:0.1,self.S114:0.1,self.S154:0.1,self.S155:0.1,self.S151:0.1,self.S146:0.1},'2012-05-01':{self.S97:0.1,self.S156:0.1,self.S157:0.1,self.S29:0.1,self.S137:0.1,self.S114:0.1,self.S158:0.1,self.S59:0.1,self.S159:0.1,self.S160:0.1},'2012-05-04':{self.TLT:1},'2012-09-11':{self.S124:0.1,self.S29:0.1,self.S161:0.1,self.S162:0.1,self.S157:0.1,self.S163:0.1,self.S127:0.1,self.S164:0.1,self.S149:0.1,self.S165:0.1},'2012-10-01':{self.S124:0.1,self.S157:0.1,self.S38:0.1,self.S29:0.1,self.S127:0.1,self.S160:0.1,self.S162:0.1,self.S114:0.1,self.S166:0.1,self.S167:0.1},'2012-11-01':{self.S124:0.1,self.S168:0.1,self.S29:0.1,self.S169:0.1,self.S170:0.1,self.S171:0.1,self.S114:0.1,self.S172:0.1,self.S38:0.1,self.S173:0.1},'2012-12-03':{self.S172:0.1,self.S174:0.1,self.S38:0.1,self.S153:0.1,self.S148:0.1,self.S123:0.1,self.S103:0.1,self.S173:0.1,self.S175:0.1,self.S114:0.1},'2013-01-02':{self.S148:0.1,self.S163:0.1,self.S38:0.1,self.S131:0.1,self.S176:0.1,self.S177:0.1,self.S123:0.1,self.S178:0.1,self.S175:0.1,self.S173:0.1},'2013-02-01':{self.S96:0.1,self.S179:0.1,self.S173:0.1,self.S131:0.1,self.S38:0.1,self.S114:0.1,self.S180:0.1,self.S83:0.1,self.S52:0.1,self.S162:0.1},'2013-03-01':{self.S96:0.1,self.S147:0.1,self.S38:0.1,self.S181:0.1,self.S29:0.1,self.S182:0.1,self.S176:0.1,self.S172:0.1,self.S131:0.1,self.S114:0.1},'2013-04-01':{self.S183:0.1,self.S97:0.1,self.S170:0.1,self.S38:0.1,self.S147:0.1,self.S184:0.1,self.S185:0.1,self.S3:0.1,self.S81:0.1,self.S186:0.1},'2013-04-03':{self.TLT:1},'2013-06-05':{self.S97:0.1,self.S187:0.1,self.S188:0.1,self.S189:0.1,self.S98:0.1,self.S38:0.1,self.S190:0.1,self.S191:0.1,self.S41:0.1,self.S81:0.1},'2013-07-01':{self.S97:0.1,self.S109:0.1,self.S188:0.1,self.S38:0.1,self.S192:0.1,self.S41:0.1,self.S170:0.1,self.S98:0.1,self.S190:0.1,self.S27:0.1},'2013-08-01':{self.S97:0.1,self.S27:0.1,self.S33:0.1,self.S38:0.1,self.S193:0.1,self.S190:0.1,self.S194:0.1,self.S195:0.1,self.S83:0.1,self.S98:0.1},'2013-09-03':{self.S196:0.1,self.S97:0.1,self.S197:0.1,self.S190:0.1,self.S38:0.1,self.S198:0.1,self.S139:0.1,self.S163:0.1,self.S199:0.1,self.S81:0.1},'2013-10-01':{self.S97:0.1,self.S41:0.1,self.S196:0.1,self.S200:0.1,self.S81:0.1,self.S201:0.1,self.S38:0.1,self.S202:0.1,self.S163:0.1,self.S190:0.1},'2013-11-01':{self.S96:0.1,self.S203:0.1,self.S97:0.1,self.S28:0.1,self.S101:0.1,self.S92:0.1,self.S204:0.1,self.S196:0.1,self.S38:0.1,self.S99:0.1},'2013-12-02':{self.S97:0.1,self.S96:0.1,self.S203:0.1,self.S196:0.1,self.S205:0.1,self.S206:0.1,self.S207:0.1,self.S208:0.1,self.S209:0.1,self.S210:0.1},'2014-01-02':{self.S97:0.1,self.S96:0.1,self.S196:0.1,self.S211:0.1,self.S212:0.1,self.S213:0.1,self.S214:0.1,self.S41:0.1,self.S64:0.1,self.S22:0.1},'2014-02-03':{self.S185:0.1,self.S215:0.1,self.S216:0.1,self.S97:0.1,self.S217:0.1,self.S218:0.1,self.S196:0.1,self.S219:0.1,self.S60:0.1,self.S162:0.1},'2014-03-03':{self.S59:0.1,self.S220:0.1,self.S97:0.1,self.S221:0.1,self.S185:0.1,self.S222:0.1,self.S223:0.1,self.S224:0.1,self.S190:0.1,self.S175:0.1},'2014-03-17':{self.TLT:1},'2014-05-30':{self.S222:0.1,self.S108:0.1,self.S148:0.1,self.S225:0.1,self.S209:0.1,self.S199:0.1,self.S215:0.1,self.S221:0.1,self.S190:0.1,self.S226:0.1},'2014-06-02':{self.S148:0.1,self.S108:0.1,self.S222:0.1,self.S225:0.1,self.S215:0.1,self.S209:0.1,self.S199:0.1,self.S221:0.1,self.S226:0.1,self.S190:0.1},'2014-06-06':{self.TLT:1},'2014-06-30':{self.S227:0.1,self.S148:0.1,self.S108:0.1,self.S228:0.1,self.S229:0.1,self.S230:0.1,self.S231:0.1,self.S222:0.1,self.S199:0.1,self.S226:0.1},'2014-07-01':{self.S148:0.1,self.S227:0.1,self.S108:0.1,self.S229:0.1,self.S228:0.1,self.S231:0.1,self.S230:0.1,self.S199:0.1,self.S222:0.1,self.S226:0.1},'2014-08-01':{self.S232:0.1,self.S228:0.1,self.S193:0.1,self.S233:0.1,self.S3:0.1,self.S231:0.1,self.S148:0.1,self.S234:0.1,self.S222:0.1,self.S199:0.1},'2014-09-02':{self.S235:0.1,self.S232:0.1,self.S236:0.1,self.S237:0.1,self.S238:0.1,self.S222:0.1,self.S190:0.1,self.S148:0.1,self.S3:0.1,self.S239:0.1},'2014-09-23':{self.TLT:1},'2015-02-27':{self.S240:0.1,self.S241:0.1,self.S242:0.1,self.S243:0.1,self.S3:0.1,self.S59:0.1,self.S244:0.1,self.S245:0.1,self.S97:0.1,self.S246:0.1},'2015-03-02':{self.S183:0.1,self.S240:0.1,self.S243:0.1,self.S3:0.1,self.S241:0.1,self.S242:0.1,self.S245:0.1,self.S59:0.1,self.S97:0.1,self.S246:0.1},'2015-04-01':{self.S240:0.1,self.S183:0.1,self.S241:0.1,self.S59:0.1,self.S247:0.1,self.S248:0.1,self.S3:0.1,self.S245:0.1,self.S97:0.1,self.S242:0.1},'2015-05-01':{self.S249:0.1,self.S240:0.1,self.S153:0.1,self.S59:0.1,self.S3:0.1,self.S248:0.1,self.S97:0.1,self.S250:0.1,self.S28:0.1,self.S251:0.1},'2015-06-01':{self.S3:0.1,self.S249:0.1,self.S252:0.1,self.S253:0.1,self.S59:0.1,self.S254:0.1,self.S255:0.1,self.S246:0.1,self.S256:0.1,self.S257:0.1},'2015-06-18':{self.TLT:1},'2015-07-01':{self.S258:0.1,self.S3:0.1,self.S252:0.1,self.S251:0.1,self.S259:0.1,self.S260:0.1,self.S261:0.1,self.S249:0.1,self.S226:0.1,self.S59:0.1},'2015-07-08':{self.TLT:1},'2015-10-23':{self.S3:0.1,self.S262:0.1,self.S255:0.1,self.S263:0.1,self.S264:0.1,self.S265:0.1,self.S59:0.1,self.S127:0.1,self.S28:0.1,self.S226:0.1},'2015-11-02':{self.S3:0.1,self.S28:0.1,self.S264:0.1,self.S263:0.1,self.S255:0.1,self.S59:0.1,self.S265:0.1,self.S38:0.1,self.S22:0.1,self.S13:0.1},'2015-12-01':{self.S3:0.1,self.S135:0.1,self.S266:0.1,self.S267:0.1,self.S59:0.1,self.S142:0.1,self.S28:0.1,self.S205:0.1,self.S268:0.1,self.S269:0.1},'2015-12-21':{self.TLT:1},'2016-03-09':{self.S270:0.1,self.S30:0.1,self.S271:0.1,self.S223:0.1,self.S272:0.1,self.S98:0.1,self.S114:0.1,self.S273:0.1,self.S163:0.1,self.S274:0.1},'2016-04-01':{self.S275:0.1,self.S276:0.1,self.S277:0.1,self.S95:0.1,self.S30:0.1,self.S278:0.1,self.S279:0.1,self.S211:0.1,self.S38:0.1,self.S274:0.1},'2016-05-02':{self.S3:0.1,self.S280:0.1,self.S275:0.1,self.S281:0.1,self.S282:0.1,self.S283:0.1,self.S284:0.1,self.S241:0.1,self.S44:0.1,self.S127:0.1},'2016-06-01':{self.S285:0.1,self.S3:0.1,self.S286:0.1,self.S44:0.1,self.S31:0.1,self.S24:0.1,self.S287:0.1,self.S288:0.1,self.S276:0.1,self.S289:0.1},'2016-07-01':{self.S290:0.1,self.S291:0.1,self.S44:0.1,self.S3:0.1,self.S292:0.1,self.S280:0.1,self.S293:0.1,self.S285:0.1,self.S294:0.1,self.S289:0.1},'2016-08-01':{self.S280:0.1,self.S115:0.1,self.S3:0.1,self.S285:0.1,self.S295:0.1,self.S95:0.1,self.S81:0.1,self.S296:0.1,self.S297:0.1,self.S290:0.1},'2016-09-01':{self.S267:0.1,self.S14:0.1,self.S3:0.1,self.S44:0.1,self.S296:0.1,self.S280:0.1,self.S242:0.1,self.S81:0.1,self.S275:0.1,self.S256:0.1},'2016-10-03':{self.S298:0.1,self.S299:0.1,self.S267:0.1,self.S242:0.1,self.S300:0.1,self.S3:0.1,self.S301:0.1,self.S302:0.1,self.S303:0.1,self.S245:0.1},'2016-11-01':{self.S242:0.1,self.S267:0.1,self.S245:0.1,self.S303:0.1,self.S304:0.1,self.S305:0.1,self.S98:0.1,self.S306:0.1,self.S307:0.1,self.S308:0.1},'2016-12-01':{self.S309:0.1,self.S303:0.1,self.S310:0.1,self.S311:0.1,self.S157:0.1,self.S305:0.1,self.S93:0.1,self.S312:0.1,self.S313:0.1,self.S274:0.1},'2017-01-03':{self.S309:0.1,self.S310:0.1,self.S314:0.1,self.S315:0.1,self.S312:0.1,self.S157:0.1,self.S316:0.1,self.S135:0.1,self.S305:0.1,self.S15:0.1},'2017-02-01':{self.S309:0.1,self.S310:0.1,self.S245:0.1,self.S317:0.1,self.S3:0.1,self.S318:0.1,self.S314:0.1,self.S319:0.1,self.S176:0.1,self.S235:0.1},'2017-03-01':{self.S309:0.1,self.S245:0.1,self.S317:0.1,self.S320:0.1,self.S321:0.1,self.S314:0.1,self.S197:0.1,self.S59:0.1,self.S322:0.1,self.S323:0.1},'2017-04-03':{self.S245:0.1,self.S321:0.1,self.S320:0.1,self.S324:0.1,self.S197:0.1,self.S174:0.1,self.S325:0.1,self.S323:0.1,self.S326:0.1,self.S240:0.1},'2017-05-01':{self.S309:0.1,self.S245:0.1,self.S320:0.1,self.S324:0.1,self.S137:0.1,self.S197:0.1,self.S321:0.1,self.S327:0.1,self.S328:0.1,self.S329:0.1},'2017-05-10':{self.TLT:1},'2017-05-26':{self.S198:0.1,self.S56:0.1,self.S330:0.1,self.S59:0.1,self.S331:0.1,self.S321:0.1,self.S332:0.1,self.S305:0.1,self.S140:0.1,self.S215:0.1},'2017-06-01':{self.S198:0.1,self.S331:0.1,self.S56:0.1,self.S333:0.1,self.S330:0.1,self.S59:0.1,self.S332:0.1,self.S305:0.1,self.S321:0.1,self.S256:0.1},'2017-06-05':{self.TLT:1},'2017-07-03':{self.S309:0.1,self.S198:0.1,self.S330:0.1,self.S321:0.1,self.S334:0.1,self.S305:0.1,self.S59:0.1,self.S335:0.1,self.S56:0.1,self.S313:0.1},'2017-08-01':{self.S309:0.1,self.S336:0.1,self.S251:0.1,self.S337:0.1,self.S198:0.1,self.S338:0.1,self.S210:0.1,self.S330:0.1,self.S339:0.1,self.S340:0.1},'2017-09-01':{self.S309:0.1,self.S341:0.1,self.S342:0.1,self.S251:0.1,self.S343:0.1,self.S344:0.1,self.S345:0.1,self.S305:0.1,self.S150:0.1,self.S346:0.1},'2017-10-02':{self.S309:0.1,self.S202:0.1,self.S196:0.1,self.S347:0.1,self.S348:0.1,self.S349:0.1,self.S350:0.1,self.S135:0.1,self.S61:0.1,self.S351:0.1},'2017-11-01':{self.S309:0.1,self.S352:0.1,self.S353:0.1,self.S354:0.1,self.S351:0.1,self.S202:0.1,self.S355:0.1,self.S356:0.1,self.S346:0.1,self.S305:0.1},'2017-12-01':{self.S309:0.1,self.S357:0.1,self.S202:0.1,self.S305:0.1,self.S351:0.1,self.S265:0.1,self.S105:0.1,self.S205:0.1,self.S358:0.1,self.S359:0.1},'2018-01-02':{self.S309:0.1,self.S357:0.1,self.S360:0.1,self.S351:0.1,self.S174:0.1,self.S305:0.1,self.S359:0.1,self.S3:0.1,self.S361:0.1,self.S316:0.1},'2018-02-01':{self.S309:0.1,self.S359:0.1,self.S55:0.1,self.S351:0.1,self.S346:0.1,self.S357:0.1,self.S305:0.1,self.S362:0.1,self.S363:0.1,self.S364:0.1},'2018-03-01':{self.S309:0.1,self.S365:0.1,self.S366:0.1,self.S346:0.1,self.S305:0.1,self.S359:0.1,self.S367:0.1,self.S368:0.1,self.S369:0.1,self.S370:0.1},'2018-04-02':{self.S367:0.1,self.S371:0.1,self.S369:0.1,self.S372:0.1,self.S351:0.1,self.S370:0.1,self.S373:0.1,self.S305:0.1,self.S352:0.1,self.S312:0.1},'2018-04-10':{self.TLT:1},'2018-06-05':{self.S248:0.1,self.S374:0.1,self.S245:0.1,self.S26:0.1,self.S375:0.1,self.S376:0.1,self.S122:0.1,self.S27:0.1,self.S377:0.1,self.S45:0.1},'2018-07-02':{self.S245:0.1,self.S375:0.1,self.S378:0.1,self.S26:0.1,self.S379:0.1,self.S376:0.1,self.S380:0.1,self.S381:0.1,self.S382:0.1,self.S377:0.1},'2018-08-01':{self.TLT:1},'2019-02-19':{self.S376:0.1,self.S187:0.1,self.S383:0.1,self.S159:0.1,self.S115:0.1,self.S323:0.1,self.S384:0.1,self.S385:0.1,self.S386:0.1,self.S279:0.1},'2019-03-01':{self.S387:0.1,self.S376:0.1,self.S187:0.1,self.S383:0.1,self.S388:0.1,self.S389:0.1,self.S147:0.1,self.S262:0.1,self.S390:0.1,self.S384:0.1},'2019-04-01':{self.S376:0.1,self.S197:0.1,self.S324:0.1,self.S391:0.1,self.S92:0.1,self.S392:0.1,self.S393:0.1,self.S380:0.1,self.S394:0.1,self.S395:0.1},'2019-05-01':{self.S391:0.1,self.S396:0.1,self.S147:0.1,self.S397:0.1,self.S324:0.1,self.S197:0.1,self.S398:0.1,self.S392:0.1,self.S387:0.1,self.S399:0.1},'2019-05-14':{self.TLT:1},'2019-09-11':{self.S400:0.1,self.S401:0.1,self.S402:0.1,self.S403:0.1,self.S404:0.1,self.S405:0.1,self.S397:0.1,self.S221:0.1,self.S276:0.1,self.S406:0.1},'2019-10-01':{self.S400:0.1,self.S407:0.1,self.S408:0.1,self.S401:0.1,self.S397:0.1,self.S403:0.1,self.S267:0.1,self.S404:0.1,self.S409:0.1,self.S410:0.1},'2019-11-01':{self.S411:0.1,self.S406:0.1,self.S404:0.1,self.S412:0.1,self.S337:0.1,self.S409:0.1,self.S413:0.1,self.S414:0.1,self.S15:0.1,self.S415:0.1},'2019-12-02':{self.S416:0.1,self.S417:0.1,self.S418:0.1,self.S419:0.1,self.S420:0.1,self.S386:0.1,self.S79:0.1,self.S421:0.1,self.S206:0.1,self.S361:0.1},'2020-01-02':{self.S419:0.1,self.S386:0.1,self.S417:0.1,self.S422:0.1,self.S418:0.1,self.S206:0.1,self.S86:0.1,self.S409:0.1,self.S423:0.1,self.S249:0.1},'2020-02-03':{self.TLT:1},'2020-07-09':{self.S424:0.1,self.S425:0.1,self.S426:0.1,self.S392:0.1,self.S427:0.1,self.S376:0.1,self.S428:0.1,self.S429:0.1,self.S357:0.1,self.S430:0.1},'2020-08-03':{self.S431:0.1,self.S426:0.1,self.S432:0.1,self.S433:0.1,self.S376:0.1,self.S434:0.1,self.S357:0.1,self.S435:0.1,self.S436:0.1,self.S424:0.1},'2020-09-01':{self.S426:0.1,self.S232:0.1,self.S437:0.1,self.S429:0.1,self.S434:0.1,self.S438:0.1,self.S439:0.1,self.S198:0.1,self.S376:0.1,self.S440:0.1},'2020-10-01':{self.S441:0.1,self.S433:0.1,self.S434:0.1,self.S442:0.1,self.S426:0.1,self.S437:0.1,self.S277:0.1,self.S205:0.1,self.S443:0.1,self.S174:0.1},'2020-11-02':{self.S433:0.1,self.S376:0.1,self.S437:0.1,self.S426:0.1,self.S194:0.1,self.S441:0.1,self.S444:0.1,self.S445:0.1,self.S446:0.1,self.S228:0.1},'2020-12-01':{self.S431:0.1,self.S447:0.1,self.S433:0.1,self.S231:0.1,self.S376:0.1,self.S73:0.1,self.S18:0.1,self.S281:0.1,self.S448:0.1,self.S449:0.1}}
        self.year = 2008
        self.Schedule.On(
            self.DateRules.EveryDay(),
            self.TimeRules.AfterMarketOpen('SPY', 45),
            self.rebalance
        )
        
    def rebalance(self):
        currentDate = self.UtcTime.date()
        # if currentDate.year != self.year:
        #     self.Portfolio.SetCash(self.Portfolio.Cash + 5000);
        # self.year = currentDate.year
        try:
            new_position = self.signals["{}".format(currentDate)]
        except:
            return
        self.Transactions.CancelOpenOrders()
        invested = dict.fromkeys([ x.Symbol for x in self.Portfolio.Values if x.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.85 * price)
from sklearn import preprocessing


def normalize_column(df, column_name):
    column = df[column_name]
    df['{}_norm'.format(column_name)] = (column - df[column_name].min()) / (df[column_name].max() - df[column_name].min())


def normalize_column_opp(df, column_name):
    column = df[column_name]
    df['{}_norm'.format(column_name)] = (column.max() - column) / (column.max() - column.min())


def norm_data(df):
    sectors = df.sector.unique()
    ratios_to_normalize = ['roic_wacc', 'roic_wacc_1',
                          'roic', 'roic_1',
                          'roic_nopat', 'roic_nopat_1',
                          'ebit_ev', 'ebit_ev_1',
                          'diluted_eps', 'diluted_eps_1',
                          'cr', 'cr_1',
                          'fcf', 'fcf_1',
                          'price_to_cf', 'price_to_cf_1',
                          'dbt_eq', 'dbt_eq_1',
                          'gross_margin', 'gross_margin_1',
                          'market_cap'
                          ]
    tech_to_normalize = ['avg_vol',
                         'std',
                         'std_close',
                         'vol_momentum',
                         'momentum',
                         'trend_log',
                         'trend_pct',
                         'trend_slope'
                         ]
    # for sector in sectors:
    #     for ratio in ratios_to_normalize:
    #         df.loc[df['sector'] == sector, '{}_norm'.format(ratio)] = preprocessing.scale(df[df['sector'] == sector][ratio])

    df[ratios_to_normalize+tech_to_normalize] = preprocessing.scale(df[ratios_to_normalize+tech_to_normalize])

    # for ratio in ratios_to_normalize:
    #     # df['{}_norm'.format(ratio)] = (df[ratio] - df[ratio].min()) / (df[ratio].max() - df[ratio].min())
    #     df['{}_norm'.format(ratio)] = preprocessing.scale(df[ratio])
    #
    # for tech in tech_to_normalize:
    #     # df['{}_norm'.format(tech)] = (df[tech] - df[tech].min()) / (df[tech].max() - df[tech].min())
    #     df['{}_norm'.format(tech)] = preprocessing.scale(df[tech])
    return df.copy()


def calculate_rank_1(df):
    df = df.dropna()
    df = df[df['wacc'] > 0].copy()
    df = df[df['price'] > 5].copy()
    df = df[df['avg_vol'] > 5000000].copy()

    normalize_column(df, 'roic_wacc')
    normalize_column(df, 'roic')
    normalize_column(df, 'roic_nopat')
    normalize_column(df, 'roic_nopat_wacc')
    normalize_column(df, 'ebit_ev')
    normalize_column(df, 'diluted_eps')
    normalize_column(df, 'cr')
    normalize_column(df, 'fcf')
    normalize_column(df, 'price_to_cf')
    normalize_column(df, 'roic_wacc_1')
    normalize_column(df, 'roic_1')
    normalize_column(df, 'roic_nopat_1')
    normalize_column(df, 'roic_nopat_wacc_1')
    normalize_column(df, 'ebit_ev_1')
    normalize_column(df, 'cr_1')
    normalize_column(df, 'fcf_1')
    normalize_column(df, 'price_to_cf_1')

    normalize_column(df, 'piotrowski_score')
    normalize_column_opp(df, 'std')
    normalize_column(df, 'vol_momentum')
    normalize_column(df, 'momentum')
    normalize_column_opp(df, 'std_close')
    normalize_column(df, 'trend_log')
    normalize_column(df, 'trend_pct')
    normalize_column(df, 'trend_slope')
    normalize_column(df, 'market_cap')

    df['overall_rank'] = df['roic_norm'] + df['ebit_ev_norm'] + df['cr_norm'] + df['momentum_norm'] + df['vol_momentum_norm']
    df = df.sort_values(by=['overall_rank'], ascending=False)
    res = df[['ticker', 'piotrowski_score', 'roic', 'roic_wacc', 'ebit_ev', 'cr', 'momentum', 'vol_momentum', 'overall_rank']]
    return res.head(5).to_markdown()


def calculate_rank_2(df):
    df = df.dropna()
    df = df[df['wacc'] > 0].copy()
    df = df[df['price'] > 5].copy()
    df = df[df['avg_vol'] > 5000000].copy()

    normalize_column(df, 'roic_wacc')
    normalize_column(df, 'roic')
    normalize_column(df, 'roic_nopat')
    normalize_column(df, 'roic_nopat_wacc')
    normalize_column(df, 'ebit_ev')
    normalize_column(df, 'diluted_eps')
    normalize_column(df, 'cr')
    normalize_column(df, 'fcf')
    normalize_column(df, 'price_to_cf')
    normalize_column(df, 'roic_wacc_1')
    normalize_column(df, 'roic_1')
    normalize_column(df, 'roic_nopat_1')
    normalize_column(df, 'roic_nopat_wacc_1')
    normalize_column(df, 'ebit_ev_1')
    normalize_column(df, 'cr_1')
    normalize_column(df, 'fcf_1')
    normalize_column(df, 'price_to_cf_1')

    normalize_column(df, 'piotrowski_score')
    normalize_column_opp(df, 'std')
    normalize_column(df, 'vol_momentum')
    normalize_column(df, 'momentum')
    normalize_column_opp(df, 'std_close')
    normalize_column(df, 'trend_log')
    normalize_column(df, 'trend_pct')
    normalize_column(df, 'trend_slope')
    normalize_column(df, 'market_cap')

    df['overall_rank'] = df['roic_wacc'] + df['ebit_ev'] + df['fcf']
    df = df.sort_values(by=['overall_rank'], ascending=False)
    res = df[['ticker', 'piotrowski_score', 'roic', 'roic_wacc', 'ebit_ev', 'cr', 'momentum', 'vol_momentum', 'overall_rank']]
    return res.head(5).to_markdown()


def calculate_rank_3(df):
    df = df.dropna()
    df = df[df['price_last'] > 5].copy()
    df = df[df['avg_vol'] > 5000000].copy()
    df = remove_extreme_values(df.copy())
    df = norm_data(df.copy())

    # overall rank
    df['overall_rank'] = df['price_to_cf_norm'] + df['roic_wacc_norm'] + df['ebit_ev_norm'] + df['cr_norm'] + df['vol_momentum_norm'] + df['momentum_norm']
    df = df.sort_values(by=['overall_rank'], ascending=False)
    res = df[['ticker', 'piotrowski_score', 'price_to_cf_norm', 'roic_wacc_norm', 'ebit_ev_norm', 'cr_norm', 'vol_momentum_norm', 'momentum_norm', 'overall_rank']]
    return res.head(10).to_markdown()


def remove_extreme_values(df):
    df_copy = df.copy()
    ratios_to_remove = ['roic_wacc',
                        'ebit_ev',
                        'cr',
                        'price_to_cf',
                        'vol_momentum',
                        'momentum',
                        'trend_slope']
    for ratio in ratios_to_remove:
        Q1 = df[ratio].quantile(0.006)
        Q3 = df[ratio].quantile(0.995)
        IQR = Q3 - Q1
        df_copy = df_copy.loc[(df_copy[ratio] > (Q1 - 1.2 * IQR)) & (df_copy[ratio] < (Q3 + 1.2 * IQR))].copy()

    return df_copy


def calculate_rank(df):
    df = df.dropna()
    # df = df[df['wacc'] > 0].copy()
    df = df[df['price_last'] > 5].copy()
    # df = df[df['price_to_cf'] > 0].copy()
    # df = df[df['roic_wacc'] > 0].copy()
    # df = df[df['ebit_ev'] > 0].copy()
    # df = df[df['cr'] > 0].copy()
    df = df[df['avg_vol'] > 5000000].copy()
    df = remove_extreme_values(df.copy())
    df = norm_data(df.copy())

    # overall rank
    df['overall_rank'] = df['price_to_cf'] + df['roic_wacc'] + df['ebit_ev'] + df['cr'] + df['vol_momentum'] + df['momentum']
    df = df.sort_values(by=['overall_rank'], ascending=False)
    return df.head(10)