Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
feature request: allow time range in history request.

I understand i can call this, data.history(context.assets, "price", 20, "1d"), to get the most recent 20 day's prices. I would like to be able to query for data in specific time range, data.history(context.assets, "price", 20, "1d", end_date) to get 20 day's prices previous to "end_date."

4 responses

toan,

You could do something like this:

import datetime

def before_trade_start(context, data):  
        date = get_datetime('US/Eastern')  
        month = date.month  
        day = date.day  
        if month == x and day == y:  
               my_special_history = data.history(context.assets, "price", 20, "1d")

Then "my_special_history" will always hold the prices for the 20 days preceding your specific date.

Frank, I thought about this one, but i suspect it will not persist when the algo goes into paper trading or live trading.

Hi Toan,

Just load the data you need using data.history and then select the dates of interest. You do raise an interesting point, though. If the all of the data won't fit in memory, then it won't be possible to select a subset. In the research platform, I think you can actually request data over a specific time frame. The paradigm for the trading platform is that you'll need a limited trailing window of data, versus pulling huge datasets. Then, the data can be analyzed on a rolling basis.

Grant

Grant, Indeed. I was able to load the correct dataframe last night and it did not invoke a memory error. I've tried to load something similar (a much smaller dataframe) in to past and got memory error--I think they have increased the limit by quite a bit since maybe last month.

Now I just need to think of how to chop my elephant algorithm into little gazelle algorithms so the finish in the 5 min/1 min allocations. =)