1 """
2 Created on March 22, 2011
3
4 @author: Drew Bratcher & Tucker Balch
5 @contact: tucker@cc.gatech.edu
6
7 utilities for date and time manipulation
8 """
9
10 __version__ = "$Revision: 295 $"
11
12 import datetime as dt
13 from datetime import timedelta
14 import time as t
15 import numpy as np
16 import os
17
19 return(['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'])
20
22 years=[]
23 for date in funds.index:
24 if(not(date.year in years)):
25 years.append(date.year)
26 return(years)
27
29 months=[]
30 for date in funds.index:
31 if((date.year==year) and not(date.month in months)):
32 months.append(date.month)
33 return(months)
34
36 days=[]
37 for date in funds.index:
38 if((date.year==year) and (date.month==month)):
39 days.append(date)
40 return(days)
41
43 days=[]
44 for i in range(0,(ts_end-ts_start).days):
45 days.append(ts_start+timedelta(days=1)*i)
46 return(days)
47
49 for date in funds.index:
50 if((date.year==year) and (date.month==month)):
51 return(date)
52 return('ERROR')
53
54 -def getNYSEdays(startday = dt.datetime(1964,7,5), endday = dt.datetime(2020,12,31),
55 timeofday = dt.timedelta(0)):
56 """
57 @summary: Create a list of timestamps between startday and endday (inclusive)
58 that correspond to the days there was trading at the NYSE. This function
59 depends on a separately created a file that lists all days since July 4,
60 1962 that the NYSE has been open, going forward to 2020 (based
61 on the holidays that NYSE recognizes).
62
63 @param startday: First timestamp to consider (inclusive)
64 @param endday: Last day to consider (inclusive)
65 @return list: of timestamps between startday and endday on which NYSE traded
66 @rtype datetime
67 """
68
69 try:
70 filename = os.environ['QS'] + "/qstkutil/NYSE_dates.txt"
71 except KeyError:
72 print "Please be sure to set the value for QS in config.sh or\n"
73 print "in local.sh and then \'source local.sh\'.\n"
74
75 datestxt = np.loadtxt(filename,dtype=str)
76 dates = []
77
78 for i in datestxt:
79 dates.append(dt.datetime.strptime(i,"%m/%d/%Y")+timeofday)
80
81 dates = [x for x in dates if x >= startday]
82 dates = [x for x in dates if x <= endday]
83
84 return(dates)
85
87 """
88 @summary: Create a list of timestamps from startday that is days days long
89 that correspond to the days there was trading at NYSE. This function
90 depends on the file used in getNYSEdays and assumes the dates within are
91 in order.
92 @param startday: First timestamp to consider (inclusive)
93 @param days: Number of timestamps to return
94 @return list: List of timestamps starting at startday on which NYSE traded
95 @rtype datetime
96 """
97 try:
98 filename = os.environ['QS'] + "/qstkutil/NYSE_dates.txt"
99 except KeyError:
100 print "Please be sure to set the value for QS in config.sh or\n"
101 print "in local.sh and then \'source local.sh\'.\n"
102
103 datestxt = np.loadtxt(filename,dtype=str)
104 dates=[]
105 for i in datestxt:
106 if(len(dates)<days):
107 if((dt.datetime.strptime(i,"%m/%d/%Y")+timeofday)>=startday):
108 dates.append(dt.datetime.strptime(i,"%m/%d/%Y")+timeofday)
109 return(dates)
110
112 """
113 @summary: This function returns the last valid trading day before the start
114 day, or returns the start day if it is a valid trading day. This function
115 depends on the file used in getNYSEdays and assumes the dates within are
116 in order.
117 @param startday: First timestamp to consider (inclusive)
118 @param days: Number of timestamps to return
119 @return list: List of timestamps starting at startday on which NYSE traded
120 @rtype datetime
121 """
122 try:
123 filename = os.environ['QS'] + "/qstkutil/NYSE_dates.txt"
124 except KeyError:
125 print "Please be sure to set the value for QS in config.sh or\n"
126 print "in local.sh and then \'source local.sh\'.\n"
127
128 datestxt = np.loadtxt(filename,dtype=str)
129 dates=[]
130
131 ''' Set return to first day '''
132 dtReturn = dt.datetime.strptime( datestxt[0],"%m/%d/%Y")+timeofday
133
134 ''' Loop through all but first '''
135 for i in datestxt[1:]:
136 dtNext = dt.datetime.strptime(i,"%m/%d/%Y")
137
138 ''' If we are > startday, then use previous valid day '''
139 if( dtNext > startday ):
140 break
141
142 dtReturn = dtNext + timeofday
143
144 return(dtReturn)
145
147 """
148 @summary: Convert YMD info into a unix epoch value.
149 @param year: The year
150 @param month: The month
151 @param day: The day
152 @return epoch: number of seconds since epoch
153 """
154 return(t.mktime(dt.date(year,month,day).timetuple()))
155
157 """
158 @summary Convert seconds since epoch into date
159 @param ts: Seconds since epoch
160 @return thedate: A date object
161 """
162 tm = t.gmtime(ts)
163 return(dt.date(tm.tm_year,tm.tm_mon,tm.tm_mday))
164