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