Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do i translate a list of stocks into the format Quantopian wants?

Hello all,

I have ended up with a list of stocks that I now want to order, but they are not in Quantopian-orderable format. Here is the list of 2 stocks as of 3:50 pm:

2016-10-06 15:50 PRINT [Equity(41271, symbol=u'STAG', asset_name=u'STAG INDUSTRIAL INC', exchange=u'NYSE', start_date=Timestamp('2011-04-15 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-10-07 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2016-10-12 00:00:00+0000', tz='UTC'), exchange_full=u'NEW YORK STOCK EXCHANGE'), Equity(45656, symbol=u'GLPI', asset_name=u'GAMING AND LEISURE PROPERTIES INC', exchange=u'NASDAQ', start_date=Timestamp('2013-10-11 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-10-07 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2016-10-12 00:00:00+0000', tz='UTC'), exchange_full=u'NASDAQ GLOBAL SELECT MARKET')]

How do I order those two stocks, STAG and GLPI?

Thanks,

ted

3 responses

What is the line of code that prints this out?

My code searches for stocks that have negative performance with rising volume at 3:50 pm. A list of qualifying stocks gets selected. Those stocks are then sorted by average performance to arrive at the two top buys. The problem is that the list of qualifiers is a dictionary where the individual stocks can be ordered, while the sorted top two stocks are a list where the individual stocks are no longer orderable.

Here is the code:

def my_rebalance(context,data):  
    numBars = 6  
    price_history = data.history(context.security_list, ["price", "volume"], bar_count=numBars, frequency="1d")  
    fBuys = {}  
    for s in context.security_list:  
        avgPerf = price_history["price"][s].pct_change().mean()  
        fPerfSignal = (price_history["price"][s].iloc[-1] / price_history["price"][s].iloc[-1] - 1) - price_history["price"][s].pct_change().mean()  
        fVolSignal = price_history["volume"][s].iloc[-1] / price_history["volume"][s].iloc[1:].mean()  
        if fPerfSignal < -0 and fVolSignal > 1.5:  
            fBuys[s] = avgPerf  
#the keys in fBuys are Quantopian stocks that can be ordered  
    sorted_fBuys = sorted(fBuys.items(), key=operator.itemgetter(1))  
    long_list = [i[0] for i in sorted_fBuys[:2]]  
#long_list is a list of those keyed stocks that can no longer be ordered directly  
    print long_list  

I guess I need to parse the list results and rebuild the stocks as Quantopian objects that can be ordered?

ted

MY ERROR - turns out you can order just fine from a list. This works:

    order_target_percent(long_list[0], 0.5)  

while this worked for a dictionary:

    order_target_percent(fBuys.keys()[0], 0.5)