Different universes with same code ran twice ?

Hi, I run this code here to pull the stocks within the 10th and 15th percentile of dolarvolume.


def universe_filter_course(self, coarse_data):
columns = ['Price', 'DollarVolume', 'HasFundamentalData', 'Volume']

data_df = pd.DataFrame.from_records(
[[getattr(s, name) for name in columns] for s in coarse_data],
index=[s.Symbol for s in coarse_data],

data_df = data_df.query("HasFundamentalData " \
"and (DollarVolume > @data_df.DollarVolume.quantile(.10))" \
"and (DollarVolume < @data_df.DollarVolume.quantile(.15))")
data_df = data_df[:10]

Although, when I run the exact same algo twice in a row, I get different results/universes:


This is very strange to me as the numbers are the same but the tickers change (???)

Also, as you can see, some tickers have the symbol and the unique ID and others dont.

What am I doing wrong ?

Thanks !

Update Backtest

The issue is you didn't sort the coarse fundamental objects. The engine loads files in parallel, so the order of "coarse_data" is non-deterministic. Each time you construct the data frame with index=[s.Symbol for s in coarse_data], the order of symbols are different. When you filter symbols by criteria, you need to sort the data frame "data_df" by properties like DollarVolume, Volume or AdjustedPrice. 

Otherwise, when there are more than ten symbols satisfy you filter conditions, data_df[:10] doesn't know how to choose the ten from the universe unless you specify the ranking criteria.

For symbol question, index=[s.Symbol for s in coarse_data] returns a list of symbol object, python dataframe convert those symbol object to the string format. They are not the real ticker but str(object). To get the string ticker, you can use [s . Symbol.Value for s in coarse_data].


Update Backtest


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.


This discussion is closed