繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

基于日K线生成周K线【与分析家、飞狐交易师一致】

作者/1xxxxxxxx 2019-05-14 17:39 0 来源: FX168财经网人物频道

利用pandas的resample方法,生成周k线数据:

  • 周线数据以每周的第一个交易日,日期为周K线索引;
    -取每自然周的交易日的第一交易日开盘价为周开盘价;
    -周内最后一个交易日的收盘价为收盘价;
    -周内最高价为最高价;
    -周内最低价为最低价;
    -成交量和成交额取和值。

注意:
研究里有2个版本的内核,python2和python 2(PacVer 2.0)
根据打印pandas的版本,发现有差异。python2的pandas是0.16的,而python 2(PacVer 2.0)的是0.20的。高版本的pandas在实现上语法更为简洁一些。

其实为了加快运行速度,这类数据反复运算消耗资源,我还是建议聚宽方面,把周、月、季、年 K线数据生成好,用户使用时从数据库直接查询提供,比放dataFrame里来回计算要节约资源的多。
周线是最麻烦的。月线、季线、年线就简单的多了。等到都做完,封装成方法之后,我会共享在函数库中,便于使用。或者有哪位兄弟比较有时间,抢先一步分享函数,个人也拿来主义用用。

利用日线生成周K线数据¶

利用pandas的resample方法,生成周k线数据。周线数据以每周的第一个交易日,日期为周K线索引。取每自然周的交易日的第一交易日开盘价为周开盘价,周内最后一个交易日的收盘价为收盘价,周内最高价为最高价,周内最低价为最低价,成交量和成交额取和值。 研究里有2个版本的内核,python2和python 2(PacVer 2.0) 根据打印pandas的版本,发现有差异。python2的pandas是0.16的,而python 2(PacVer 2.0)的是0.20的。高版本的pandas在实现上语法更为简洁一些。下面请看代码:

import pandas as pd
print pd.__version__ 
#新老版本pandas都取示例数据放在df对象里,随便选了个600030作为标的。这里要注意,skip_paused=True要设置,以免影响到索引值。
#赋权根据自己的要求来设置,如果要产生相应赋权的周线,就得让日线先在对应的赋权状态
df = get_price('600030.XSHG', start_date='2017-01-01', end_date='2018-01-25', frequency='1d',skip_paused=True,fq='pre')

