def __init__(self, startDate, endDate, start_time='9:00', end_time='16:00', holidays=[], weekmask='Mon Tue Wed Thu Fri', calendar=None, frequency='H', sample='1'): self.__startDate = startDate self.__endDate = endDate self.__sample = sample acceptable_freq = ['D', 'M', 'H', 'S'] if frequency not in acceptable_freq: raise ValueError( 'Frequency Value Not acceptable. Specify D, M, H, S') self.__frequency = frequency if (calendar != None): self.__bday = CustomBusinessDay(calendar=calendar) self.__bhour = CustomBusinessHour(start=start_time, end=end_time, calendar=calendar) else: self.__bday = CustomBusinessDay(holidays=holidays, weekmask=weekmask) self.__bhour = CustomBusinessHour(start=start_time, end=end_time, holidays=holidays, weekmask=weekmask)
def create_bussiness_hour(start='00:00', end='23:00', week_mask='Mon Tue Wed Thu Fri'): """ This function creates custom business hour for using in date ranges creation. Parameters ---------- start : str The business hours start. end : str The business hours end. week_mask : str The mask that specify work weekdays. Returns ------- object The business hour object. """ bhour = CustomBusinessHour(start='08:00', end='23:00', weekmask=week_mask)
def test_custom_businesshour_weekmask_and_holidays(weekmask, expected_time, mult): # GH 23542 holidays = ["2018-11-09"] bh = CustomBusinessHour( start="08:00", end="17:00", weekmask=weekmask, holidays=holidays ) result = Timestamp("2018-11-08 08:00") + mult * bh expected = Timestamp(expected_time) assert result == expected
def __init__(self, startDate, endDate, startTime='9:00', endTime='16:00', holidays=[], weekmask='Mon Tue Wed Thu Fri', calendar=None, frequency='H', sample='1'): self.__startDate = startDate self.__endDate = endDate self.__sample = sample acceptable_freq = [ 'B', 'C', 'D', 'W', 'M', 'SM', 'BM', 'CBM', 'MS', 'SMS', 'BMS', 'CBMS', 'Q', 'BQ', 'QS', 'BQS', 'A', 'Y', 'BA', 'BY', 'AS', 'YS', 'BAS', 'BYS', 'BH', 'H', 'T', 'S', 'L', 'U', 'N' ] if frequency not in acceptable_freq: raise ValueError( 'Frequency Value Not acceptable. Specify D, M, H, S') self.__frequency = frequency start = datetime.strptime(startTime, '%H:%M') self.startMinuteDelta = start.hour * 60 + start.minute end = datetime.strptime(endTime, '%H:%M') self.endMinuteDelta = end.hour * 60 + end.minute if (calendar != None): self.__bday = CustomBusinessDay(calendar=calendar) self.__bhour = CustomBusinessHour(start=startTime, end=endTime, calendar=calendar) else: self.__bday = CustomBusinessDay(holidays=holidays, weekmask=weekmask) self.__bhour = CustomBusinessHour(start=startTime, end=endTime, holidays=holidays, weekmask=weekmask)
def __init__(self, startDate, endDate, startTime='9:00', endTime='16:00', holidays = [], weekmask = 'Mon Tue Wed Thu Fri', calendar = None, frequency='H', sample='1'): self.__startDate = startDate self.__endDate = endDate self.__sample = sample acceptable_freq = ['D', 'M', 'H', 'S', 'm'] if frequency not in acceptable_freq: raise ValueError('Frequency Value Not acceptable. Specify D(day), M(minute), H(hour), S(second), m(month)') self.__frequency = frequency start = datetime.strptime(startTime, '%H:%M') self.startMinuteDelta = start.hour * 60 + start.minute end = datetime.strptime(endTime, '%H:%M') self.endMinuteDelta = end.hour * 60 + end.minute if(calendar != None): self.__bday = CustomBusinessDay(calendar = calendar) self.__bhour = CustomBusinessHour(start = startTime, end = endTime, calendar = calendar) else: self.__bday = CustomBusinessDay(holidays = holidays, weekmask = weekmask) self.__bhour = CustomBusinessHour(start = startTime, end = endTime, holidays = holidays, weekmask = weekmask)
def get_startdate(self): # today = pd.datetime.today() # print(today) # print( today - BDay(4)) # return self.date_pickup - BDay(self.duration.days()) #print(calendar.holidays(start='2020-01-01', end='2020-12-31')) start_after_calculation = self.date_pickup - CustomBusinessDay( self.duration.days, calendar=calendar) if self.duration.seconds > 0: start_after_calculation = start_after_calculation - CustomBusinessHour( self.duration.seconds // 3600, calendar=calendar, start='06:59', end='14:59' ) # - datetime.timedelta(seconds=self.duration.seconds); return start_after_calculation
from pandas.tseries.holiday import USFederalHolidayCalendar from pandas.tseries.offsets import CustomBusinessHour import pandas as pd import numpy as np from sklearn import metrics FX_LIST = ['EURUSD', 'USDJPY', 'GBPUSD', 'AUDUSD', 'EURJPY', 'EURGBP'] PREX = '../data/fx/prediction' CROSS_PREX = '../data/fx/prediction/cross' MON_PREX = '../data/fx/prediction/24' DAY_PREX = '../data/fx/prediction/1' CUSTOM_BH = CustomBusinessHour(calendar=USFederalHolidayCalendar(), start='00:00', end="01:00") RANGE_TIME = pd.DatetimeIndex(start='20150106', end='20160531', freq=CUSTOM_BH) def to_summary(fx): # LOAD DATA cro_cnn = pd.read_pickle('%s/NN/%sprediction.pkl' % (CROSS_PREX, fx)).reset_index() cro_knn = pd.read_pickle('%s/k-NN/%sprediction.pkl' % (CROSS_PREX, fx)).reset_index() cro_svm = pd.read_pickle('%s/SVM/%sprediction.pkl' % (CROSS_PREX, fx)).reset_index() mon_ann = pd.read_pickle('%s/NN/%sprediction.pkl' % (MON_PREX, fx)).reset_index() mon_knn = pd.read_pickle('%s/k-NN/%sprediction.pkl' % (MON_PREX, fx)).reset_index() mon_svm = pd.read_pickle('%s/SVM/%sprediction.pkl' % (MON_PREX, fx)).reset_index()
USThanksgivingDay, Holiday('Christmas', month=12, day=25, observance=nearest_workday) ] # US business day us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar()) # US financial business day fi_bd = CustomBusinessDay(calendar=USFinancialHolidayCalendar()) fi_holidays = USFinancialHolidayCalendar().holidays _ = fi_holidays('19500101', '20500101') # Activate the holiday for better efficiency # US Exchange bussiness hour fi_bh = CustomBusinessHour(calendar=USFederalHolidayCalendar(), start='9:30', end='16:30') def weekday_distance(t1, t2, convention='forward'): """ Number of weekdays between t1 and t2: t2 - t1 Rolling forward convention: from 0 am+ to 0 am+ example: Friday to Saturday is 1 Sunday to Monday is 0 Rolling back convention: from 0 am- to 0 am - example: Friday to Saturday is 0 Sunday to Monday is 1 Parameters ------------- t1: datetime.date t2: datetime.date
def calculatePricePrediction(self): # Market opens at 9:30, closes at 16:00 # Closed on weekends and national holidays # Code has to calculate and return from 1 up to 4 evenly spaced values (data points) to give a buy/sell Recommendation # The returned data points are based on the regression line's data for future prices # Because we will avoid non-trading hours, checks must be conducted for EOD and non trading days mktCloseBase = datetime(year=2018, month=1, day=1, hour=16, minute=0, second=0) mktCloseOffset = timedelta(minutes=(self.timeInterval - 1)) adjustedMktClose = (mktCloseBase - mktCloseOffset).time() mktClose = mktCloseBase.time() # 16:00 Eastern Standard Time (U.S.A.) predictBeginIndex = 100 nextBusinessHour = CustomBusinessHour( start='8:30', end='16:00', calendar=USFederalHolidayCalendar()) nextBusinessDay = CustomBusinessDay( calendar=USFederalHolidayCalendar()) validFutureTimePriceSet = [] currentTimeStamp = self.timeStampList[predictBeginIndex - 1:] # Creates a list of future time stamps and prices ignoring closing hours, weekends, and holidays for timeStampIterator in range(0, 121): if self.apiLookupFunction == 'TIME_SERIES_INTRADAY': currentTimeStamp += PdMinute(self.timeInterval) if currentTimeStamp.time > mktClose: currentTimeStamp += nextBusinessHour else: currentTimeStamp += nextBusinessDay pricePrediction = self.linearModel.predict( predictBeginIndex + timeStampIterator)[0][0] # 2D array with one value validFutureTimePriceSet.append( [currentTimeStamp[0].to_pydatetime(), pricePrediction]) # Defines modulo value for intraday and index positions for daily api data if self.apiLookupFunction == 'TIME_SERIES_INTRADAY': moduloValue = 0 if self.timeInterval == 1: moduloValue = 30 # Every 30 minutes or next day elif self.timeInterval == 5: moduloValue = 24 # Every 2 hours or next day else: moduloValue = 18 # Every 3 hours of next day else: presetIndexList = [0, 5, 10, 21 ] # Next day, next week, in two weeks, 30 days # Adds pre determined values to a dictionary based on the API interval and function selected presetTimeStampDict = {} timeFormat = '%a, %b %d %I:%M%p' for index, timeStamp in enumerate(validFutureTimePriceSet): currentTimeStamp = validFutureTimePriceSet[index][0] currentPrice = validFutureTimePriceSet[index][1] if self.apiLookupFunction == 'TIME_SERIES_INTRADAY': if currentTimeStamp.time() < adjustedMktClose: if ((index % moduloValue) == 0 and (index != 0)): presetTimeStampDict[currentTimeStamp.strftime( timeFormat)] = currentPrice else: currentTimeStamp = validFutureTimePriceSet[index + 1][0] presetTimeStampDict[currentTimeStamp.strftime( timeFormat)] = currentPrice break else: for presetIndexValue in presetIndexList: if presetIndexValue == index: presetTimeStampDict[currentTimeStamp.strftime( timeFormat)] = currentPrice return presetTimeStampDict
from pandas.tseries.holiday import (AbstractHolidayCalendar, Holiday, nearest_workday, USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, USLaborDay, USThanksgivingDay) from pandas.tseries.offsets import CustomBusinessHour, CustomBusinessDay # TODO: Check for behavior on Dec 31 class USTradingCalendar(AbstractHolidayCalendar): rules = [ Holiday('NewYearsDay', month=1, day=1, observance=nearest_workday), USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday), USLaborDay, USThanksgivingDay, Holiday('Christmas', month=12, day=25, observance=nearest_workday) ] trading_hours_us = CustomBusinessHour(start='09:30', end='16:30', calendar=USTradingCalendar()) trading_day_us = CustomBusinessDay(calendar=USTradingCalendar())
def getCustomTimeGroup(self): from pandas.tseries.offsets import CustomBusinessMonthBegin, CustomBusinessHour bh = CustomBusinessHour(start='16:00',weekmask='Sun Mon Tue Wed Thu Fri') #http://stackoverflow.com/questions/13019719/get-business-days-between-start-and-end-date-using-pandas #http://pandas.pydata.org/pandas-docs/stable/timeseries.html pass