Overall Statistics
Total Trades
595
Average Win
0.06%
Average Loss
-0.03%
Compounding Annual Return
40.524%
Drawdown
2.000%
Expectancy
0.263
Net Profit
2.167%
Sharpe Ratio
4.477
Loss Rate
59%
Win Rate
41%
Profit-Loss Ratio
2.09
Alpha
0.352
Beta
-0.056
Annual Standard Deviation
0.072
Annual Variance
0.005
Information Ratio
-2.884
Tracking Error
0.086
Treynor Ratio
-5.679
Total Fees
$597.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 = """
20170201 08:01,GIB,bull
20170201 08:22,ADP,bull
20170201 09:00,AAPL,bear
20170201 09:11,ATVI,bear
20170201 09:11,MAT,bear
20170201 09:11,TROW,bear
20170201 09:12,GS,bull
20170201 09:12,CRM,bull
20170201 09:47,AAPL,bull
20170201 10:08,QCOM,bull
20170201 10:08,IDTI,bull
20170201 10:16,AAPL,bull
20170201 10:25,GPRO,bull
20170201 10:25,SATS,bear
20170201 10:40,AMZN,bull
20170201 10:41,AMD,bull
20170201 10:59,IBM,bull
20170201 10:59,STT,bull
20170201 10:59,BRKA,bull
20170201 10:59,BRKB,bull
20170201 10:59,VZ,bull
20170201 10:59,BLK,bull
20170201 10:59,PNC,bull
20170201 10:59,GM,bull
20170201 11:04,IBM,bull
20170201 11:04,STT,bull
20170201 11:04,BRKA,bull
20170201 11:04,BRKB,bull
20170201 11:09,VZ,bear
20170201 11:09,BLK,bear
20170201 11:09,PNC,bear
20170201 11:09,GM,bear
20170201 11:27,AMD,bull
20170201 11:27,INTC,bull
20170201 11:30,AAPL,bull
20170201 12:10,CS,bull
20170201 12:10,AMD,bull
20170201 12:13,MSFT,bull
20170201 12:13,BLK,bull
20170201 12:13,STT,bull
20170201 12:13,PNC,bull
20170201 12:36,RHT,bear
20170201 12:36,JPM,bear
20170201 13:39,VZ,bull
20170201 13:39,BLK,bull
20170201 13:39,PNC,bull
20170201 13:39,GM,bull
20170201 13:41,MSFT,bull
20170201 13:41,BLK,bull
20170201 13:41,STT,bull
20170201 13:41,PNC,bull
20170201 13:45,MSFT,bull
20170201 13:45,BLK,bull
20170201 13:45,STT,bull
20170201 13:45,PNC,bull
20170201 13:53,CTXS,bull
20170201 14:00,CSCO,bull
20170201 14:16,MSFT,bear
20170201 14:16,BLK,bear
20170201 14:16,STT,bear
20170201 14:16,PNC,bear
20170201 14:19,AAPL,bull
20170201 14:30,AMZN,bull
20170201 14:30,I,bear
20170201 14:33,EA,bear
20170201 14:50,TTWO,bull
20170201 15:09,AAPL,bull
20170201 15:30,AAPL,bear
20170202 08:00,KT,bull
20170202 08:30,ADP,bull
20170202 08:32,MSFT,bull
20170202 08:32,BLK,bull
20170202 08:32,STT,bull
20170202 08:32,PNC,bull
20170202 09:30,AMD,bull
20170202 09:33,AAPL,bull
20170202 09:33,ARMH,bull
20170202 09:33,INTC,bull
20170202 09:45,SIRI,bull
20170202 09:45,BRKA,bull
20170202 09:45,BRKB,bull
20170202 10:16,AMZN,bull
20170202 10:22,FB,bear
20170202 10:30,AAPL,bull
20170202 11:00,IBM,bull
20170202 11:00,BRKA,bull
20170202 11:00,BRKB,bull
20170202 11:00,STT,bull
20170202 11:00,VZ,bull
20170202 11:00,BLK,bull
20170202 11:00,PNC,bull
20170202 11:00,GM,bull
20170202 11:30,AMD,bull
20170202 11:30,INTC,bull
20170202 11:57,FB,bear
20170202 12:30,MSFT,bull
20170202 12:30,BLK,bull
20170202 12:30,STT,bull
20170202 12:30,PNC,bull
20170202 12:37,AMD,bear
20170202 12:45,SYMC,bull
20170202 12:46,AAPL,bear
20170202 12:59,AMD,bull
20170202 12:59,INTC,bull
20170202 13:00,SLAB,bear
20170202 13:16,MU,bull
20170202 13:25,HBC,bear
20170202 13:30,GIB,bull
20170202 14:29,GM,bear
20170202 14:30,AAPL,bull
20170202 14:30,AAPL,bull
20170202 14:54,VZ,bear
20170202 14:54,BLK,bear
20170202 14:54,PNC,bear
20170202 14:54,GM,bear
20170202 15:00,ADP,bull
20170202 15:00,CDNS,bear
20170202 15:12,NOK,bull
20170202 15:30,AAPL,bull
20170202 15:40,AAPL,bull
20170202 15:59,MSFT,bull
20170202 15:59,PNC,bull
20170202 15:59,BLK,bull
20170202 15:59,STT,bull
20170202 15:59,IBM,bull
20170202 15:59,BRKA,bull
20170202 15:59,BRKB,bull
20170203 08:00,CSCO,bull
20170203 08:34,SKM,bull
20170203 08:42,BAC,bear
20170203 09:12,RUBI,bull
20170203 09:59,SNE,bull
20170203 10:00,AMD,bull
20170203 10:04,AAPL,bear
20170203 10:04,MSFT,bear
20170203 10:04,PNC,bear
20170203 10:04,BLK,bear
20170203 10:04,STT,bear
20170203 10:20,GOOG,bull
20170203 10:20,GOOGL,bull
20170203 10:29,AMOV,bear
20170203 10:48,CY,bull
20170203 10:49,AMZN,bull
20170203 11:36,FTNT,bear
20170203 12:19,ADP,bear
20170203 12:42,AMZN,bull
20170203 13:00,AMD,bull
20170203 13:26,GPRO,bull
20170203 13:26,SKM,bear
20170203 13:34,SYMC,bear
20170203 14:30,GM,bear
20170203 14:40,GOOG,bull
20170203 14:40,GOOGL,bull
20170203 14:45,ATVI,bear
20170203 14:46,QCOM,bear
20170203 15:00,VZ,bear
20170203 15:00,BLK,bear
20170203 15:00,PNC,bear
20170203 15:00,GM,bear
20170203 15:31,FB,bear
20170206 08:00,AMZN,bear
20170206 08:11,IBM,bull
20170206 08:11,STT,bull
20170206 08:11,BRKA,bull
20170206 08:11,BRKB,bull
20170206 08:37,AMSC,bull
20170206 08:38,QCOM,bull
20170206 08:38,NXPI,bull
20170206 09:22,WDC,bull
20170206 09:36,GLUU,bull
20170206 09:46,AMZN,bull
20170206 09:46,AAPL,bull
20170206 09:47,CRM,bull
20170206 10:08,SCOR,bull
20170206 10:26,PPG,bull
20170206 10:57,TRI,bear
20170206 11:22,MSFT,bear
20170206 11:22,BLK,bear
20170206 11:22,STT,bear
20170206 11:22,PNC,bear
20170206 11:43,AAPL,bull
20170206 11:49,NVDA,bear
20170206 11:53,INTC,bull
20170206 11:53,AMD,bull
20170206 12:24,GE,bull
20170206 12:53,MS,bear
20170206 13:00,FB,bear
20170206 13:02,AMD,bull
20170206 13:27,AAPL,bull
20170206 13:27,BRCM,bull
20170206 13:30,QCOM,bear
20170206 13:54,AAPL,bull
20170206 13:54,GOOG,bull
20170206 13:54,GOOGL,bull
20170206 14:00,ATVI,bear
20170206 14:08,CSCO,bear
20170206 14:27,ANGI,bear
20170206 15:01,AAPL,bear
20170206 15:30,SYMC,bear
20170207 08:00,EBAY,bear
20170207 08:28,CHKP,bull
20170207 08:30,IBM,bull
20170207 08:30,STT,bull
20170207 08:30,BRKA,bull
20170207 08:30,BRKB,bull
20170207 09:06,AAPL,bull
20170207 09:28,ADSK,bear
20170207 09:30,GE,bull
20170207 09:39,SYMC,bull
20170207 09:39,BBRY,bull
20170207 09:54,AMOV,bear
20170207 10:01,AMZN,bull
20170207 10:54,NOK,bull
20170207 10:54,MS,bull
20170207 11:00,AMZN,bull
20170207 11:00,AAPL,bull
20170207 11:10,VSAT,bull
20170207 11:18,BCS,bear
20170207 11:18,AMOV,bear
20170207 12:00,INTC,bull
20170207 12:00,AMD,bull
20170207 12:03,AAPL,bull
20170207 12:10,AAPL,bear
20170207 12:28,EBAY,bull
20170207 12:30,GE,bull
20170207 13:00,NVDA,bear
20170207 13:18,AMZN,bull
20170207 13:30,AAPL,bull
20170207 13:30,BRCM,bull
20170207 14:00,AAPL,bull
20170207 14:30,ANGI,bear
20170207 14:46,IDTI,bear
20170207 15:30,AAPL,bear
20170208 08:08,INTU,bear
20170208 08:30,INTU,bear
20170208 09:15,MBT,bull
20170208 09:46,AAPL,bear
20170208 10:00,AMOV,bear
20170208 10:17,BBRY,bull
20170208 10:40,AKAM,bull
20170208 10:40,MS,bull
20170208 10:49,AMD,bull
20170208 10:54,MCHP,bull
20170208 10:55,CSCO,bull
20170208 10:55,GM,bull
20170208 11:30,IDTI,bull
20170208 11:44,NVDA,bull
20170208 11:45,TTWO,bull
20170208 12:30,AAPL,bear
20170208 12:38,MU,bull
20170208 12:55,MBT,bull
20170208 12:59,GOOG,bull
20170208 12:59,GOOGL,bull
20170208 13:10,FB,bear
20170208 13:59,AAPL,bull
20170208 14:17,INTC,bull
20170208 15:22,FB,bull
20170208 15:22,BBY,bull
20170209 08:23,GOOG,bull
20170209 08:23,GOOGL,bull
20170209 08:53,SYMC,bull
20170209 09:12,AMZN,bull
20170209 09:30,MBT,bull
20170209 09:39,AMZN,bear
20170209 09:39,AAPL,bear
20170209 09:42,RMBS,bull
20170209 09:42,MS,bull
20170209 10:04,MU,bull
20170209 10:32,GOOG,bull
20170209 10:32,GOOGL,bull
20170209 11:02,TTWO,bull
20170209 11:25,IMPV,bull
20170209 11:25,ORCL,bull
20170209 12:06,CTSH,bull
20170209 12:08,CTSH,bear
20170209 12:08,AAPL,bear
20170209 12:20,AAPL,bear
20170209 12:30,AAPL,bull
20170209 12:51,TRI,bear
20170209 13:00,MU,bull
20170209 13:30,FB,bear
20170209 13:39,AMZN,bull
20170209 13:39,SYMC,bull
20170209 13:53,CTSH,bull
20170209 13:56,GLUU,bull
20170209 13:56,MFG,bull
20170209 14:00,AAPL,bull
20170209 14:11,INTC,bull
20170209 14:47,AMD,bull
20170209 14:47,INTC,bull
20170209 14:49,AKAM,bear
20170209 15:00,CTSH,bull
20170209 15:01,NOK,bull
20170209 15:01,JNPR,bull
20170209 15:22,FB,bull
20170210 08:23,TSLA,bull
20170210 08:23,DIS,bull
20170210 08:23,NFLX,bull
20170210 08:23,AAPL,bull
20170210 10:02,ORCL,bull
20170210 10:02,IBM,bull
20170210 10:02,STT,bull
20170210 10:02,BRKA,bull
20170210 10:02,BRKB,bull
20170210 10:04,FB,bull
20170210 10:09,CERN,bear
20170210 10:40,ATVI,bull
20170210 10:41,ATVI,bull
20170210 10:55,MU,bull
20170210 11:13,CS,bull
20170210 11:13,NVDA,bull
20170210 11:13,INTC,bull
20170210 11:13,TSLA,bull
20170210 11:14,NVDA,bull
20170210 11:23,NVDA,bull
20170210 11:30,IMPV,bull
20170210 11:30,ORCL,bull
20170210 11:47,GS,bull
20170210 12:21,ZNGA,bull
20170210 12:31,CTSH,bull
20170210 12:41,AAPL,bull
20170210 12:41,AMZN,bull
20170210 12:41,GOOG,bull
20170210 12:41,GOOGL,bull
20170210 12:44,MCHP,bull
20170210 13:02,CSCO,bull
20170210 13:31,AAPL,bear
20170210 13:31,GOOG,bear
20170210 13:31,GOOGL,bear
20170210 13:35,GS,bull
20170210 14:00,CTSH,bull
20170210 14:49,AAPL,bull
20170210 15:00,TRI,bull
20170210 15:00,ATVI,bear
20170210 15:09,AMZN,bull
20170210 15:22,AMZN,bear
20170210 15:23,MSFT,bear
20170210 15:23,BLK,bear
20170210 15:23,STT,bear
20170210 15:23,PNC,bear
20170210 15:30,FB,bull
20170213 08:27,AAPL,bull
20170213 08:39,AAPL,bear
20170213 08:39,IBM,bear
20170213 08:39,STT,bear
20170213 08:39,BRKA,bear
20170213 08:39,BRKB,bear
20170213 09:00,BRCM,bull
20170213 09:17,IBM,bull
20170213 09:17,STT,bull
20170213 09:17,BRKA,bull
20170213 09:17,BRKB,bull
20170213 09:20,VZ,bear
20170213 09:20,BLK,bear
20170213 09:20,PNC,bear
20170213 09:22,QCOM,bear
20170213 09:22,AAPL,bear
20170213 09:47,AUO,bear
20170213 10:20,DB,bull
20170213 10:20,BLK,bull
20170213 10:20,PNC,bull
20170213 10:22,ZNGA,bear
20170213 10:49,ORCL,bear
20170213 10:49,GOOG,bear
20170213 10:49,GOOGL,bear
20170213 11:11,NVDA,bull
20170213 11:15,AAPL,bear
20170213 11:30,AAPL,bull
20170213 11:46,UBS,bull
20170213 11:46,PNC,bull
20170213 11:46,BLK,bull
20170213 11:46,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:13,MBLY,bear
20170213 12:30,GOOG,bear
20170213 12:30,GOOGL,bear
20170213 12:30,NTES,bear
20170213 12:44,AAPL,bull
20170213 12:44,IBM,bull
20170213 12:44,STT,bull
20170213 12:44,BRKA,bull
20170213 12:44,BRKB,bull
20170213 12:44,NUAN,bear
20170213 12:56,MBLY,bull
20170213 13:30,ATVI,bear
20170213 14:24,AMAT,bull
20170213 14:45,AAPL,bull
20170213 14:59,YHOO,bear
20170213 14:59,BLK,bear
20170213 14:59,PNC,bear
20170213 14:59,VZ,bear
20170213 14:59,GM,bear
20170213 15:00,TGT,bear
20170213 15:00,MSFT,bear
20170213 15:00,BLK,bear
20170213 15:00,STT,bear
20170213 15:00,PNC,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:01,STT,bull
20170213 15:01,BRKA,bull
20170213 15:01,BRKB,bull
20170213 15:30,AMZN,bull
20170214 08:00,TXN,bull
20170214 08:10,AMZN,bull
20170214 08:17,SPWR,bull
20170214 08:17,TOT,bull
20170214 08:51,CSCO,bull
20170214 08:56,IBM,bull
20170214 08:56,STT,bull
20170214 08:56,BRKA,bull
20170214 08:56,BRKB,bull
20170214 09:00,AAPL,bear
20170214 09:00,IBM,bear
20170214 09:00,STT,bear
20170214 09:00,BRKA,bear
20170214 09:00,BRKB,bear
20170214 09:05,FB,bear
20170214 09:15,TD,bull
20170214 09:15,AAPL,bull
20170214 09:22,VZ,bull
20170214 09:22,BLK,bull
20170214 09:22,PNC,bull
20170214 09:22,GM,bull
20170214 09:27,NTAP,bull
20170214 09:33,MU,bear
20170214 10:00,QCOM,bear
20170214 10:00,AAPL,bear
20170214 10:30,ZNGA,bear
20170214 10:38,WDC,bull
20170214 10:44,GOOG,bear
20170214 10:44,GOOGL,bear
20170214 11:00,QCOM,bull
20170214 11:00,AAPL,bull
20170214 11:30,AAPL,bear
20170214 12:26,MBLY,bull
20170214 12:30,MBLY,bull
20170214 12:30,GM,bull
20170214 13:00,NUAN,bear
20170214 13:30,ON,bull
20170214 13:39,ORCL,bull
20170214 13:39,AMZN,bull
20170214 14:00,NVDA,bear
20170214 14:46,FB,bull
20170214 14:55,NOK,bull
20170214 15:17,LOGM,bull
20170214 15:30,INTC,bull
20170214 15:30,IBM,bull
20170214 15:30,STT,bull
20170214 15:30,BRKA,bull
20170214 15:30,BRKB,bull
20170214 15:33,ADSK,bull
20170214 15:41,BBRY,bear
20170214 15:41,NOK,bear
20170215 08:01,ADI,bull
20170215 08:22,ADI,bull
20170215 08:30,SPWR,bull
20170215 08:30,TOT,bull
20170215 08:45,ADI,bull
20170215 08:48,GRPN,bull
20170215 09:00,CSCO,bull
20170215 09:30,VZ,bull
20170215 09:30,BLK,bull
20170215 09:30,PNC,bull
20170215 09:30,GM,bull
20170215 10:00,TD,bull
20170215 10:00,AAPL,bull
20170215 10:03,CSCO,bull
20170215 10:54,YHOO,bear
20170215 10:54,BLK,bear
20170215 10:54,PNC,bear
20170215 10:54,VZ,bear
20170215 10:54,GM,bear
20170215 11:00,WDC,bull
20170215 11:12,MSFT,bull
20170215 11:12,BLK,bull
20170215 11:12,STT,bull
20170215 11:12,PNC,bull
20170215 11:12,TSLA,bull
20170215 11:16,AMZN,bull
20170215 11:30,ATVI,bull
20170215 11:40,AAPL,bull
20170215 12:07,INTC,bull
20170215 12:07,SNE,bull
20170215 12:48,VZ,bear
20170215 12:48,BLK,bear
20170215 12:48,PNC,bear
20170215 12:48,YHOO,bear
20170215 13:36,TU,bull
20170215 13:36,BCE,bull
20170215 13:49,IRDM,bear
20170215 13:51,SAP,bull
20170215 13:51,BLK,bull
20170215 13:51,PNC,bull
20170215 13:51,ORCL,bull
20170215 14:34,SSYS,bear
20170215 14:38,ATVI,bear
20170215 14:48,GM,bear
20170215 15:22,CSCO,bull
20170215 15:22,JNPR,bull
20170215 15:32,MSFT,bear
20170215 15:32,BLK,bear
20170215 15:32,STT,bear
20170215 15:32,PNC,bear
20170216 08:03,CSCO,bull
20170216 09:00,TWC,bull
20170216 09:57,AAPL,bear
20170216 10:29,CSCO,bear
20170216 10:35,MSFT,bear
20170216 10:35,BLK,bear
20170216 10:35,STT,bear
20170216 10:35,PNC,bear
20170216 10:35,AMZN,bear
20170216 12:23,SPWR,bull
20170216 12:23,TOT,bull
20170216 12:24,INFY,bull
20170216 12:29,GOOG,bear
20170216 12:29,GOOGL,bear
20170216 12:30,ANGI,bull
20170216 12:49,CSCO,bull
20170216 12:52,AMZN,bull
20170216 13:30,AAPL,bear
20170216 14:00,SAP,bull
20170216 14:00,BLK,bull
20170216 14:00,PNC,bull
20170216 14:00,ORCL,bull
20170216 14:20,AAPL,bull
20170216 14:23,SIRI,bull
20170216 14:23,BRKA,bull
20170216 14:23,BRKB,bull
20170216 15:00,NTES,bear
20170217 08:00,ADBE,bear
20170217 08:30,CSCO,bull
20170217 08:37,CY,bear
20170217 09:55,MU,bull
20170217 09:59,F,bull
20170217 10:19,SINA,bear
20170217 10:23,TSLA,bull
20170217 10:30,CSCO,bear
20170217 11:00,MSFT,bear
20170217 11:00,PNC,bear
20170217 11:00,BLK,bear
20170217 11:00,STT,bear
20170217 11:00,AMZN,bear
20170217 11:12,TTM,bull
20170217 11:30,VZ,bull
20170217 11:30,BLK,bull
20170217 11:30,PNC,bull
20170217 11:30,GM,bull
20170217 13:00,AMZN,bull
20170217 13:32,JPM,bear
20170217 13:38,GS,bull
20170217 13:57,YNDX,bear
20170217 14:00,TRIP,bull
20170217 14:08,SIRI,bull
20170217 14:08,BRKA,bull
20170217 14:08,BRKB,bull
20170217 14:59,CY,bull
20170217 15:49,GM,bull
20170217 15:49,TRI,bull
20170217 15:50,VZ,bear
20170217 15:50,BLK,bear
20170217 15:50,PNC,bear
20170217 15:50,GM,bear
20170220 09:14,FSLR,bull
20170220 10:41,BBRY,bull
20170220 10:41,F,bull
20170220 12:32,YNDX,bear
20170220 13:17,AAPL,bull
20170220 14:01,AAPL,bear
20170220 14:01,IBM,bear
20170220 14:01,STT,bear
20170220 14:01,BRKA,bear
20170220 14:01,BRKB,bear
20170220 14:03,IBM,bull
20170220 14:03,STT,bull
20170220 14:03,BRKA,bull
20170220 14:03,BRKB,bull
20170221 08:00,VZ,bull
20170221 08:00,BLK,bull
20170221 08:00,PNC,bull
20170221 08:00,GM,bull
20170221 08:16,VZ,bear
20170221 08:16,BLK,bear
20170221 08:16,PNC,bear
20170221 08:16,YHOO,bear
20170221 08:27,MBLY,bear
20170221 08:57,MSFT,bull
20170221 08:57,BLK,bull
20170221 08:57,STT,bull
20170221 08:57,PNC,bull
20170221 09:28,MBLY,bull
20170221 09:30,FSLR,bull
20170221 10:45,AAPL,bull
20170221 11:21,AAPL,bull
20170221 11:21,MS,bull
20170221 12:13,VZ,bear
20170221 12:13,PNC,bear
20170221 12:13,BLK,bear
20170221 12:13,GM,bear
20170221 12:18,GOOG,bull
20170221 12:18,GOOGL,bull
20170221 12:31,IBM,bull
20170221 12:31,STT,bull
20170221 12:31,BRKA,bull
20170221 12:31,BRKB,bull
20170221 12:45,AMZN,bear
20170221 12:45,ORCL,bear
20170221 12:50,CHU,bull
20170221 13:21,AAPL,bear
20170221 13:21,QCOM,bear
20170221 13:35,MSFT,bull
20170221 13:35,PNC,bull
20170221 13:35,BLK,bull
20170221 13:35,STT,bull
20170221 13:57,bidu,bull
20170221 13:57,AAPL,bull
20170221 14:28,AAPL,bear
20170221 14:30,IBM,bull
20170221 14:30,STT,bull
20170221 14:30,BRKA,bull
20170221 14:30,BRKB,bull
20170221 14:35,ERIC,bull
20170221 14:35,VZ,bull
20170221 14:35,BLK,bull
20170221 14:35,PNC,bull
20170221 15:09,bidu,bull
20170221 15:10,GS,bear
20170221 15:30,AAPL,bear
20170222 08:30,VZ,bear
20170222 08:30,BLK,bear
20170222 08:30,PNC,bear
20170222 08:30,GOOG,bear
20170222 08:30,GOOGL,bear
20170222 08:38,AMZN,bull
20170222 09:00,MSFT,bull
20170222 09:00,PNC,bull
20170222 09:00,BLK,bull
20170222 09:00,STT,bull
20170222 09:04,GOOG,bull
20170222 09:04,GOOGL,bull
20170222 09:23,DISH,bear
20170222 09:33,VZ,bull
20170222 09:33,BLK,bull
20170222 09:33,PNC,bull
20170222 09:54,RUBI,bear
20170222 10:10,GRMN,bear
20170222 10:45,AMD,bull
20170222 10:50,FSLR,bear
20170222 11:00,AAPL,bull
20170222 11:45,AMD,bull
20170222 11:45,INTC,bull
20170222 11:45,FB,bull
20170222 12:07,AAPL,bear
20170222 12:07,GOOG,bear
20170222 12:07,GOOGL,bear
20170222 12:18,AAPL,bull
20170222 12:34,GOOG,bull
20170222 12:34,GOOGL,bull
20170222 12:38,AAPL,bull
20170222 12:51,CRM,bull
20170222 13:15,AMZN,bear
20170222 13:15,WMT,bear
20170222 13:15,STT,bear
20170222 13:30,AAPL,bear
20170222 13:30,QCOM,bear
20170222 13:44,STI,bull
20170222 13:44,Fit,bull
20170222 13:51,INTC,bull
20170222 13:51,AMD,bull
20170222 14:00,bidu,bull
20170222 14:37,BBRY,bull
20170222 15:26,FB,bull
20170222 15:30,bidu,bull
20170222 15:49,FB,bear
20170223 08:15,NVDA,bear
20170223 08:16,AAPL,bear
20170223 08:33,MS,bull
20170223 09:01,ZBRA,bull
20170223 09:29,INTC,bear
20170223 09:29,QCOM,bear
20170223 09:29,AAPL,bear
20170223 09:30,AMZN,bull
20170223 11:00,AMAT,bull
20170223 12:08,MU,bull
20170223 12:15,SYMC,bear
20170223 12:36,Fit,bull

		"""
		
		self.trade_data = {} # dict gives constant-time key lookup
		equities = Set()
		
		# strip off seconds, we have minute granularity for now
		# 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]
				
				if not slice.ContainsKey(symbol): continue
				
				stock_price = slice[symbol].Value
				
				# self.Log(repr(self.Identity(symbol).ToString()))
				
				# cash = min(self.Portfolio.CashBook.Values[0].Amount, 100000)
				self.Log(str(stock_price))
				
				if True: # stock_price > 0:
					# qty = int(Decimal(cash) // Decimal(stock_price)) / 2
					
					# self.Log("Making trade on %s - %s %s" % (timekey, symbol, strategy))
	
					if strategy == "bull":
						newTicket = self.MarketOrder(symbol, 100, asynchronous = False)
						# self.SetHoldings(symbol, 0.5)
					elif strategy == "bear":
						newTicket = self.MarketOrder(symbol, -100, asynchronous = False)
						# self.SetHoldings(symbol, -0.5)