#dp = get_price(['000300.XSHG','600030.XSHG'], start_date='2018-01-01', end_date='2018-01-25', frequency='1d',skip_paused=False,fq='pre')
#增加一列date列,用于生成周线的日期索引
df['date']=df.index
#打印一下信息,看看
#dp['close'][:2]
df
0.20.3
open close high low volume money date
2017-01-03 15.75 15.86 15.90 15.71 63620068.0 1.007140e+09 2017-01-03
2017-01-04 15.85 15.89 15.93 15.80 52760756.0 8.375141e+08 2017-01-04
2017-01-05 15.90 15.82 15.92 15.79 48870988.0 7.740279e+08 2017-01-05
2017-01-06 15.84 15.68 15.84 15.66 52752831.0 8.293983e+08 2017-01-06
2017-01-09 15.68 15.74 15.79 15.64 40680379.0 6.395246e+08 2017-01-09
2017-01-10 15.70 15.78 15.88 15.69 46358711.0 7.326209e+08 2017-01-10
2017-01-11 15.81 15.77 15.90 15.77 40718031.0 6.445079e+08 2017-01-11
2017-01-12 15.78 15.82 15.94 15.76 57138829.0 9.063276e+08 2017-01-12
2017-01-13 15.81 15.88 16.01 15.69 84259946.0 1.337354e+09 2017-01-13
2017-01-16 15.85 16.02 16.09 15.69 175717493.0 2.795619e+09 2017-01-16
2017-01-17 15.92 15.92 15.98 15.79 46520006.0 7.391539e+08 2017-01-17
2017-01-18 15.88 15.93 16.09 15.85 49059293.0 7.838194e+08 2017-01-18
2017-01-19 15.90 15.93 16.05 15.90 40678397.0 6.501369e+08 2017-01-19
2017-01-20 15.94 16.07 16.09 15.91 64490763.0 1.034924e+09 2017-01-20
2017-01-23 16.06 16.10 16.25 16.06 48499782.0 7.830028e+08 2017-01-23
2017-01-24 16.12 16.04 16.12 16.01 41086050.0 6.594562e+08 2017-01-24
2017-01-25 16.04 16.05 16.05 15.98 32898172.0 5.270127e+08 2017-01-25
2017-01-26 16.09 16.14 16.22 16.09 47810174.0 7.729048e+08 2017-01-26
2017-02-03 16.21 16.03 16.21 16.02 27857273.0 4.483021e+08 2017-02-03
2017-02-06 16.07 16.00 16.09 15.92 45739900.0 7.311071e+08 2017-02-06
2017-02-07 15.99 15.96 16.02 15.90 32460931.0 5.177569e+08 2017-02-07
2017-02-08 15.95 16.18 16.26 15.86 81693977.0 1.312890e+09 2017-02-08
2017-02-09 16.13 16.17 16.32 16.11 66131622.0 1.071782e+09 2017-02-09
2017-02-10 16.17 16.33 16.41 16.17 81970312.0 1.335786e+09 2017-02-10
2017-02-13 16.35 16.35 16.48 16.31 76536506.0 1.255339e+09 2017-02-13
2017-02-14 16.40 16.30 16.46 16.23 61805616.0 1.009170e+09 2017-02-14
2017-02-15 16.29 16.22 16.42 16.19 61407812.0 1.001180e+09 2017-02-15
2017-02-16 16.22 16.37 16.39 16.17 68247581.0 1.112894e+09 2017-02-16
2017-02-17 16.42 16.33 16.73 16.28 123624739.0 2.043051e+09 2017-02-17
2017-02-20 16.33 16.53 16.57 16.24 96248504.0 1.575748e+09 2017-02-20
... ... ... ... ... ... ... ...
2017-12-14 18.41 18.14 18.45 18.06 76469313.0 1.391583e+09 2017-12-14
2017-12-15 18.17 17.95 18.23 17.92 77736629.0 1.403545e+09 2017-12-15
2017-12-18 17.95 18.03 18.23 17.92 58531399.0 1.055856e+09 2017-12-18
2017-12-19 18.03 18.27 18.29 17.98 79928721.0 1.452203e+09 2017-12-19
2017-12-20 18.26 17.87 18.26 17.86 87315687.0 1.571652e+09 2017-12-20
2017-12-21 17.84 18.19 18.33 17.78 113695808.0 2.063078e+09 2017-12-21
2017-12-22 18.15 18.19 18.33 18.13 52847665.0 9.629686e+08 2017-12-22
2017-12-25 18.23 18.06 18.35 18.00 66201336.0 1.204270e+09 2017-12-25
2017-12-26 18.01 18.41 18.43 17.95 99335812.0 1.817544e+09 2017-12-26
2017-12-27 18.35 18.10 18.40 18.05 78546460.0 1.431445e+09 2017-12-27
2017-12-28 18.06 18.12 18.45 17.98 102101410.0 1.864401e+09 2017-12-28
2017-12-29 18.12 18.10 18.26 18.00 64352159.0 1.165504e+09 2017-12-29
2018-01-02 18.13 18.44 18.52 18.13 139328457.0 2.562810e+09 2018-01-02
2018-01-03 18.36 18.61 18.85 18.35 151966900.0 2.831080e+09 2018-01-03
2018-01-04 18.64 18.67 18.84 18.47 114791454.0 2.141315e+09 2018-01-04
2018-01-05 18.68 18.88 19.09 18.59 172884868.0 3.271662e+09 2018-01-05
2018-01-08 19.00 19.54 19.80 18.91 248970642.0 4.827830e+09 2018-01-08
2018-01-09 19.55 19.44 19.87 19.28 143919389.0 2.805148e+09 2018-01-09
2018-01-10 19.47 19.61 19.74 19.23 150258482.0 2.923746e+09 2018-01-10
2018-01-11 19.46 19.28 19.55 19.12 113379540.0 2.185830e+09 2018-01-11
2018-01-12 19.25 19.33 19.58 19.12 104603804.0 2.023121e+09 2018-01-12
2018-01-15 19.25 19.45 19.69 18.98 224135432.0 4.355326e+09 2018-01-15
2018-01-16 19.26 20.25 20.37 19.19 233073315.0 4.587774e+09 2018-01-16
2018-01-17 20.50 20.94 21.89 20.50 497887681.0 1.051009e+10 2018-01-17
2018-01-18 21.15 21.41 21.64 20.90 295669219.0 6.301946e+09 2018-01-18
2018-01-19 21.36 21.29 21.85 20.92 363017665.0 7.792348e+09 2018-01-19
2018-01-22 21.10 21.20 21.44 20.94 180999744.0 3.833396e+09 2018-01-22
2018-01-23 21.37 21.21 21.86 20.90 335664503.0 7.140624e+09 2018-01-23
2018-01-24 21.40 22.92 22.95 21.15 507088583.0 1.120049e+10 2018-01-24
2018-01-25 22.50 22.33 22.85 22.24 317331694.0 7.162054e+09 2018-01-25

