Skip to content

Latest commit

 

History

History
495 lines (389 loc) · 16.3 KB

DataStruct.md

File metadata and controls

495 lines (389 loc) · 16.3 KB

QUANTAXIS的核心数据结构以及方法

datastruct

属性用@property装饰器装饰,进行懒运算 提高效率

QA_DataStruct具有的功能:

  • 数据容器
  • 数据变换 [分拆/合并/倒序] split/merge
  • 数据透视 pivot
  • 数据筛选 selects/select_time/select_time_with_gap/select_code/get_bar/select_month
  • 数据复权 to_qfq/to_hfq
  • 数据显示 show
  • 格式变换 to_json/to_pandas/to_list/to_numpy/to_hdf
  • 数据库式查询 query
  • 画图 plot
  • 计算指标 add_func
  • 生成器 panel_gen(按时间分类的面板生成器)/security_gen(按股票分类的股票生成器)

QA_DataStruct_Stock_block

  • (属性)该类下的所有板块名称 block_name
  • 查询某一只股票所在的所有板块 get_code(code)
  • 查询某一个/多个板块下的所有股票 get_block(blockname)
  • 展示当前类下的所有数据 show

我们可以通过

import QUANTAXIS as QA

# QA.QA_fetch_stock_day_adv
# QA.QA_fetch_stock_min_adv
# QA.QA_fetch_index_day_adv
# QA.QA_fetch_index_min_adv

day线的参数是code, start, end min线的参数是code, start, end, frequence='1min'

其中 code 可以是一个股票,也可以是一列股票(list)

1. 取一个股票的数据

QA.QA_fetch_stock_day_adv('000001','2017-01-01','2017-10-01')
In [5]: QA.QA_fetch_stock_day_adv('000001','2017-01-01','2017-10-01')
Out[5]: QA_DataStruct_Stock_day with 1 securities

2. 取多个股票的数据

QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-01-01','2017-10-01')
In [6]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-01-01','2017-10-01')
Out[6]: QA_DataStruct_Stock_day with 2 securities

3. 显示结构体的数据 .data

In [10]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').data
Out[10]:
                     code   open   high    low  close    volume       date
date       code
2017-09-20 000001  000001  11.14  11.37  11.05  11.29  787154.0 2017-09-20
2017-09-21 000001  000001  11.26  11.51  11.20  11.46  692407.0 2017-09-21
2017-09-22 000001  000001  11.43  11.52  11.31  11.44  593927.0 2017-09-22
2017-09-25 000001  000001  11.44  11.45  11.18  11.29  532391.0 2017-09-25
2017-09-26 000001  000001  11.26  11.30  10.96  11.05  967460.0 2017-09-26
2017-09-27 000001  000001  11.01  11.08  10.90  10.93  727188.0 2017-09-27
2017-09-28 000001  000001  10.98  10.98  10.82  10.88  517220.0 2017-09-28
2017-09-29 000001  000001  10.92  11.16  10.86  11.11  682280.0 2017-09-29
2017-09-20 000002  000002  28.50  29.55  28.00  28.73  613095.0 2017-09-20
2017-09-21 000002  000002  28.50  29.06  27.75  28.40  536324.0 2017-09-21
2017-09-22 000002  000002  28.39  28.67  27.52  27.81  423093.0 2017-09-22
2017-09-25 000002  000002  27.20  27.20  26.10  26.12  722702.0 2017-09-25
2017-09-26 000002  000002  26.12  27.22  26.10  26.76  593044.0 2017-09-26
2017-09-27 000002  000002  27.00  27.28  26.52  26.84  367534.0 2017-09-27
2017-09-28 000002  000002  27.00  27.15  26.40  26.41  262347.0 2017-09-28
2017-09-29 000002  000002  26.56  26.80  26.00  26.25  345752.0 2017-09-29

4. 显示结构体的开/高/收/低 .open/.high/.close/.low

