Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
handle_data(context, data) to fetch hourly data....

Hi, currently I have an algorithm to monitor a pipeline every minute.... how would I modify this

def handle_data(context, data):
today_dt = get_datetime('US/Eastern');
now = today_dt.time();

to have it monitor a pipeline/universe every hour?

8 responses

Insert after the above code

if now.minute != 17: return  

if you want to trade only after the 17th minute of each hour.

Thanks Andre
I think what I meant was, how do I monitor a universe on an hourly basis?
So not neccessarily on the 17th minute, but every minute, looking for hourly actions. For example, to return a list of etfs trading above 70 RSI over the last hour.

I don't need the logic to set the universe or pipeline, simply the logic to make it change from minute detection to hourly detection, over the course of the last hour of market trading.

I tried replacing it with this: and it threw an error

def handle_data(context, data):
prices = data.history(300, '1m', field='price')
weekly_prices = prices.resample('H', how='last')
today_dt = get_datetime('US/Eastern');
now = today_dt.time();

If I understand you correctly, you want to run code every minute that willcompare current prices to those from an hour before - right?

Then call history with a window length of 61 bars, and compare row [-1] of the result (the latest) with row [0] (the oldest). You shouldn't run it before an hour has passed since the market opened, or the oldest row will be from the last hour of the previous trading day, probably not what you want.

Thank you, yes, I want to run code every minute that will compare current prices to those from an hour before. its okay to call it from the last hour of the prev trading day. can you paste the example code you to refer to ?

Thank you, yes, I want to run code every minute that will compare current prices to those from an hour before. its okay to call it from the last hour of the prev trading day. can you paste the example code

# OneHourReturns160507S - calculates returns vs. one trading hour ago. (C) AP 160507S

def initialize(context):  
    context.assets=symbols('AAPL', 'MSFT', 'F')

def handle_data(context, data):  
    prices=data.history(context.assets, 'price', 61, '1m')  
    current_prices=prices.ix[-1]  
    one_hour_ago_prices=prices.ix[0]  
    ret_1h=(current_prices/one_hour_ago_prices - 1.0) # returns vs. 1 hour ago, as a decimal  

thank you!