Hi guys,
I'm one of the folks just migrated from Quantopian. I've completed the bootcamp and I was starting converting my algos to the quantconnect framework.
I'm just struggling now with the following stock selection method, to filter and sort a fixed amount of stocks, from the Q1500 universe. Sorting should be done for a set of multiple parameters (LongTermDebt, cash ret, yield, etc.)
universe = Q1500US()
cash_return = ms.cash_return.latest.rank(mask=universe)
fcf_yield = ms.fcf_yield.latest.rank(mask=universe
roic = ms.roic.latest.rank(mask=universe)
ltd_to_eq = ms.long_term_debt_equity_ratio.latest.rank(mask=universe)
value = (cash_return + fcf_yield).rank()
quality = ltd_to_eq
# Momentum Factor
returns_overall = Returns(window_length=context.MomentumDays)
returns_recent = Returns(window_length=context.MomentumRecendDays)
momentum = returns_overall - returns_recent
# Filters for top quality and momentum to use in our selection criteria
top_quality = quality.top(context.BestROEQTY, mask=universe)
top_quality_filter = quality.top(context.BestQualityQTY, mask=universe)
top_quality_momentum = momentum.top(context.SecuritiesToSelectQTY, mask=top_quality)
I've started using the following selection methodology via the Coarse and Fine selection, but still a bit more to go:
class LiquidValueUniverseSelectionModel(FundamentalUniverseSelectionModel):
def __init__(self):
super().__init__(True, None, None)
self.lastWeek = -1
def SelectCoarse(self, algorithm, coarse):
if not algorithm.Time.day % 7 == 0:
return Universe.Unchanged
sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData],
key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in sortedByDollarVolume[:100]]
def SelectFine(self, algorithm, fine):
if not algorithm.Time.day % 7 == 0:
return Universe.Unchanged
sortedByCashReturn = sorted(fine, key=lambda f: f.ValuationRatios.CashReturn, reverse=True)
sortedByYields = sorted(sortedByCashReturn, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
Would somebody try to help me out on how to sort for multiple fundamentals charateristics, and
Derek Melchin
Hi Simone,
In the Quantopian example above, the `value` factor is determined by ranking the cash return and yield of a security, summing these ranks, then ranking the resulting sum.
We can accomplish the same by using pandas. For example, the code below will return the top 10 securities based on the `value` feature.
factors_by_security_id = pd.DataFrame() for f in fine: factors_by_security_id.loc[str(f.Symbol), 'cash_return'] = f.ValuationRatios.CashReturn factors_by_security_id.loc[str(f.Symbol), 'earnings_yield'] = f.ValuationRatios.EarningYield universe = factors_by_security_id.rank().sum(axis=1).sort_values().index[-10:] return [algorithm.Symbol(security_identifier) for security_identifier in universe]
See the attached backtest for reference. Our Introduction to Financial Python tutorial is a great place to learn pandas.
Best,
Derek Melchin
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.
Simone Pantaleoni
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!