In [5]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').high
Out[5]:
date        code
2017-09-20  000001    11.37
2017-09-21  000001    11.51
2017-09-22  000001    11.52
2017-09-25  000001    11.45
2017-09-26  000001    11.30
2017-09-27  000001    11.08
2017-09-28  000001    10.98
2017-09-29  000001    11.16
2017-09-20  000002    29.55
2017-09-21  000002    29.06
2017-09-22  000002    28.67
2017-09-25  000002    27.20
2017-09-26  000002    27.22
2017-09-27  000002    27.28
2017-09-28  000002    27.15
2017-09-29  000002    26.80
Name: high, dtype: float64

5. 结构体拆分 splits()

当一个DataStruct里面存在多个证券时,可以通过拆分的方法,将其变成多个DataStruct

In [3]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').splits()
Out[3]:
[< QA_DataStruct_Stock_day with 1 securities >,
 < QA_DataStruct_Stock_day with 1 securities >]

6. 数据结构复权to_qfq()/to_hfq()

返回的是一个DataStruct,用.data展示返回的数据的结构

其中DataStruct.if_fq的属性会改变

In [4]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').to_qfq().data

Out[4]:
                     code   open   high    low  close    volume       date  \
date       code
2017-09-20 000001  000001  11.14  11.37  11.05  11.29  787154.0 2017-09-20
2017-09-21 000001  000001  11.26  11.51  11.20  11.46  692407.0 2017-09-21
2017-09-22 000001  000001  11.43  11.52  11.31  11.44  593927.0 2017-09-22
2017-09-25 000001  000001  11.44  11.45  11.18  11.29  532391.0 2017-09-25
2017-09-26 000001  000001  11.26  11.30  10.96  11.05  967460.0 2017-09-26
2017-09-27 000001  000001  11.01  11.08  10.90  10.93  727188.0 2017-09-27
2017-09-28 000001  000001  10.98  10.98  10.82  10.88  517220.0 2017-09-28
2017-09-29 000001  000001  10.92  11.16  10.86  11.11  682280.0 2017-09-29
2017-09-20 000002  000002  28.50  29.55  28.00  28.73  613095.0 2017-09-20
2017-09-21 000002  000002  28.50  29.06  27.75  28.40  536324.0 2017-09-21
2017-09-22 000002  000002  28.39  28.67  27.52  27.81  423093.0 2017-09-22
2017-09-25 000002  000002  27.20  27.20  26.10  26.12  722702.0 2017-09-25
2017-09-26 000002  000002  26.12  27.22  26.10  26.76  593044.0 2017-09-26
2017-09-27 000002  000002  27.00  27.28  26.52  26.84  367534.0 2017-09-27
2017-09-28 000002  000002  27.00  27.15  26.40  26.41  262347.0 2017-09-28
2017-09-29 000002  000002  26.56  26.80  26.00  26.25  345752.0 2017-09-29

                   preclose  adj
date       code
2017-09-20 000001       NaN  1.0
2017-09-21 000001     11.29  1.0
2017-09-22 000001     11.46  1.0
2017-09-25 000001     11.44  1.0
2017-09-26 000001     11.29  1.0
2017-09-27 000001     11.05  1.0
2017-09-28 000001     10.93  1.0
2017-09-29 000001     10.88  1.0
2017-09-20 000002       NaN  1.0
2017-09-21 000002     28.73  1.0
2017-09-22 000002     28.40  1.0
2017-09-25 000002     27.81  1.0
2017-09-26 000002     26.12  1.0
2017-09-27 000002     26.76  1.0
2017-09-28 000002     26.84  1.0
2017-09-29 000002     26.41  1.0

7. 数据透视 .pivot()

In [6]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').pivot('open')
Out[6]:
code        000001  000002
date
2017-09-20   11.14   28.50
2017-09-21   11.26   28.50
2017-09-22   11.43   28.39
2017-09-25   11.44   27.20
2017-09-26   11.26   26.12
2017-09-27   11.01   27.00
2017-09-28   10.98   27.00
2017-09-29   10.92   26.56

