Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Best EBIT/TEV from each sector

Hello,
If you could offer me a help, I would greatly appreciate. I try to trade that has best EBIT/TEV. The result is not so impressive. So I added sector code, so maybe I could only invest in the best EBIT/TEVs from each sector. In order to do so, I need the "ranked_2000" in the line 87 to be organized such that I could have the best EBIT/TEV stock from each sector. I know I am asking a lot here. I really tried hard on this one and am struggling to understand the data structure of the Pipeline outputs. Thank you

2 responses

I don't have much time to test the code, but this should put you in the right direction:

def before_trading_start(context, data):  
    [...]  
    ranked_2000 = context.output.fillna(0)  
    # build a list of available sectors  
    sectors = ranked_2000['SectorCodes'].unique()  
    context.long_list   = []  
    context.short_list = []  
    # loop through the list of sectors and pick the best/worst ones from each sector  
    for current_sector in sectors:  
        # filter out results don't belonging to current sector code  
        ranked_2000_current_sector = ranked_2000[ ranked_2000['SectorCodes'] == current_sector]  
        len_long_short = min(len(ranked_2000_current_sector), context.max_buys)  
        context.long_list.extend(   ranked_2000_current_sector.sort(['EBITTEV'], ascending=False).iloc[:len_long_short] )  
        context.short_list.extend(  ranked_2000_current_sector.sort(['EBITTEV'], ascending=True).iloc[:len_long_short] )

    [...]   

Luca,
It helped me to get to where I wanted. Thank you so much!
Ujae