请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365877 新帖:10

分享一个多股票获得周线数据并创建panel或dataframe的实现思路

交易资深人士发表于:5 月 10 日 08:01回复(1)

最近一直想获得周线的数据,首先想到的是用history或者attribu_history函数,unit改成‘5d’,也就是以五天的数据为一个周期,后来发现这样执行下去完全不可靠。首先一周不一定有五天,比如在节假日的时候,一周的交易日到不了五天。再者,用这种方法,获得的数据并不是按周的节点进行分割,而是每五个交易日划分一个节点,如果恰好是在周五执行,那么能把一周五天的交易日分在一起。否则,就完全混乱了,跟主流交易软件的行情数据不一致。

然后,看社区里以及函数库里各位老师的文章,才想到可以用get_bars函数。

截了一张图,就是下面酱样子啦~

get_bars.png

import pandas as pd
data = {}
#设置股票池,此处以次新股为例
stocks=get_concept_stocks('GN189')
for stock in stocks:
    #获取5个单位长度为‘5d’的开高低收数据
    array1 = attribute_history(stock,5,unit = '5d',fields = ['open','close','high','low'])
    #获得一个字典,key为股票名称,value为df数据
    data[stock]  = array1
#根据data字典创建panel
p = pd.Panel(data)
p
<class 'pandas.core.panel.Panel'>
Dimensions: 27 (items) x 11 (major_axis) x 4 (minor_axis)
Items axis: 002932.XSHE to 603713.XSHG
Major_axis axis: 2018-08-27 00:00:00 to 2018-09-25 00:00:00
Minor_axis axis: open to low
p['603713.XSHG']
open close high low
2018-08-27 42.58 42.95 46.50 38.70
2018-08-29 NaN NaN NaN NaN
2018-08-31 NaN NaN NaN NaN
2018-09-02 NaN NaN NaN NaN
2018-09-03 43.08 48.05 49.22 42.54
2018-09-05 NaN NaN NaN NaN
2018-09-07 NaN NaN NaN NaN
2018-09-10 50.50 42.08 58.10 41.00
2018-09-12 NaN NaN NaN NaN
2018-09-17 41.40 38.56 42.65 37.81
2018-09-25 38.49 39.51 41.36 37.89
for stock in stocks:
    p[stock].dropna(inplace = True)
p['603713.XSHG']
open close high low
2018-08-27 42.58 42.95 46.50 38.70
2018-09-03 43.08 48.05 49.22 42.54
2018-09-10 50.50 42.08 58.10 41.00
2018-09-17 41.40 38.56 42.65 37.81
2018-09-25 38.49 39.51 41.36 37.89
frames =[]
for stock in stocks: 
    array1 = get_bars(stock,4,unit = '1w',fields = ['open','close','high','low']) 
    s = pd.DataFrame(array1, index=[stock]*len(array1), columns=['open', 'close', 'high', 'low']) 
    frames.append(s) 
    result = pd.concat(frames)
