Hello,
I am new to the algorithmic framework, from what I can understand is that:
Universe Selection: to select assets
alpha model: to create the trading signals
1. But, from Bootcamp I was unable to understand how can I select assets based on a condition. For example: I am trying to select between `IWM` and `SPY`. So, at a given point my portfolio will have either SPY or IWM.
2. For the alpha model, how can we use the rolling window for EMA?
Thank you.
Shile Wen
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
Shile Wen
Hi Amulya,
Sorry for the late reply. To print items in general, we would typically use self.Debug. However, when Universe Selection is done through a separate class (for a separation of concerns), in this case, we will call need Debug using the algorithm parameter passed in through the SelectFine function (so it would be algorithm.Debug). However, keep in mind that there are over 8000 symbols, so this is not usually recommended. This is shown in the code of the attached backtest.
As for using indicators with Universe Selection, feel free to check out our BootCamp, just be sure to change EMA to SMA, and then check out our documentation on how to use RollingWindows with indicators here. I would suggest storing the SMA values in RollingWindow of size 2, then calculating slope with: slope = smaWindow[0] - smaWindow[1], where smaWindow stores the previous SMA values. This can also be found in the backtest.
Best,
Shile Wen
Shile Wen
Hi Amulya,
My apologies, I’ve accordingly attached the backtest to the previous comment. To see the stocks, replace self.Debug(self.technology) with
for security in self.technology: self.Debug(security.Symbol)
and this is used in the backtest in the previous comment, or alternatively
self.Debug([security.Symbol for security in self.technology])
Please note that this is not encouraged, and it will likely cause browser flooding.
Best,
Shile Wen
Amulya Jitendra Agarwal
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!