主要代码:
import data.stock as st
import time
start_date='2021-07-01'
oneday_time=24*60*60
stock_list=st.get_stock_list()[:5]
def timer():
now_date=time.strftime('%Y-%m-%d')
now_strptime=int(time.time())
close_strptime = int(time.mktime(time.strptime(now_date+' 16:00:00', "%Y-%m-%d %H:%M:%S")))
yesterday_strptime = int(time.mktime(time.strptime(now_date, "%Y-%m-%d")))-oneday_time
yesterday_date=time.strftime('%Y-%m-%d',time.localtime(yesterday_strptime))
tomorrow_strptime = int(time.mktime(time.strptime(now_date, "%Y-%m-%d")))+oneday_time
tomorrow_date=time.strftime('%Y-%m-%d',time.localtime(tomorrow_strptime))
sleep_time=tomorrow_strptime-now_strptime
close_time=close_strptime-now_strptime
end_date=now_date
if close_time>0:
sleep_time=close_time
end_date=yesterday_date
for code in stock_list:
try:
data=st.get_csv_data(code=code,type='price')
last_tomorrow_date=time.mktime(time.strptime(data.date[data.date.size - 1],'%Y-%m-%d'))+oneday_time
tomorrow_date=time.strftime('%Y-%m-%d',time.localtime(last_tomorrow_date))
data = st.get_single_price(
code=code,
time_freq='daily',
start_date=tomorrow_date,
end_date=end_date
)
st.export_data(
data=data,
filename=code,
type='price',
mode='a'
)
except Exception as err:
print('当前股票不存在或追加失败,重新存储 csv!',err)
data = st.get_single_price(
code=code,
time_freq='daily',
start_date=start_date,
end_date=end_date
)
st.export_data(
data=data,
filename=code,
type='price'
)
return sleep_time;
while True:
sleep_time=timer()
print('更新完毕!',sleep_time)
time.sleep(sleep_time)
其它
def export_data(data,filename,type,mode='w'):
"""
导出股票相关数据
:param data:
:param filename:
:param type: 股票数据类型,可以是->price、finance
:param mode: 默认w->只能写入,a->追加
:return:
"""
file_root=data_root+type+'/'+filename+'.csv'
data.index.names=['date']
header=True
if mode=='a':
header=False
data.to_csv(file_root,mode=mode,header=header)
print('已经成功存储至:',file_root)