Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to create baskets of stocks for differential treatment

I have a conceptual question, that may have a simple answer but lies just beyond my googling
capacities...

I would like to query the fundamentals database for disparate "baskets of stocks"
eg df_fast_growers, df_solid_but_slow, df_knife_fallers
etc
then populate the universe with the merged list of all three

Later, as I look through my "handle_data" I would do different things
(go long/short/dollar cost average etc) to each security dependent on which basket it resides in.

I can't figure out how to first empty the universe, then populate it
with all my baskets.

Here is sample code from my winners and losers baskets, but the
losers never make it into my universe. Can't figure out what I'm doing
wrong


def before_trading_start(context):
fundamentals_df = {}
num_winners = 250
context.winners_df = get_fundamentals(
query(
# To add a metric. Start by typing "fundamentals."
fundamentals.valuation.market_cap,
#fundamentals.operation_ratios.roic
)
.filter(fundamentals.operation_ratios.roic >3)
# very high roic's suggest reits which we want to avoid
.filter(fundamentals.operation_ratios.roic 10)
# Filter where price NOT too low (penny stocks)
.filter(fundamentals.valuation_ratios.book_value_per_share >5)
.order_by(
#fundamentals.operation_ratios.roic.desc(),
fundamentals.valuation.market_cap.desc()
)
.limit(num_winners)
)
#add these values to fundamentals_df, used to populate universe of data
fundamentals_df= context.winners_df.copy()
num_losers = 250
context.losers_df = get_fundamentals(
query(
# To add a metric. Start by typing "fundamentals."
fundamentals.valuation.market_cap,
#fundamentals.operation_ratios.roic
)
# Filter where roic less than 10
.filter(fundamentals.operation_ratios.roic < -1)
.filter(fundamentals.operation_ratios.long_term_debt_equity_ratio>1)
# Filter where price too low (penny stocks)
.filter(fundamentals.valuation_ratios.book_value_per_share >1)
.order_by(
fundamentals.valuation.market_cap.desc(),
#fundamentals.operation_ratios.roic.asc()
)
.limit(num_losers)
)
fundamentals_df.append(context.losers_df)
update_universe(fundamentals_df)
if context.counter == 0:
context.counter+=1
log.info(context.winners_df.count(axis=1))
log.info(context.losers_df.count(axis=1))

2 responses

Ok. Found the solution to my own problem. Maybe this will help others:

problem was in my append syntax
"fundamentals_df.append(context.losers_df)" should have been "fundamentals_df=fundamentals_df.append(context.losers_df)"

Now the following correctly populates my universe with all stocks, but allows me to check against
context.losers_df and context.winners_df

def before_trading_start(context):
fundamentals_df = {}
num_winners = 250
context.winners_df = get_fundamentals(
query(
# To add a metric. Start by typing "fundamentals."
fundamentals.valuation.market_cap,
#fundamentals.operation_ratios.roic
)
.filter(fundamentals.operation_ratios.roic>0.20)
# very high roic's suggest reits which we want to avoid
.filter(fundamentals.operation_ratios.roic 10)
# Filter where price NOT too low (penny stocks)
.filter(fundamentals.valuation_ratios.book_value_per_share >5)
.order_by(
#fundamentals.operation_ratios.roic.desc(),
fundamentals.valuation.market_cap.desc()
)
.limit(num_winners)
)
#add these values to fundamentals_df, used to populate universe of data
fundamentals_df= context.winners_df.copy()
num_losers = 250
context.losers_df = get_fundamentals(
query(
# To add a metric. Start by typing "fundamentals."
fundamentals.valuation.market_cap,
#fundamentals.operation_ratios.roic
)
# Filter where roic less than 10
.filter(fundamentals.operation_ratios.roic < -0.10)
.filter(fundamentals.operation_ratios.long_term_debt_equity_ratio>1)
# Filter where price too low (penny stocks)
.filter(fundamentals.valuation_ratios.book_value_per_share >1)
.order_by(
fundamentals.valuation.market_cap.desc(),
#fundamentals.operation_ratios.roic.asc()
)
.limit(num_losers)
)
fundamentals_df=fundamentals_df.append(context.losers_df)

update_universe(fundamentals_df)  
if context.counter == 0:  
    context.counter+=1  
    log.info(context.winners_df.count(axis=1))  
    log.info(context.losers_df.count(axis=1))  

Thanks for posting your solution! Looks good. Next time, try to format the code properly - otherwise the indents get messed up.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.