<s> '''<STR_LIT>''' <EOL> import datetime as dt <EOL> from datetime import timedelta <EOL> import time as t <EOL> import numpy as np <EOL> import os <EOL> import pandas as pd <EOL> def _cache_dates ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> filename = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> except KeyError : <EOL> print "<STR_LIT>" <EOL> datestxt = np . loadtxt ( filename , dtype = str ) <EOL> dates = [ ] <EOL> for i in datestxt : <EOL> dates . append ( dt . datetime . strptime ( i , "<STR_LIT>" ) ) <EOL> return pd . TimeSeries ( index = dates , data = dates ) <EOL> GTS_DATES = _cache_dates ( ) <EOL> def getMonthNames ( ) : <EOL> return ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def getYears ( funds ) : <EOL> years = [ ] <EOL> for date in funds . index : <EOL> if ( not ( date . year in years ) ) : <EOL> years . append ( date . year ) <EOL> return ( years ) <EOL> def getMonths ( funds , year ) : <EOL> months = [ ] <EOL> for date in funds . index : <EOL> if ( ( date . year == year ) and not ( date . month in months ) ) : <EOL> months . append ( date . month ) <EOL> return ( months ) <EOL> def getDays ( funds , year , month ) : <EOL> days = [ ] <EOL> for date in funds . index : <EOL> if ( ( date . year == year ) and ( date . month == month ) ) : <EOL> days . append ( date ) <EOL> return ( days ) <EOL> def getDaysBetween ( ts_start , ts_end ) : <EOL> days = [ ] <EOL> for i in range ( <NUM_LIT:0> , ( ts_end - ts_start ) . days ) : <EOL> days . append ( ts_start + timedelta ( days = <NUM_LIT:1> ) * i ) <EOL> return ( days ) <EOL> def getFirstDay ( funds , year , month ) : <EOL> for date in funds . index : <EOL> if ( ( date . year == year ) and ( date . month == month ) ) : <EOL> return ( date ) <EOL> return ( '<STR_LIT>' ) <EOL> def getLastDay ( funds , year , month ) : <EOL> return_date = '<STR_LIT>' <EOL> for date in funds . index : <EOL> if ( ( date . year == year ) and ( date . month == month ) ) : <EOL> return_date = date <EOL> return ( return_date ) <EOL> def getNextOptionClose ( day , trade_days , offset = <NUM_LIT:0> ) : <EOL> year_off = <NUM_LIT:0> <EOL> if day . month + offset > <NUM_LIT:12> : <EOL> year_off = <NUM_LIT:1> <EOL> offset = offset - <NUM_LIT:12> <EOL> first = dt . datetime ( day . year + year_off , day . month + offset , <NUM_LIT:1> , hour = <NUM_LIT:16> ) <EOL> day_num = first . weekday ( ) <EOL> dif = <NUM_LIT:5> - day_num <EOL> if dif < <NUM_LIT:1> : <EOL> dif = dif + <NUM_LIT:7> <EOL> dif = dif + <NUM_LIT> <EOL> friday = first + dt . timedelta ( days = ( dif - <NUM_LIT:1> ) ) <EOL> if friday in trade_days : <EOL> month_close = first + dt . timedelta ( days = dif ) <EOL> else : <EOL> month_close = friday <EOL> if month_close < day : <EOL> return_date = getNextOptionClose ( day , trade_days , offset = <NUM_LIT:1> ) <EOL> else : <EOL> return_date = month_close <EOL> return ( return_date ) <EOL> def getLastOptionClose ( day , trade_days ) : <EOL> start = day <EOL> while getNextOptionClose ( day , trade_days ) >= start : <EOL> day = day - dt . timedelta ( days = <NUM_LIT:1> ) <EOL> return ( getNextOptionClose ( day , trade_days ) ) <EOL> def getNYSEoffset ( mark , offset ) : <EOL> '''<STR_LIT>''' <EOL> mark = mark . replace ( hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> i = GTS_DATES . index . searchsorted ( mark , side = '<STR_LIT:right>' ) <EOL> if GTS_DATES [ i ] != mark : <EOL> i -= <NUM_LIT:1> <EOL> ret = GTS_DATES [ i + offset ] <EOL> ret = ret . replace ( hour = <NUM_LIT:16> ) <EOL> return ret <EOL> def getNYSEdays ( startday = dt . datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:5> ) , endday = dt . datetime ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT> ) , <EOL> timeofday = dt . timedelta ( <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> start = startday - timeofday <EOL> end = endday - timeofday <EOL> dates = GTS_DATES [ start : end ] <EOL> ret = [ x + timeofday for x in dates ] <EOL> return ( ret ) <EOL> def getNextNNYSEdays ( startday , days , timeofday ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> filename = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> except KeyError : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> datestxt = np . loadtxt ( filename , dtype = str ) <EOL> dates = [ ] <EOL> for i in datestxt : <EOL> if ( len ( dates ) < days ) : <EOL> if ( ( dt . datetime . strptime ( i , "<STR_LIT>" ) + timeofday ) >= startday ) : <EOL> dates . append ( dt . datetime . strptime ( i , "<STR_LIT>" ) + timeofday ) <EOL> return ( dates ) <EOL> def getPrevNNYSEday ( startday , timeofday ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> filename = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> except KeyError : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> datestxt = np . loadtxt ( filename , dtype = str ) <EOL> dtReturn = dt . datetime . strptime ( datestxt [ <NUM_LIT:0> ] , "<STR_LIT>" ) + timeofday <EOL> for i in datestxt [ <NUM_LIT:1> : ] : <EOL> dtNext = dt . datetime . strptime ( i , "<STR_LIT>" ) <EOL> if ( dtNext > startday ) : <EOL> break <EOL> dtReturn = dtNext + timeofday <EOL> return ( dtReturn ) <EOL> def ymd2epoch ( year , month , day ) : <EOL> """<STR_LIT>""" <EOL> return ( t . mktime ( dt . date ( year , month , day ) . timetuple ( ) ) ) <EOL> def epoch2date ( ts ) : <EOL> """<STR_LIT>""" <EOL> tm = t . gmtime ( ts ) <EOL> return ( dt . date ( tm . tm_year , tm . tm_mon , tm . tm_mday ) ) <EOL> def _trade_dates ( dt_start , dt_end , s_period ) : <EOL> '''<STR_LIT>''' <EOL> ldt_timestamps = getNYSEdays ( dt_start , <EOL> dt_end , dt . timedelta ( hours = <NUM_LIT:16> ) ) <EOL> ts_dates = pd . TimeSeries ( index = ldt_timestamps , data = ldt_timestamps ) <EOL> if s_period [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> dr_range = pd . DateRange ( dt_start , dt_end , <EOL> timeRule = s_period [ <NUM_LIT:1> : ] ) <EOL> dr_range = np . asarray ( dr_range ) <EOL> li_even = np . array ( range ( len ( dr_range ) ) ) <EOL> dr_range = dr_range [ li_even [ li_even % <NUM_LIT:2> == <NUM_LIT:0> ] ] <EOL> else : <EOL> dr_range = pd . DateRange ( dt_start , dt_end , <EOL> timeRule = s_period ) <EOL> dr_range = np . asarray ( dr_range ) <EOL> dr_range = np . copy ( dr_range ) <EOL> dr_range += pd . DateOffset ( hours = <NUM_LIT:16> ) <EOL> ts_dates = ts_dates . reindex ( dr_range , method = '<STR_LIT>' ) <EOL> ldt_dates = ts_dates [ ts_dates . notnull ( ) ] . values <EOL> sdt_unique = set ( ) <EOL> ldt_dates = [ x for x in ldt_dates <EOL> if x not in sdt_unique and not sdt_unique . add ( x ) ] <EOL> return ldt_dates </s>
