Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How To Retrieve the Current Return on Day

What's the easiest method to retrieve the current return on day?

To provide additional clarity on what I am looking for; let's say:
Current_Price = Closing Price of Most Recent Minute
Open_Price = Today's Open Price (closing price of first minute of the day is less preferred, but also acceptable)
Current_Return_On_Day = (Current_Price - Open_Price) / Open_Price

Current_Return_On_Day calculated every minute is what I want.

Thank you for taking the time to look this over! I apologize if this is a dumb question (I'm an amateur). Any assistance in the matter would be greatly appreciated!

1 response

A lot of ways to do this. Here's one...


    # Set lookback length to something greater than a trading days worth of minutes  
    LOOKBACK = 7 * 60 


    # Get all the open prices for today and a bit of yesterday  
    open_prices = data.history(SPY, 'open', LOOKBACK, '1m')  


    # Resample to get the first open of each day and take the latest (todays) value  
    daily_open_prices = open_prices.resample('D').first()  
    todays_open = daily_open_prices[-1]  


    # Get the current close and calculate the return  
    current_close = data.current(SPY, 'close')  


    current_return_on_day = (current_close - todays_open) / todays_open

Check out the attached backtest and note the logs. At the first minute of trading the current open equals the todays open. After that, todays_open remains the same.