| Overall Statistics |
|
Total Trades 269 Average Win 0.15% Average Loss -0.18% Compounding Annual Return -1.810% Drawdown 6.900% Expectancy -0.097 Net Profit -1.663% Sharpe Ratio -0.177 Probabilistic Sharpe Ratio 10.739% Loss Rate 51% Win Rate 49% Profit-Loss Ratio 0.83 Alpha -0.012 Beta -0.03 Annual Standard Deviation 0.07 Annual Variance 0.005 Information Ratio -0.186 Tracking Error 0.229 Treynor Ratio 0.411 Total Fees $296.82 |
from datetime import timedelta
from QuantConnect.Data.UniverseSelection import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel
class LiquidValueStocks(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2011, 1, 1)
self.SetEndDate(2011, 12, 1)
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.DateRules.MonthStart()))
self.SetExecution(ImmediateExecutionModel())
self.Settings.RebalancePortfolioOnInsightChanges = False
self.Settings.RebalancePortfolioOnSecurityChanges = False
def OnData(self, data):
self.Plot("Positions", "Number of open positions", len(self.Portfolio))
class LiquidValueUniverseSelectionModel(FundamentalUniverseSelectionModel):
def __init__(self):
super().__init__(True, None, None)
self.lastMonth = -1
def SelectCoarse(self, algorithm, coarse):
if self.lastMonth == algorithm.Time.month:
return Universe.Unchanged
self.lastMonth = algorithm.Time.month
sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData],
key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in sortedByDollarVolume[:100]]
def SelectFine(self, algorithm, fine):
sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
universe = sortedByYields[:10] + sortedByYields[-10:]
return [f.Symbol for f in universe]
# Define the LongShortAlphaModel class
class LongShortEYAlphaModel(AlphaModel):
def __init__(self):
self.lastMonth = None
def Update(self, algorithm, data):
insights = []
#2. If else statement to emit signals once a month
if self.lastMonth == algorithm.Time.month:
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(1), direction))
return insights