Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Best way to retrieve a trailing window of prices

Hi,

I've beginning toying around with strategies, which typically require each day a trailing window of past prices. Attempting to be efficient, I wrote a simple function (below) to store the price data in context, so that I do not have to retrieve each day the whole window from the database. But now I'm worried that the data obtained in this way will probably not be adjusted for mergers/splits/dividends...

So here are a few questions:
- Will I encounter problem with mergers/splits/dividends by storing each day the current data? I suspect I will.
- Is it good practice to make daily calls to retrieve a price trailing window using the data.history function? (This slows down the backtest considerably...)
- If not, what is the proper way of retrieving the price trailing window?
- In the help, what do you mean exactly when you say that in data.history, "price is always forward-filled"?

Thanks and best regards,

Samuel Monnier

# Stores the price history in a variable context.price_data.  
def store_price_data(context,data):  
    # Retrieves the price data  
    new_price_data = pd.DataFrame(data.current(context.security_list, 'price').to_dict(), index = [0])  
    # Appends it to the data already stored  
    context.price_data = new_price_data.append(context.price_data, ignore_index = True)  
    # If the data stored is longer than the lookback period, truncate it.  
    if len(context.price_data) > context.lookback:  
        context.price_data = context.price_data[0: context.lookback]  
2 responses

Welcome!

I'm making a big assumption that you're looking for daily data and not intra-day minute data. So...

  1. It's not good practice to store any data that relies on price or volume. You will encounter issues with splits etc.

  2. Use the pipeline model to retrieve data (look in the documentation for info). You probably don't really want a bunch of trailing prices. You probably want the result of a calculation based on those prices. Use the built in factors or write a custom factor to do the calculations.

  3. Best practice is to get your data from the pipeline once each day from inside the "before _trading_start" function. Use scheduled functions to place orders etc.

  4. "Forward filled" refers to what to do if there isn't a value for something at a particular time. This mostly occurs with minute data. The history function retrieves the price and volume data for each minute (or bar). If a security didn't trade during that particular minute then the data will be NaN. Often one wants to know the last price which a security traded at. Rather than searching previous bars for the last non-NaN data, some of the fields "forward fill" the NaNs with the last value simply as a convenience.

Hope this helps.

Many thanks for the thorough answer! I was speaking about daily data indeed.

So far I have been looking at strategies trading a small number of fixed securities, which is why I haven't really dug into pipelines yet. I'll try and see if using pipelines speeds up the backtest.