Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
attempting to create custom trailing data frames

I am attempting to create a trailing data frame containing the mean normalized price of the securities in the universe for some look back period. i.e. on every day in the data frame the price for each security will be the price of the security on that day divided by the mean of x previous days on that day.

currently what i have is something like what I've included below but I'm missing some key insight somewhere... probably a matter of not knowing my way around python all that well.

from collections import defaultdict, deque  
from functools import partial  
import numpy as np

def initialize(context):  
   context.sid1 = sid(23112)    #Chevron  
   context.lookbackPeriod = 30  
   context.normalizedPrices  = defaultdict(partial(deque, maxlen=context.lookbackPeriod)) 

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    price_history = history(bar_count=30, frequency='1d', field='price')  
    normalizedPrice = data[context.sid1].price/price_history[context.sid1].mean()  
    context.normalizedPrices[context.sid1].append(normalizedPrice)  
    stock_series = np.array(context.normalizedPrices[context.sid1])  
    record(price=normalizedPrice)  
4 responses

Hello Stephan,

Are you needing to accumulate normalized minute bar closing prices? Or normalized daily closing prices? Or something else?

Note that for bar_count > 1, history returns both daily closing prices and the current minute bar closing price.

Grant

I think I am looking for normalized daily prices for the last 30 days where each is normalized by the mean of the preceding 30 days on that day... Is that code mixing minutely and daily?

Yes, you are mixing minutely & daily data. I suggest having a look at the Quantopian help page to understand the output of the history API.

There should be a clean way to do the computation using the rolling stats. available in pandas. I suggest having a look at the rolling_mean in http://pandas.pydata.org/pandas-docs/stable/computation.html. I don't have time to work up an example now, but basically if you do a rolling_mean with window = 30, then you can use the resulting dataframe as your divisor vector. Then you can do an elementwise division of the price vector for normalization.

--Grant

Stephan,

Here's what I had in mind:

import pandas as pd

def initialize(context):  
   context.sid1 = [sid(23112), sid(8347)]    # Chevron & Exxon  

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    prices = history(bar_count=60, frequency='1d', field='price')[0:-1]  
    price_means = pd.rolling_mean(prices,30).tail(30)  
    prices_normalized = prices.tail(30)/price_means  
    print len(prices_normalized)  
    print prices_normalized.head()  
    print prices_normalized.tail()  

Make sense?

Grant