The alg below is from the 'help' section and it compares yesterdays closing price to todays current price and places an order of 20 shares if the current price is higher. Can someone please explain why/how [-2] refers to yesterday's closing price and [-1] refers to today's current price? It doesn't make a lot of sense to me because I can't see the connection, and it isn't explained anywhere. I'm assuming that [-1] and [-2] are both parameters being fed into the "price" field, but I may be wrong.
def initialize(context):
# AAPL, MSFT, and SPY
context.securities = [sid(24), sid(5061), sid(8554)]
def handle_data(context, data):
price_history = data.history(context.securities, "price", bar_count=2, frequency="1d")
for s in context.securities:
prev_bar = price_history[s][-2]
curr_bar = price_history[s][-1]
if curr_bar > prev_bar:
order(s, 20)