Skip to content

Latest commit

 

History

History
247 lines (217 loc) · 10.2 KB

DataStruct.md

File metadata and controls

247 lines (217 loc) · 10.2 KB

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

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

QA_DataStruct具有的功能:

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

QA_DataStruct_Stock_block

  • (属性)该类下的所有板块名称 block_name
  • 查询某一只股票所在的所有板块 get_code(code)
  • 查询某一个板块下的所有股票 get_block(block)
  • 展示当前类下的所有数据 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)

取一个股票的数据

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

取多个股票的数据

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

显示结构体的数据 .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

显示结构体的开/高/收/低 .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

数据结构复权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

数据透视 .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

数据的时间筛选.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

数据按时间往前/往后推 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

选取结构组里面某一只股票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

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

第三个选项 默认是True
第三选项的意义在于,如果出现了停牌,参数如果是True 那么就会返回空值 而如果是False,就会返回停牌前最后一个交易日的值

In [18]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').get_bar('000001','2017-09-20',True)
Out[18]: QA_DataStruct_Stock_day with 1 securities

In [19]: QA.QA_fetch_stock_day_adv(['000001','000002'],'2017-09-20','2017-10-01').get_bar('000001','2017-09-20',True).data
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

画图 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