2. Acquiring and Managing Data

You can acquire and manage a large amount of historical data using qteasy. The historical data that qteasy can manage covers stocks, funds, indices, futures, etc., including price data, technical indicators, macroeconomics, company financial reports, macro finance, etc.

All data can be obtained through the tushare interface. After downloading to the local, you can manage and call it through the qteasy interface.

Looking for supported Historical Data

Use qt.find_history_data() to search across all known historical data types by name, Chinese description, or wildcard, and return either a list of data_id values compatible with get_history_data() or a structured DataFrame result as needed, making it easier to explore available data fields.

qteasy.find_history_data(s: str, match_description: bool = False, fuzzy: bool = False, freq: Optional[str] = None, asset_type: Optional[str] = None, match_threshold: float = 0.85, table: Optional[Union[str, list[str]]] = None, as_data_frame: bool = False) Union[list[str], DataFrame][source]

Based on the input string, find or match historical data types and display detailed information about the historical data. Supports fuzzy search, wildcards, searching/matching historical data types using English characters or Chinese, and returning more structured results in the form of a DataFrame.

Parameters:
  • s (str) – A string used to find or match historical data types

  • match_description (bool, Default: False) – Whether to fuzzy-match the data description. If the provided string contains non-ASCII characters, it will automatically be set to True - False: Match only the data type name - True: Match both the data type name and the data description

  • fuzzy (bool, Default: False) – Whether to fuzzy-match data names. If the provided string contains non-ASCII characters or the wildcard characters */?, it will automatically be set to True - False: exact match on data name - True: fuzzy match on data name or data description

  • freq (str, Default: None) – Frequency of data, if provided, only match the frequency of data. You can enter a single frequency or multiple frequencies separated by commas

  • asset_type (str, Default: None) – Asset type, if provided, only match the asset type of data. You can enter a single asset type or multiple asset types separated by commas

  • match_threshold (float, default 0.85) – Threshold for matching degree, items with a matching degree exceeding this threshold will be judged as matched

  • table (str or list of str, Default: None) – Data table name filter. If provided, only match data types from these tables; can be a single table name or a comma-separated string

  • as_data_frame (bool, Default: False) –

    • False: Return a backward-compatible list of data_id values, and print information about the matched data types

    • True: Return a DataFrame containing detailed information about the matched results, without forcing reliance on printed output

Returns:

  • data_id (list[str]) – When as_data_frame is False, the list of data_id values for the matched data types; can be used to download data via qt.get_history_data()

  • pandas.DataFrame – When as_data_frame is True, returns a DataFrame. Each row corresponds to a matched data type and contains at least the following columns:

    • name: Data type name

    • description: Chinese description of the data type

    • freq: Frequency

    • asset_type: Asset type

    • table_name: Underlying data table name

    • column: Corresponding data field name

Examples

>>> import qteasy as qt
>>> qt.find_history_data('pe')
matched following history data,
use "qt.get_history_data()" to load these historical data by its name:
------------------------------------------------------------------------
           freq asset             table                            desc
data_id
initial_pe    d     E         new_share                  新股上市信息 - 发行市盈率
pe            d   IDX   index_indicator                    指数技术指标 - 市盈率
pe            d     E   stock_indicator  股票技术指标 - 市盈率(总市值/净利润, 亏损的PE为空)
pe_2          d     E  stock_indicator2                  股票技术指标 - 动态市盈率
========================================================================
>>> qt.find_history_data('ep*')
matched following history data,
use "qt.get_history_data()" to load these historical data by its data_id:
------------------------------------------------------------------------
              freq asset      table                 desc
data_id
eps_last_year    q     E    express  上市公司业绩快报 - 去年同期每股收益
eps              q     E  financial    上市公司财务指标 - 基本每股收益
========================================================================
>>> qt.find_history_data('每股收益')
matched following history data,
use "qt.get_history_data()" to load these historical data by its data_id:
------------------------------------------------------------------------
                freq asset      table                 desc
data_id
basic_eps              q     E     income           上市公司利润表 - 基本每股收益
diluted_eps            q     E     income           上市公司利润表 - 稀释每股收益
express_diluted_eps    q     E    express     上市公司业绩快报 - 每股收益(摊薄)(元)
yoy_eps                q     E    express    上市公司业绩快报 - 同比增长率:基本每股收益
eps_last_year          q     E    express        上市公司业绩快报 - 去年同期每股收益
eps                    q     E  financial          上市公司财务指标 - 基本每股收益
dt_eps                 q     E  financial          上市公司财务指标 - 稀释每股收益
diluted2_eps           q     E  financial        上市公司财务指标 - 期末摊薄每股收益
q_eps                  q     E  financial       上市公司财务指标 - 每股收益(单季度)
basic_eps_yoy          q     E  financial  上市公司财务指标 - 基本每股收益同比增长率(%)
dt_eps_yoy             q     E  financial  上市公司财务指标 - 稀释每股收益同比增长率(%)
========================================================================
Raises:

TypeError – The input s is not a string, or freq/asset_type/table is not a string or a list

Download Historical Data

With qt.refill_data_source(), you can batch-download data for specified tables or purposes from a remote financial data API (supports filtering by data type, frequency, asset type, etc.), and complete cleaning and writing in the local DataSource; refreshing dependent tables and the trading calendar is handled automatically internally. For the specific workflow and recommended parameter combinations, see the manage_data series documentation.

qteasy.refill_data_source(tables, *, channel=None, data_source=None, dtypes=None, freqs=None, asset_types=None, refresh_trade_calendar=False, refill_dependent_tables=True, symbols=None, start_date=None, end_date=None, list_arg_filter=None, reversed_par_seq=False, parallel=True, process_count=None, chunk_size=100, download_batch_size=0, download_batch_interval=0, merge_type='update', log=False) None[source]

Download data in batches from the API channel of the network data provider, clean it, and fill it into the local data source

Parameters:
  • tables (str or list of str, default: None) – Data table name, must be the data table defined in the database, used to specify the data table to be downloaded. You can give the data table name, such as ‘stock_daily, stock_weekly’, or you can give the purpose of the data table, such as ‘data, basic’

  • data_source (DataSource, Default None) – DataSource to be filled with data. If None, fill the data source to QT_DATA_SOURCE

  • channel (str, optional, Default 'tushare') – Data acquisition channel, financial data API, supports the following options: - ‘tushare’ : Get financial data from Tushare API, please apply for the corresponding permission and points by yourself - ‘akshare’ : Get financial data from AKshare API - ‘eastmoney’ : Get financial data from Eastmoney

  • tables – Data table name, must be the data table defined in the database, used to specify the data table to be downloaded

  • dtypes (str or list of str, default: None) – Data type to be downloaded, used to further filter the data table, must be the data type defined in the database

  • freqs (str or list of str, default: None) – Data frequency to be downloaded, used to further filter the data table, must be the data frequency defined in the database

  • asset_types (str or list of str, default: None) – Asset type to be downloaded, used to further filter the data table, must be the asset type defined in the database

  • refresh_trade_calendar (Bool, Default False) – If True, the trade_calendar table will be downloaded

  • refill_dependent_tables (Bool, Default True, New in v1.4.3) – If set to False, ignore the dependency table, which may cause data download failure

  • start_date (str YYYYMMDD) – Limit the time range of data download. If start_date/end_date is given, only the data within this time period will be downloaded

  • end_date (str YYYYMMDD) – Limit the time range of data download. If start_date/end_date is given, only the data within this time period will be downloaded

  • list_arg_filter (str or list of str, default: None 注意,不是所有情况下filter_arg参数都有效) – Filter parameters used to constrain downloads. Some data tables provide filterable parameters as a list; for example, the stock_basic table has a filter parameter “exchange” with options ‘SSE’, ‘SZSE’, ‘BSE’. You can use this parameter to limit the scope of the download. If filter_arg is None, all data will be downloaded. For example, when downloading stock_basic table data, the following inputs are all valid: - ‘SZSE’ download only stocks listed on the Shenzhen Stock Exchange - [‘SSE’, ‘SZSE’] - ‘SSE, SZSE’ the two forms above are equivalent; download stocks listed on the Shanghai and Shenzhen Stock Exchanges

  • symbols (str or list of str, default: None) – Stock codes used to download data. If symbols are given, only the data of these stock codes will be downloaded

  • reversed_par_seq (Bool, Default False) – If True, download data in reverse order - False: download data in normal order

  • parallel (Bool, Default True) – If True, enable multi-threaded data download - False: disable multi-threaded download

  • process_count (int) – Number of threads opened simultaneously when multi-threaded download is enabled, default value is the number of CPU cores of the device

  • chunk_size (int) – Number of data to be accumulated before saving to local, chunk_size is the batch size, default value is 100

  • download_batch_size (int, default 0) – Number of data to be downloaded before pausing, this parameter is only valid when parallel=False. If 0, no pause, download all data at once

  • download_batch_interval (int, default 0) – Number of seconds to pause before continuing to download data, this parameter is only valid when parallel=False. If <=0, no pause, immediately start the next batch of data download

  • merge_type (str, Default 'update') – Merge method when writing data to data source, supports the following options: - ‘update’ : Update data, if data already exists, update data - ‘ignore’ : Ignore data, if data already exists, discard downloaded data

  • log (Bool, Default False) – If True, record data download log

Return type:

None

Examples

>>> import qteasy as qt
>>> qt.refill_data_source(tables='stock_basic')

After downloading historical data to the local, you can check, manage, and call this data.

Check Local Data