result
open close high low
002932.XSHE 69.01 68.72 73.88 67.99
002932.XSHE 67.53 63.38 67.71 61.94
002932.XSHE 63.38 61.00 69.31 61.00
002932.XSHE 58.09 57.60 60.24 56.94
002933.XSHE 26.94 43.03 43.03 26.94
002933.XSHE 47.33 69.30 69.30 47.33
002933.XSHE 73.00 61.48 73.95 61.48
002933.XSHE 58.81 59.03 62.40 56.03
002935.XSHE 25.59 40.87 40.87 25.59
002935.XSHE 44.96 59.16 65.84 44.96
002935.XSHE 55.50 53.73 59.55 52.30
002938.XSHE 19.28 26.17 30.00 19.28
300724.XSHE 32.43 30.94 35.35 30.68
300724.XSHE 31.69 34.47 34.80 30.11
300724.XSHE 34.48 28.15 37.00 28.15
300724.XSHE 27.38 26.30 28.09 25.82
300746.XSHE 23.68 22.68 25.02 22.61
300746.XSHE 22.00 23.79 24.80 20.95
300746.XSHE 23.36 21.28 24.18 21.05
300746.XSHE 20.97 20.93 21.97 20.00
300747.XSHE 160.51 161.00 177.00 160.51
300747.XSHE 160.11 169.50 175.50 156.22
300747.XSHE 168.00 151.87 174.95 151.87
300747.XSHE 146.13 154.63 156.30 144.00
300750.XSHE 68.58 65.50 74.32 65.01
300750.XSHE 64.80 63.32 64.88 60.32
300750.XSHE 63.56 65.90 69.00 61.00
300750.XSHE 64.57 69.19 69.90 62.99
601066.XSHG 8.31 8.16 8.63 8.12
601066.XSHG 8.15 8.29 8.57 8.08
... ... ... ... ...
603587.XSHG 26.43 25.87 27.38 25.82
603587.XSHG 25.50 25.43 25.54 24.71
603590.XSHG 35.05 51.33 51.33 35.05
603590.XSHG 56.46 51.74 56.46 51.00
603590.XSHG 50.92 45.00 52.42 44.66
603590.XSHG 44.01 43.01 45.00 42.30
603650.XSHG 30.90 29.13 32.43 29.00
603650.XSHG 28.70 28.31 29.35 26.66
603650.XSHG 28.50 27.37 31.00 27.00
603650.XSHG 26.88 26.45 27.18 25.67
603657.XSHG 52.44 47.42 61.98 46.52
603657.XSHG 46.98 44.57 48.98 43.38
603657.XSHG 46.00 38.30 46.80 37.61
603657.XSHG 37.68 37.40 41.62 36.44
603666.XSHG 91.02 83.58 94.35 83.58
603666.XSHG 82.67 87.60 90.60 80.83
603666.XSHG 86.80 83.00 90.88 82.00
603666.XSHG 81.98 83.21 83.97 78.50
603693.XSHG 19.59 18.11 19.96 17.84
603693.XSHG 18.31 19.78 20.43 17.70
603693.XSHG 19.78 18.51 22.35 18.42
603693.XSHG 17.79 16.96 17.79 16.66
603706.XSHG 40.51 36.88 41.63 36.59
603706.XSHG 37.00 37.85 40.70 34.10
603706.XSHG 37.75 35.56 41.64 35.52
603706.XSHG 34.19 32.88 36.35 32.25
603713.XSHG 41.11 43.68 49.22 41.03
603713.XSHG 44.70 42.01 58.10 41.00
603713.XSHG 41.70 39.22 42.80 38.05
603713.XSHG 38.08 38.75 41.36 37.81

102 rows × 4 columns

#输入一个股票池,获取其get_bars数据,并返回一个DataFrame
def get_stocklist_bars(stocklist1,count1,unit1 = '1w',fields1 = ['date','open','high','low','close'],include_now1 = False,end_dt1=None, fq_ref_date1=None):
    frames =[]
    for stock in stocklist1: 
        array1 = get_bars(stock,count=count1,unit=unit1,fields=fields1 ,include_now =include_now1,end_dt=end_dt1, fq_ref_date=fq_ref_date1) 
        s = pd.DataFrame(array1, index=[stock]*len(array1), columns=fields1) 
        frames.append(s) 
        result = pd.concat(frames)
    return result
