1. 计算逻辑
日数据包含的字段有【日期,开盘价,收盘价,最低价,最高价,成交量,成交金额】,从日数据计算周数据、月数据、季度数据、年数据逻辑:
日数据 | 日期 | 开盘价 | 收盘价 | 最低价 | 最高价 | 成交量 | 成交金额 |
周数据 | 每周五日期 | 每周第一日开盘价 | 每周最后一日收盘价 | 本周最低价 | 本周最高价 | 本周每日成交量加和 | 本周每日成交金额加和 |
月数据 | 每个月最后一日 | 每月第一日开盘价 | 每月最后一日收盘价 | 本月最低价 | 本月最高价 | 本月每日成交量加和 | 本月每日成交金额加和 |
季度数据 | 每季度最后一日 | 每季度第一日开盘价 | 每季度最后一日收盘价 | 本季度最低价 | 本季度最高价 | 本季度每日成交量加和 | 本季度每日成交金额加和 |
年数据 | 每年最后一日 | 每年第一日开盘价 | 每年最后一日收盘价 | 本年最低价 | 本年最高价 | 本年每日成交量加和 | 本年每日成交金额加和 |
附加说明:表格中提交的“第一日”,“最后一日”,“每日”都指的是交易日
2. 计算过程
以000001日数据为例
date | open | close | low | high | volume | money |
2005/1/4 | 1.47 | 1.46 | 1.45 | 1.47 | 7867519 | 11465603 |
2005/1/5 | 1.46 | 1.45 | 1.42 | 1.47 | 14396761 | 20718559 |
2005/1/6 | 1.45 | 1.46 | 1.44 | 1.47 | 11913717 | 17333840 |
2005/1/7 | 1.47 | 1.46 | 1.45 | 1.48 | 8427452 | 12302853 |
2005/1/10 | 1.46 | 1.47 | 1.43 | 1.47 | 11760203 | 17111498 |
2005/1/11 | 1.47 | 1.47 | 1.47 | 1.47 | 0 | 0 |
2005/1/12 | 1.48 | 1.46 | 1.45 | 1.48 | 7321686 | 10672635 |
2005/1/13 | 1.46 | 1.47 | 1.45 | 1.48 | 7152368 | 10504590 |
2005/1/14 | 1.47 | 1.45 | 1.45 | 1.48 | 7170223 | 10476658 |
2005/1/17 | 1.45 | 1.4 | 1.39 | 1.45 | 14170065 | 20039971 |
2005/1/18 | 1.4 | 1.39 | 1.38 | 1.41 | 11580278 | 16109620 |
2005/1/19 | 1.39 | 1.38 | 1.38 | 1.4 | 7530456 | 10416563 |
2005/1/20 | 1.38 | 1.35 | 1.34 | 1.38 | 17644127 | 23815009 |
2005/1/21 | 1.34 | 1.43 | 1.31 | 1.45 | 44849867 | 62408193 |
column=['date','open','close','low','high','volume','money']
df = pd.read_csv('000001.csv',encoding='utf-8')
df['date'] = pd.to_datetime(df['date'])
week_group = df.resample('W-FRI',on='date')
month_group = df.resample('M',on='date')
quarter_group = df.resample('Q',on='date')
year_group = df.resample('A',on='date')
week_df = week_group.last()
week_df['endDate'] = week_df.index.get_level_values('date')
week_df['open'] = week_group.first()['open']
week_df['low'] = week_group.min()['low']
week_df['high'] = week_group.max()['high']
week_df['volume'] = week_group.sum()['volume']
week_df['money'] = week_group.sum()['money']
week_df.rename(columns={'date':'ori_date','endDate':'date'},inplace=True)
month_df = month_group.last()
month_df['endDate'] = month_df.index.get_level_values('date')
month_df['open'] = month_group.first()['open']
month_df['low'] = month_group.min()['low']
month_df['high'] = month_group.max()['high']
month_df['volume'] = month_group.sum()['volume']
month_df['money'] = month_group.sum()['money']
month_df.rename(columns={'date': 'ori_date', 'endDate': 'date'}, inplace=True)
quarter_df = quarter_group.last()
quarter_df['endDate'] = quarter_df.index.get_level_values('date')
quarter_df['open'] = quarter_group.first()['open']
quarter_df['low'] = quarter_group.min()['low']
quarter_df['high'] = quarter_group.max()['high']
quarter_df['volume'] = quarter_group.sum()['volume']
quarter_df['money'] = quarter_group.sum()['money']
quarter_df.rename(columns={'date': 'ori_date', 'endDate': 'date'}, inplace=True)
year_df = year_group.last()
year_df['endDate'] = year_df.index.get_level_values('date')
year_df['open'] = year_group.first()['open']
year_df['low'] = year_group.min()['low']
year_df['high'] = year_group.max()['high']
year_df['volume'] = year_group.sum()['volume']
year_df['money'] = year_group.sum()['money']
year_df.rename(columns={'date': 'ori_date', 'endDate': 'date'}, inplace=True)
附加说明:周数据重采样W-FRI表示取周五的数据,否则默认W是取得周日的数据