### Install AKShare on Raspberry Pi Source: https://github.com/akfamily/akshare/blob/main/docs/installation.md Steps to install AKShare on a Raspberry Pi 4B, including OS setup, virtual environment creation, and installation. ```bash sudo apt-get install python3-venv ``` ```bash python3 -m venv myenv ``` ```bash source myenv/bin/activate ``` ```bash pip install akshare --upgrade ``` -------------------------------- ### Get Stock Equity Pledge Data Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches stock equity pledge data for a specific date. Ensure the akshare library is installed. ```python import akshare as ak stock_cg_equity_mortgage_cninfo_df = ak.stock_cg_equity_mortgage_cninfo(date="20210930") print(stock_cg_equity_mortgage_cninfo_df) ``` -------------------------------- ### Get B-Share Daily Data (Forward Adjusted) Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches historical daily B-share data with forward adjustment. Ensure you have the akshare library installed. ```python import akshare as ak stock_zh_b_daily_qfq_df = ak.stock_zh_b_daily(symbol="sh900901", start_date="19900103", end_date="20240722", adjust="qfq") print(stock_zh_b_daily_qfq_df) ``` -------------------------------- ### Get Individual Stock Spot Quote (Xueqiu) Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches the latest real-time quote data for a given stock symbol from Xueqiu. Ensure you have the akshare library installed. ```python import akshare as ak stock_individual_spot_xq_df = ak.stock_individual_spot_xq(symbol="SH600000") print(stock_individual_spot_xq_df) ``` -------------------------------- ### Install AKTools Library Source: https://github.com/akfamily/akshare/blob/main/docs/deploy_http.md Install the AKTools library using pip. This is the first step to set up the AKShare HTTP deployment. ```shell pip install aktools ``` -------------------------------- ### Verify AKShare Installation (Docstring) Source: https://github.com/akfamily/akshare/blob/main/docs/anaconda.md Enter the Python interpreter within the activated virtual environment and import AKShare to verify the installation by printing its docstring. A successful output indicates AKShare is ready to use. ```python import akshare as ak print(ak.__doc__) ``` -------------------------------- ### Get Fund Stock Holdings Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches historical fund stock holdings for a given stock symbol. Requires the 'akshare' library to be installed. ```python import akshare as ak stock_fund_stock_holder_df = ak.stock_fund_stock_holder(symbol="601318") print(stock_fund_stock_holder_df) ``` -------------------------------- ### Install AKShare with a Domestic Source Source: https://github.com/akfamily/akshare/blob/main/docs/answer.md Use a domestic Python package index to speed up AKShare installation. This is recommended for users in regions with slower internet access to external repositories. ```bash pip install akshare -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com --upgrade ``` ```bash pip install akshare -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com --user --upgrade ``` -------------------------------- ### Run AKTools Source: https://github.com/akfamily/akshare/blob/main/docs/deploy_http.md Execute the AKTools module to start the HTTP deployment. This command initiates the AKShare HTTP service. ```shell python -m aktools ``` -------------------------------- ### Get General Holding Statistics Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Retrieves general shareholder holding statistics for a specified date from Eastmoney.com. Requires the 'akshare' library to be installed. ```python import akshare as ak stock_gdfx_holding_statistics_em_df = ak.stock_gdfx_holding_statistics_em(date="20210930") print(stock_gdfx_holding_statistics_em_df) ``` -------------------------------- ### Get Fund Bond Holdings Source: https://github.com/akfamily/akshare/blob/main/docs/data/fund/fund_public.md Retrieves bond holding data for a specified fund symbol and year. Ensure you have the akshare library installed. ```python import akshare as ak fund_portfolio_bond_hold_em_df = ak.fund_portfolio_bond_hold_em(symbol="000001", date="2023") print(fund_portfolio_bond_hold_em_df) ``` -------------------------------- ### Backtrader Setup with AKShare Data Source: https://github.com/akfamily/akshare/blob/main/docs/demo.md This Python snippet demonstrates the initial setup for Backtrader, including importing necessary libraries, configuring matplotlib for Chinese characters, and fetching historical stock data using AKShare. ```python from datetime import datetime import backtrader as bt # 升级到最新版 import matplotlib.pyplot as plt # 由于 Backtrader 的问题,此处要求 pip install matplotlib==3.2.2 import akshare as ak # 升级到最新版 import pandas as pd plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams["axes.unicode_minus"] = False stock_hfq_df = ak.stock_zh_a_hist(symbol="000001", adjust="hfq").iloc[:, :7] del stock_hfq_df['股票代码'] stock_hfq_df.columns = [ 'date', 'open', 'close', 'high', 'low', 'volume', ] ``` -------------------------------- ### Get Hurun Rich List Data Source: https://github.com/akfamily/akshare/blob/main/docs/data/others/others.md Fetches data for the Hurun Rich List for a specified year. Ensure you have the akshare library installed. ```python import akshare as ak hurun_rank_df = ak.hurun_rank(indicator="胡润百富榜", year="2023") print(hurun_rank_df) ``` -------------------------------- ### Get A-Share Daily Transaction Details Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches daily transaction details for A-shares. Specify the start and end dates for the data retrieval. ```python import akshare as ak stock_dzjy_mrmx_df = ak.stock_dzjy_mrmx(symbol='A股', start_date='20220104', end_date='20220104') print(stock_dzjy_mrmx_df) ``` -------------------------------- ### Configure and Run PyBroker Backtest Source: https://github.com/akfamily/akshare/blob/main/docs/demo.md This snippet demonstrates how to configure a PyBroker strategy with initial cash, add the defined execution logic, and run a backtest. The resulting metrics are then printed. ```python my_config = pb.StrategyConfig(initial_cash=500000) strategy = Strategy(akshare, start_date='20200131', end_date='20230228', config=my_config) strategy.add_execution(fn=buy_with_stop_loss, symbols=[pb.param(name='stock_code')]) result = strategy.backtest() print(result.metrics_df.round(4)) ``` -------------------------------- ### Get Breakout Stock Pool Data Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches the breakout stock pool data for a specific date from Eastmoney. Ensure you have the akshare library installed. ```python import akshare as ak stock_zt_pool_zbgc_em_df = ak.stock_zt_pool_zbgc_em(date='20241011') print(stock_zt_pool_zbgc_em_df) ``` -------------------------------- ### View Generated Documentation Source: https://github.com/akfamily/akshare/blob/main/docs/README.rst Opens the previously generated HTML documentation in your default web browser. This command assumes 'make html' has already been run. ```bash make htmlview ``` -------------------------------- ### Get Strong Stocks Pool Data Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches data for the strong stocks pool from Eastmoney for a specified date. Ensure you have the akshare library installed. ```python import akshare as ak stock_zt_pool_strong_em_df = ak.stock_zt_pool_strong_em(date='20241231') print(stock_zt_pool_strong_em_df) ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/akfamily/akshare/blob/main/docs/README.rst Installs the necessary Python libraries for compiling documentation using Sphinx. Ensure you are in the project directory containing 'requirements.txt'. ```bash pip install -r requirements.txt ``` -------------------------------- ### Get Stock Changes by Symbol Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Retrieves recent 'pan-mouth anomaly' data for a specific stock symbol from Eastmoney. Ensure you have the akshare library installed. ```python import akshare as ak stock_changes_em_df = ak.stock_changes_em(symbol="大笔买入") print(stock_changes_em_df) ``` -------------------------------- ### Verify AKShare Installation (Version) Source: https://github.com/akfamily/akshare/blob/main/docs/anaconda.md Within the activated virtual environment's Python interpreter, import AKShare and print its version to confirm the installation. This is a quick check to ensure the library is correctly installed and accessible. ```python import akshare as ak print(ak.__version__) ``` -------------------------------- ### Install and Upgrade AKShare Source: https://github.com/akfamily/akshare/blob/main/docs/anaconda.md Install or upgrade the AKShare library within the activated virtual environment. This command ensures you have the latest version and all necessary dependencies are installed. ```bash pip install akshare --upgrade ``` -------------------------------- ### Get Free Float Holding Statistics Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Retrieves free float holding statistics for a specified date from Eastmoney.com. Requires the 'akshare' library to be installed. ```python import akshare as ak stock_gdfx_free_holding_statistics_em_df = ak.stock_gdfx_free_holding_statistics_em(date="20210930") print(stock_gdfx_free_holding_statistics_em_df) ``` -------------------------------- ### Install AKShare (China) Source: https://github.com/akfamily/akshare/blob/main/docs/installation.md Use this command for installation within China, utilizing a Tsinghua University mirror for faster downloads. ```bash pip install akshare --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple ``` -------------------------------- ### Get Shanghai Securities Fund Ratings Source: https://github.com/akfamily/akshare/blob/main/docs/data/fund/fund_public.md Fetches all fund rating data from Shanghai Securities for a specified date. Requires the 'akshare' library to be installed. ```python import akshare as ak fund_rating_sh_df = ak.fund_rating_sh(date='20230630') print(fund_rating_sh_df) ``` -------------------------------- ### Compile Documentation to HTML Source: https://github.com/akfamily/akshare/blob/main/docs/README.rst Generates the documentation in classic HTML format. The output will be placed in the 'build/html' directory. ```bash make html ``` -------------------------------- ### Get Bond Daily Transaction Details Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Retrieves daily transaction details for bonds. The function requires specifying the start and end dates for the data. ```python import akshare as ak stock_dzjy_mrmx_df = ak.stock_dzjy_mrmx(symbol='债券', start_date='20220104', end_date='20220104') print(stock_dzjy_mrmx_df) ``` -------------------------------- ### AKQuant Strategy Example Source: https://github.com/akfamily/akshare/blob/main/docs/demo.md A simple AKQuant strategy that buys when the closing price is higher than the opening price (bullish) and sells when the closing price is lower than the opening price (bearish). Requires AKShare for data and AKQuant for the strategy framework. ```python import akquant as aq import akshare as ak from akquant import Strategy # 1. 准备数据 # 使用 akshare 获取 A 股历史数据 (需安装: pip install akshare) df = ak.stock_zh_a_daily(symbol="sh600000", start_date="20230101", end_date="20231231") class MyStrategy(Strategy): def on_bar(self, bar): # 简单策略示例: # 当收盘价 > 开盘价 (阳线) -> 买入 # 当收盘价 < 开盘价 (阴线) -> 卖出 # 获取当前持仓 current_pos = self.get_position(bar.symbol) if current_pos == 0 and bar.close > bar.open: self.buy(bar.symbol, 100) print(f"[{bar.timestamp_str}] Buy 100 at {bar.close:.2f}") elif current_pos > 0 and bar.close < bar.open: self.close_position(bar.symbol) print(f"[{bar.timestamp_str}] Sell 100 at {bar.close:.2f}") # 运行回测 result = aq.run_backtest( data=df, strategy=MyStrategy, symbol="sh600000" ) # 打印回测结果 print("\n=== Backtest Result ===") print(result.metrics_df) ``` -------------------------------- ### Get Fund Daily Transaction Details Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches daily transaction details for funds. Ensure the start and end dates are correctly formatted for the query. ```python import akshare as ak stock_dzjy_mrmx_df = ak.stock_dzjy_mrmx(symbol='基金', start_date='20220104', end_date='20220104') print(stock_dzjy_mrmx_df) ``` -------------------------------- ### Initialize AKShare Data Source and Query Stock Data Source: https://github.com/akfamily/akshare/blob/main/docs/demo.md This snippet shows how to initialize the AKShare data source and query historical stock data for a specified symbol and date range using PyBroker parameters. ```python import pybroker as pb from pybroker import Strategy, ExecContext from pybroker.ext.data import AKShare pb.param(name='stock_code', value='600000') pb.param(name='percent', value=1) pb.param(name='stop_loss_pct', value=10) pb.param(name='stop_profit_pct', value=10) akshare = AKShare() df = akshare.query(symbols=[pb.param(name='stock_code')], start_date='20200131', end_date='20230228') ``` -------------------------------- ### Get Stock Shareholder Count Details Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches detailed shareholder count information for a specific A-share stock symbol. Ensure you have the `akshare` library installed. ```python import akshare as ak stock_zh_a_gdhs_detail_em_df = ak.stock_zh_a_gdhs_detail_em(symbol="000001") print(stock_zh_a_gdhs_detail_em_df) ``` -------------------------------- ### Install Specific AKTools Version for HTTP API Source: https://github.com/akfamily/akshare/blob/main/docs/deploy_http.md Install version 0.0.68 of AKTools if you only need to experience the HTTP API functionality. ```shell pip install aktools==0.0.68 ``` -------------------------------- ### Historical Sector Fund Flow Data Example Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Example output of historical fund flow data for the '汽车服务' sector. Data includes daily net inflow, net inflow percentage, and breakdown by order size. ```python 日期 主力净流入-净额 主力净流入-净占比 ... 中单净流入-净占比 小单净流入-净额 小单净流入-净占比 0 2024-08-20 -54471010.0 -10.09 ... 2.28 37641239.0 6.97 1 2024-08-21 10178690.0 1.91 ... -0.79 -10308872.0 -1.94 2 2024-08-22 -44754582.0 -8.39 ... 1.49 32534534.0 6.10 3 2024-08-23 -27649222.0 -6.52 ... -0.86 29341810.0 6.92 4 2024-08-26 -42767862.0 -10.11 ... -2.63 53748702.0 12.70 .. ... ... ... ... ... ... ... 116 2025-02-18 -93269892.0 -6.56 ... 1.78 58625760.0 4.12 117 2025-02-19 21681328.0 1.51 ... 1.97 -32209872.0 -2.25 118 2025-02-20 11260284.0 0.89 ... 0.43 -6260576.0 -0.50 119 2025-02-21 -81443762.0 -5.66 ... 3.29 47217792.0 3.28 120 2025-02-24 20329299.0 1.44 ... 1.47 -58079088.0 -4.12 [121 rows x 11 columns] ``` -------------------------------- ### Get Sina ETF Fund Category Data Source: https://github.com/akfamily/akshare/blob/main/docs/data/fund/fund_public.md Fetches real-time market data for closed-end funds from Sina Finance. Ensure you have the akshare library installed. ```python import akshare as ak fund_etf_category_sina_df = ak.fund_etf_category_sina(symbol="封闭式基金") print(fund_etf_category_sina_df) ``` -------------------------------- ### Get Regional Oil Price Details Source: https://github.com/akfamily/akshare/blob/main/docs/data/energy/energy.md Fetches historical regional oil price data for a specified adjustment date. Ensure you have the akshare library installed. ```python import akshare as ak energy_oil_detail_df = ak.energy_oil_detail(date="20240118") print(energy_oil_detail_df) ``` -------------------------------- ### Install AKShare (China - Anaconda) Source: https://github.com/akfamily/akshare/blob/main/docs/installation.md This command is for installing AKShare using Anaconda within China, with the --user flag and a Tsinghua University mirror. ```bash pip install akshare --upgrade --user -i https://pypi.tuna.tsinghua.edu.cn/simple ``` -------------------------------- ### Call AKShare API with Parameters in MATLAB Source: https://github.com/akfamily/akshare/blob/main/docs/installation.md Demonstrates how to call AKShare's stock_zh_a_hist interface with specific parameters in MATLAB. Parameters must be passed in order. ```matlab py.akshare.stock_zh_a_hist("000001", "daily", "20170301", '20210907', "") ``` -------------------------------- ### Get Institutional Statistics (Eastmoney) Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Retrieves institutional statistics for Hong Kong-Shanghai-Shenzhen Stock Connect from Eastmoney. Specify the market, start date, and end date. ```python import akshare as ak stock_hsgt_institution_statistics_em_df = ak.stock_hsgt_institution_statistics_em(market="北向持股", start_date="20201218", end_date="20201218") print(stock_hsgt_institution_statistics_em_df) ``` -------------------------------- ### Get 1-Minute Stock Board Concept Historical Data Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches 1-minute historical data for a specified stock board concept. Ensure you have the akshare library installed. ```python import akshare as ak stock_board_concept_hist_min_em_df = ak.stock_board_concept_hist_min_em(symbol="长寿药", period="1") print(stock_board_concept_hist_min_em_df) ``` -------------------------------- ### Get US Stock Daily Data (Forward Adjustment Factor) Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches the forward adjustment factors (qfq) for a specified US stock symbol. This data is useful for understanding how historical prices were adjusted. Ensure you have the akshare library installed. ```python import akshare as ak qfq_df = ak.stock_us_daily(symbol="AAPL", adjust="qfq-factor") print(qfq_df) ``` -------------------------------- ### Get HK Stock Profit Forecast Overview Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Fetches the profit forecast overview for a specific Hong Kong stock symbol. Ensure you have the akshare library installed. ```python import akshare as ak stock_hk_profit_forecast_et_df = ak.stock_hk_profit_forecast_et(symbol="09999", indicator="盈利预测概览") print(stock_hk_profit_forecast_et_df) ``` -------------------------------- ### Get Individual Fund Performance Data (Xueqiu) Source: https://github.com/akfamily/akshare/blob/main/docs/data/fund/fund_public.md Fetches detailed performance data for a specific fund from Xueqiu. Requires a fund symbol as input. ```python import akshare as ak fund_individual_achievement_xq_df = ak.fund_individual_achievement_xq(symbol="000001") print(fund_individual_achievement_xq_df) ``` -------------------------------- ### Get Quarterly Cash Flow Statement (Eastmoney) Source: https://github.com/akfamily/akshare/blob/main/docs/data/stock/stock.md Retrieves the quarterly cash flow statement for a given stock symbol from Eastmoney. Ensure you have the akshare library installed. ```python import akshare as ak stock_cash_flow_sheet_by_quarterly_em_df = ak.stock_cash_flow_sheet_by_quarterly_em(symbol="SH600519") print(stock_cash_flow_sheet_by_quarterly_em_df) ``` -------------------------------- ### Get Hong Kong Index Spot Data (Sina) Source: https://github.com/akfamily/akshare/blob/main/docs/data/index/index.md Use this function to fetch real-time Hong Kong index spot data. It returns all available data in a single response. ```python import akshare as ak stock_hk_index_spot_sina_df = ak.stock_hk_index_spot_sina() print(stock_hk_index_spot_sina_df) ``` -------------------------------- ### Create Anaconda Virtual Environment Source: https://github.com/akfamily/akshare/blob/main/docs/anaconda.md Use this command to create a new virtual environment named 'ak_test' with a specified Python version (3.9 or higher is recommended for AKShare). Confirm the creation by typing 'y' when prompted. ```bash conda create -n ak_test python=3.9 ``` ```bash Proceed 输入 y ``` -------------------------------- ### Get China Merchants Securities Fund Ratings Source: https://github.com/akfamily/akshare/blob/main/docs/data/fund/fund_public.md Fetches all fund rating data from China Merchants Securities for a specified date. Requires the 'akshare' library to be installed. ```python import akshare as ak fund_rating_zs_df = ak.fund_rating_zs(date='20230331') print(fund_rating_zs_df) ``` -------------------------------- ### Resolve Pip Permission Denied Error Source: https://github.com/akfamily/akshare/blob/main/docs/installation.md Offers a solution for 'Permission denied' errors during pip installation by using the --user flag. This installs packages in the user's home directory. ```bash pip install akshare --user ```