Overall Statistics |
Total Trades 500 Average Win 0.04% Average Loss -0.02% Compounding Annual Return 41.118% Drawdown 0.500% Expectancy 0.360 Net Profit 2.066% Sharpe Ratio 8.275 Loss Rate 51% Win Rate 49% Profit-Loss Ratio 1.79 Alpha 0.254 Beta 0.14 Annual Standard Deviation 0.042 Annual Variance 0.002 Information Ratio -5.266 Tracking Error 0.057 Treynor Ratio 2.454 Total Fees $501.00 |
import csv from StringIO import StringIO from datetime import datetime from decimal import * from sets import Set class BasicTemplateAlgorithm(QCAlgorithm): def Initialize(self): raw_trade_data = """ 20170207 11:53,IBN,bear 20170207 12:07,TSLA,bear 20170207 12:07,GM,bear 20170207 12:07,F,bear 20170207 12:10,AAPL,bear 20170207 12:51,GPRO,bull 20170207 13:07,MO,bull 20170207 13:18,AMZN,bull 20170207 13:25,BMY,bull 20170207 13:33,DIS,bull 20170207 13:40,TSLA,bull 20170207 13:40,JNJ,bear 20170207 14:04,MO,bear 20170207 14:04,BUD,bear 20170207 14:30,JNJ,bull 20170207 14:40,ABB,bear 20170207 14:40,NDAQ,bear 20170207 15:34,HTZ,bull 20170208 08:01,AVY,bull 20170208 08:09,WFC,bear 20170208 08:41,GM,bull 20170208 08:46,GSK,bear 20170208 09:15,MBT,bull 20170208 09:15,GE,bull 20170208 09:22,GILD,bear 20170208 09:36,SBUX,bull 20170208 09:44,MRK,bear 20170208 09:44,BMY,bear 20170208 10:03,GILD,bull 20170208 10:07,GILD,bear 20170208 10:17,BBRY,bull 20170208 10:32,BRKA,bull 20170208 10:32,BRKB,bull 20170208 10:35,JPM,bull 20170208 10:49,AMD,bull 20170208 10:55,CSCO,bull 20170208 10:55,GM,bull 20170208 11:18,MELI,bull 20170208 11:18,AMZN,bull 20170208 11:18,EBAY,bull 20170208 11:32,TGT,bear 20170208 11:44,NVDA,bull 20170208 12:02,TSN,bull 20170208 12:11,LMT,bull 20170208 12:24,BP,bear 20170208 12:38,MU,bull 20170208 13:12,HAS,bull 20170208 13:59,AAPL,bull 20170208 14:19,UA,bear 20170208 14:19,AMZN,bear 20170208 14:42,GM,bull 20170208 14:44,GE,bull 20170208 14:50,NFLX,bull 20170208 14:59,CHK,bull 20170208 15:13,SBUX,bear 20170208 15:30,fbr,bull 20170208 15:58,GE,bull 20170208 15:59,DIS,bull 20170209 08:23,GOOG,bull 20170209 08:23,GOOGL,bull 20170209 08:30,SHLD,bear 20170209 08:57,BA,bull 20170209 09:01,ABB,bear 20170209 09:16,JNJ,bull 20170209 09:16,BMY,bull 20170209 09:22,PRU,bull 20170209 09:31,GSK,bear 20170209 09:39,AMZN,bear 20170209 09:39,AAPL,bear 20170209 09:59,CELG,bull 20170209 10:13,SCOR,bull 20170209 10:14,BP,bull 20170209 10:16,EXPD,bull 20170209 10:19,BMY,bear 20170209 10:21,UA,bull 20170209 10:32,GOOG,bull 20170209 10:32,GOOGL,bull 20170209 10:33,KO,bear 20170209 10:33,SYY,bear 20170209 10:35,F,bear 20170209 10:42,TGT,bull 20170209 11:12,F,bull 20170209 11:13,PFE,bear 20170209 11:21,DIS,bull 20170209 11:25,IMPV,bull 20170209 11:25,ORCL,bull 20170209 11:45,BAC,bear 20170209 11:55,GILD,bull 20170209 11:59,NFLX,bear 20170209 12:08,CTSH,bear 20170209 12:08,AAPL,bear 20170209 12:20,AAPL,bear 20170209 12:29,GIS,bull 20170209 12:29,MDLZ,bull 20170209 12:52,GS,bull 20170209 13:13,TEVA,bear 20170209 13:26,TSN,bull 20170209 13:39,AMZN,bull 20170209 13:39,SYMC,bull 20170209 13:53,CTSH,bull 20170209 14:16,PFE,bull 20170209 14:16,RDS.A,bull 20170209 14:16,RDS.B,bull 20170209 14:32,WWE,bull 20170209 14:35,VRX,bull 20170209 14:47,AMD,bull 20170209 14:47,INTC,bull 20170209 14:49,AKAM,bear 20170209 15:01,NOK,bull 20170209 15:01,JNPR,bull 20170209 15:02,AMGN,bull 20170209 15:42,PEP,bear 20170209 15:47,RGR,bear 20170210 08:01,MAT,bull 20170210 08:01,HAS,bull 20170210 09:03,LUV,bull 20170210 09:14,SHLD,bear 20170210 10:02,ORCL,bull 20170210 10:02,IBM,bull 20170210 10:55,MU,bull 20170210 11:13,NVDA,bull 20170210 11:13,INTC,bull 20170210 11:13,TSLA,bull 20170210 11:19,JNJ,bear 20170210 11:33,JNJ,bear 20170210 11:51,PNRA,bull 20170210 12:02,LGF,bull 20170210 12:09,CELG,bear 20170210 12:31,CTSH,bull 20170210 12:31,ACN,bear 20170210 12:31,VRSN,bear 20170210 12:35,VRX,bull 20170210 12:41,AAPL,bull 20170210 12:41,AMZN,bull 20170210 12:41,GOOG,bull 20170210 12:41,GOOGL,bull 20170210 12:48,GILD,bull 20170210 12:50,GILD,bull 20170210 13:00,SBUX,bull 20170210 13:02,CSCO,bull 20170210 13:31,AAPL,bear 20170210 13:31,GOOG,bear 20170210 13:31,GOOGL,bear 20170210 13:45,EBAY,bull 20170210 14:08,GE,bull 20170210 14:08,IBM,bull 20170210 14:34,CTAS,bull 20170210 14:34,ADP,bull 20170210 14:34,VRX,bull 20170210 14:55,F,bear 20170210 14:55,GM,bear 20170210 15:01,SKX,bull 20170210 15:09,AMZN,bull 20170210 15:23,MSFT,bear 20170213 08:08,FORR,bull 20170213 08:13,EMR,bull 20170213 08:17,F,bull 20170213 08:17,GM,bull 20170213 08:39,AAPL,bear 20170213 08:39,IBM,bear 20170213 09:05,NVO,bull 20170213 09:15,JNJ,bull 20170213 09:22,QCOM,bear 20170213 09:22,AAPL,bear 20170213 09:24,MDLZ,bull 20170213 09:24,ITUB,bull 20170213 10:22,ZNGA,bear 20170213 10:24,UBS,bear 20170213 10:28,CSCO,bull 20170213 10:28,JNJ,bull 20170213 10:28,IBM,bull 20170213 10:28,GE,bull 20170213 11:01,JNJ,bull 20170213 11:11,NVDA,bull 20170213 11:15,AAPL,bear 20170213 11:28,JNJ,bear 20170213 11:30,AAPL,bull 20170213 11:51,ZNGA,bull 20170213 12:04,MBLY,bull 20170213 12:04,GOOG,bull 20170213 12:04,GOOGL,bull 20170213 12:04,GM,bull 20170213 12:08,AKS,bull 20170213 12:10,GSK,bull 20170213 12:35,JNJ,bull 20170213 12:38,PEP,bull 20170213 12:44,AAPL,bull 20170213 12:44,IBM,bull 20170213 12:44,NUAN,bear 20170213 13:05,ACN,bull 20170213 13:34,TEVA,bear 20170213 13:42,TSLA,bull 20170213 13:55,PFE,bull 20170213 13:55,JNJ,bull 20170213 14:16,ESRX,bear 20170213 14:22,VRX,bear 20170213 14:28,FMC,bear 20170213 14:28,TSLA,bear 20170213 14:42,C,bear 20170213 14:45,AAPL,bull 20170213 14:54,BX,bear 20170213 15:00,GOOG,bear 20170213 15:00,GOOGL,bear 20170213 15:00,AMZN,bear 20170213 15:01,INTC,bull 20170213 15:01,IBM,bull 20170213 15:04,F,bear 20170213 15:08,ABT,bull 20170213 15:55,F,bull 20170213 15:55,GM,bull 20170213 15:55,TSLA,bull 20170213 15:56,AAPL,bull 20170214 08:10,AMZN,bull 20170214 08:17,SPWR,bull 20170214 08:51,CSCO,bull 20170214 08:52,BMY,bear 20170214 08:54,MINT,bull 20170214 09:02,JNJ,bull 20170214 09:06,NTDOY,bull 20170214 09:11,BX,bear 20170214 09:13,JNJ,bull 20170214 09:13,IBM,bull 20170214 09:15,TD,bull 20170214 09:15,AAPL,bull 20170214 09:22,GILD,bull 20170214 09:22,JNJ,bull 20170214 09:22,GSK,bull 20170214 09:22,VZ,bull 20170214 09:22,GM,bull 20170214 09:23,ECL,bull 20170214 09:53,BBY,bull 20170214 09:56,PRU,bull 20170214 10:00,MINT,bull 20170214 10:02,TEVA,bull 20170214 10:17,VRX,bear 20170214 10:22,MPC,bull 20170214 10:31,MAT,bull 20170214 10:31,MO,bull 20170214 10:38,WDC,bull 20170214 10:49,GOOG,bear 20170214 10:49,GOOGL,bear 20170214 10:49,SBUX,bear 20170214 10:59,AA,bull 20170214 11:07,GILD,bull 20170214 11:13,K,bear 20170214 11:24,BBY,bull 20170214 11:48,HLF,bull 20170214 13:34,CS,bull 20170214 13:37,CS,bull 20170214 13:39,CS,bull 20170214 13:52,C,bull 20170214 14:04,TSLA,bull 20170214 14:04,GM,bull 20170214 14:04,NSANY,bull 20170214 14:32,UAL,bull 20170214 14:33,CS,bull 20170214 14:33,CS,bull 20170214 14:35,GM,bull 20170214 14:35,GM,bull 20170214 14:55,NOK,bull 20170214 15:22,NFLX,bull 20170214 15:33,ADSK,bull 20170214 15:50,TSLA,bull 20170215 08:13,AAPL,bear 20170215 08:22,KO,bull 20170215 08:24,BG,bear 20170215 08:45,ADI,bull 20170215 09:33,SDRL,bull 20170215 09:35,GILD,bull 20170215 09:35,BMY,bull 20170215 09:44,TSLA,bull 20170215 09:44,GM,bull 20170215 09:47,BP,bear 20170215 09:47,TGT,bear 20170215 10:03,CSCO,bull 20170215 10:16,GM,bull 20170215 10:30,AAPL,bull 20170215 10:32,NFLX,bull 20170215 10:39,GE,bull 20170215 10:39,PPL,bull 20170215 10:49,KO,bear 20170215 10:54,GILD,bear 20170215 10:59,ABB,bear 20170215 11:12,MSFT,bull 20170215 11:12,TSLA,bull 20170215 11:29,GILD,bull 20170215 11:29,GSK,bull 20170215 11:40,AAPL,bull 20170215 11:44,RIO,bear 20170215 12:07,INTC,bull 20170215 12:11,HD,bear 20170215 13:30,TEVA,bull 20170215 13:30,CELG,bull 20170215 13:51,SAP,bull 20170215 13:51,ORCL,bull 20170215 13:56,ESRX,bull 20170215 14:15,NVS,bull 20170215 14:27,PAAS,bear 20170215 14:43,BAC,bull 20170215 14:43,C,bull 20170215 14:44,GILD,bull 20170215 14:48,GM,bear 20170215 15:01,C,bear 20170215 15:15,GE,bull 20170215 15:18,FCX,bull 20170215 15:30,GE,bull 20170215 15:30,MCD,bull 20170215 15:30,AAPL,bull 20170215 15:55,TGT,bull 20170216 08:03,CSCO,bull 20170216 08:09,AIG,bear 20170216 08:10,AMGN,bull 20170216 08:10,JNJ,bull 20170216 09:13,WFC,bull 20170216 09:21,F,bull 20170216 09:21,TM,bull 20170216 09:21,GM,bull 20170216 09:24,MDLZ,bear 20170216 09:35,WFC,bear 20170216 09:42,BUD,bear 20170216 09:51,FCX,bull 20170216 09:58,NFLX,bull 20170216 10:01,MGA,bull 20170216 10:18,BRKA,bull 20170216 10:18,BRKB,bull 20170216 10:29,CSCO,bear 20170216 10:35,MSFT,bear 20170216 10:35,AMZN,bear 20170216 11:55,GS,bull 20170216 11:55,JNJ,bull 20170216 12:06,VRX,bear 20170216 12:06,JNJ,bear 20170216 12:08,UAL,bull 20170216 12:18,VRSK,bear 20170216 12:18,AIG,bear 20170216 12:24,INFY,bull 20170216 12:49,CSCO,bull 20170216 12:52,AMZN,bull 20170216 14:16,PAAS,bear 20170216 14:16,WMG,bull 20170216 14:20,AAPL,bull 20170216 14:26,SODA,bull 20170216 14:36,TSLA,bull 20170216 14:40,BMY,bull 20170216 14:48,AMGN,bear 20170216 15:13,ABX,bull 20170216 15:18,SDRL,bull 20170216 15:32,GILD,bull 20170216 15:32,JNJ,bull 20170216 15:43,KMB,bull 20170216 15:44,GS,bear 20170216 15:46,JNJ,bull 20170217 08:27,CHK,bull 20170217 08:42,CTB,bull 20170217 08:56,AMZN,bear 20170217 09:36,GS,bull 20170217 09:36,IBM,bull 20170217 09:36,INTC,bull 20170217 09:55,MU,bull 20170217 10:19,SINA,bear 20170217 10:23,TSLA,bull 20170217 10:37,JPM,bear 20170217 11:30,VZ,bull 20170217 11:30,GM,bull 20170217 11:50,NKE,bear 20170217 12:58,WMT,bull 20170217 12:58,HD,bull 20170217 13:30,CBOE,bull 20170217 13:35,HAL,bear 20170217 13:39,F,bear 20170217 13:42,GSK,bull 20170217 13:42,AMGN,bull 20170217 13:47,CELG,bull 20170217 13:52,UN,bear 20170217 13:57,YNDX,bear 20170217 14:13,BLDP,bull 20170217 14:23,QCOM,bull 20170217 14:31,APA,bull 20170217 15:01,F,bear 20170217 15:50,VZ,bear 20170217 15:50,GM,bear 20170220 09:00,BBL,bull 20170220 09:14,FSLR,bull 20170220 09:20,AIG,bull 20170220 10:02,TSLA,bull 20170220 10:13,AIG,bear 20170220 10:41,BBRY,bull 20170220 10:41,F,bull 20170220 11:12,LUK,bull 20170220 12:32,YNDX,bear 20170220 12:37,PAYX,bull 20170220 12:50,TTM,bull 20170220 13:17,AAPL,bull 20170220 13:43,ANF,bear 20170220 14:01,AAPL,bear 20170220 14:01,IBM,bear 20170220 14:03,IBM,bull 20170220 14:05,QCOM,bull 20170220 14:22,WMT,bull 20170220 14:36,AUY,bear 20170220 14:53,NTT,bear 20170220 14:53,bidu,bear 20170220 14:53,JD,bear 20170220 14:56,BSX,bull 20170221 08:00,LE,bear 20170221 08:04,AAPL,bull 20170221 08:04,AMZN,bull 20170221 08:20,BBL,bear 20170221 08:30,AMZN,bull 20170221 08:41,HBC,bear 20170221 08:56,WMT,bull 20170221 08:57,MSFT,bull 20170221 09:08,WMT,bear 20170221 10:03,VRX,bear 20170221 10:09,UN,bear 20170221 10:17,MDLZ,bull 20170221 10:31,TEVA,bull 20170221 10:33,WMT,bull 20170221 10:34,HD,bull 20170221 10:45,AAPL,bull 20170221 11:12,GWPH,bear 20170221 11:47,MRK,bull 20170221 12:25,CHK,bull 20170221 12:31,IBM,bull 20170221 12:45,AMZN,bear 20170221 12:45,ORCL,bear 20170221 12:59,TGT,bull 20170221 13:19,MO,bull 20170221 13:19,BUD,bull 20170221 13:20,CSCO,bull 20170221 13:20,NXPI,bull 20170221 13:21,AAPL,bear 20170221 13:21,QCOM,bear 20170221 13:43,CELG,bull 20170221 13:46,TSLA,bull 20170221 13:47,YHOO,bear 20170221 13:57,bidu,bull 20170221 13:57,AAPL,bull 20170221 14:00,TEVA,bull 20170221 14:16,MCD,bear 20170221 14:23,AEP,bull 20170221 14:23,JNJ,bull 20170221 14:51,HBC,bear 20170221 15:04,TD,bull 20170221 15:04,BNS,bull 20170221 15:09,GSK,bull 20170221 15:09,bidu,bull 20170222 08:38,AMZN,bull 20170222 08:51,GILD,bear 20170222 08:51,BMY,bear 20170222 09:00,JNJ,bull 20170222 09:12,AKS,bull 20170222 09:16,DIS,bear 20170222 09:36,SKM,bear 20170222 09:38,JNJ,bull 20170222 09:53,TSLA,bull 20170222 09:53,AAPL,bull 20170222 10:04,LYG,bull 20170222 10:24,BSX,bull 20170222 10:25,WMT,bull 20170222 10:30,HD,bull 20170222 10:42,TSLA,bear 20170222 10:48,HLF,bear 20170222 10:57,JNJ,bull 20170222 11:22,WMT,bull 20170222 11:23,F,bull 20170222 11:23,GM,bull 20170222 11:23,TSLA,bull 20170222 11:45,AMD,bull 20170222 11:45,INTC,bull 20170222 11:55,WMT,bull 20170222 12:07,AAPL,bear 20170222 12:07,GOOG,bear 20170222 12:07,GOOGL,bear 20170222 12:18,AAPL,bull 20170222 12:35,DVN,bear 20170222 12:38,AAPL,bull 20170222 12:51,CRM,bull 20170222 12:59,DB,bear 20170222 12:59,HBC,bear 20170222 12:59,GS,bear 20170222 13:29,AWI,bull 20170222 13:51,INTC,bull 20170222 13:51,AMD,bull 20170222 14:31,MRO,bull 20170222 14:37,BBRY,bull 20170222 14:50,KO,bull 20170222 14:53,CELG,bull 20170222 15:15,MS,bear 20170222 15:15,JCP,bear 20170222 15:17,ADP,bull 20170222 15:17,GE,bull 20170222 15:17,MCD,bull 20170222 15:17,JNJ,bull 20170222 15:29,TEVA,bear 20170222 15:48,FNV,bear 20170222 15:48,TRP,bull 20170222 15:49,WFC,bull 20170223 08:11,ORIG,bull 20170223 08:16,AAPL,bear 20170223 09:14,GM,bull 20170223 09:30,JNJ,bull 20170223 10:23,TSLA,bear 20170223 10:33,PFE,bull 20170223 10:53,FOXA,bull 20170223 10:53,BT,bull 20170223 11:00,AMAT,bull 20170223 11:27,GWPH,bull 20170223 11:38,TSLA,bull 20170223 11:38,GM,bull 20170223 11:38,F,bull 20170223 12:00,RIG,bull 20170223 12:03,GPS,bear 20170223 12:08,MU,bull 20170223 12:42,CHU,bull 20170223 12:50,CELG,bull 20170223 12:53,IBM,bear 20170223 12:58,GOOG,bear 20170223 12:58,GOOGL,bear 20170223 13:02,INTC,bull 20170223 13:03,FCX,bull 20170223 13:08,CVX,bull 20170223 13:08,TGT,bull 20170223 13:16,GSK,bull """ self.trade_data = {} equities = Set() # store a list of trades if there are multiple per minute f = StringIO(raw_trade_data) for row in csv.reader(f, delimiter=','): if row: # blank lines key = row[0] # [:-3] # take last 3 chars off if we have seconds if key in self.trade_data: self.trade_data[key].append(row) else: self.trade_data[key] = [row] if len(row) > 1: equities.add(row[1]) for symbol in equities: self.AddEquity(symbol) self.SetCash(100000) self.SetStartDate(2017,2,1) self.SetEndDate(2017,2,22) self.AddEquity("SPY", Resolution.Minute) self.Schedule.On( self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 5), Action(self.Liquidate) ) def OnData(self, slice): timekey = datetime(slice.Time).strftime("%Y%m%d %H:%M") if timekey in self.trade_data: rows = self.trade_data[timekey] # TODO delete the key from self.trade_data, just in case OnData fires # more than once within same minute. for row in rows: symbol = row[1] strategy = row[2] self.Log(repr(self.Identity(symbol).ToString())) # cash = min(self.Portfolio.CashBook.Values[0].Amount, 100000) # stock_price = self.Identity(symbol).Current.Value # self.Log(str(stock_price)) # qty = int(Decimal(cash) // Decimal(stock_price)) / 2 if strategy == "bull": newTicket = self.MarketOrder(symbol, 100, asynchronous = False) elif strategy == "bear": newTicket = self.MarketOrder(symbol, -100, asynchronous = False) # self.SetHoldings(symbol, -0.5)