Back to Community
Converting Lists to DataFrame

I am looking to use the intersection for small_value and big_growth below as my defined long and short lists. Essentially, I am looking to:

  • Only go long small_value that have earnings surprise beats
  • Only go short big_growth that have earnings surprise misses

In my backtests I can't seem to get my algo to pull any long or short securities from the defined intersection in order to place orders. I am thinking it might be due to the small_value and bigh_growth being lists that need to be converted to DataFrames? I am not sure if it is this or another reason. Any help is appreciated.

def before_trading_start(context, data):
spy = sid(8554)

factors = pipeline_output('peads_ff')  
factors = factors[factors['pe'] == 1]  

# get the data we're going to use  
returns = factors['returns']  
mkt_cap = factors.sort(['market_cap'], ascending=True)  
be_me = factors.sort(['be_me'], ascending=True)  
longs = factors['longs']  
shorts = factors['shorts']  

# to compose the six portfolios, split our universe into portions  
half = int(len(mkt_cap)*0.5)  
small_caps = mkt_cap[:half]  
big_caps = mkt_cap[half:]  

thirty = int(len(be_me)*0.3)  
seventy = int(len(be_me)*0.7)  
growth = be_me[:thirty]  
neutral = be_me[thirty:seventy]  
value = be_me[seventy:]  

# now use the portions to construct the portfolios.  
# note: these portfolios are just lists (indices) of equities  
small_value = small_caps.index.intersection(value.index)  
big_growth = big_caps.index.intersection(growth.index)  

sv = longs[small_value]  
bg = shorts[big_growth]  

assets_in_universe = sv.index  
context.positive_surprise = assets_in_universe  

return context.positive_surprise  

assets_in_universe = bg.index  
context.negative_surprise = assets_in_universe  

return context.negative_surprise