-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathTradeDay.py
154 lines (151 loc) · 5.36 KB
/
TradeDay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import tushare as ts
import json,datetime
import pandas as pd
import pymysql
from dboprater import DB as db
configfile='./config/mysqlconfig.json'
class tradeday:
def __init__(self):
self.pro = ts.pro_api(db.get_config()['tushare'])
#将交易日入库,1年执行一次即可
def gettradeday(self):
tradedaylist=[]
try:
alldays = self.pro.trade_cal() # 得到所有日期,到今年年尾
alldays=alldays.iloc[10000:,::]
# alldays=pd.DataFrame(alldays.loc[:,'cal_date','is_open'])
conn=db.dbconnect()
currsor=conn.cursor(cursor=pymysql.cursors.DictCursor)
sql ='insert into datelist (date,isopen) values(%s,%s)'
for days in alldays.iterrows():
date=days[1]['cal_date']
is_open=days[1]['is_open']
values=(date,is_open)
currsor.execute(sql,values)
conn.commit()
currsor.close()
conn.close()
except BaseException as b:
print(b)
#判断当日是否为交易日
@staticmethod
def isTradeDay(*date1):
'''date type: 2021-05-28'''
len1=len(date1)
if len1>0:
date=str(date1[0])
else:
date = str(datetime.datetime.now().strftime('%Y-%m-%d'))
flag=True
conn = db.dbconnect()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = 'select * from datelist where date=\''+date+'\''
cursor.execute(sql)
result=cursor.fetchall()
if result:
for data in result:
isopen=data['isopen']
if str(isopen)=='0':
flag=False
return flag
else:
return flag
else:
print('取数据异常!')
#获取最近一个交易日
@staticmethod
def getlastTradeday():
conn = db.dbconnect()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
date=datetime.datetime.now().strftime('%Y-%m-%d')
sql = 'select * from datelist where date<=\'' + date + '\'' #取到今天为止的所有日期数据
cursor.execute(sql)
result = cursor.fetchall()
pddata=pd.DataFrame(result)
if pddata.empty:
return None
else:
pddata=pddata[pddata['isopen'] == 1] #留下全是交易日的数据
res=pddata.iloc[-1, 0]
return str(res)
#获取上一个交易日
@staticmethod
def getyestodayTradeday(*date1):
len1=len(date1)
if len1>0:
date=str(date1[0])
else:
date = str(datetime.datetime.now().strftime('%Y-%m-%d'))
conn = db.dbconnect()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = 'select * from datelist where date<=\'' + date + '\'' #取到今天为止的所有日期数据
cursor.execute(sql)
result = cursor.fetchall()
pddata=pd.DataFrame(result)
if pddata.empty:
return None
else:
pddata=pddata[pddata['isopen'] == 1] #留下全是交易日的数据
res=pddata.iloc[-2, 0]
return str(res)
#获取最近的第N个交易日
@staticmethod
def getlastNtradeday(n):
'''n type int'''
if str(n).isalpha():
print('输入错误')
return None
conn = db.dbconnect()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
date = str(datetime.datetime.now().strftime('%Y-%m-%d'))
sql = 'select * from datelist where date<=\'' + date + '\'' # 取到今天为止的所有日期数据
cursor.execute(sql)
result = cursor.fetchall()
pddata = pd.DataFrame(result)
if pddata.empty:
return None
else:
pddata = pddata[pddata['isopen'] == 1] # 留下全是交易日的数据
res = pddata.iloc[-n, 0]
return str(res)
#获取前N个交易日列表
@staticmethod
def getlastNtradedaylist(n) ->list:
if str(n).isalpha():
print('输入错误')
return None
conn = db.dbconnect()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
date = str(datetime.datetime.now().strftime('%Y-%m-%d'))
sql = 'select * from datelist where date<=\'' + date + '\'' # 取到今天为止的所有日期数据
cursor.execute(sql)
result = cursor.fetchall()
pddata = pd.DataFrame(result)
if pddata.empty:
return None
else:
pddata = pddata[pddata['isopen'] == 1] # 留下全是交易日的数据
pddata.reset_index(drop=True)
# print(pddata.index)
res = pddata.iloc[len(pddata)-n:len(pddata),0].values
datelist=[]
for date in res:
date=date.strftime('%Y-%m-%d')
# date=time.strftime('%Y-%m-%d',date)
# print(date)
datelist.append(str(date))
return datelist
if __name__ == '__main__':
test=tradeday()
# isopen=test.isTradeDay('2021-05-29')
# print(isopen)
lasttradeday=test.getlastNtradedaylist(7)
print(lasttradeday)
# test.gettradeday()
'''
--南向资金数据
CREATE TABLE IF NOT EXISTS `datelist`(
date date,
isopen int
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
'''