Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New to the forum, is that possible to get history for one or two specific stocks?
# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.

def initialize(context):  
    context.stocks = [sid(24), sid(698)]  
    context.fired = False  


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    if not context.fired:  
        historical_data = history(200, '1d', 'price')  
        print historical_data.head()  
        stops =  historical_data.iloc[-1]  
        print stops.head()  
        context.fired = True  
    else:  
        return  

This is not working.

It seems history will only pick the stocks in universe. Yet I didnt find a way to set specific stocks to universe. Maybe i miss read the doc.
Please help

2 responses

Yes, history picks the stocks currently traded. In you algo, you set the stocks in initialize(...):

context.stocks = [sid(24), sid(698)]  

So the history function will give you results for those stocks:

historical_data[sid(24)][XXX]  
historical_data[sid(698)][XXX]  

if you add more sids to context.stocks you'll get those too from history. Alternatively you can use the set_universe function to select what stocks to trade and you'll get those too from history. The last option is to select what stocks to trade using fundamentals.

yeah, i got it.
Some how there are some bugs, which doesn't show the logs initially. But now it's working.

Thanks,
Seabook