Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to add more stocks

Hello this is my first algo that I am trying to build. It is obviously a very simple concept that I will try to build on in the future. I was wondering how I would add multiple stocks to the context and if I could run the same process of the five day period history on each of those stocks to determine when to buy?
The code is attached
Thanks!

def initialize(context):

context.security = sid(42950)

def handle_data(context, data):

average_price = data[context.security].mavg(5)  
current_price = data[context.security].price  


cash = context.portfolio.cash  


if current_price > 0.95*average_price and cash > current_price:  


    number_of_shares = int(cash/current_price)  


    order(context.security, +number_of_shares)  
    log.info("Buying %s" % (context.security.symbol))  


elif current_price > average_price:  


    order_target(context.security, 0)  
    log.info("Selling %s" % (context.security.symbol))  
1 response

Hello Tom,

You can simply create a Python list of your securities:

context.stocks = [sid(1),sid(2),sid(3),sid(4)]  

Then, in your code, it is a matter of looping over the list:

for stock in context.stocks:  
    # do something  

Grant