Overall Statistics Total Trades106Average Win0.74%Average Loss-0.58%Compounding Annual Return7.014%Drawdown13.300%Expectancy0.404Net Profit10.078%Sharpe Ratio0.655Loss Rate38%Win Rate62%Profit-Loss Ratio1.26Alpha0.037Beta1.731Annual Standard Deviation0.106Annual Variance0.011Information Ratio0.477Tracking Error0.106Treynor Ratio0.04Total Fees\$207.29
```# https://quantpedia.com/Screener/Details/7
# The investment universe consists of global large cap stocks (or US large cap stocks).
# At the end of the each month, the investor constructs equally weighted decile portfolios
# by ranking the stocks on the past one year volatility of daily price. The investor
# goes long stocks with the lowest volatility.
from QuantConnect.Data.UniverseSelection import *
import math
import numpy as np
import pandas as pd
import scipy as sp

class ShortTermReversalAlgorithm(QCAlgorithm):

def Initialize(self):

self.SetStartDate(2017, 1, 1)  # Set Start Date
self.SetEndDate(2018, 6, 1)    # Set Start Date
self.SetCash(100000)           # Set Strategy Cash
self.lookback = 252

self.UniverseSettings.Resolution = Resolution.Daily
self.Schedule.On(self.DateRules.MonthStart("SPY"),self.TimeRules.AfterMarketOpen("SPY"), self.rebalance)

def CoarseSelectionFunction(self, coarse):
# drop stocks which have no fundamental data or have too low prices
selected = [x for x in coarse if (x.HasFundamentalData) and (float(x.Price) > 5)]
# rank the stocks by dollar volume
filtered = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)
return [ x.Symbol for x in filtered[:100]]

def FineSelectionFunction(self, fine):
# filter stocks with the top market cap
top = sorted(fine, key = lambda x: x.EarningReports.BasicAverageShares.ThreeMonths * (x.EarningReports.BasicEPS.TwelveMonths*x.ValuationRatios.PERatio), reverse=True)
return [x.Symbol for x in top[:50]]

def rebalance(self):
# pick 5 stocks with the lowest volatility
long_stocks = sorted_symbolData[:5]
stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
# liquidate stocks not in the list
for i in stocks_invested:
if i not in long_stocks:
self.Liquidate(i)
# long stocks with the lowest volatility
for i in long_stocks:
self.SetHoldings(i, 1/5)

def OnData(self, data):
# update the indicator value for newly added securities

self.removedSymbols = []

def OnSecuritiesChanged(self, changes):

# clean up data for removed securities
self.removedSymbols = [x.Symbol for x in changes.RemovedSecurities]
for removed in changes.RemovedSecurities:

# warm up the indicator with history price for newly added securities
self.addedSymbols = [ x.Symbol for x in changes.AddedSecurities if x.Symbol.Value != "SPY"]

symbolData = SymbolData(symbol, self.lookback)
if str(symbol) in history.index:
symbolData.WarmUpIndicator(history.loc[str(symbol)])

class SymbolData:
'''Contains data specific to a symbol required by this model'''

def __init__(self, symbol, lookback):
self.symbol = symbol
self.Price = RollingWindow[IndicatorDataPoint](lookback)

def WarmUpIndicator(self, history):
# warm up the RateOfChange indicator with the history request
for tuple in history.itertuples():
item = IndicatorDataPoint(self.symbol, tuple.Index, float(tuple.close))