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)
==============================================
日K转换为月K
==============================================
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 ... 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
标签:11,10,函数,04,...,df,Resample,05,pandas From: https://www.cnblogs.com/gzhbk/p/17412437.html