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))  
Clone Algorithm
16
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):

    context.security = symbol('AAPL')


def handle_data(context, data):

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

    cash = context.portfolio.cash
    
    # Here is the meat of our algorithm.
    # If the current price is 1% above the 5-day average price 
    # AND we have enough cash, then we will order.
    # If the current price is below the average price, 
    # then we want to close our position to 0 shares.
    if current_price > 0.95*average_price and cash > current_price:
        
        # Need to calculate how many shares we can buy
        number_of_shares = int(cash/current_price)
        
        # Place the buy order (positive means buy, negative means sell)
        order(context.security, +number_of_shares)
        log.info("Buying %s" % (context.security.symbol))
        

    elif current_price > average_price:
        
        # Sell all of our shares by setting the target position to zero
        order_target(context.security, 0)
        log.info("Selling %s" % (context.security.symbol))
    
    # You can use the record() method to track any custom signal. 
    # The record graph tracks up to five different variables. 
    # Here we record the Apple stock price.
    record(stock_price=data[context.security].price)
There was a runtime error.
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