Overall Statistics
Total Trades
9734
Average Win
0.23%
Average Loss
-0.20%
Compounding Annual Return
12.976%
Drawdown
26.400%
Expectancy
0.071
Net Profit
111.646%
Sharpe Ratio
0.635
Loss Rate
51%
Win Rate
49%
Profit-Loss Ratio
1.17
Alpha
0.113
Beta
0.167
Annual Standard Deviation
0.184
Annual Variance
0.034
Information Ratio
0.412
Tracking Error
0.223
Treynor Ratio
0.699
Total Fees
$202367.32
from clr import AddReference
AddReference("System.Core")
AddReference("System.Collections")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
import statistics
from datetime import datetime
from System.Collections.Generic import List

class ShortTimeReversal(QCAlgorithm):
	def Initialize(self):
		self.SetStartDate(2001, 1, 1)
		self.SetEndDate(2016, 12, 31)
		self.SetCash(1000000)
		
		self.UniverseSettings.Resolution = Resolution.Daily
		self.AddUniverse(self.CoarseSelectionFunction)
		self._numberOfSymbols = 100
		self._numberOfTradings = 10
		
		self._numOfWeeks = 0
		self._LastDay = -1
		self._ifWarmUp = False
		
		self._stocks = []
		self._values = {}

	def CoarseSelectionFunction(self, coarse):
		sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
		top100 = sortedByDollarVolume[:self._numberOfSymbols]
		list = List[Symbol]()
		for x in top100:
		    list.Add(x.Symbol)
		    #self.Log("This is symbol: {0}".format(x.Symbol))
		return list

	def OnData(self, data):
		if not self._ifWarmUp:
			if self._LastDay == -1:
				self._LastDay = self.Time.date()
				self._stocks = []
				self.uni_symbol = None
				symbols = self.UniverseManager.Keys
				for i in symbols:
					if str(i.Value) == "QC-UNIVERSE-COARSE-USA":
						self.uni_symbol = i
				for i in self.UniverseManager[self.uni_symbol].Members:
					self._stocks.append(i.Value.Symbol)
					self._values[i.Value.Symbol] = [self.Securities[i.Value.Symbol].Price]
			else:
				delta = self.Time.date() - self._LastDay
				if delta.days >= 7:
					self._LastDay = self.Time.date()
					for stock in self._stocks:
						self._values[stock].append(self.Securities[stock].Price)
			self._numOfWeeks += 1
			if self._numOfWeeks == 3:
				self._ifWarmUp = True
		else:
			delta = self.Time.date() - self._LastDay
			if delta.days >= 7:
				self._LastDay = self.Time.date()
				
				returns = {}
				for stock in self._stocks:
					newPrice = self.Securities[stock].Price
					oldPrice = self._values[stock].pop(0)
					self._values[stock].append(newPrice)
					returns[stock] = newPrice/oldPrice

				newArr = [(v,k) for k,v in returns.items()]
				newArr.sort()
				for ret, stock in newArr[self._numberOfTradings:-self._numberOfTradings]:
					self.SetHoldings(stock, 0)
				for ret, stock in newArr[0:self._numberOfTradings]:
					self.SetHoldings(stock, 0.5/self._numberOfTradings)
				for ret, stock in newArr[-self._numberOfTradings:]:
					self.SetHoldings(stock, -0.5/self._numberOfTradings)
				self._LastDay = self.Time.date()