1 功能说明
处理holiday列表,判断某天是否为股票交易日,获取上一个交易日等。
python3把行情监控的交易日处理函数改成了类,加深对类不同method,datetime模块的理解。
holiday.ini配置文件如下:
[holiday]
#上证
sh=20180101;20180215-20180221;20180405-20180407;20180429-20180501;20180616-20180618;20180922-20180924;20181001-20181007
#深证
sz=20180101;20180215-20180221;20180405-20180407;20180429-20180501;20180616-20180618;20180922-20180924;20181001-20181007
2 python源码
# -*- coding:utf-8 -*-
## Edit by hanlin
## date 20190124
## 564868230@qq.com
import datetime
class TradeDay:
'''Days that securities can be traded'''
date=datetime.date.today()
holiday='20191111'
__today=datetime.date.today()
__holiday_list=[]
def __init__(self,date='20190101',holiday='20190101'):
self.date=date
self.holiday=holiday
@staticmethod
def date2str(date,format='%Y%m%d'):
return datetime.datetime.strftime(date,format)
@staticmethod
def str2date(date,format='%Y%m%d'):
return datetime.datetime.strptime(date,format)
@staticmethod
def getDaysByDaysSeg(days_seg):
'''eg input:20160729-20160801 output:[20160729,20160730,20160801]'''
days=days_seg.split('-')
start_day=TradeDay.str2date(days[0])
end_day=TradeDay.str2date(days[1])
oneday=datetime.timedelta(days=1)
li=[days[0]]
while True:
start_day=start_day+oneday
li.append(TradeDay.date2str(start_day))
if start_day == end_day:
break
return li
@classmethod
def getToday(self):
#today=datetime.datetime.strftime(self.__today,'%Y%m%d')
today=TradeDay.date2str(self.__today,'%Y%m%d')
return today
@classmethod
def getYesterday(self):
oneday=datetime.timedelta(days=1)
yesterday=self.date-oneday
yesterday=TradeDay.date2str(yesterday,format='%Y%m%d')
return yesterday
@classmethod
def getTomorrow(self):
oneday=datetime.timedelta(days=1)
tomorrow=self.date+oneday
tomorrow=TradeDay.date2str(tomorrow,format='%Y%m%d')
return tomorrow
@staticmethod
def getWkday(date):
#wkday=datetime.datetime(date).strftime("%w")
week_day_dict = {
0 : 'Mon',
1 : 'Tues',
2 : 'Wed',
3 : 'Thur',
4 : 'Fri',
5 : 'Sat',
6 : 'Sun',
}
#date=TradeDay.str2date(self.date,'%Y%m%d')
dayn = date.weekday()
return week_day_dict[dayn]
@classmethod
def getHolidayList(self):
#holiday_list=[]
#holiday
length=len(holiday)
for i in range(length):
if '-' in holiday[i]:
li=TradeDay.getDaysByDaysSeg(holiday[i])
for li_day in li:
self.__holiday_list.append(li_day)
else:
self.__holiday_list.append(holiday[i])
return self.__holiday_list
def isTradeDay(self):
TradeDay.getHolidayList()
today=TradeDay.str2date(self.date,'%Y%m%d')
weekend=['Sat','Sun']
if TradeDay.getWkday(today) in weekend:
return False
elif TradeDay.date2str(today) in self.__holiday_list:
return False
return True
def getLastTradeDay(self):
TradeDay.getHolidayList()
lastday=TradeDay.str2date(self.date,'%Y%m%d')
while True:
oneday=datetime.timedelta(days=1)
lastday=lastday-oneday
weekend=['Sat','Sun']
if TradeDay.getWkday(lastday) in weekend:
continue
elif TradeDay.date2str(lastday) in self.__holiday_list:
continue
else:
break
return TradeDay.date2str(lastday)
#### test test test
holiday=['20181229-20190101']
dates=TradeDay('20190122',holiday)
#dates2=TradeDay(holiday=holiday)
print(dates.date,dates.holiday)
print("Class-Today: %s"%TradeDay.getToday())
print("Object-Today: %s"%dates.getToday())
print(TradeDay.getYesterday())
print(TradeDay.getTomorrow())
print('--------------------')
print(dates.getHolidayList())
print("LastTradeDay: %s"%dates.getLastTradeDay())
print("%s is TradeDay: %s"%(dates.date,dates.isTradeDay()))
网友评论