Back to Community
1 minute history now available

The history() function has been extended and now supports trailing windows of one minute granularity. This was a popular request and we’re excited to expand the feature.

As a reminder, history() is a function that returns price and volume information for a trailing window of data. Before today, we only supported day-level, or ‘1d’ values. Now, we support '1m' values. The remaining syntax is the same.

This feature now enables a number of ideas to be easily implemented. For instance, you can create a vwap, or moving average, or other transform on much shorter time frames using minute bars. You can also inspect the minutes before market close or after the open, if that interests you.

For example the code below returns the last 20 minutes of the stock price as a pandas dataframe:

history(20, ‘1m’, ‘price’)  

This is one step closer to deprecating batch_transform().  If you're using batch_transform to generate trailing windows in minute mode, you should consider changing the code over to history().

The attached example compares the 90 minute VWAP to the 30 minute VWAP for a stock. Happy coding!

Clone Algorithm
99
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.stock= sid(24)

def my_vwap(prices, volumes):
    """
    Takes a DataFrame of prices and a DataFrame of volumes
    and returns a Series of the Volume Weighted Average Price
    for each sid in both frames.
    """
    return (prices * volumes).sum() / volumes.sum()

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):

    # Get a 90 minute vwap
    prices_90m = history(90, '1m', 'price')
    volumes_90m = history(90, '1m', 'volume')
    vwap_90m = my_vwap(prices_90m, volumes_90m)
    
    # Get a 30 min vwap
    prices_30m = history(30, '1m', 'price')
    volumes_30m = history(30, '1m', 'volume')
    vwap_30m = my_vwap(prices_30m, volumes_30m)
    
    # Ordering logic
    if vwap_30m[context.stock] > vwap_90m[context.stock]:
        order_target(context.stock, 100)
There was a runtime error.
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.

12 responses

Thanks! --Grant

Time to throw a party!

Quick question on the TA usage. I can use this to compute on all sids?

import talib

def initialize(context):  
    context.my_sid = [sid(24), sid(1234)]

def handle_data(context, data):  
    price_history = history(bar_count=30, frequency='1d', field='price')  
    ema_result = talib.EMA(price_history)  

Also if I am to use TA Lib functions like ATR (use H, L, C), MFI (Use C, V) how is this expressed in code?

great feature. However, is there a way to backfill NA values (to deal with cases where the first element of the vector happens to be 'NA')?

A runtime error results for bar_count = 1. Is this the expected behavior or a bug? --Grant


def initialize(context):  
    context.stocks = [sid(8554)] # SPY

def handle_data(context, data):  
    prices = history(2,'1m','price')  
    # prices = history(1,'1m','price') # Runtime Error "IndexError: index out of bounds"  
Clone Algorithm
3
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    
    context.stocks = [sid(8554)] # SPY

def handle_data(context, data):
    
    prices = history(2,'1m','price')
    # prices = history(1,'1m','price') # Runtime Error "IndexError: index out of bounds"
There was a runtime error.

Hi Grant,

I cloned and re-ran your posted algorithm from 2014-06-19 to 2014-06-20 and didn't get the IndexError mentioned in your comment. Are you still seeing this behavior with the code you posted, or is there a more complex setup necessary to repro the error?

Thanks,
-Scott

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.

Also, to answer your original question a 1m history of length one should return data for the current minute.

Thanks Scott,

This code crashes for the range of dates in my backtest above:

def initialize(context):  
    context.stocks = [sid(8554)] # SPY

def handle_data(context, data):  
    # prices = history(2,'1m','price')  
    prices = history(1,'1m','price') # Runtime Error "IndexError: index out of bounds"  

Let me know if you need more info.

Grant

Ah, looks like it's only crashing if you only have length-1 history with no length-2 entries (I ran my test with both lines uncommented). I'll take a look at what's happening here.

Yep, there's a bug in the HistoryContainer logic that special-cases a single length-1 1m history for a field. Bugfix has been posted to https://github.com/quantopian/zipline/pull/354 and should make its way to Quantopian relatively soon.

UPDATE: This is fixed on zipline/master as of https://github.com/quantopian/zipline/commit/6a2ee7c417440d59c59bc9430ef4394384cc912e

Thanks for the bug report!

Wow -- that's great!

Looking forward to using a 15 minute indicator.