262 rows × 7 columns

#新版本内核(pandas 0.20)实现方式¶

#直接使用resample方法搞定
df2=df.resample('W').agg({'open': 'first', 
                     'close': 'last', 
                     'high': 'max', 
                     'low': 'min',
                     'money':'sum',
                     'volume':'sum',
                     'date':'first'})
#主要是让索引使用我们定义的每周第一个交易日,而不是星期日

#把索引的别名删了,保持数据视图一致性

df2=df2[df2['open'].notnull()]
df2.reset_index(inplace=True)


df2.set_index('date',inplace=True)
del df2['index']
del df2.index.name
#打印信息
df2
volume money high low close open
2017-01-03 2.180046e+08 3.448080e+09 15.93 15.66 15.68 15.75
2017-01-09 2.691559e+08 4.260335e+09 16.01 15.64 15.88 15.68
2017-01-16 3.764660e+08 6.003653e+09 16.09 15.69 16.07 15.85
2017-01-23 1.702942e+08 2.742377e+09 16.25 15.98 16.14 16.06
2017-02-03 2.785727e+07 4.483021e+08 16.21 16.02 16.03 16.21
2017-02-06 3.079967e+08 4.969322e+09 16.41 15.86 16.33 16.07
2017-02-13 3.916223e+08 6.421635e+09 16.73 16.17 16.33 16.35
2017-02-20 3.584853e+08 5.891984e+09 16.75 16.24 16.37 16.33
2017-02-27 2.598450e+08 4.206835e+09 16.42 15.95 16.03 16.31
2017-03-06 1.915477e+08 3.067753e+09 16.13 15.89 15.90 16.00
2017-03-13 2.834511e+08 4.556086e+09 16.30 15.79 15.97 15.90
2017-03-20 2.823598e+08 4.477019e+09 16.03 15.70 15.95 16.02
2017-03-27 2.235308e+08 3.535581e+09 16.05 15.66 15.78 15.92
2017-04-05 1.547144e+08 2.462549e+09 15.99 15.79 15.94 15.80
2017-04-10 3.002497e+08 4.785690e+09 16.15 15.73 15.85 15.93
2017-04-17 2.247704e+08 3.537721e+09 15.92 15.53 15.91 15.78
2017-04-24 2.595218e+08 4.117886e+09 16.00 15.71 15.90 15.87
2017-05-02 2.466684e+08 3.839831e+09 15.92 15.22 15.45 15.91
2017-05-08 2.271525e+08 3.464628e+09 15.48 14.94 15.33 15.30
2017-05-15 2.160621e+08 3.349251e+09 15.71 15.35 15.39 15.35
2017-05-22 4.229663e+08 6.686568e+09 16.34 15.16 16.05 15.39
2017-05-31 1.971193e+08 3.177974e+09 16.41 15.94 16.04 16.18
2017-06-05 3.407556e+08 5.479243e+09 16.40 15.80 16.23 16.02
2017-06-12 2.635834e+08 4.244843e+09 16.38 15.95 15.97 16.17
2017-06-19 5.573577e+08 9.188684e+09 16.83 15.91 16.59 15.92
2017-06-26 5.439984e+08 9.136007e+09 17.05 16.47 16.67 16.59
2017-07-03 3.841730e+08 6.322371e+09 16.70 16.26 16.60 16.65
2017-07-10 6.843106e+08 1.157603e+10 17.17 16.56 17.02 16.58
2017-07-17 7.973258e+08 1.352791e+10 17.46 16.30 17.16 16.96
2017-07-24 4.537773e+08 7.752429e+09 17.32 16.85 16.92 17.13
2017-07-31 6.463520e+08 1.114122e+10 17.49 16.85 17.20 16.91
2017-08-07 4.882033e+08 8.225036e+09 17.16 16.50 16.52 17.10
2017-08-14 2.849387e+08 4.755836e+09 16.84 16.41 16.70 16.57
2017-08-21 3.811794e+08 6.435678e+09 17.15 16.60 17.14 16.70
2017-08-28 1.026149e+09 1.861162e+10 18.52 17.27 18.30 17.31
2017-09-04 4.464087e+08 8.072975e+09 18.35 17.72 17.77 18.27
2017-09-11 4.189278e+08 7.524671e+09 18.22 17.66 18.03 17.70
2017-09-18 5.045833e+08 9.190243e+09 18.48 17.81 18.14 18.08
2017-09-25 3.134206e+08 5.670006e+09 18.35 17.89 18.19 18.10
2017-10-09 3.703520e+08 6.728820e+09 18.76 17.92 18.00 18.64
2017-10-16 3.198862e+08 5.739497e+09 18.23 17.68 17.78 18.00
2017-10-23 2.746486e+08 4.889915e+09 18.05 17.53 17.86 17.79
2017-10-30 3.877810e+08 6.703056e+09 17.83 16.94 17.27 17.80
2017-11-06 7.081138e+08 1.274872e+10 18.52 16.99 18.39 17.11
2017-11-13 6.528992e+08 1.185311e+10 18.61 17.77 18.30 18.37
2017-11-20 1.471590e+09 2.809600e+10 19.95 17.40 19.01 18.15
2017-11-27 9.140774e+08 1.745698e+10 19.90 18.50 18.94 18.89
2017-12-04 7.204366e+08 1.367074e+10 19.45 18.40 18.51 18.80
2017-12-11 4.084757e+08 7.500015e+09 18.78 17.92 17.95 18.46
2017-12-18 3.923193e+08 7.105757e+09 18.33 17.78 18.19 17.95
2017-12-25 4.105372e+08 7.483164e+09 18.45 17.95 18.10 18.23
2018-01-02 5.789717e+08 1.080687e+10 19.09 18.13 18.88 18.13
2018-01-08 7.611319e+08 1.476568e+10 19.87 18.91 19.33 19.00
2018-01-15 1.613783e+09 3.354749e+10 21.89 18.98 21.29 19.25
2018-01-22 1.341085e+09 2.933657e+10 22.95 20.90 22.33 21.10

