from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel
class RedGhhost(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 12)
self.SetEndDate(2021, 1, 15)
self.SetCash(100000)
self.AddUniverseSelection(UniverseModule())
self.UniverseSettings.Resolution = Resolution.Daily
def OnData(self, slice):
self.Debug(slice.Bars)
class UniverseModule(FundamentalUniverseSelectionModel):
def __init__(self, filterFineData = True, universeSettings = None):
super().__init__(filterFineData, universeSettings)
self.averages = {}
'''
Filter by price
'''
def SelectCoarse(self, algorithm, coarse):
return [
x.Symbol for x in coarse if (
x.Price > 10
and x.HasFundamentalData
)
]
'''
Filter by min market cap and share count
'''
def SelectFine(self, algorithm, fine):
MIN_MKT_CAP = 1 * 10 ^ 9
MIN_SHARE_COUNT = 1 * 10 ^ 9
MAX_BARS_BACK = 200
initialFilter = [
x.Symbol for x in fine if (
x.FinancialStatements.BalanceSheet.OrdinarySharesNumber.Value >= MIN_SHARE_COUNT
and x.MarketCap >= MIN_MKT_CAP
)
]
historySlices = algorithm.History(initialFilter, MAX_BARS_BACK, Resolution.Daily)
for (symbol, time), history in historySlices.groupby(level=[0,1]):
if symbol not in self.averages:
self.averages[symbol] = SymbolData(symbol, history)
avg = self.averages[symbol]
avg.update(time, history.loc[symbol].close.iloc[-1])
upTrending = list(filter(lambda x: x.isUptrend, self.averages.values()))
finalSymbols = [x.symbol for x in upTrending]
return finalSymbols
class SymbolData(object):
def __init__(self, symbol, history):
self.symbol = symbol
self.longTerm = ExponentialMovingAverage(200)
self.mediumTerm = ExponentialMovingAverage(100)
self.isUptrend = False
for time, row in history.loc[self.symbol].iterrows():
self.longTerm.Update(time, row.close)
self.mediumTerm.Update(time, row.close)
def update(self, time, value):
self.longTerm.Update(time, value)
self.mediumTerm.Update(time, value)
longTerm = self.longTerm.Current.Value
mediumTerm = self.mediumTerm.Current.Value
self.isUptrend = (
mediumTerm > longTerm
)