get_stocklist_bars(stocks,4,unit1 ='1w',fields1 = ['date','open','high','low','close'])
date open high low close
002932.XSHE 2018-08-31 69.49 74.40 68.47 69.20
002932.XSHE 2018-09-07 68.00 68.18 62.37 63.82
002932.XSHE 2018-09-14 63.82 69.80 61.43 61.43
002932.XSHE 2018-09-21 58.50 60.66 57.00 57.60
002933.XSHE 2018-08-31 26.94 43.03 26.94 43.03
002933.XSHE 2018-09-07 47.33 69.30 47.33 69.30
002933.XSHE 2018-09-14 73.00 73.95 61.48 61.48
002933.XSHE 2018-09-21 58.81 62.40 56.03 59.03
002935.XSHE 2018-09-07 25.59 40.87 25.59 40.87
002935.XSHE 2018-09-14 44.96 65.84 44.96 59.16
002935.XSHE 2018-09-21 55.50 59.55 52.30 53.73
002938.XSHE 2018-09-21 19.28 30.00 19.28 26.17
300724.XSHE 2018-08-31 32.43 35.35 30.68 30.94
300724.XSHE 2018-09-07 31.69 34.80 30.11 34.47
300724.XSHE 2018-09-14 34.48 37.00 28.15 28.15
300724.XSHE 2018-09-21 27.38 28.09 25.82 26.30
300746.XSHE 2018-08-31 23.68 25.02 22.61 22.68
300746.XSHE 2018-09-07 22.00 24.80 20.95 23.79
300746.XSHE 2018-09-14 23.36 24.18 21.05 21.28
300746.XSHE 2018-09-21 20.97 21.97 20.00 20.93
300747.XSHE 2018-08-31 160.51 177.00 160.51 161.00
300747.XSHE 2018-09-07 160.11 175.50 156.22 169.50
300747.XSHE 2018-09-14 168.00 174.95 151.87 151.87
300747.XSHE 2018-09-21 146.13 156.30 144.00 154.63
300750.XSHE 2018-08-31 68.58 74.32 65.01 65.50
300750.XSHE 2018-09-07 64.80 64.88 60.32 63.32
300750.XSHE 2018-09-14 63.56 69.00 61.00 65.90
300750.XSHE 2018-09-21 64.57 69.90 62.99 69.19
601066.XSHG 2018-08-31 8.31 8.63 8.12 8.16
601066.XSHG 2018-09-07 8.15 8.57 8.08 8.29
... ... ... ... ... ...
603587.XSHG 2018-09-14 26.43 27.38 25.82 25.87
603587.XSHG 2018-09-21 25.50 25.54 24.71 25.43
603590.XSHG 2018-08-31 35.05 51.33 35.05 51.33
603590.XSHG 2018-09-07 56.46 56.46 51.00 51.74
603590.XSHG 2018-09-14 50.92 52.42 44.66 45.00
603590.XSHG 2018-09-21 44.01 45.00 42.30 43.01
603650.XSHG 2018-08-31 30.90 32.43 29.00 29.13
603650.XSHG 2018-09-07 28.70 29.35 26.66 28.31
603650.XSHG 2018-09-14 28.50 31.00 27.00 27.37
603650.XSHG 2018-09-21 26.88 27.18 25.67 26.45
603657.XSHG 2018-08-31 52.44 61.98 46.52 47.42
603657.XSHG 2018-09-07 46.98 48.98 43.38 44.57
603657.XSHG 2018-09-14 46.00 46.80 37.61 38.30
603657.XSHG 2018-09-21 37.68 41.62 36.44 37.40
603666.XSHG 2018-08-31 91.02 94.35 83.58 83.58
603666.XSHG 2018-09-07 82.67 90.60 80.83 87.60
603666.XSHG 2018-09-14 86.80 90.88 82.00 83.00
603666.XSHG 2018-09-21 81.98 83.97 78.50 83.21
603693.XSHG 2018-08-31 19.59 19.96 17.84 18.11
603693.XSHG 2018-09-07 18.31 20.43 17.70 19.78
603693.XSHG 2018-09-14 19.78 22.35 18.42 18.51
603693.XSHG 2018-09-21 17.79 17.79 16.66 16.96
603706.XSHG 2018-08-31 40.51 41.63 36.59 36.88
603706.XSHG 2018-09-07 37.00 40.70 34.10 37.85
603706.XSHG 2018-09-14 37.75 41.64 35.52 35.56
603706.XSHG 2018-09-21 34.19 36.35 32.25 32.88
603713.XSHG 2018-08-31 41.11 49.22 41.03 43.68
603713.XSHG 2018-09-07 44.70 58.10 41.00 42.01
603713.XSHG 2018-09-14 41.70 42.80 38.05 39.22
603713.XSHG 2018-09-21 38.08 41.36 37.81 38.75

102 rows × 5 columns

 

全部回复

0/140

量化课程

    移动端课程