I just started with quantopian and I can wrap my head around indexing stock prices. I know how indexing works, [0] = beginning/start while [-1] would reference the last piece of data in the list/df or what ever
please someone shed some light on this, my brain if fried
In this block of code the current stock price is referenced as [-1] and the previous day is as [-2]
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)
In this block of code todays New Price is referenced as [-1] while the Old price is referenced as [0]
def initialize(context):
# AAPL, MSFT, and SPY
context.securities = [sid(24), sid(5061), sid(8554)]
def handle_data(context, data):
prices = data.history(context.securities, "price", bar_count=10, frequency="1d")
pct_change = (prices.ix[-1] - prices.ix[0]) / prices.ix[0]
log.info(pct_change)
then again using the iloc, where I would guess the New Price is [0] and the Old Price is [-1]
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=10, frequency="1d")
pct_change = price_history.iloc[[0, -1]].pct_change()
log.info(pct_change)