Overall Statistics
Total Trades
9907
Average Win
0.01%
Average Loss
-0.01%
Compounding Annual Return
-14.168%
Drawdown
8.800%
Expectancy
-0.219
Net Profit
-7.608%
Sharpe Ratio
-2.683
Probabilistic Sharpe Ratio
0.089%
Loss Rate
63%
Win Rate
37%
Profit-Loss Ratio
1.13
Alpha
-0.114
Beta
-0.062
Annual Standard Deviation
0.046
Annual Variance
0.002
Information Ratio
-2.607
Tracking Error
0.11
Treynor Ratio
2.017
Total Fees
$9907.76
from datetime import timedelta
from QuantConnect.Data.UniverseSelection import * 
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class LiquidValueStocks(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2014, 5, 15)
        self.SetEndDate(2018, 5, 15)
        self.SetCash(100000)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverseSelection(LiquidValueUniverseSelectionModel())
        
        #1. Create and instance of the LongShortEYAlphaModel
        self.AddAlpha(LongShortEYAlphaModel())
        
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())

class LiquidValueUniverseSelectionModel(FundamentalUniverseSelectionModel):
    
    def __init__(self):
        super().__init__(True, None, None)
        self.lastMonth = -1 
        
    def SelectCoarse(self, algorithm, coarse):
        if not algorithm.Time.weekday() == 1:
            return Universe.Unchanged

        sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData],
            key=lambda x: x.DollarVolume, reverse=True)

        return [x.Symbol for x in sortedByDollarVolume[:500]]

    def SelectFine(self, algorithm, fine):
        sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
        universe = sortedByYields[:100] + sortedByYields[-100:]
        return [f.Symbol for f in universe]

# Define the LongShortAlphaModel class  
class LongShortEYAlphaModel(AlphaModel):

    def __init__(self):
        self.lastMonth = -1

    def Update(self, algorithm, data):
        insights = []
        
        #2. If else statement to emit signals once a month 
        if not algorithm.Time.weekday() == 1:
            return insights
        #self.lastMonth = algorithm.Time.month
        
        #3. For loop to emit insights with insight directions 
        # based on whether earnings yield is greater or less than zero once a month
        for security in algorithm.ActiveSecurities.Values:
            direction = 1 if security.Fundamentals.ValuationRatios.EarningYield > 0 else -1 
            insights.append(Insight.Price(security.Symbol, timedelta(7), direction)) 
        return insights