#老版本内核(pandas 0.16,实际上0.18以下都得这样写)实现¶

#以下的代码大部分从网上复制,并且在编写过程中,得到简书“邢不行”的微信协助。感谢邢不行量化的指点。
#so不更改他的原始变量名称体系和大部分注解
period_type = 'W'
#进行转换,周线的每个变量都等于那一周中最后一个交易日的变量值
period_stock_data = df.resample('W',how='last')
#周线的open等于那一周中第一个交易日的open
period_stock_data['open'] = df['open'].resample(period_type,how='first')
#周线的high等于那一周中的high的最大值
period_stock_data['high'] = df['high'].resample(period_type,how='max')
#周线的low等于那一周中的low的最大值
period_stock_data['low'] = df['low'].resample(period_type,how='min')
#周线的volume和money等于那一周中volume和money各自的和
period_stock_data['volume'] = df['volume'].resample(period_type,how='sum')
period_stock_data['money'] = df['money'].resample(period_type,how='sum')

#这里给新数据集增加一个index字段,对应原始数据的date字段,并取每周的第一个交易日
period_stock_data['date']=df['date'].resample(period_type,how='first')

#股票在有些周一天都没有交易,将这些周去除。注意:这里邢不行的代码中用的股票的code,我这里只针对单只股票,所以随便选了个字段
#没有交易的周所有字段都是Nan
period_stock_data = period_stock_data[period_stock_data['open'].notnull()]
#重整索引
period_stock_data.reset_index(inplace=True)
#设置每周第一个交易日的日期为索引
period_stock_data.set_index('date',inplace=True)

