Overall Statistics
Total Trades
9444
Average Win
0.11%
Average Loss
-0.04%
Compounding Annual Return
1.677%
Drawdown
39.200%
Expectancy
0.127
Net Profit
19.740%
Sharpe Ratio
0.166
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
2.59
Alpha
0.02
Beta
0.099
Annual Standard Deviation
0.142
Annual Variance
0.02
Information Ratio
-0.044
Tracking Error
0.222
Treynor Ratio
0.237
Total Fees
$30888.70
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(2002, 1, 3)
        self.SetEndDate(2016, 12, 1)
        self.SetCash(1000000)

        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction)
        self._numberOfSymbols = 100
        self._numberOfTradings = 10

        self._LastMonth = -1
        self._LastDay = -1
        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._values:
			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.Log("This is sym: {0}".format(i.Value.Symbol))
				self._values[i.Value.Symbol] = self.Securities[i.Value.Symbol].Price
			self._LastDay = self.Time.date()
			self._LastMonth = self.Time.month
		else:
			if self.Time.month != self._LastMonth:
				self._LastMonth = self.Time.month
					
				returns = {}
				for stock in self._stocks:
					newPrice = self.Securities[stock].Price
					returns[stock] = newPrice/self._values[stock]

				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()
				return
			delta = self.Time.date() - self._LastDay
			if delta.days >= 7:
				for stock in self._stocks:
					if self.Portfolio[stock].IsLong:
						self.SetHoldings(stock, 0.5/self._numberOfTradings)
					if self.Portfolio[stock].IsShort:
						self.SetHoldings(stock, -0.5/self._numberOfTradings)
				self._LastDay = self.Time.date()