目前可获得的交易数据主要有日线数据与分时数据两种,其它周期数据如5分种、15分钟、30分钟与60分钟数据都是根据分时数据生成的,周线、月线、季线和年线是根据日线数据生成的。为了在我们自已的应用中对周期数据时行分析需要根进行多周期数据的生成。
将可用的周期数据(用得比较多的是日线数据),装入pandas的DataFrame中,利用pandas的运算能实现需要周期的数据DataFrame。
- def read_w_stock_line(self, stock_dat):
- T_FRQ = 'W-FRI'
- # stock_dat['date'] = pd.to_datetime(df['date'], format='%Y%m%d')
- stock_dat.set_index('date', inplace=True)
- week_df = pd.DataFrame()
- week_df['open'] = stock_dat['open'].resample(T_FRQ, closed='right', label='right').first()
- week_df['close'] = stock_dat['close'].resample(T_FRQ, closed='right', label='right').last()
- week_df['high'] = stock_dat['high'].resample(T_FRQ, closed='right', label='right').max()
- week_df['low'] = stock_dat['low'].resample(T_FRQ, closed='right', label='right').min()
- week_df['amount'] = stock_dat['amount'].resample(T_FRQ, closed='right', label='right').sum()
- week_df['vol'] = stock_dat['vol'].resample(T_FRQ, closed='right', label='right').sum()
- week_df = week_df.dropna(axis=0, how='any')
- return week_df
主要点是利用了,DateFrame的resample函数。其它周期的数据也可以同样实现。