Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
data.history help - SPY Price after 1 hour

Hello everyone,

I am new to quantopian and I am having some issues with data.history. I am trying to get the price of the SPY ETF 1 hour after the market opens. Does anyone know how to do that?

I saw on the overview this piece on info on the history section, but I do not know how to use it

start (str or pd.Timestamp) – String or Timestamp representing a start date or start intraday minute for the returned data.

6 responses

This is the code I used for the algorithm. Any advice?

def initialize(context):

context.sandp = sid(8554)  
#above is the s&p 500 ETF Trust  

schedule_function(get_history, date_rules.every_day(), time_rules.market_open(minutes=60))  

def get_history(context, data):
print get_datetime('US/Eastern')
print data.history(context.sandp, fields = 'price', bar_count = 15, frequency = '1d')

Try this:

def initialize(context):  
    schedule_function(get_history, date_rules.every_day(), time_rules.market_open(minutes = 60))    

def get_history(context, data):  
    stock = symbol('SPY')  
    print data.current(stock, 'price')  

Vladimir,
One more question, I was trying to get the past 15 days of the of the SPY price at 10:30am EST (1 hour after the market opens), not just the current price at 10:30am. Do you know how to do that?

Thanks for the help!!

This may help you:

import numpy as np

def initialize(context):  
    schedule_function(get_history, date_rules.every_day(), time_rules.market_open(minutes = 60))  
    context.prices_hour_after_open = np.array([])

def get_history(context, data):  
    stock = symbol('SPY'); period = 15

    price_hour_after_open = data.current(stock, 'price')  
    context.prices_hour_after_open = np.append(context.prices_hour_after_open, price_hour_after_open)

    if len(context.prices_hour_after_open) < period: return  
    print context.prices_hour_after_open[-period:]  

I like using the pandas 'between_time' method for getting a single time (or range of times) for each day. Something like this should get the 10:30 prices of SPY for the last 15 days (there are 390 minutes in a full trading day so 15*390 will fetch 15 days worth of minutes)

stock = symbol('SPY')  
prices =  data.history(stock, fields="price", bar_count=15*390, frequency="1m")  
prices_10_30 = prices.tz_convert('US/Eastern').between_time('10:30', '10:30')

See the attached notebook. Good luck.

(updated above with corrected code to get minute data and not daily data)

Perfect!

Thanks Vladimir and Dan!