Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to access minute-bars from past days?

Hello,

In the "before_trading_starts" function I want to process some of the (minute)data from the last days.
So for example I want to access the first 10 minute-bars from the day before yesterday and getting the high&low from each of these 10 bars.
Is that possible and how?

2 responses

Typically it's best practice to get all pricing data using pipeline. However, pipeline only fetches 'end of day' daily data. To get minute pricing one must use the data.history method (https://www.quantopian.com/docs/api-reference/algorithm-api-reference#quantopian.algorithm.interface.BarData.history)

The trick is then to extract the desired minute bars. The confounding issues are 1) a trading day may have been a half day with fewer minutes, and 2) two tradings days ago may not be the same as two calendar days ago. One way around these issues is to use the pandas between_time method to extract just the first 10 minutes of each day. Then take the first 10 rows of data. Something like this works

def before_trading_start(context, data):

    pipeline_data = pipeline_output('my_pipe')  
    universe = pipeline_data.index

    MINUTES_IN_FULL_TRADING_DAY = 390  
    AT_LEAST_TWO_TRADING_DAYS = MINUTES_IN_FULL_TRADING_DAY * 2

    highs_utc = data.history(universe, 'high', AT_LEAST_TWO_TRADING_DAYS, '1m')  
    lows_utc = data.history(universe, 'low', AT_LEAST_TWO_TRADING_DAYS, '1m')  


    # Convert from UTC to ET to account for daylight saving times  
    highs_et = highs_utc.tz_convert('US/Eastern')  
    lows_et = lows_utc.tz_convert('US/Eastern')

    # Take just those bars in first 10 minutes of trading  
    # Can't simply take first 10 bars because one of the days may have been a half trading day  
    highs_first_10_minutes = highs_et.between_time('9:31', '9:40')  
    lows_first_10_minutes = lows_et.between_time('9:31', '9:40')

    # Now take the first 10 bars  
    highs_day_before_yesterday = highs_first_10_minutes.head(10)  
    lows_day_before_yesterday = lows_first_10_minutes.head(10)

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Exactly what I was looking for. Thank you!