Hi All,
If we have an algorithm using coars/fine universe selection, how do we implement the idea of e.g.: price of a security from the universe is above its moving average?
It will be greately apprciated if somebody can point/give an example.
Thanks
Nik
Alexandre Catarino
Please checkout the EmaCrossUniverseSelectionAlgorithm below.
In this example, we are working only with Coarse Universe Selection:
// In Initialize: AddUniverse(CoarseUniverseFunction); public IEnumerable<Symbol> CoarseSelectionFunction( IEnumerable<CoarseFundamental> coarse) { return (from cf in coarse // grab th SelectionData instance for this symbol let avg = _averages.GetOrAdd(cf.Symbol, sym => new SelectionData()) // Update returns true when the indicators are ready, // so don't accept until they are where avg.Update(cf.EndTime, cf.Price) // only pick symbols who have their 50 day ema // over their 100 day ema where avg.Fast > avg.Slow*(1 + Tolerance) // prefer symbols with a larger delta by // percentage between the two averages orderby avg.ScaledDelta descending // we only need to return the symbol and // return 'Count' symbols select cf.Symbol).Take(Count); }
To include a Fine Universe Selection to the mix, we can use:
// In Initialize: AddUniverse(CoarseUniverseFunction, FineSelectionFunction); // For example, pick 2 stocks with highest P/E ratio public IEnumerable<Symbol> FineSelectionFunction( IEnumerable<FineFundamental> fine) { // sort descending by P/E ratio var sortedByPeRatio = fine .OrderByDescending(x => x.ValuationRatios.PERatio); // take the top 2 entries from our sorted collection var topFine = sortedByPeRatio.Take(2); // we need to return only the symbol objects return topFine.Select(x => x.Symbol); }
Each day, we will hold the 2 stocks with the highest P/E ratio from an universe of stocks that are trending upwards (EMA.Fast > EMA.Slow).
Nik milev
Alexandre, thanks!
Appreciate your quick respond :)
Nik milev
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!