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.