#删除索引的别名,不然显示会乖乖的,感觉多了一行空数据似的
del period_stock_data.index.name
#把原始数据集带过来的index列删掉
del period_stock_data['index']

#打印【和分析家、飞狐一致的周K数据就出来了】
period_stock_data
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:5: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).last()
  """
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:7: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).first()
  import sys
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:9: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).max()
  if __name__ == '__main__':
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:11: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).min()
  # This is added back by InteractiveShellApp.init_path()
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:13: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).sum()
  del sys.path[0]
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:14: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).sum()
  
/opt/conda/envs/python2new/lib/python2.7/site-packages/ipykernel_launcher.py:17: FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...).first()
open close high low volume money
2017-01-03 15.75 15.68 15.93 15.66 2.180046e+08 3.448080e+09
2017-01-09 15.68 15.88 16.01 15.64 2.691559e+08 4.260335e+09
2017-01-16 15.85 16.07 16.09 15.69 3.764660e+08 6.003653e+09
2017-01-23 16.06 16.14 16.25 15.98 1.702942e+08 2.742377e+09
2017-02-03 16.21 16.03 16.21 16.02 2.785727e+07 4.483021e+08
2017-02-06 16.07 16.33 16.41 15.86 3.079967e+08 4.969322e+09
2017-02-13 16.35 16.33 16.73 16.17 3.916223e+08 6.421635e+09
2017-02-20 16.33 16.37 16.75 16.24 3.584853e+08 5.891984e+09
2017-02-27 16.31 16.03 16.42 15.95 2.598450e+08 4.206835e+09
2017-03-06 16.00 15.90 16.13 15.89 1.915477e+08 3.067753e+09
2017-03-13 15.90 15.97 16.30 15.79 2.834511e+08 4.556086e+09
2017-03-20 16.02 15.95 16.03 15.70 2.823598e+08 4.477019e+09
2017-03-27 15.92 15.78 16.05 15.66 2.235308e+08 3.535581e+09
2017-04-05 15.80 15.94 15.99 15.79 1.547144e+08 2.462549e+09
2017-04-10 15.93 15.85 16.15 15.73 3.002497e+08 4.785690e+09
2017-04-17 15.78 15.91 15.92 15.53 2.247704e+08 3.537721e+09
2017-04-24 15.87 15.90 16.00 15.71 2.595218e+08 4.117886e+09
2017-05-02 15.91 15.45 15.92 15.22 2.466684e+08 3.839831e+09
2017-05-08 15.30 15.33 15.48 14.94 2.271525e+08 3.464628e+09
2017-05-15 15.35 15.39 15.71 15.35 2.160621e+08 3.349251e+09
2017-05-22 15.39 16.05 16.34 15.16 4.229663e+08 6.686568e+09
2017-05-31 16.18 16.04 16.41 15.94 1.971193e+08 3.177974e+09
2017-06-05 16.02 16.23 16.40 15.80 3.407556e+08 5.479243e+09
2017-06-12 16.17 15.97 16.38 15.95 2.635834e+08 4.244843e+09
2017-06-19 15.92 16.59 16.83 15.91 5.573577e+08 9.188684e+09
2017-06-26 16.59 16.67 17.05 16.47 5.439984e+08 9.136007e+09
2017-07-03 16.65 16.60 16.70 16.26 3.841730e+08 6.322371e+09
2017-07-10 16.58 17.02 17.17 16.56 6.843106e+08 1.157603e+10
2017-07-17 16.96 17.16 17.46 16.30 7.973258e+08 1.352791e+10
2017-07-24 17.13 16.92 17.32 16.85 4.537773e+08 7.752429e+09
2017-07-31 16.91 17.20 17.49 16.85 6.463520e+08 1.114122e+10
2017-08-07 17.10 16.52 17.16 16.50 4.882033e+08 8.225036e+09
2017-08-14 16.57 16.70 16.84 16.41 2.849387e+08 4.755836e+09
2017-08-21 16.70 17.14 17.15 16.60 3.811794e+08 6.435678e+09
2017-08-28 17.31 18.30 18.52 17.27 1.026149e+09 1.861162e+10
2017-09-04 18.27 17.77 18.35 17.72 4.464087e+08 8.072975e+09
2017-09-11 17.70 18.03 18.22 17.66 4.189278e+08 7.524671e+09
2017-09-18 18.08 18.14 18.48 17.81 5.045833e+08 9.190243e+09
2017-09-25 18.10 18.19 18.35 17.89 3.134206e+08 5.670006e+09
2017-10-09 18.64 18.00 18.76 17.92 3.703520e+08 6.728820e+09
2017-10-16 18.00 17.78 18.23 17.68 3.198862e+08 5.739497e+09
2017-10-23 17.79 17.86 18.05 17.53 2.746486e+08 4.889915e+09
2017-10-30 17.80 17.27 17.83 16.94 3.877810e+08 6.703056e+09
2017-11-06 17.11 18.39 18.52 16.99 7.081138e+08 1.274872e+10
2017-11-13 18.37 18.30 18.61 17.77 6.528992e+08 1.185311e+10
2017-11-20 18.15 19.01 19.95 17.40 1.471590e+09 2.809600e+10
2017-11-27 18.89 18.94 19.90 18.50 9.140774e+08 1.745698e+10
2017-12-04 18.80 18.51 19.45 18.40 7.204366e+08 1.367074e+10
2017-12-11 18.46 17.95 18.78 17.92 4.084757e+08 7.500015e+09
2017-12-18 17.95 18.19 18.33 17.78 3.923193e+08 7.105757e+09
2017-12-25 18.23 18.10 18.45 17.95 4.105372e+08 7.483164e+09
2018-01-02 18.13 18.88 19.09 18.13 5.789717e+08 1.080687e+10
2018-01-08 19.00 19.33 19.87 18.91 7.611319e+08 1.476568e+10
2018-01-15 19.25 21.29 21.89 18.98 1.613783e+09 3.354749e+10
2018-01-22 21.10 22.33 22.95 20.90 1.341085e+09 2.933657e+10

ok,效果已经出来了。需要的自己封装成方法,调用即可。

 
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2192文章4145粉丝45

    暂无个人简介信息

  • 王启蒙现货黄金

    人气296文章3215粉丝8

    本人做分析师以来,并专注于贵金属投资市场,尤其是在现货黄金...

  • 指导老师

    人气1864文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2320文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 梁孟梵

    人气2176文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2328文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4760文章8329粉丝119

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...

FX168财经

FX168财经学院

FX168财经

FX168北美