Overall Statistics
Total Trades
122
Average Win
2.71%
Average Loss
-1.12%
Compounding Annual Return
29.901%
Drawdown
6.700%
Expectancy
0.400
Net Profit
29.901%
Sharpe Ratio
1.353
Loss Rate
59%
Win Rate
41%
Profit-Loss Ratio
2.42
Alpha
0.249
Beta
-0.213
Annual Standard Deviation
0.169
Annual Variance
0.028
Information Ratio
0.585
Tracking Error
0.219
Treynor Ratio
-1.072
Total Fees
$129.17
from clr import AddReference
AddReference("System.Core")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")

from System import *
from System.Collections.Generic import List
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Orders import *
from QuantConnect.Data.UniverseSelection import *
from datetime import datetime

class BasicTemplateAlgorithm(QCAlgorithm):
    def Initialize(self):
        # Set the cash we'd like to use for our backtest
        # This is ignored in live trading
        self.SetCash(2000)

        # Start and end dates for the backtest.
        # These are ignored in live trading.
        self.SetStartDate(2016, 1, 1)
        self.SetEndDate(2017, 1, 1)

        self.UniverseSettings.Resolution = Resolution.Minute
        self.AddUniverse(self.CoarseSelectionFunction)

        self._values = {}
        self._investedPrice = {}

        self.ifWarmUp = False

    def OnData(self, slice):
    	
    	if not self.ifWarmUp:
	    	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._values[i.Value.Symbol] = self.Securities[i.Value.Symbol].Price
	        self.ifWarmUp = True
	        self.Log(str(self._values))
            
        if (not self.Portfolio.Invested) and self.ifWarmUp:
            longCandidates = []
            shortCandidates = []
            for symbol in self._values.keys():
                oldPrice = self._values[symbol]
                if not oldPrice:
                	continue
                newPrice = self.Securities[symbol].Price
                increase = newPrice / oldPrice
                if increase > 1.07:
                    shortCandidates.append(symbol)
                if increase < 0.93:
                    longCandidates.append(symbol)
                self._values[symbol] = newPrice
            for symbol in longCandidates:
                self.SetHoldings(symbol, 1 / len(longCandidates))
                self._investedPrice[symbol] = self.Securities[symbol].Price
            for symbol in shortCandidates:
                self.SetHoldings(symbol, -1 / len(shortCandidates))
                self._investedPrice[symbol] = self.Securities[symbol].Price
        elif self.ifWarmUp:
            for symbol in self._values.keys():
                if self.Portfolio[symbol].IsLong:
                    newPrice = self.Securities[symbol].Price
                    oldPrice = self._investedPrice[symbol]
                    if not oldPrice:
                    	continue
                    increase = newPrice / oldPrice
                    if increase > 1.02 or increase < 0.99:
                        self.Liquidate(symbol)
                        del self._investedPrice[symbol]
                        del self._values[symbol]
                elif self.Portfolio[symbol].IsShort:
                    newPrice = self.Securities[symbol].Price
                    oldPrice = self._investedPrice[symbol]
                    if not oldPrice:
                    	continue
                    increase = newPrice / oldPrice
                    if increase > 1.01 or increase < 0.98:
                        self.Liquidate(symbol)
                        del self._investedPrice[symbol]
                        del self._values[symbol]

    def CoarseSelectionFunction(self, coarse):
        if not self.ifWarmUp:
            self.Log("Here")
            sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
            top100 = sortedByDollarVolume[:200]
            list = List[Symbol]()
            for x in top100:
                list.Add(x.Symbol)
            self.Log("ENd")
            return list