Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
anilb: How to get number of minutes in a trading day

if I use following call, this call gives me per minute data for m minutes.

price_hist = data.history(security, 'price', m ,'1m')  

But suppose I want to find moving volatility based on minute data for last 5 days, then how do I compute the number of minutes?

The problem is, I cannot assume 7 hours of trading per day and to get past 5 days per minute data simply multiply 5 * 7 * 60.
This is so because number of trading hours may vary (say due to heavy snow, etc.).

So, if my requirement is to find moving volatility of per day but based on minute data, how can I find number of minutes of each day?

This question also extends to per day data. Suppose I want to compute moving volatility for 1 quarter, then I cannot simply give
price_hist = data.history(security, 'price', 90 ,'1d') The reason is that if I give 90 as number of days, it will span beyond a quarter due to weekends and holidays.

So, what is a good way to specify number of days in a more controlled way to get exactly 90 trading days?

Please advice.

3 responses

There are probably a lot of approaches, but something like this works to get minute data for 5 business days (ie trading days) and will not be affected by any half days.

import pandas as pd  
previous_days = 5

# There are 6.5 hrs in a trading day. Get an extra day to be safe.  
max_minutes = 6.5 * 60 * (previous_days  + 1)  
price_hist = data.history(security, 'price', max_minutes ,'1m')  


# Normalize the times to be all 00 hours  
dates = price_hist.index.normalize()


# Find unique dates. Normalize the datetimes first so all the times begin at 00:00  
unique_dates = pd.unique(dates)

# Get date 'previous_days' ago. Note this is trade days not calendar days.  
date_5_trading_days_ago = unique_dates[-5]


# Then simply use the loc method to select only those dates after date_5_trading_days_ago  
price_hist.loc[date_5_trading_days_ago : ]

That will select all the minutes of each day for the past 5 trading days whether a half day or not.

Now, for the second question, the simplistic method is to assume there are 5 trading days for every 7 calendar days. If one wants 90 calendar days then select 5/7 x 90 ~ 65 trading days.

price_hist = data.history(security, 'price', 65 ,'1d')

If one truly wants 90 calendar days then something like this.

import pandas as pd  
previous_days = 90

# Get a little more data than we need  
price_hist = data.history(security, 'price', previous_days+1 ,'1d')  


# Find unique dates  
unique_dates = pd.unique(price_hist.index)


# Get the last date  
last_date = unique_dates[-1]


# Get date 90 days ago. Note this is now calendar days.  
date_90_trading_days_ago = last_date - pd.Timedelta(days=previous_days)


# Then simply use the loc method to select only those dates after date_90_trading_days_ago  
prices_last_90_calendar_days = price_hist.loc[date_90_trading_days_ago : ]

See attached notebook for ideas.

Hi Dan,

Thanks a ton for your detailed and educating answer. I have one (perhaps noob level) query for the line:

# Find unique dates  
unique_dates = pd.unique(dates)  

What is dates here? I am asking this because it is not a literal (like a string), and not a variable too in your code.

Thanks.
...- anil

@ Anil Bhatnagar

Oops, sorry had the wrong variable name. I had 'price_dates' in the original. I updated the post above. Basically, 'dates' is just the index of all the datetimes in 'price_hist'. It's normalized so all the time data is changed to midnight 00:00. This effectively gives just the dates of each data point.


dates = price_hist.index.normalize()