在做股票相关的项目,需要把通达信的5分钟k线数据转为15、30、60分钟线来做后续处理,参考了一些资料,发现pandas的resample可以实现。
#通过5分钟线生成15、30、60分钟线
def changeLc5Cycle(stockid,cycle):
cycle_list = ['15min', '30min', '60min']
if cycle not in cycle_list:
return
# 获取股票5分钟数据
conn = dbopen()
sql = "select tradingday,id,stockid,open,close,high,low,volume from stock_5min where stockid=%s"
val = (
stockid
)
df = SQLQuery(conn,sql,val)
df.set_index(['tradingday'], inplace=True)
if cycle=='60min':
date = []
for i in df.index:
if i.hour<12:
i = i + pandas.Timedelta('00:30:00')
date.append(i)
df = df.assign(Date=pd.Series(date, index=df.index))
df.set_index(['Date'], inplace=True)
#print(df)
# 重新采样各列数据
df_open = round(df['open'].resample(rule=cycle, closed='right', label='right').first(), 2)
df_close = round(df['close'].resample(rule=cycle, closed='right', label='right').last(), 2)
df_high = round(df['high'].resample(rule=cycle, closed='right', label='right').max(), 2)
df_low = round(df['low'].resample(rule=cycle, closed='right', label='right').min(), 2)
df_volume = round(df['volume'].resample(rule=cycle, closed='right', label='right').sum(), 2)
# 生成新周期数据
df_new = pd.DataFrame()
df_new = df_new.assign(open=df_open)
df_new = df_new.assign(close=df_close)
df_new = df_new.assign(high=df_high)
df_new = df_new.assign(low=df_low)
df_new = df_new.assign(volume=df_volume)
# 去除空值
df_new = df_new.dropna()
if cycle=='60min':
date = []
for i in df_new.index:
if i.hour<=12:
i = i - pandas.Timedelta('00:30:00')
date.append(i)
df_new = df_new.assign(Date=pd.Series(date, index=df_new.index))
df_new.set_index(['Date'], inplace=True)
#写入数据库
for index, row in df_new.iterrows():
#此处代码省略
conn.close()
return
发现resample在处理60分钟重采样时是按小时采样的,股市上午是9:30到11:30,直接重采样的话会出3段数据,因此在处理60分钟数据时先对早上时段的全部5分钟线加了30分钟,重采样完后再对早上的各60分钟线减回30分钟,这样就只出现2段数据了。
参考了
python通达信5分钟转,10分钟,15分钟,30分钟,60分钟,量化交易,K线_通达信15分钟止损-CSDN博客
Pandas中resample方法详解_pandas resample-CSDN博客
标签:15,Python,resample,30,分钟,60,cycle From: https://blog.csdn.net/fsyysf/article/details/142864364