8. 数据的自定义筛选.selects(code,start,end)

In [4]: data.selects('000005','2018-06-01','2018-06-05').data
Out[4]:
                   open  high   low  close   volume      amount
date       code
2018-06-01 000005  3.42  3.46  3.39   3.44  47210.0  16151548.0
2018-06-04 000005  3.43  3.45  3.39   3.40  35039.0  11945360.0
2018-06-05 000005  3.39  3.42  3.38   3.42  34129.0  11609377.0

9. 数据的时间筛选.select_time(start,end)

In [10]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_time('2017-09-20','2017-09-25')
Out[10]: QA_DataStruct_Stock_day with 2 securities

In [11]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_time('2017-09-20','2017-09-25').data
Out[11]:
                     code   open   high    low  close    volume       date
date       code
2017-09-20 000001  000001  11.14  11.37  11.05  11.29  787154.0 2017-09-20
2017-09-21 000001  000001  11.26  11.51  11.20  11.46  692407.0 2017-09-21
2017-09-22 000001  000001  11.43  11.52  11.31  11.44  593927.0 2017-09-22
2017-09-25 000001  000001  11.44  11.45  11.18  11.29  532391.0 2017-09-25
2017-09-20 000002  000002  28.50  29.55  28.00  28.73  613095.0 2017-09-20
2017-09-21 000002  000002  28.50  29.06  27.75  28.40  536324.0 2017-09-21
2017-09-22 000002  000002  28.39  28.67  27.52  27.81  423093.0 2017-09-22
2017-09-25 000002  000002  27.20  27.20  26.10  26.12  722702.0 2017-09-25

10. 数据按时间往前/往后推 select_time_with_gap(time,gap,methods)

time是你选择的时间 gap是长度 (int) methods有 '<=','lte','<','lt','eq','==','>','gt','>=','gte'的选项

In [14]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_time_with_gap('2017-09-20',2,'gt')
Out[14]: QA_DataStruct_Stock_day with 2 securities

In [15]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_time_with_gap('2017-09-20',2,'gt').data
Out[15]:
                     code   open   high    low  close    volume       date
date       code
2017-09-21 000001  000001  11.26  11.51  11.20  11.46  692407.0 2017-09-21
2017-09-22 000001  000001  11.43  11.52  11.31  11.44  593927.0 2017-09-22
2017-09-21 000002  000002  28.50  29.06  27.75  28.40  536324.0 2017-09-21
2017-09-22 000002  000002  28.39  28.67  27.52  27.81  423093.0 2017-09-22

11. 选取某一个月份的数据 select_month(month)

可以通过select_month 来选取某一个月份的数据

In [4]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_month('2017-09')
Out[4]: < QA_DataStruct_Stock_day with 2 securities >

12. 选取结构组里面某一只股票select_code(code)

In [16]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_code('000001')
Out[16]: QA_DataStruct_Stock_day with 1 securities
In [17]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').select_code('000001').data
Out[17]:
                     code   open   high    low  close    volume       date
date       code
2017-09-20 000001  000001  11.14  11.37  11.05  11.29  787154.0 2017-09-20
2017-09-21 000001  000001  11.26  11.51  11.20  11.46  692407.0 2017-09-21
2017-09-22 000001  000001  11.43  11.52  11.31  11.44  593927.0 2017-09-22
2017-09-25 000001  000001  11.44  11.45  11.18  11.29  532391.0 2017-09-25
2017-09-26 000001  000001  11.26  11.30  10.96  11.05  967460.0 2017-09-26
2017-09-27 000001  000001  11.01  11.08  10.90  10.93  727188.0 2017-09-27
2017-09-28 000001  000001  10.98  10.98  10.82  10.88  517220.0 2017-09-28
2017-09-29 000001  000001  10.92  11.16  10.86  11.11  682280.0 2017-09-29

13. 取某一只股票的某一个时间的bar(code,time,if_trade)

In [19]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').get_bar('000001','2017-09-20')
Out[19]:
                     code   open   high    low  close    volume       date
