量化投资学习之【5】极简方法将日线数据转为周线、月线或其他周期

文章来源: 量化小课堂

本篇文章实现将日线数据转换为周线数据,并保存到csv格式文件中。

#coding: utf-8
import pandas as pd

stock_data = pd.read_csv('all_trading_data/stock data/sh600000.csv', parse_dates=[1])

period_type = 'w'

stock_data.set_index('date', inplace=True)

period_stock_data = stock_data.resample(period_type, how='last')
period_stock_data['change'] = stock_data['change'].resample(period_type, how=lambda x:(x+1.0).prod()-1.0)
period_stock_data['open'] = stock_data['open'].resample(period_type, how='first')
period_stock_data['high'] = stock_data['high'].resample(period_type, how='max')
period_stock_data['low'] = stock_data['low'].resample(period_type, how='min')
period_stock_data['volume'] = stock_data['volume'].resample(period_type, how='sum')
period_stock_data['money'] = stock_data['money'].resample(period_type, how='sum')
period_stock_data['turnover'] = period_stock_data['volume'] / \
                                (period_stock_data['traded_market_value'] / period_stock_data['close'])

period_stock_data = period_stock_data[period_stock_data['code'].notnull()]
period_stock_data.reset_index(inplace=True)

period_stock_data.to_csv('week_stock_data.csv', index=False)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容