Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New Features: History and New Order Methods

This week we've launched some great new stuff in our API. This batch of improvements is extra-exciting because they're all things that have been strongly requested and shaped by our community.

History

history() is a function that returns price and volume information for a trailing window of data. In general terms, it returns the close price for the last n days, and lets you easily manipulate the data and performs well in minute-mode testing. The actual behavior is more nuanced and detailed, and you can read about it in the help documentation.

As a quick example, if you have an algorithm that is trading AAPL (sid(24)), and you call history on Jan-04-2008 as described below:

    apple_history = history(3,'1d','price')  
    log.debug(apple_history)  

You'll get output that looks like this:
2008-01-04 handle_data:17 DEBUG 24 2008-01-02 21:00:00+00:00 194.93 2008-01-03 21:00:00+00:00 194.85 2008-01-04 14:31:00+00:00 191.15 A few high level notes:

  • History only works in minute-level algorithms.
  • History only works with '1d' frequency.
  • You don't need to "warm up" the function, because the function
    automatically loads from history and "warms" itself.
  • Read the docs for lots more

Many of you have read or commented on our history() spec, and you've surely already noticed that this feature is only a part of the spec. Here at Quantopian we believe in shipping incremental improvements as quickly as we can. There's no reason for us to keep this neat feature under wraps while we work on the rest of the spec. First off, you get the benefit of this sooner. Also, the rest of the work will be informed by your feedback on the part we're releasing today, and when we finally finish it all, it will be better for the feedback. We will continue to ship improvements and expansions to the history() function going forward.

New Order Methods

Most everyone writing an algo has struggled at some point with the order() command. If you have $50,000, how many shares of stock should you order? If you have ten stocks and you want to buy equal amounts of each one, how do you do that? If you have some shares of a stock already, and you want to buy more, how do you do that? Up to now you've had to do some calculations in your code. Now, you can call one of these special order commands and the math is handled for you on the back end.

  • order_target_percent() - Place an order to adjust a position to a target percent of the current portfolio value.
  • order_target_value() - Places an order to adjust a position to a target value.
  • order_target() - Places an order to adjust a position to a target number of shares.
  • order_percent() - Places an order sized as a percent of the current portfolio value
  • order_value() - Place an order by desired value rather than desired number of shares.

Each one of these order methods has additional details, explanation, and example on our help docs.

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.

8 responses

Approximately how far back can the history command go?
Could I use data from the past several years, for instance, in an algorithm?
Thanks,
-Paul

You can go as far back as our database goes. Attached is the same example. but with 10 years worth of data in the standard deviation. Help doc has the key points: "Obviously, we can't load history where the requested data extends farther into the past than our database. The database limit is Jan 2, 2002 for backtesting and Mar 1, 2012 for paper trading and live trading."

Hello Dan,

Does 'history' work with TA-Lib yet? If so can you advise on the syntax, please?

P.

Hey Dan,

I found in one instance, after converting the data provided by 'history' I had to use x.astype(float):

data_z = stats.zscore(data_uncoded.astype(float), axis=0, ddof=1)

Is this the expected behavior? I can post the entire code, if you want, along with the error message that results without the x.astype(float).

Grant

Dan,

Another question is how are volumes filled, if historical trade data are missing? Are you using zeros, NaN's, or something else?

Grant

Peter,

We may try to improve the interface down the line, but the following should work for TA-Lib functions with history, since TA-Lib has functionality for taking pandas structures as input.

import talib

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    set_universe(universe.DollarVolumeUniverse(90.0, 90.1))

def handle_data(context, data):  
    prices = history(30, '1d', 'price')  
    emas = {}

    for s in data:  
        ema = talib.EMA(prices[s])  
        emas[s] = ema[-1]  
    log.info(emas)  
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.

Hello Eddie,

Thank you. I tried all sorts of things but importing 'talib' was not one of them!

Please add 'talib' to the list of Whitelisted modules in the Help docs and the list that pops up with an import error.

P.

Grant, on the volume question, I don't think we have significant "missing" volume. When there is a bar where the stock didn't trade, a zero is added.

I'm not sure on the float problem, I'm asking about that one.