Back to Community
Applying Strategy to Multiple Stocks - FAIL

Forgive me if this has already been asked. How does one effectively apply an algorithm to multiple stocks? I have a strategy that works well against a pool of over 300 stocks however I'm not sure how to implement in the IDE.

For example, the strategy declares dozens of arrays and variables, however these need to be stock specific. When iterating over a list of stocks, any array that is initialized gets applied to each stock.

Will I need to declare 6000 arrays in the IDE to give each stock individual treatment, or is there an easier way?

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.

import talib  
from datetime import datetime  
import numpy as np

def initialize(context):  
    context.max_notional = 100000

    context.stocks = [symbol('AAPL'),symbol('MSFT'),symbol('SPY'),symbol('CHI'),  
                     symbol('NVDA'),symbol('PLL'),symbol('PDCO')]  
    set_benchmark(symbol('SPY'))  
    context.lookback = 500

    context.charttype = '1d'  
    context.stock_specfic_array = []#needs to be specific to given stock



def handle_data(context, data):  
    for stock in context.stocks:  
        prices_open = history(context.lookback, context.charttype, 'open_price')[stock]  
        prices_open = list(prices_open.values.flatten())    

        context.stock_specfic_array.append(prices_open[-1])  
        print context.stock_specfic_array[-1]#has data from all stocks :(  
        print stock  
8 responses

Yes I see your problem. Do you really have dozens of variables and arrays (for each stock) that you need to capture for use after the complete iteration of all of your stocks?

Yes, the strategy writes prices values to various arrays over the course of several days to a couple weeks and then evaluates them for setups.

Interesting. I'm new here so I haven't tried something like this yet. I did some Googling and couldn't find anything about memory limits on Quantopian, though I'm sure there are. If you put massive amounts of data into dozens of arrays, you may hit a memory limit issue. The only real way is to find out. You probably don't need to declare each array manually, you can use a Python Dictionary data-structure; key being the symbol/sid and value initialized to an empty array. You can do it in a for-loop in your initialize function.

Chris, does your algo really require storing prices, since you can use history() to retrieve prices anytime? Similarly, if you're storing price-based indicators, couldn't you instead recompute them whenever you need them? As long as your computations don't take too long and cause timeout errors, computing everything on demand makes your algo better able to handle restarts in live trading.

Think you can create a class and init eack stock to have it's own properties...Have not created classes in quantopian yet...I'll try it and let you know.

class MyStock:
# class variable shared by all instances

def __init__(self, stock):  
    self.name = name    # instance variable unique to each instance  
    self.stock_specfic_array=[]
# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.

import talib  
from datetime import datetime  
import numpy as np  
class myStock:  
    stock_specfic_array = []  
    def __init__(self, name):  
        self.name = name  
def initialize(context):  
    context.max_notional = 100000

    context.stocks = [symbol('AAPL'),symbol('MSFT'),symbol('SPY'),symbol('CHI'),  
                     symbol('NVDA'),symbol('PLL'),symbol('PDCO')]  
    set_benchmark(symbol('SPY'))  
    context.lookback = 500

    context.charttype = '1d'  
    context.stock_AllMySpecfic_array = []#needs to be specific to given stock



def handle_data(context, data):  
    for stock in context.stocks:  
        prices_open = history(context.lookback, context.charttype, 'open_price')[stock]  
        prices_open = list(prices_open.values.flatten())  
        #if you want to append more info for the same stock I would either add more properties to the calss or I would give different name  
        #eg: stock + '_' + i (i is just number times handle-data was called for that specific stock  
        stk = myStock(stock)  
        stk.stock_specfic_array.append(prices_open[-1])  
        context.stock_AllMySpecfic_array.append(stk)  
    # reading from stock_AllMySpecfic_array  
    for myStock in  context.stock_AllMySpecfic_array:  
        y = myStock.stock_specfic_array  




Thanks all. I believe I can work around this by moving my entire handle_data into another function and then feed it the historical data.

You might find this post relevant, it's simple design pattern for going from one to many stocks.