Hello.
IEnumerable<Symbol> MyCoarseFilterFunction(IEnumerable<CoarseFundamental> coarse) {
// Linq makes this a piece of cake;
var stocks = (from c in coarse
let avg = _stateData.GetOrAdd(c.Symbol, sym => new SelectionData(200))
where avg.Update(c.EndTime, c.AdjustedPrice)
where c.DollarVolume > 1000000000 &&
c.Price > avg.Ema
orderby c.DollarVolume descending
select c.Symbol).Take(10).ToList();
return stocks;
}
I would like to generate an EMA using the high prices for the day as opposed to the adjusted price (adjusted Close?) for use in my coarse filter. How can I pass the high to the Update method? I tried using 'History<TradeBar>(c.Symbol, 1, Resolution.Daily).First().High' in place of 'c.AdjustedPrice', but then I get "Runtime Error: Sequence contains no elements."
Any suggestions? Thanks!
Alexandre Catarino
Hi kgmanager ,
Since the CoarseFundamental object doesn't have a "High" property with the previous day high, the algorithm will have to make history requests on every MyCoarseFilterFunction call. It's not an issue for backtesting, but making over 9000 historical requests in live mode will probably time out. Consequently, you will need to filter out some securities before the history request call. Here is an example of how this can be implemented:
private IEnumerable<Symbol> MyCoarseFilterFunction(IEnumerable<CoarseFundamental> coarse) { var prices = (from c in coarse where c.DollarVolume > 1000000000 orderby c.DollarVolume descending select c) // We can increase this number to 20k in backtest to include all securities .Take(3000) .ToDictionary(k => k.Symbol, v => v); // Historical data request to warm up the indicator(s) in SelectionData // and/or add new item to _stateData History(prices.Keys, 200, Resolution.Daily) .PushThrough(bar => { SelectionData avg; if (_stateData.TryGetValue(bar.Symbol, out avg)) { avg.Reset(); } else { avg = new SelectionData(200); _stateData[bar.Symbol] = avg; } avg.Update(bar.EndTime, ((TradeBar)bar).High); // Update the EMA with High }); // Only consider Adjusted Price above EMA and get the securities with higher dollar volume return (from kvp in prices let c = kvp.Value let avg = _stateData[kvp.Key] where c.AdjustedPrice > avg.Ema orderby c.DollarVolume descending select c.Symbol).Take(10); }
You will need to implement SelectionData.Reset to reset the EMA in SelectionData before updating with the historical data.
This method can be smarter! For instance, for invested securities, we can make a history request for just one data point and verify whether they should remain invested. If all should remain invested, this method could just return Universe.Unchanged.
Kgmanager
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!