qstock Documentation

repository·master·Indexed 24 days ago

https://github.com/tkfy920/qstock

An open-source Python library for financial quantitative research and analysis. qstock provides interfaces for accessing real-time and historical market data (stocks, funds, futures, indices), financial statements, fundamental data, and macro-economic indicators. It includes tools for stock selection via Wencai, capital flow analysis (including Northbound capital), and a visualization module for K-line, Heikin-Ashi, Ichimoku Cloud, and treemap charts.

Tokens
3.2K
Snippets
1
Records
32
Agent score
34%

What's inside qstock

  1. Install and update qstock

    master

    Install the qstock library using pip. To update to the latest version, use the --upgrade flag.

    Note for Wencai users: To use the wencai feature, you must upgrade pywencai (pip install --upgrade pywencai) and install jsdom via Node.js by running npm install jsdom in your terminal.

  2. Generate a word cloud from news data

    master

    You can create a word cloud by fetching news data using qs.news_data, processing the text content to handle missing values and convert it to a string, and then using the plot module to process the text with jieba and render the chart.

    Note: This workflow assumes you have a plot object available (likely a utility module within the library) that provides cloud_data and chart_wordcloud methods.

  3. Get real-time market data with realtime_data()

    master

    Use qs.realtime_data(market, code) to retrieve the latest market indicators for an entire market or specific securities.

    Parameters:

    • market: The market name or list. Common values include:
      • '沪深京A', '沪深A', '沪A', '深A', '北A' (A-shares)
      • '可转债' (Convertible bonds)
      • '期货' (Futures)
      • '创业板' (GEM)
      • '美股' (US stocks)
      • '港股' (HK stocks)
      • '行业板块', '概念板块' (Sectors/Concepts)
      • 'ETF', 'LOF'
      • '沪深指数', '上证指数', '深证指数'
    • code: A single security name/code or a list of names/codes (e.g., '中国平安', '000001', or ['AAPL', '000001']). If omitted, returns data for the entire market.
  4. Monitor real-time market anomalies with realtime_change()

    master

    Retrieve real-time market anomaly data (often used for monitoring price action).

    Parameters:

    • flag: The type of anomaly to filter for. You can use descriptive strings or integer IDs (1-22).

    Available Flags (Examples):

    • '火箭发射' (Rocket launch)
    • '快速反弹' (Rapid rebound)
    • '加速下跌' (Accelerated fall)
    • '60日新高' (60-day high)
    • '60日新低' (60-day low)
    • '大笔买入' (Large buy order)
    • '封涨停板' (Hit upper limit)
  5. Plot Scatter plots with regression and marginals

    master

    Use plot.scatter() to visualize relationships between two variables.

    Parameters:

    • trend: Set to 'ols' to add an Ordinary Least Squares regression line.
    • marginal_x: Add marginal distributions (e.g., 'histogram', 'violin').
    • marginal_y: Add marginal distributions (e.g., 'box').
    • size: Column name to scale point size based on value.
  6. Plot Treemaps for sector/concept data

    master

    Use plot.treemap() to visualize hierarchical data like industry sectors or market concepts.

    Parameters:

    • data: DataFrame containing the data.
    • label: A list of column names or labels to display (e.g., ['Industry', 'Stock Name']).
    • weight: The column name used to determine the size/weight of the rectangles.
    • value: The column name used for the values.
    • color: A list of colors for the visualization.
  7. Retrieve macro-economic indicators with macro_data()

    master

    Access common macro-economic indicators.

    Parameters:

    • flag: The indicator type:
      • 'gdp': GDP data (default).
      • 'cpi': Consumer Price Index.
      • 'ppi': Producer Price Index.
      • 'pmi': Purchasing Managers' Index.
      • 'ms': Money Supply.
      • 'lpr': Loan Prime Rate.

    Alternatively, you can use dedicated convenience functions: qs.cpi(), qs.gdp(), qs.ms(), qs.ppi(), qs.pmi(), or qs.lpr().

  8. Get interbank lending rates with ib_rate()

    master

    Retrieve interbank lending rates for various markets.

    Parameters:

    • market: The market identifier:
      • 'sh': Shanghai Interbank Market.
      • 'ch': China Interbank Market.
      • 'l': London Interbank Market (Note: use lowercase 'l').
      • 'eu': European Interbank Market.
      • 'hk': Hong Kong Interbank Market.
      • 's': Singapore Interbank Market.
    • fc: Foreign currency code (e.g., 'USD', 'GBP', 'EUR', 'JPY', 'HKD', 'SGD', 'CNY').

    Returns a pandas DataFrame.

  9. Plot Statistical Distribution charts (Hist, KDE, Box, Violin)

    master

    The library provides several tools for statistical analysis:

    • plot.hist(data, ...): Histograms. Use histnorm to set normalization: 1:'percent', 2:'probability', 3:'density', 4:'probability density'.
    • plot.hist_kde(data, ...): Histogram with Kernel Density Estimation. Use stat to choose: 'count', 'frequency', 'density', or 'probability'. Set kde=True to add the curve.
    • plot.box(data, ...): Box plots for visualizing quartiles.
    • plot.violin(data, ...): Violin plots, combining box plots with kernel density estimation to show data distribution shape.
  10. Retrieve financial statements with financial_statement()

    master

    Use financial_statement(flag, date) to fetch various types of financial reports.

    Parameters:

    • flag: The type of report. Supported values:
      • '业绩报表' or 'yjbb': Annual/Quarterly financial indicators.
      • '业绩快报' or 'yjkb': Latest performance previews (Note: currently has a known bug in some versions).
      • '业绩预告' or 'yjyg': Latest performance forecasts.
      • '资产负债表' or 'zcfz': Latest balance sheet indicators.
      • '利润表' or 'lrb': Latest income statement indicators.
      • '现金流量表' or 'xjll': Latest cash flow statement indicators.
    • date: Report date in 'YYYYMMDD' format (e.g., '20220630'). Defaults to the latest available report.

    Returns a pandas DataFrame.