qteasy.get_table_info(table_name, data_source=None, verbose=True) dict[source]

Retrieve and print information about a data table in the data source, including data volume, disk space usage, primary key name, contents, and the names, data types, and descriptions of the data columns.

Parameters:
  • table_name (str) – Datatable name to be queried

  • data_source (DataSource) – The data source to obtain the data table information, default None, at this time obtain the QT_DATA_SOURCE information

  • verbose (bool, Default: True,) – If True, print the complete list of data column names and types

Returns:

data_struct – Structured information about the data table: {

table name: Data table name table_exists: bool, whether the data table exists table_size: int/str, disk space occupied by the data table, human If True, return an easy-to-read string table_rows: int/str, number of rows in the data table, human If True, return an easy-to-read string primary_key1: str, name of the first primary key of the data table pk_count1: int, number of records of the first primary key of the data table pk_min1: obj, starting record of the primary key 1 of the data table pk_max1: obj, final record of the primary key 2 of the data table primary_key2: str, name of the second primary key of the data table pk_count2: int, number of records of the second primary key of the data table pk_min2: obj, starting record of the primary key 2 of the data table pk_max2: obj, final record of the primary key 2 of the data table

}

Return type:

dict

Examples

>>> get_table_info('STOCK_BASIC')
<stock_basic>, 1.5MB/5K records on disc
primary keys:
-----------------------------------
1:  ts_code:
    <unknown> entries
    starts: 000001.SZ, end: 873527.BJ
columns of table:
------------------------------------
        columns       dtypes remarks
0       ts_code   varchar(9)    证券代码
1        symbol   varchar(6)    股票代码
2          name  varchar(20)    股票名称
3          area  varchar(10)      地域
4      industry  varchar(10)    所属行业
5      fullname  varchar(50)    股票全称
6        enname  varchar(80)    英文全称
7       cnspell  varchar(40)    拼音缩写
8        market   varchar(6)    市场类型
9      exchange   varchar(6)   交易所代码
10    curr_type   varchar(6)    交易货币
11  list_status   varchar(4)    上市状态
12    list_date         date    上市日期
13  delist_date         date    退市日期
14        is_hs   varchar(2)  是否沪深港通

Acquire the overview of the downloaded local data

qt.get_table_overview() and qt.get_data_overview() summarize and display whether each type of data table in the local data source currently has data, disk space usage, record count, and time coverage range, making them suitable as an entry point for checking data readiness.

qteasy.get_table_overview(data_source=None, tables=None, include_sys_tables=False) DataFrame[source]

Display the data overview of the default data source or the specified data source

Parameters:
  • data_source (Object) – A data_source object, default is None. If None, display the overview of the default data source

  • tables (str or list of str, Default: None) – If given, display the dat table overview, if None, display the overview of all data tables

  • include_sys_tables (bool, Default: False) – If True, display the overview of system data tables

Return type:

pd.DataFrame

Notes

Example usage see get_data_overview()

qteasy.get_data_overview(data_source=None, tables=None, include_sys_tables=False) DataFrame[source]

Display the data overview of the data source, equivalent to get_table_overview()

Information includes the data volume of all data tables, disk space occupied, primary key name, content, etc.

Parameters:
  • data_source (Object) – A data_source object, default is None. If None, display the overview of the default data source

  • tables (str or list of str, Default: None) – If given, display the dat table overview, if None, display the overview of all data tables

  • include_sys_tables (bool, Default: False) – If True, display the overview of system data tables

Returns:

  • pd.DataFrame

  • Returns a DataFrame containing the overview information of the data table

Examples

