Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
problem isolating sid values to enter into the order function

here is my dilemma, I've taken a collection of stocks from get_fundamentals and pulled apart the sid and prices, but after I recombine them I have not figured out a way to remove only the sid value to parse into the ordering function. could somebody please provide me with some insight? (also, this is fairly rough code as I haven't tried to streamline it. I welcome any thoughts relating to the code's flow as well. )

Thanks!

def before_trading_start(context, data):

    #get a list of securities sorted by market cap  
    items = get_fundamentals(  
        query(fundamentals.valuation.market_cap,  
              )  
        .filter(fundamentals.valuation.market_cap != None)  
        .filter(fundamentals.company_reference.primary_exchange_id != "OTCPK") # no pink sheets  
#        .filter(fundamentals.asset_classification.morningstar_sector_code == 309) # Energy sector  
        .filter(fundamentals.share_class_reference.security_type == 'ST00000001') # common stock only  
        .filter(~fundamentals.share_class_reference.symbol.contains('_WI')) # drop when-issued  
        .filter(fundamentals.share_class_reference.is_primary_share == True) # remove ancillary classes  
        .filter(fundamentals.share_class_reference.is_depositary_receipt == False) # !ADR/GDR  
        .filter(fundamentals.valuation.market_cap > 1e8)  
        .order_by(fundamentals.valuation.market_cap.desc())  
        .limit(list_length)  
        )  
#    context.stocks = data[context.stocks]  
#    print 'data'  
#    print context.stocks  
    #Pull out the sids from the list of securities returned by get_fundamentals  
    column_labels = [key for key in dict(items.dtypes) if dict(items.dtypes)[key] in ['float64', 'int64']]  
#    print 'column_labels'  
#    print len(column_labels)  
    #pulling out the sid values.  
    num = len(column_labels)  
    sid_hold = []  
    j=0  
    for n in range(num):  
         j = column_labels[n].sid; sid_hold.append(j)

    #transfer the extracted sid values into context.relations  
    context.relations = []  
    context.relations = sid_hold  
    # Updates the universe to include the securities returned by get_fundamentals  
    update_universe(column_labels)  


def handle_data(context, data):  

    # Get historical data for the S&P tracking SPY ETF to stand in for the index  
    exchanges = history(10, "1d", "price")[context.spy]

    # Get historical data for the stocks returned by get_fundamentals  
    prices = history(10, "1d", "open_price")  
     # get correlation between securities from prices and indices from exchanges  
    nop = []  
    j=0  
    for i in range(prices.shape[1]):  
             j = prices.icol(i).corr(exchanges); nop.append(j)  


# Beginning of set up to make data good to use  
    numLF = len(context.relations)  
    numRF = len(nop)  
    left_frame = pd.DataFrame({'key': range(numLF),  
                               'sid': context.relations})  
    right_frame = pd.DataFrame({'key': range(numRF),  
                               'cor': nop})     

    temp = pd.merge(left_frame, right_frame, on='key', how='outer')



    neg_corr = []  
    pos_corr = []  
    temp_sid_neg = []  
    temp_sid_pos = []  
    j = 0  
    y = 0  
    for q in range(len(temp)):  
        if temp.cor[q] < 0:  
            j = temp.cor[q]; neg_corr.append(j); j = temp.sid[q]; temp_sid_neg.append(j)  
        elif temp.cor[q] > 0:  
            y = temp.cor[q]; pos_corr.append(y); y = temp.sid[q]; temp_sid_pos.append(y)  
    lsn = len(temp_sid_neg)  
    lcn = len(neg_corr)  
    lsp = len(temp_sid_pos)  
    lcp = len(pos_corr)


    TempSidNeg = pd.DataFrame({'key': range(lsn),  
                               'sid': temp_sid_neg})  
    TempCorNeg = pd.DataFrame({'key': range(lcn),  
                               'cor': neg_corr})  
    TempSidPos = pd.DataFrame({'key': range(lsp),  
                               'sid': temp_sid_pos})  
    TempCorPos = pd.DataFrame({'key': range(lcp),  
                               'cor': pos_corr})

    neg = pd.merge(TempSidNeg, TempCorNeg, on='key', how='outer')  
    pos = pd.merge(TempSidPos, TempCorPos, on='key', how='outer')  
    #sort list by the lowest to the higest correlation  
    context.holdingneg = neg.sort(['cor'], ascending=[True]).head(NumPositions)  
    context.holdingpos = pos.sort(['cor'], ascending=[False]).head(NumPositions)

#can't get sid values out to parse into order function