### Get Security Start Date Source: https://www.joinquant.com/help/api/doc?id=10244&name=JQDatadoc Example of retrieving the listing date for a security. ```python #获取000001.XSHE的上市时间 start_date = get_security_info('000001.XSHE').start_date print(start_date) >>>1991-04-03 ``` -------------------------------- ### Example: Get All Financial Indicator Data for a Specific Day Source: https://www.joinquant.com/help/api/doc?id=9885&name=JQDatadoc This example demonstrates how to retrieve all available financial indicator data for a given day. ```python ``` -------------------------------- ### Example: Displaying JQData Characteristic Securities Source: https://www.joinquant.com/help/api/doc?id=10759&name=JQDatadoc This example demonstrates how to call the get_all_securities function with 'spi' type and then slices the result to display the first 5 entries. It shows the structure of the returned data, including display name, name, start date, end date, and type. ```python get_all_securities(types=['spi'], date=None)[:5] ``` -------------------------------- ### Example: Display Account Information Source: https://www.joinquant.com/help/api/doc?id=10532&name=logon This example demonstrates how to call `get_account_info()` and print the returned dictionary containing account details. ```python #查询账号信息 infos = get_account_info() print(infos) ``` -------------------------------- ### Get Price Function - Panel Format Example Source: https://www.joinquant.com/help/api/doc?id=9978&name=JQDatadoc This comment indicates an example for retrieving multiple futures contracts and returning the data in a Panel format, which is relevant for older pandas versions (<0.25). ```python #获取多支期货 ``` -------------------------------- ### Example: Query Market Trading Calendar Data Source: https://www.joinquant.com/help/api/doc?id=9867&name=JQDatadoc This example demonstrates how to query the market trading calendar for a date range starting from '2015-01-04' and retrieves up to 4 records. It shows the output format including the day, market ID, market name, type ID, and type. ```python q=query(finance.STK_EXCHANGE_LINK_CALENDAR).filter(finance.STK_EXCHANGE_LINK_CALENDAR.day>='2015-01-04').limit(4) df=finance.run_query(q) print(df) ``` -------------------------------- ### Get Financing and Securities Lending Data for Multiple Funds Source: https://www.joinquant.com/help/api/doc?id=10343&name=JQDatadoc Fetch financing and securities lending data for multiple funds over a specified period. This example first retrieves a list of all fund codes available on a given date. ```python fund=get_all_securities("fund",date='2023-02-01').index.tolist() df=get_mtss(fund,'2023-02-07','2023-02-09',fields=['date','sec_code','fin_value','fin_buy_value','fin_refund_value','sec_value','sec_sell_value','sec_refund_value','fin_sec_value']) ``` -------------------------------- ### Install JQData Source: https://www.joinquant.com/help/api/doc?id=10748&name=JQDatadoc Instructions for installing the jqdatasdk package for Python users. ```APIDOC ## Install JQData To install the JQData SDK for Python, open your terminal or command prompt and run: ``` pip install jqdatasdk ``` If you encounter any issues, please refer to the detailed JQData installation tutorial. ``` -------------------------------- ### Import JQData Library and Get Factor Values Source: https://www.joinquant.com/help/api/doc?id=10635&name=JQDatadoc Imports the JQData library and demonstrates the usage of the `get_factor_values` function to retrieve specific style factor data for a given security and date range. The example then prints the 'btop' factor values. ```python # 导入函数库 from jqdatasdk import * # 取值函数 get_factor_values(securities, factors, start_date, end_date, count) ``` ```python from jqdatasdk import get_factor_values factor_data = get_factor_values(securities=['000001.XSHE'], factors=['btop','divyild','earnqlty'], start_date='2022-01-01', end_date='2022-01-10') print(factor_data['btop']) ``` -------------------------------- ### Install JQData Source: https://www.joinquant.com/help/api/doc?id=9823&name=logon Instructions for installing or upgrading the jqdatasdk package for Python users. ```APIDOC ## Install JQData ### Description Install the jqdatasdk package using pip. ### Method pip install ### Endpoint N/A ### Parameters N/A ### Request Example ```bash pip install jqdatasdk ``` ### Response N/A ``` ```APIDOC ## Upgrade JQData ### Description Upgrade the jqdatasdk package to the latest version. ### Method pip install -U ### Endpoint N/A ### Parameters N/A ### Request Example ```bash pip install -U jqdatasdk ``` ### Response N/A ``` ```APIDOC ## Install Thriftpy2 (if needed) ### Description Manually install a lower version of thriftpy2 if C++ components are required during installation and you wish to avoid installing them. ### Method pip install ### Endpoint N/A ### Parameters N/A ### Request Example ```bash pip install thriftpy2==0.4.20 ``` ### Response N/A ``` -------------------------------- ### Install ThriftPy2 for Compatibility Source: https://www.joinquant.com/help/api/doc?id=10748&name=JQDatadoc If installation prompts for C++ components and you wish to avoid installing them, install a specific lower version of thriftpy2. This is a common workaround for certain dependency issues. ```bash pip install thriftpy2==0.4.20 ``` -------------------------------- ### Example: Query Latest Daily Pre-open Options Data Source: https://www.joinquant.com/help/api/doc?id=9925&name=JQDatadoc This example demonstrates how to query the latest daily pre-open static data for a specific option contract code. It selects specific fields, filters by contract code, orders by date in descending order, and limits the result to 10 entries. ```python #查询"10001313.XSHG "最新的期权每日盘前静态数据。 from jqdatasdk import * q=query(opt.OPT_DAILY_PREOPEN.code, opt.OPT_DAILY_PREOPEN.trading_code, opt.OPT_DAILY_PREOPEN.name, opt.OPT_DAILY_PREOPEN.exercise_date,).filter(opt.OPT_DAILY_PREOPEN.code=='10001313.XSHG').order_by(opt.OPT_DAILY_PREOPEN.date.desc()).limit(10) df=opt.run_query(q) print(df) ``` -------------------------------- ### Example: Get Hengrui Medicine Status Changes Source: https://www.joinquant.com/help/api/doc?id=10023&name=JQDatadoc This example demonstrates how to retrieve the status change history for a specific stock, '600276.XSHG' (Hengrui Medicine), returning up to 10 records. ```python # Specify the query object for Hengrui Medicine (600276.XSHG) company status changes q=query(finance.STK_STATUS_CHANGE).filter(finance.STK_STATUS_CHANGE.code=='600276.XSHG').limit(10) df=finance.run_query(q) print(df) ``` -------------------------------- ### Example: Fetching Net Operating Cash Flow Growth Rate Source: https://www.joinquant.com/help/api/doc?id=10436&name=JQDatadoc Demonstrates how to fetch specific growth factor values for a given stock and date range. The example shows how to print the retrieved net operating cash flow growth rate. ```python from jqdatasdk import get_factor_values factor_data = get_factor_values(securities=['000001.XSHE'], factors=['operating_revenue_growth_rate','total_asset_growth_rate','net_operate_cashflow_growth_rate'], start_date='2022-01-01', end_date='2022-01-10') # 查看因子值 print(factor_data['net_operate_cashflow_growth_rate']) ``` -------------------------------- ### Get Security Display Name Source: https://www.joinquant.com/help/api/doc?id=10244&name=JQDatadoc Example of retrieving the Chinese display name for a security on a specific date. ```python # 获取000001.XSHE某一日期的中文名称 display_name = get_security_info('000001.XSHE',date='2017-08-31’).display_name print(display_name) >>>平安银行 ``` -------------------------------- ### Get Security Information Source: https://www.joinquant.com/help/api/doc?id=9840&name=JQDatadoc Retrieves detailed information about a specific security. Use this to get attributes like display name, start date, end date, type, and parent fund for a given security code. ```python get_security_info(code,date=None) ``` ```python # 获取000001.XSHE某一日期的中文名称 display_name = get_security_info('000001.XSHE',date='2017-08-31’).display_name print(display_name) >>>平安银行 ``` ```python #获取000001.XSHE的上市时间 start_date = get_security_info('000001.XSHE').start_date print(start_date) >>>1991-04-03 ``` ```python #获取150008.XSHE的母基金代码 parent = get_security_info('150008.XSHE').parent print(parent) ``` -------------------------------- ### Example: Fetching and Printing AH Stock Price Data Source: https://www.joinquant.com/help/api/doc?id=9868&name=JQDatadoc This example demonstrates how to construct a query for the `STK_AH_PRICE_COMP` table, specifying a stock code and limiting the results. The fetched data is then printed to the console. ```python q=query(finance.STK_AH_PRICE_COMP).filter(finance.STK_AH_PRICE_COMP.a_code=='000002.XSHE').order_by(finance.STK_AH_PRICE_COMP.day).limit(4) df=finance.run_query(q) print(df) ``` -------------------------------- ### Get Convertible Bond Listing Date Source: https://www.joinquant.com/help/api/doc?id=10341&name=JQDatadoc Example of retrieving the listing date for a convertible bond using its security code. ```python # 获取000001.XSHE的上市时间 start_date = get_security_info('110043.XSHG').start_date print(start_date) >>>2018-03-14 ``` -------------------------------- ### Get Convertible Bond Chinese Name Source: https://www.joinquant.com/help/api/doc?id=10341&name=JQDatadoc Example of retrieving the Chinese display name for a convertible bond using its security code. ```python # 获取110043.XSHG的中文名称 display_name = get_security_info('110043.XSHG').display_name print(display_name) >>>无锡转债 ``` -------------------------------- ### Example: Query Latest Option Contract Data Source: https://www.joinquant.com/help/api/doc?id=9918&name=JQDatadoc This example demonstrates how to query the latest basic data for a specific option contract using its code. The result is returned as a pandas DataFrame. ```python #查询"10001313.XSHG "最新的期权基本资料数据。 from jqdatasdk import * q=query(opt.OPT_CONTRACT_INFO).filter(opt.OPT_CONTRACT_INFO.code=='10001313.XSHG') df=opt.run_query(q) print(df) ``` -------------------------------- ### Query Bond Database for Repo Daily Prices (Python) Source: https://www.joinquant.com/help/api/doc?id=10524&name=JQDatadoc Example showing how to query the bond database for daily repo prices within a specified date range using `run_offset_query`. It's recommended to filter by indexed fields like date or security code for faster queries. ```python # 查询bond库, 国债逆回购日行情2019年到2021年的数据,数据量约1.3万条 from jqdatasdk import query,bond # 或from jqdatasdk import * q = query(bond.REPO_DAILY_PRICE).filter(bond.REPO_DAILY_PRICE.date>'2019-01-01', bond.REPO_DAILY_PRICE.date<='2021-12-31' ) df = bond.run_offset_query(q) ``` -------------------------------- ### Install or Upgrade JQData SDK Source: https://www.joinquant.com/help/api/doc?id=10532&name=logon If the `get_account_info` interface is not defined, upgrade your jqdatasdk to the latest version using pip. ```bash pip install jqdatasdk -U ``` -------------------------------- ### Get Valuation Data for a Specific Date Source: https://www.joinquant.com/help/api/doc?id=9884&name=JQDatadoc Fetches valuation data for all stocks on a specified trading day. The example shows data for '2021-01-05' and truncates the output. ```python get_fundamentals(query(valuation),date="2021-01-05")[:4] ``` -------------------------------- ### Get Historical Data for a Single Futures Contract Source: https://www.joinquant.com/help/api/doc?id=9980&name=JQDatadoc Retrieves historical bar data for a specified futures contract. This example fetches 5 bars of 120-minute data for the 'A0603.XDCE' contract ending before '2006-03-15'. ```APIDOC ## get_bars (Single Contract) ### Description Retrieves historical bar data for a single futures contract. ### Method Python Function Call ### Parameters - **'A0603.XDCE'** (string) - Required - The symbol of the futures contract. - **5** (integer) - Required - The number of bars to retrieve. - **unit='120m'** (string) - Required - The time unit for the bars (e.g., '120m' for 120 minutes). - **fields=['open','close','low','high','volume','money','open_interest']** (list of strings) - Required - The fields to retrieve. - **include_now=False** (boolean) - Optional - Whether to include data from the current incomplete bar. - **end_dt='2006-03-15'** (string) - Optional - The end date for the data retrieval. ### Request Example ```python df = get_bars('A0603.XDCE', 5, unit='120m',fields=['open','close','low','high','volume','money','open_interest'],include_now=False,end_dt='2006-03-15') print(df) ``` ### Response #### Success Response - **DataFrame** - A pandas DataFrame containing the historical bar data with columns for date, open, close, low, high, volume, money, and open_interest. ``` -------------------------------- ### Example Usage of get_factor_kanban_values Source: https://www.joinquant.com/help/api/doc?id=10656&name=JQDatadoc Demonstrates how to call the get_factor_kanban_values function and print the head of the resulting DataFrame. This example fetches data for the 'hs300' universe with a 3-month backtest cycle using the 'long_only' model and 'style_pro' category. ```python from jqdatasdk import * df = get_factor_kanban_values(universe='hs300',bt_cycle='month_3',model='long_only', category=['style_pro'],skip_paused=False,commision_slippage=0) print(df.head(5)) date category code universe bt_cycle skip_paused \ 0 2024-03-22 style_pro resvol hs300 month_3 False 1 2024-03-22 style_pro market_beta hs300 month_3 False 2 2024-03-22 style_pro midcap hs300 month_3 False 3 2024-03-22 style_pro profit hs300 month_3 False 4 2024-03-22 style_pro liquidty hs300 month_3 False commision_slippage compound_return_1q compound_return_5q \ 0 0 0.082971 0.095416 1 0 0.132900 0.087422 2 0 0.115523 0.012262 3 0 0.096083 0.045657 4 0 0.091171 0.074147 annual_return_1q annual_return_5q max_drawdown_1q max_drawdown_5q \ 0 0.418498 0.491396 0.028479 0.201936 1 0.728554 0.444245 0.033119 0.197513 2 0.615251 0.054910 0.046911 0.192985 3 0.495380 0.216301 0.032075 0.100147 4 0.575279 0.451410 0.028270 0.157285 sharpe_1q sharpe_5q turnover_mean_1q turnover_mean_5q annual_return_bm \ 0 2.965449 1.225099 0.015205 0.016667 0.324823 1 4.462848 1.209714 0.011111 0.017836 0.324823 2 3.804401 0.048918 0.552632 0.017544 0.324823 3 3.318204 0.946735 0.000292 0.000000 0.324823 4 3.551314 1.142525 0.010069 0.004861 0.324823 ic_mean ir good_ic 0 -0.075242 -0.207315 0.982456 1 -0.067486 -0.166541 0.964912 2 -0.045928 -0.156631 0.982456 3 -0.043151 -0.203651 0.929825 4 -0.036663 -0.102687 0.958333 ``` -------------------------------- ### Fetch All Government Bond Reverse Repo Daily Market Data Source: https://www.joinquant.com/help/api/doc?id=9948&name=JQDatadoc This example retrieves all available daily market data for government bond reverse repos. It then sorts the results by 'deal_number' in descending order and displays the top 5 entries. ```python from jqdatasdk import * df=bond.run_query(query(bond.REPO_DAILY_PRICE)) print(df.sort_values(by='deal_number',ascending=False)[:5]) ``` -------------------------------- ### Get Historical Industry Component Records for an Entire Industry Source: https://www.joinquant.com/help/api/doc?id=10769&name=JQDatadoc Fetch historical inclusion and exclusion records for all stocks within a specified industry. This example shows how to retrieve the first 5 records for the ZJW industry. ```python get_history_industry('zjw')[:5] ``` -------------------------------- ### Get Historical Data for Multiple Futures Contracts Source: https://www.joinquant.com/help/api/doc?id=9980&name=JQDatadoc Retrieves historical bar data for multiple futures contracts simultaneously. This example fetches 5 bars of 120-minute data for 'A2109.XDCE' and 'A2111.XDCE' contracts ending before '2020-12-19'. ```APIDOC ## get_bars (Multiple Contracts) ### Description Retrieves historical bar data for multiple futures contracts. ### Method Python Function Call ### Parameters - **['A2109.XDCE', 'A2111.XDCE']** (list of strings) - Required - The symbols of the futures contracts. - **5** (integer) - Required - The number of bars to retrieve. - **unit='120m'** (string) - Required - The time unit for the bars (e.g., '120m' for 120 minutes). - **fields=['open','close','low','high','volume','money','open_interest']** (list of strings) - Required - The fields to retrieve. - **include_now=False** (boolean) - Optional - Whether to include data from the current incomplete bar. - **end_dt='2020-12-19'** (string) - Optional - The end date for the data retrieval. ### Request Example ```python df = get_bars(['A2109.XDCE', 'A2111.XDCE'], 5, unit='120m',fields=['open','close','low','high','volume','money','open_interest'],include_now=False,end_dt='2020-12-19') print(df) ``` ### Response #### Success Response - **DataFrame** - A pandas DataFrame containing the historical bar data for multiple contracts, indexed by contract symbol and date, with columns for open, close, low, high, volume, money, and open_interest. ``` -------------------------------- ### Querying Bond Daily Prices with run_offset_query Source: https://www.joinquant.com/help/api/doc?id=10528&name=JQDatadoc Example showing how to retrieve daily price data for government reverse repos within a specified date range using `run_offset_query`. This function helps in fetching large volumes of historical bond data. ```python # 查询bond库, 国债逆回购日行情2019年到2021年的数据,数据量约1.3万条 from jqdatasdk import query,bond # 或from jqdatasdk import * q = query(bond.REPO_DAILY_PRICE).filter(bond.REPO_DAILY_PRICE.date>'2019-01-01', bond.REPO_DAILY_PRICE.date<='2021-12-31' ) df = jq.bond.run_offset_query(q) ``` -------------------------------- ### Get Consolidated Balance Sheet Data Source: https://www.joinquant.com/help/api/doc?id=9902&name=JQDatadoc This example demonstrates how to query the consolidated balance sheet data for a financial company, extracting specific financial metrics like cash equivalents, total assets, and total liabilities. ```APIDOC ## Get Consolidated Balance Sheet Data ### Description Retrieves consolidated balance sheet data for financial companies. ### Method `finance.run_query()` ### Parameters - `query`: A query object specifying the desired fields and filters. - `finance.FINANCE_BALANCE_SHEET.company_name` (string) - `finance.FINANCE_BALANCE_SHEET.code` (string) - `finance.FINANCE_BALANCE_SHEET.pub_date` (string) - `finance.FINANCE_BALANCE_SHEET.start_date` (string) - `finance.FINANCE_BALANCE_SHEET.end_date` (string) - `finance.FINANCE_BALANCE_SHEET.cash_equivalents` (float) - `finance.FINANCE_BALANCE_SHEET.total_assets` (float) - `finance.FINANCE_BALANCE_SHEET.total_liability` (float) - `filter`: - `finance.FINANCE_BALANCE_SHEET.code` (string) - e.g., '601318.XSHG' - `finance.FINANCE_BALANCE_SHEET.pub_date` (string) - e.g., '>=2015-01-01' - `finance.FINANCE_BALANCE_SHEET.report_type` (integer) - e.g., 0 - `limit`: Maximum number of results to return. ### Request Example ```python from jqdatasdk import finance q = query(finance.FINANCE_BALANCE_SHEET.company_name, \ finance.FINANCE_BALANCE_SHEET.code, \ finance.FINANCE_BALANCE_SHEET.pub_date, \ finance.FINANCE_BALANCE_SHEET.start_date, \ finance.FINANCE_BALANCE_SHEET.end_date, \ finance.FINANCE_BALANCE_SHEET.cash_equivalents, \ finance.FINANCE_BALANCE_SHEET.total_assets, \ finance.FINANCE_BALANCE_SHEET.total_liability \ ).filter(finance.FINANCE_BALANCE_SHEET.code=='601318.XSHG', \ finance.FINANCE_BALANCE_SHEET.pub_date>='2015-01-01', \ finance.FINANCE_BALANCE_SHEET.report_type==0).limit(8) df = finance.run_query(q) print(df) ``` ### Response #### Success Response (DataFrame) Returns a pandas DataFrame containing the requested financial data. ``` -------------------------------- ### Get Bars for Multiple Securities Source: https://www.joinquant.com/help/api/doc?id=9999&name=JQDatadoc This example shows how to fetch historical bar data for multiple securities simultaneously. The function call is similar to fetching data for a single security, but the security code parameter accepts a list of identifiers. ```APIDOC ## Get Bars for Multiple Securities ### Description Fetches historical bar data for multiple securities simultaneously. ### Method `get_bars` ### Parameters - **security_codes** (list of strings) - Required - A list of unique identifiers for the securities (e.g., ['150049.XSHE','150050.XSHE']). - **count** (integer) - Required - The number of historical bars to retrieve. - **unit** (string) - Required - The time unit for the bars (e.g., '120m' for 120 minutes). - **fields** (list of strings) - Required - A list of fields to retrieve (e.g., ['date', 'open', 'close', 'high', 'low', 'volume', 'money', 'factor']). - **include_now** (boolean) - Optional - Whether to include data from the current, incomplete bar. - **end_dt** (string) - Optional - The end date for the data retrieval (YYYY-MM-DD). - **fq_ref_date** (string) - Optional - The reference date for forward/backward factor adjustment (YYYY-MM-DD). ### Request Example ```python df = get_bars(['150049.XSHE','150050.XSHE'], 5, unit='120m',fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money','factor'],include_now=False, end_dt='2018-12-05',fq_ref_date='2000-01-01') print(df) ``` ### Response #### Success Response (DataFrame) - The response is a pandas DataFrame with a MultiIndex, where the first level is the security code and the second level is the index of the bar. - **date** (datetime) - The timestamp of the bar. - **open** (float) - The opening price of the bar. - **close** (float) - The closing price of the bar. - **high** (float) - The highest price during the bar. - **low** (float) - The lowest price during the bar. - **volume** (float) - The trading volume during the bar. - **money** (float) - The trading value during the bar. - **factor** (float) - The adjustment factor for the bar. ```