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))