Dear Amulya,
Your understanding of what Universe Selection and Alpha Model are used for it spot on!
To select assets based on a condition, Course and Fine filters are used. For an introduction to Course filters, check out this BootCamp, for Fine filters, this BootCamp. A quick example (filtering by Share Price) is as follows:
def CoarseSelectionFilter(self, coarse):
filteredByPrice = [c.Symbol for c in coarse if c.Price > 10]
return filteredByPrice
As for selecting between assets, try setting the allocation to one asset to 0.0 (0% of cash), essentially selling all of that asset, using self.SetHoldings(“SPY”, 0.0), while setting the allocation of the other asset to 1.0 (100% of cash), with self.SetHoldings(“BND”, 1.0).
For EMA, feel free to use a pre-built EMA indicator instead of a RollingWindow. The documentation on how to use pre-built technical indicators can be found here, and our BootCamp on using Indicators as conditions can be found here. But for quick reference, here's a simple EMA algorithm:
class ModulatedTransdimensionalReplicator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 12, 14) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity('SPY')
self.ema10 = self.EMA('SPY', 10, Resolution.Daily)
self.ema20 = self.EMA('SPY', 20, Resolution.Daily)
def OnData(self, data):
if self.ema10.Current.Value > self.ema20.Current.Value:
self.SetHoldings('SPY', 1.0)
else:
self.SetHoldings('SPY', 0.0)
I have also attached a backtest for reference.
Best,
Shile Wen