The welcome screen invites users to write their first algo, and it offers a toy example (click "clone a sample algo"). This may have two bugs, and it'd be nice to fix since it's on the first page.
Price & vwap should be updated for each stock. Currently, when stock 1 goes up, it trades all 4 stocks. I think you really wanted it to trade stock 1 when 1 goes up, stock 2 when 2 goes up, etc.
The algo can exceed its notional limits. Instead, notional should be initialized across all stocks before any trades are made. One could also have created separate limits for each stock, but I don't think that's the intent (and it'd still require a code change anyway).
An update is below. Please correct if I've missed anything.
-Parker
def handle_data(data, context):
#get notional of entire portfolio
notional = 0
for stock in context.stocks:
price = data[stock].price
notional = notional + data.portfolio.positions[stock].amount * price
#trade stocks sequentially, provided stay within portfolio notional limit
for stock in context.stocks:
vwap = data[stock].vwap(3)
price = data[stock].price
if price < vwap * 0.995 and notional > context.min_notional:
order(stock,-100)
notional = notional - price*100
elif price > vwap * 1.005 and notional < context.max_notional:
order(stock,+100)
notional = notional - price*100