date       code
2017-09-20 000001  000001  11.14  11.37  11.05  11.29  787154.0 2017-09-20

14. 统计学部分

14.1. 平均价 price

为了统计学指标的需要, price=AVERAGE(open+high+low+close)

price是一个pd.Series

In [7]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').price
Out[7]:
date        code
2017-09-20  000001    11.2125
2017-09-21  000001    11.3575
2017-09-22  000001    11.4250
2017-09-25  000001    11.3400
2017-09-26  000001    11.1425
2017-09-27  000001    10.9800
2017-09-28  000001    10.9150
2017-09-29  000001    11.0125
dtype: float64

14.2. price均值 mean

mean是price的均值

In [6]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').mean
Out[6]: 11.173125

14.3. max/min

max/min 分别是price序列的最大值和最小值

In [8]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').max
Out[8]: 11.424999999999999

In [9]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').min
Out[9]: 10.915000000000001

14.4. 方差/样本方差 pvariance/variance

分别是price的方差和样本方差

In [10]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').variance
Out[10]: 0.0367852678571427

In [11]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').pvariance
Out[11]: 0.03218710937499986

14.5. 标准差/样本标准差 pstdev/stdev

分别是price的总体标准差和样本标准差

In [12]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').pstdev
Out[12]: 0.17940766253145338

In [13]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').stdev
Out[13]: 0.19179485878704544

14.6. 调和平均数 mean_harmonic

price的调和平均数

In [14]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').mean_harmonic
Out[14]: 11.170242242781745

14.7. 众数 mode

返回price的众数 (注意: price序列可能没有众数,因此可能会报错,内部处理后,返回None)

In [31]: QA.QA_fetch_stock_day_adv('000001','2017-01-20','2017-10-01').mode
Out[31]: 9.1375

In [31]: QA.QA_fetch_stock_day_adv('000001','2017-01-20','2017-10-01').mode
Out[31]: None

14.8. 振幅 amplitude

返回price的振幅

In [33]: QA.QA_fetch_stock_day_adv('000001','2017-01-20','2017-10-01').amplitude
Out[33]: 3.1325000000000003

14.9. 偏度 skew

返回price的偏度

In [35]: QA.QA_fetch_stock_day_adv('000001','2017-01-20','2017-10-01').skew
Out[35]: 0.70288041557825753

14.10. 峰度 kurt

返回price的峰度

In [37]: QA.QA_fetch_stock_day_adv('000001','2017-01-20','2017-10-01').kurt
Out[37]: -1.0703273213086726

14.11. 百分比变化 pct_change

返回price的百分比变化

In [40]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').pct_change
Out[40]:
date        code
2017-09-20  000001         NaN
2017-09-21  000001    0.012932
2017-09-22  000001    0.005943
2017-09-25  000001   -0.007440
2017-09-26  000001   -0.017416
2017-09-27  000001   -0.014584
2017-09-28  000001   -0.005920
2017-09-29  000001    0.008933
dtype: float64

14.12. 平均绝对偏差 mad

返回price的平均绝对偏差

In [41]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').mad
Out[41]: 0.16062499999999957

14.13. 价格差分 price_diff

返回价格的一阶差分

In [42]: QA.QA_fetch_stock_day_adv('000001','2017-09-20','2017-10-01').price_diff
Out[42]:
date        code
2017-09-20  000001       NaN
2017-09-21  000001    0.1450
2017-09-22  000001    0.0675
2017-09-25  000001   -0.0850
2017-09-26  000001   -0.1975
2017-09-27  000001   -0.1625
2017-09-28  000001   -0.0650
2017-09-29  000001    0.0975
dtype: float64

15. 画图 plot(code)

如果是()空值 就会把全部的股票都画出来

In [20]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').plot()
QUANTAXIS>> The Pic has been saved to your path: .\QA_stock_day_codepackage_bfq.html

In [21]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').plot('000001')
QUANTAXIS>> The Pic has been saved to your path: .\QA_stock_day_000001_bfq.html