二.使用pandas.Resample函数转换日K为周K,日K转月K
from jqdatasdk import * import pandas as pd import time auth('193340***', '****') # 账号是申请时所填写的手机号;密码为聚宽官网登录密码 stocks = list(get_all_securities(['stock']).index) # for stock_code in stocks: # print("正在股票所有数据:"+stock_code) # df = get_price(stock_code, end_date="2022-05-17 14:00:00", count=5, frequency="1d", fields=['open', 'close', 'high', 'low', 'volume', 'money']) # print(df) # time.sleep(3) df = get_price("000001.XSHG", end_date="2021-05-20 14:00:00", count=20, frequency="1d", fields=['open', 'close', 'high', 'low', 'volume', 'money']) #添加一列数据。星期几,0-6,周一为0
2 df['weekday'] = df.index.weekday print(df) #转换周k df_week = pd.DataFrame() df_week['open'] = df['open'].resample('W').first() df_week['close'] = df['close'].resample('W').last() df_week['high'] = df['high'].resample('W').max() df_week['low'] = df['low'].resample('W').min() df_week['volume(sum)'] = df['volume'].resample('W').sum()#周汇总成交量 df_week['money(sum)'] = df['money'].resample('W').sum()#周汇总成交金额 print(df_week)
df = get_price("000001.XSHG", end_date="2021-05-20 14:00:00", start_date="2020-05-20 14:00:00", frequency="1d", fields=['open', 'close', 'high', 'low', 'volume', 'money']) #添加一列数据。星期几,0-6,周一为0 #2021-04-20 3467.15 3472.94 3494.30 ... 2.853001e+10 3.589828e+11 1 #2021-04-21 3456.30 3472.93 3481.25 ... 2.537779e+10 3.239141e+11 2 df['weekday'] = df.index.weekday print(df) #转换周-W-k,月M-K df_week = pd.DataFrame() df_week['open'] = df['open'].resample('M').first() df_week['close'] = df['close'].resample('M').last() df_week['high'] = df['high'].resample('M').max() df_week['low'] = df['low'].resample('M').min() df_week['volume(sum)'] = df['volume'].resample('M').sum()#月汇总成交量 df_week['money(sum)'] = df['money'].resample('M').sum()#月汇总成交金额 print(df_week) open close high ... volume money weekday 2021-04-20 3467.15 3472.94 3494.30 ... 2.853001e+10 3.589828e+11 1 2021-04-21 3456.30 3472.93 3481.25 ... 2.537779e+10 3.239141e+11 2 2021-04-22 3482.83 3465.11 3485.36 ... 2.511145e+10 3.170810e+11 3 2021-04-23 3462.09 3474.17 3482.36 ... 2.493604e+10 3.230159e+11 4 2021-04-26 3484.11 3441.17 3497.12 ... 2.769708e+10 3.881617e+11 0 2021-04-27 3440.09 3442.61 3443.85 ... 2.530324e+10 3.270460e+11 1 2021-04-28 3432.16 3457.07 3457.07 ... 2.473811e+10 3.496692e+11 2 2021-04-29 3458.08 3474.90 3478.23 ... 2.766314e+10 3.716116e+11 3 2021-04-30 3468.30 3446.86 3469.09 ... 3.126604e+10 4.014423e+11 4 2021-05-06 3446.07 3441.28 3471.24 ... 3.104242e+10 4.005252e+11 3 2021-05-07 3446.41 3418.87 3457.89 ... 3.537854e+10 4.110788e+11 4 2021-05-10 3423.59 3427.99 3429.74 ... 3.741709e+10 3.997175e+11 0 2021-05-11 3406.60 3441.85 3448.10 ... 3.509341e+10 3.903263e+11 1 2021-05-12 3429.75 3462.75 3466.37 ... 3.114445e+10 3.438604e+11 2 2021-05-13 3432.14 3429.54 3448.02 ... 3.193253e+10 3.563769e+11 3 2021-05-14 3436.09 3490.38 3490.64 ... 3.369823e+10 4.111169e+11 4 2021-05-17 3490.41 3517.62 3530.51 ... 3.221360e+10 4.244768e+11 0 2021-05-18 3520.65 3529.01 3529.01 ... 2.713086e+10 3.360288e+11 1 2021-05-19 3521.11 3510.96 3521.11 ... 2.783211e+10 3.499732e+11 2 2021-05-20 3500.88 3506.94 3517.74 ... 3.260093e+10 3.903553e+11 3
open close high low volume(sum) money(sum)
2020-05-31 2896.47 2852.35 2896.47 2802.47 1.583052e+11 1.875487e+12
2020-06-30 2871.96 2984.67 2990.83 2871.96 4.699820e+11 5.733483e+12
2020-07-31 2991.18 3310.01 3458.79 2984.98 1.024500e+12 1.324089e+13
2020-08-31 3332.18 3395.68 3456.72 3263.27 7.359126e+11 9.621175e+12
2020-09-30 3389.74 3218.05 3425.63 3202.34 4.906229e+11 6.193725e+12
2020-10-31 3262.61 3224.53 3371.09 3219.42 3.036961e+11 4.268415e+12
2020-11-30 3228.72 3391.76 3456.74 3209.91 5.680577e+11 7.256434e+12
2020-12-31 3388.99 3473.07 3474.92 3325.17 6.445146e+11 8.351828e+12
2021-01-31 3474.68 3483.07 3637.10 3446.55 6.639624e+11 9.659809e+12
2021-02-28 3477.17 3509.08 3731.69 3465.77 4.840738e+11 6.510009e+12
2021-03-31 3531.48 3441.91 3577.62 3328.31 7.440631e+11 8.402503e+12
2021-04-30 3444.81 3446.86 3497.12 3373.09 5.695268e+11 6.871377e+12
2021-05-31 3446.07 3506.94 3530.51 3384.70 3.554842e+11 4.213836e+12