Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculate an indicator between intraday window

I'm not sure if this is possible, but I would like to calculate an indicator (e.g., EWMA) between the times of 9:30 am and 10 am every day. I would also like to do this over multiple days (e.g., the 10 day EWMA between 9:30 and 10 am). Any idea of how can I go about doing this?

4 responses

See schedule_function on the help page for starters.

Here are a few methods which may help. Both the methods 'get_pricing' (in a notebook) and 'history' (in an algorithm) return a pandas dataframe indexed by datetime. Dataframes have a lot of handy methods including 'tz_convert' and 'between_time'. Use the two methods to first convert the data to Eastern Time and then select only the data between 9:30-10:00. Something like this.

my_prices = get_pricing(symbols=my_securities,  
                        start_date=start_date,  
                        end_date=end_date,  
                        frequency='minute',  
                        fields=['close_price'])  
prices_et = my_prices.tz_convert('US/Eastern')  
prices_930_1000 = prices_et.between_time('9:30', '10:00')

One can also group by day (or business day) to perform calculations over this same 9:30-10:00 timeframe each day.

daily_groups = prices_et.between_time('9:30', '10:00').groupby(pd.TimeGrouper('B'))  
daily_groups.mean()

Use the 'apply' method to apply an arbitrary function to each group.

def my_mean(group):  
    mean = np.sum(group) / len(group)  
    return mean

daily_groups.apply(my_mean)

See attached notebook with this in action.

Wow that's useful on so many levels. Thanks a lot, Dan!

Glad to help. There are a couple of subtleties to watch out for when selecting absolute times (eg between 9:30-10:00) and when using the 'groupby' method.

First, converting to Eastern Time is more than just for easier reading, it's required because of Daylight Saving Time. If one were to do the same 'between_time' but with the UTC values, the results would be 'off' by an hour during summer time. The 'convert_tz' method is smart and not only offsets for the timezone but also for Daylight Saving Time. Furthermore, the method also adjusts correctly for dates in the past when Daylight Saving Time started at different times (eg in 2009 the US extended DST).

Second, the 'TimeGrouper' method adds missing datetime indexes and puts NaN values for the column data. If one uses the 'TimeGrouper' method and specifies 'Days' (note the 'D' parameter) like this

daily_groups = prices_et.between_time('9:31', '9:35').groupby(pd.TimeGrouper('D'))

The result will magically add a number of rows for weekends in the data and the associated data will be NaN. This can mostly be avoided by using the 'B' parameter which groups by Business days (ie no weekends). However, this still may create issues if the market is closed on a particular business day. Again, the method will add rows with NaN data. I typically just use 'dropna' to avoid this but just a word of caution if this isn't the desired behavior.

daily_averages = daily_groups.mean().dropna()