>>> import qteasy as qt
>>> qt.get_data_overview()  # 获取当前默认数据源的数据总览
Analyzing local data source tables... depending on size of tables, it may take a few minutes
[########################################]62/62-100.0%  Analyzing completed!
db:mysql://localhost@3306/ts_db
Following tables contain local data, to view complete list, print returned DataFrame
                 Has_data Size_on_disk Record_count Record_start  Record_end
table
trade_calendar     True        2.5MB         73K     1990-10-12   2023-12-31
stock_basic        True        1.5MB          5K           None         None
stock_names        True        1.5MB         14K     1990-12-10   2023-07-17
stock_company      True       18.5MB          3K           None         None
stk_managers       True      150.4MB        126K     2020-01-01   2022-07-27
index_basic        True        3.5MB         10K           None         None
fund_basic         True        4.5MB         17K           None         None
future_basic       True        1.5MB          7K           None         None
opt_basic          True       15.5MB         44K           None         None
stock_1min         True      42.83GB      273.0M       20220318     20230710
stock_5min         True      34.33GB      233.2M       20090105     20230710
stock_15min        True      14.45GB      141.2M       20090105     20230710
stock_30min        True       7.78GB       77.1M       20090105     20230710
stock_hourly       True       4.22GB       42.0M       20090105     20230710
stock_daily        True       1.49GB       11.6M     1990-12-19   2023-07-17
stock_weekly       True      231.9MB        2.6M     1990-12-21   2023-07-14
stock_monthly      True       50.6MB        635K     1990-12-31   2023-06-30
index_1min         True       4.25GB       27.6M       20220318     20230712
index_5min         True       6.18GB       47.2M       20090105     20230712
index_15min        True       2.61GB       26.1M       20090105     20230712
index_30min        True      884.0MB       12.9M       20090105     20230712
index_hourly       True      536.0MB        7.6M       20090105     20230712
index_daily        True      309.0MB        3.7M     1990-12-19   2023-07-10
index_weekly       True       61.6MB        674K     1991-07-05   2023-07-14
index_monthly      True       13.5MB        158K     1991-07-31   2023-06-30
fund_1min          True       5.46GB       55.8M       20220318     20230712
fund_5min          True       3.68GB       12.3M       20220318     20230712
fund_15min         True      835.9MB        3.9M       20220318     20230712
fund_30min         True      385.7MB        1.9M       20220318     20230712
fund_hourly        True      124.8MB        1.6M       20210104     20230629
fund_daily         True      129.7MB        1.6M     1998-04-07   2023-07-10
fund_nav           True      693.0MB       13.6M     2000-01-07   2023-07-07
fund_share         True       72.7MB        1.4M     1998-03-27   2023-07-14
fund_manager       True      109.7MB         37K     2000-02-22   2023-03-30
future_hourly      True         32KB           0           None         None
future_daily       True      190.8MB        2.0M     1995-04-17   2023-07-10
options_hourly     True         32KB           0           None         None
options_daily      True      436.0MB        4.6M     2015-02-09   2023-07-10
stock_adj_factor   True      897.0MB       11.8M     1990-12-19   2023-07-12
fund_adj_factor    True       74.6MB        1.8M     1998-04-07   2023-07-12
stock_indicator    True       2.06GB       11.8M     1999-01-01   2023-07-17
stock_indicator2   True      734.8MB        4.1M     2017-06-14   2023-07-10
index_indicator    True        4.5MB         45K     2004-01-02   2023-07-10
index_weight       True      748.0MB        8.7M     2005-04-08   2023-07-14
income             True       59.7MB        213K     1990-12-31   2023-06-30
balance            True       97.8MB        218K     1989-12-31   2023-06-30
cashflow           True       69.7MB        181K     1998-12-31   2023-06-30
financial          True      289.0MB        203K     1989-12-31   2023-06-30
forecast           True       32.6MB         98K     1998-12-31   2024-03-31
express            True        3.5MB         23K     2004-12-31   2023-06-30
shibor             True         16KB         212           None         None

Use the downloaded data - Basic Data

qt.get_basic_info() and qt.get_stock_info() provide entry points for querying basic information on stocks/funds/indices, etc. by code or name. They can be used together with filter_stock_codes() and filter_stocks() to build an asset pool or perform pre-filtering.

qteasy.get_basic_info(code_or_name: str, asset_types=None, match_full_name=False, printout=True, verbose=False)[source]

Equivalent to get_stock_info(); looks up basic information on stocks, funds, indices, or futures and options based on the input.

Parameters:
  • code_or_name (str) – Security code or name: - If it is a security code, it may include a suffix or not. With a suffix, perform an exact lookup; without a suffix, perform a global match. - If it is a security name, you can use wildcard patterns for fuzzy search, or perform a fuzzy search by name. - If exactly one security code is matched, return a dictionary containing information related to that security code.

  • asset_types (str, list of str, optional) – Security type. Accepts a list or a comma-separated string, including the recognized asset types: - E Stocks - IDX Indices - FD Funds - FT Futures - OPT Options

  • match_full_name (bool, default False) – If True, match the full name of the stock or fund, default is False. If the full name is matched, it takes longer

  • printout (bool, default True) – If True, print the matched results

  • verbose (bool, default False) – When too many securities are matched (more than five), whether to display full information - False (default): only show the highest-relevance matches - True: show all matched results

Returns:

stock_basic – When only one match is found, returns a dict containing the basic information found. The information varies by security type: - Stock: company name, region, industry, full name, listing status, listing date - Index: index name, full name, issuer, category, issue date - Fund: fund name, manager, custodian, fund type, issue date, issue amount, investment type, category - Futures: futures name - Options: options name

Return type:

dict

Examples

>>> get_basic_info('000001.SZ')
found 1 matches, matched codes are {'E': {'000001.SZ': '平安银行'}, 'count': 1}
More information for asset type E:
------------------------------------------
ts_code       000001.SZ
name               平安银行
area                 深圳
industry             银行
fullname     平安银行股份有限公司
list_status           L
list_date    1991-04-03
-------------------------------------------
>>> get_basic_info('000001')
found 4 matches, matched codes are {'E': {'000001.SZ': '平安银行'}, 'IDX': {'000001.CZC': '农期指数', '000001.SH': '上证指数'}, 'FD': {'000001.OF': '华夏成长'}, 'count': 4}
More information for asset type E:
------------------------------------------
ts_code       000001.SZ
name               平安银行
area                 深圳
industry             银行
fullname     平安银行股份有限公司
list_status           L
list_date    1991-04-03
-------------------------------------------
More information for asset type IDX:
------------------------------------------
ts_code   000001.CZC   000001.SH
name            农期指数        上证指数
fullname        农期指数      上证综合指数
publisher    郑州商品交易所        中证公司
category        商品指数        综合指数
list_date       None  1991-07-15
-------------------------------------------
More information for asset type FD:
------------------------------------------
ts_code        000001.OF
name                华夏成长
management          华夏基金
custodian         中国建设银行
fund_type            混合型
issue_date    2001-11-28
issue_amount     32.3683
invest_type          成长型
type              契约型开放式
-------------------------------------------
>>> get_basic_info('平安银行')
found 4 matches, matched codes are {'E': {'000001.SZ': '平安银行', '600928.SH': '西安银行'}, 'IDX': {'802613.SI': '平安银行养老新兴投资指数'}, 'FD': {'700001.OF': '平安行业先锋'}, 'count': 4}
More information for asset type E:
------------------------------------------
ts_code       000001.SZ   600928.SH
name               平安银行        西安银行
area                 深圳          陕西
industry             银行          银行
fullname     平安银行股份有限公司  西安银行股份有限公司
list_status           L           L
list_date    1991-04-03  2019-03-01
-------------------------------------------
More information for asset type IDX:
------------------------------------------
ts_code       802613.SI
name       平安银行养老新兴投资指数
fullname   平安银行养老新兴投资指数
publisher          申万研究
category           价值指数
list_date    2017-01-03
-------------------------------------------
More information for asset type FD:
------------------------------------------
ts_code        700001.OF
name              平安行业先锋
management          平安基金
custodian           中国银行
fund_type            混合型
issue_date    2011-08-15
issue_amount     31.9816
invest_type          混合型
type              契约型开放式
-------------------------------------------
>>> get_basic_info('贵州钢绳', match_full_name=False)
No match found! To get better result, you can
- pass "match_full_name=True" to match full names of stocks and funds
>>> get_basic_info('贵州钢绳', match_full_name=True)
found 1 matches, matched codes are {'E': {'600992.SH': '贵绳股份'}, 'count': 1}
More information for asset type E:
------------------------------------------
ts_code       600992.SH
name               贵绳股份
area                 贵州
industry            钢加工
fullname     贵州钢绳股份有限公司
list_status           L
list_date    2004-05-14
-------------------------------------------
qteasy.get_stock_info(code_or_name: str, asset_types=None, match_full_name=False, printout=True, verbose=False)[source]
Same as get_basic_info()

Look for the basic information of stocks, funds, indices, futures, or options based on the input information

Parameters:
  • code_or_name (str) – Stock code or name, if it is a stock code, it can contain suffixes or not. When it contains suffixes, it is precisely searched. When it does not contain suffixes, it is globally matched. If it is a stock name, it can contain wildcard fuzzy search, or fuzzy search by name. If an exact match is found for a stock code, return a dictionary containing the relevant information of the stock code

  • asset_types (str or list of str, optional) – Security type. Accepts a list or a comma-separated string, including the recognized asset types: - E Stocks - IDX Indices - FD Funds - FT Futures - OPT Options

  • match_full_name (bool, default False) – If True, match the full name of the stock or fund, default is False. If the full name is matched, it takes longer

  • printout (bool, default True) – If True, print the matched results

  • verbose (bool, default False) – When too many securities are matched (more than five), whether to display full information - False (default): only show the highest-relevance matches - True: show all matched results

Returns:

stock_info – When only one match is found, return a dict containing the basic information found. The information varies by security type: - Stock info: company name, region, industry, full name, listing status, listing date - Index info: index name, full name, issuer, category, issue date - Fund:

Return type:

dict

Notes

Example usage see: get_basic_info()

qteasy.filter_stock_codes(date: str = 'today', **kwargs) list[source]

Filter stocks based on the input parameters and call filter_stocks to return a list of stock codes

Parameters:
  • date (date-like str) – Filter the listing date of the stock. Stocks listed after this date will be excluded:

  • kwargs (str or list of str) – You can filter stocks through the following parameters. Multiple filtering conditions can be input at the same time, and only stocks that meet the requirements will be filtered out

Return type:

list, 股票代码清单

See also

filter_stocks

qteasy.filter_stocks(date: str = 'today', **kwargs) DataFrame[source]

Filter stocks based on the input parameters and return a DataFrame containing stock codes and related information

Parameters:
  • date (date-like str) – Filter the listing date of the stock. Stocks listed after this date will be excluded:

  • kwargs (str or list of str) – You can filter stocks through the following parameters. Multiple filtering conditions can be input at the same time, and only stocks that meet the requirements will be filtered out - index: Filter by index, stocks not included in the specified index will be excluded - industry: Industry of the company, only listed industries will be selected - area: Province where the company is located, only stocks from listed provinces will be selected - market: Market, divided into main board, GEM, etc. - exchange: Exchange, including Shanghai Stock Exchange and Shenzhen Stock Exchange

Returns:

DataFrame

Return type:

筛选出来的股票的基本信息

Examples

>>> # 筛选出2019年1月1日以后的上证300指数成分股
>>> filter_stocks(date='2019-01-01', index='000300.SH')
       symbol   name area industry market  list_date exchange
ts_code
000001.SZ  000001   平安银行   深圳       银行     主板 1991-04-03     SZSE
000002.SZ  000002    万科A   深圳     全国地产     主板 1991-01-29     SZSE
000063.SZ  000063   中兴通讯   深圳     通信设备     主板 1997-11-18     SZSE
000069.SZ  000069   华侨城A   深圳     全国地产     主板 1997-09-10     SZSE
000100.SZ  000100  TCL科技   广东      元器件     主板 2004-01-30     SZSE
           ...    ...  ...      ...    ...        ...      ...
600732.SH  600732   爱旭股份   上海     电气设备     主板 1996-08-16      SSE
600754.SH  600754   锦江酒店   上海     酒店餐饮     主板 1996-10-11      SSE
600875.SH  600875   东方电气   四川     电气设备     主板 1995-10-10      SSE
601699.SH  601699   潞安环能   山西     煤炭开采     主板 2006-09-22      SSE
688223.SH  688223   晶科能源   江西     电气设备    科创板 2022-01-26      SSE
[440 rows x 7 columns]
>>> # 筛选出2019年1月1日以后上市的上海银行业的股票
>>> filter_stocks(date='2019-01-01', industry='银行', area='上海')
           name area industry market  list_date exchange
ts_code
600000.SH  浦发银行   上海       银行     主板 1999-11-10      SSE
601229.SH  上海银行   上海       银行     主板 2016-11-16      SSE
601328.SH  交通银行   上海       银行     主板 2007-05-15      SSE

Use the downloaded data - Get price or technical indicators

Data retrieval entry points: For day-to-day analysis, strategy work, and visualization prep, prefer qt.get_history_data(). qteasy.history.get_history_panel() is intended for lower-level scenarios where you already have an explicit DataType list and DataSource and need to assemble a HistoryPanel directly; the user docs mainly follow get_history_data—see the HistoryPanel chapter in manage_data.

qteasy.get_history_data(htypes=None, *, htype_names=None, data_types=None, data_source=None, shares=None, symbols=None, start=None, end=None, freq=None, rows=None, asset_type=None, adj=None, as_data_frame=None, group_by=None, **kwargs)[source]

Given the specified instruments, data types, and frequency, fetch historical data from the local data source and assemble it into a structure that can be used directly by the strategy.

You can specify the required data types via htype_names or data_types, and combine them with shares / symbols, a time range, and freq to control the retrieval scope; depending on the settings of as_data_frame and group_by, the function returns a HistoryPanel or a dict of DataFrames grouped by instrument/data type. For advanced usage such as data type inference, frequency conversion, and trade_time_only, see the relevant sections in the documentation “Historical Data Retrieval get_history_data”.

Parameters:
  • htype_names (str or list of str, optional) – Collection of historical data names to retrieve; can be a comma-separated string (e.g., 'open, high, low, close') or a list (e.g., ['open', 'high', 'low', 'close']). If empty, the system will infer available htypes based on parameters such as freq / asset_type.

  • htypes (list of DataType, optional, deprecated) – List of historical data type objects; semantics are similar to htype_names. Prefer the new htype_names / data_types interface.

  • data_types (list of DataType, optional) – The set of historical data types to be obtained, must be a legal data type object. If this parameter is given, htype_names will be ignored, otherwise possible htypes will be created based on the htype_names parameter

  • data_source (DataSource, optional) – Data source to obtain historical data

  • shares (str or list of str, optional) – Collection of security codes; can be a comma-separated string (e.g., '000001.SZ, 000002.SZ') or a list (e.g., ['000001.SZ', '000002.SZ']).

  • symbols (str or list of str, optional) – Collection of security codes; can be a comma-separated string (e.g., '000001, 000002') or a list (e.g., ['000001', '000002']).

  • start (str, optional) – YYYYMMDD HH:MM:SS format date/time, start date/time of historical data to be obtained (if available)

  • end (str, optional) – YYYYMMDD HH:MM:SS format date/time, end date/time of historical data to be obtained (if available)

  • rows (int, default 10) – Number of rows of historical data to be obtained. If start and end are specified, this parameter is ignored, and the time range of the obtained data is [start, end]. If start and end are not specified, the most recent rows of data in the data table will be obtained. Using row to obtain data is much slower than using date

  • freq (str, optional) – Frequency; supports minute intervals such as 1min/5min/15min/30min, as well as hourly/daily/weekly/monthly intervals such as H/D/W/M (e.g., candlesticks).

  • asset_type (str or list of str, optional) – Asset type filter; can be a comma-separated string (e.g. 'E, IDX') or a list (e.g. ['E', 'IDX']). Common values include any, E, IDX, FT, FD, etc.

  • adj (str, optional, deprecated) – Deprecated adjustment options (none/n, back/b, forward/fw/f). For new code, explicitly use the adjusted column name in htype (e.g. close|b).

  • as_data_frame (bool, default True) – Returns HistoryPanel when True; returns a dictionary of DataFrame when False.

  • group_by (str, default 'shares') – Grouping key when returning a dict of DataFrames; commonly 'shares'/'share'/'s' or 'htypes'/'htype'/'h'.

  • **kwargs – Additional parameters passed through to the underlying data retrieval / frequency conversion (e.g. drop_nan, resample_method, etc.). For detailed available values and semantics, see the documentation “Historical Data Retrieval get_history_data” and the infer_data_types notes.

Returns:

  • HistoryPanel – When as_data_frame is False, returns a HistoryPanel object containing all requested data.

  • dict of pandas.DataFrame – When as_data_frame is True, returns a dict of DataFrames grouped by group_by.

Examples

>>> import qteasy as qt
# 给出历史数据类型和证券代码,起止时间,可以获取该时间段内该股票的历史数据
>>> qt.get_history_data(htype_names='open, high, low, close, vol', shares='000001.SZ', start='20191225', end='20200110')
{'000001.SZ':
             open   high    low  close         vol
2019-12-25  16.45  16.56  16.24  16.30   414917.98
2019-12-26  16.34  16.48  16.32  16.47   372033.86
2019-12-27  16.53  16.93  16.43  16.63  1042574.72
2019-12-30  16.46  16.63  16.10  16.57   976970.31
2019-12-31  16.57  16.63  16.31  16.45   704442.25
2020-01-02  16.65  16.95  16.55  16.87  1530231.87
2020-01-03  16.94  17.31  16.92  17.18  1116194.81
2020-01-06  17.01  17.34  16.91  17.07   862083.50
2020-01-07  17.13  17.28  16.95  17.15   728607.56
2020-01-08  17.00  17.05  16.63  16.66   847824.12
2020-01-09  16.81  16.93  16.53  16.79  1031636.65
2020-01-10  16.79  16.81  16.52  16.69   585548.45
}
>>> # 除了股票的价格数据以外,也可以获取基金、指数的价格数据,如下面的代码获取000300.SH的指数价格
>>> qt.get_history_data(htype_names='close', shares='000300.SH', start='20191225', end='20200105')
{'000300.SH':
              close
2019-12-25  3990.87
2019-12-26  4025.99
2019-12-27  4022.03
2019-12-30  4081.63
2019-12-31  4096.58
2020-01-02  4152.24
2020-01-03  4144.96
}
>>> # 以及基金的净值数据
>>> qt.get_history_data(htype_names='unit_nav, accum_nav', shares='000001.OF', start='20191225', end='20200105')
{'000001.OF':
            unit_nav  accum_nav
2019-12-25     1.086      3.547
2019-12-26     1.096      3.557
2019-12-27     1.091      3.552
2019-12-30     1.100      3.561
2019-12-31     1.105      3.566
2020-01-02     1.123      3.584
2020-01-03     1.127      3.588
}
>>> # 不光价格数据,其他类型的数据也可以同时获取:
>>> qt.get_history_data(htype_names='close, pe, pb', shares='000001.SZ', start='20191225', end='20200105')
{'000001.SZ':
            close       pe      pb
2019-12-25  16.30  12.7454  1.1798
2019-12-26  16.47  12.8784  1.1921
2019-12-27  16.63  13.0035  1.2036
2019-12-30  16.57  12.9566  1.1993
2019-12-31  16.45  12.8627  1.1906
2020-01-02  16.87  13.1911  1.2210
2020-01-03  17.18  13.4335  1.2434
}
>>> # 可以同时混合获取多只股票、指数、多种数据类型的数据,如果某些数据类型缺失,会用NaN填充,注意000001.SZ是股票平安银行,000001.SH是上证指数
>>> qt.get_history_data(htype_names='close, pe, pb, total_mv, eps', shares='000001.SZ, 000001.SH', start='20191225', end='20200105')
{'000001.SZ':
            close       pe      pb      total_mv   eps
2019-12-25  16.30  12.7454  1.1798  3.163165e+07   NaN
2019-12-26  16.47  12.8784  1.1921  3.196155e+07   NaN
2019-12-27  16.63  13.0035  1.2036  3.227204e+07   NaN
2019-12-30  16.57  12.9566  1.1993  3.215561e+07   NaN
2019-12-31  16.45  12.8627  1.1906  3.192274e+07  1.54
2020-01-02  16.87  13.1911  1.2210  3.273778e+07  1.54
2020-01-03  17.18  13.4335  1.2434  3.333937e+07  1.54,
'000001.SH':
              close     pe    pb      total_mv  eps
2019-12-25  2981.88  13.74  1.38  3.987686e+13  NaN
2019-12-26  3007.35  13.85  1.39  4.020871e+13  NaN
2019-12-27  3005.04  13.85  1.39  4.019086e+13  NaN
2019-12-30  3040.02  14.00  1.40  4.064796e+13  NaN
2019-12-31  3050.12  14.05  1.41  4.079249e+13  NaN
2020-01-02  3085.20  14.22  1.42  4.128453e+13  NaN
2020-01-03  3083.79  14.22  1.42  4.127933e+13  NaN
}
>>> # 通过设置freq参数,可以获取不同频率的K线数据,如设置freq='H'可以获取1小时频率的数据
>>> qt.get_history_data(htype_names='open:b, high:b, low:b, close:b', shares='000001.SZ', start='20191229', end='20200106', freq='H', asset_type='E')
 {'000001.SZ':
                           open        high         low       close
2019-12-30 10:00:00  1796.92174  1796.92174  1796.92174  1796.92174
2019-12-30 11:00:00  1790.37160  1800.19681  1758.71259  1786.00484
2019-12-30 14:00:00  1811.11371  1813.29709  1795.83005  1806.74695
2019-12-30 15:00:00  1805.65526  1808.93033  1793.64667  1808.93033
2019-12-31 10:00:00  1808.93033  1808.93033  1808.93033  1808.93033
2019-12-31 11:00:00  1806.74695  1806.74695  1780.54639  1788.18822
2019-12-31 14:00:00  1786.00484  1788.18822  1781.63808  1786.00484
2019-12-31 15:00:00  1786.00484  1796.92174  1783.82146  1795.83005
2020-01-02 10:00:00  1817.66385  1817.66385  1817.66385  1817.66385
2020-01-02 11:00:00  1819.84723  1848.23117  1807.83864  1840.58934
2020-01-02 14:00:00  1842.77272  1847.13948  1828.58075  1843.86441
2020-01-02 15:00:00  1843.86441  1844.95610  1836.22258  1841.68103
2020-01-03 10:00:00  1849.32286  1849.32286  1849.32286  1849.32286
2020-01-03 11:00:00  1849.32286  1879.89018  1849.32286  1877.70680
2020-01-03 14:00:00  1863.51483  1889.71539  1863.51483  1884.25694
2020-01-03 15:00:00  1884.25694  1884.25694  1872.24835  1875.52342
}
>>> # 可以设置b_days_only参数来将价格填充到非交易日,形成完整的日期序列
>>> qt.get_history_data(htype_names='open, high, low, close, vol', shares='000001.SZ', start='20191225', end='20200105', b_days_only=False)
{'000001.SZ':
              open   high    low  close         vol
 2019-12-25  16.45  16.56  16.24  16.30   414917.98
 2019-12-26  16.34  16.48  16.32  16.47   372033.86
 2019-12-27  16.53  16.93  16.43  16.63  1042574.72
 2019-12-28  16.53  16.93  16.43  16.63  1042574.72
 2019-12-29  16.53  16.93  16.43  16.63  1042574.72
 2019-12-30  16.46  16.63  16.10  16.57   976970.31
 2019-12-31  16.57  16.63  16.31  16.45   704442.25
 2020-01-01  16.57  16.63  16.31  16.45   704442.25
 2020-01-02  16.65  16.95  16.55  16.87  1530231.87
 2020-01-03  16.94  17.31  16.92  17.18  1116194.81
 2020-01-04  16.94  17.31  16.92  17.18  1116194.81
 2020-01-05  16.94  17.31  16.92  17.18  1116194.81
 }
>>> # 使用特殊的htypes,可以获取特定的数据,如指数权重数据,下面的代码获取000001.SZ在HS300指数重的权重数据,单位为百分比
>>> qt.get_history_data(htype_names='wt_id:000300.SH', shares='000001.SZ, 000002.SZ', start='20191225', end='20200105')
{'000001.SZ':
            wt_idx:000300.SH
2020-01-02        1.1714
2020-01-03        1.1714,
'000002.SZ':
            wt_idx:000300.SH
2020-01-02        1.3595
2020-01-03        1.3595
}