Hello there! I'm new to QuantConnect and this is my first post. I am trying to figure out how to use coarse universe selection to give me a basket of stocks to work with each day. Specifically I would like to filter stocks by looking at the values of various indicators, kind of like a stock scanner. I found the EMA cross universe selection example which I have been using as a starting point. However, it looks like the CoarseFundamental object has only one price point. Unless I'm missing something, this limits my filtering options when it comes to using indicators. How would I go about filtering by an indicator like ATR (or NATR), which would require more information, specifically the open, close, high, and low?
Alexandre Catarino
Currently, Coarse Universe selection only have closing price:
class CoarseFundamental { public long Volume; // Traded shares public decimal DollarVolume; // Traded shares x Price public decimal Price; // Yesterday close price public Symbol Symbol; // Asset symbol }
In order to use bar indicators (those that need OHLC), you will need to add securities to the universe. When securities are added and removed from an universe, OnSecuritiesChanged event is triggered and we can make a History Request and update the bar indicator:
private List<Symbol> _allowedSymbols; public override void OnSecuritiesChanged(SecurityChanges changes) { // Create a list of ATR var atrList = new List<AverageTrueRange>(); foreach (var security in changes.AddedSecurities) { // Creat an ATR and update it with Historical data var atr = new AverageTrueRange(security.Symbol, 14); foreach (var bar in History(security.Symbol, 20, Resolution.Daily)) { atr.Update(bar); } atrList.Add(atr); } // On the two symbols with the lowerest ATR are allowed to be traded _allowedSymbols = atrList.OrderBy(x => x).Select(x => x.Name).Take(2).ToList(); }
Finally in OnData, we will use _allowedSymbols filter which symbols to trade.
Reverb
Thanks Alexandre. I gave up trying to do any kind of advanced filtering with universe selection, and now I'm focusing on keeping track of indicators in a custom wrapper class and doing filtering in OnData, like you suggested. I ended up using a similar approach, guided mostly by your responses to another question about universe + history. I've found your code snippets to be very helpful!
Reverb
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!