Hikyuu Quantitative Research Framework

repository·master·Indexed 25 days ago

https://github.com/fasiondog/hikyuu

A high-performance, open-source quantitative research framework featuring a C++ core and Python interface. Optimized for the Chinese A-share market, it provides tools for high-speed financial computing, strategy backtesting, and data analysis. The framework includes specialized data types for K-line (KData), intraday (TimeLineList), and tick-by-tick (TransList) data, along with comprehensive date and time manipulation via the Datetime and TimeDelta classes.

Tokens
49.8K
Snippets
68
Records
315
Agent score
85%

What's inside Hikyuu

  1. Overview of Hikyuu Technical Indicators

    master
    The hikyuu.indicator module provides a comprehensive suite of functions for financial analysis, categorized into several functional groups including market data, technical indicators, mathematical functions, and statistical tools. These indicators can be used to build complex trading strategies and factor models.
  2. Manage quantitative trading strategy repositories via HUB

    master
    The HUB is a platform for managing quantitative trading strategy components. It allows for storing, sharing, and managing strategies (including investment logic, trading rules, and risk control). Strategies can be written in both Python and C++. You can use the public hikyuu_hub or set up your own local repositories to manage different factor libraries (e.g., alpha_101, alpha_36) and use Git for version control.
  3. Use Selector Algorithm Components (SE) in hikyuu.trade_sys

    master

    Selector Algorithm Components (SE) implement algorithms for evaluating and selecting targets and system strategies.

    Common parameters for SE instances:

    • get_n (int, default 0): Only selects the top get_n systems. If $\le 0$, all systems are selected.
    • depend_on_proto_sys (bool, default False): If True, the selector requires the prototype system to run independently. This is used when the actual system behavior must follow the companion system (e.g., depending on SG).
  4. Understand Hikyuu Licensing and Plugin Model

    master

    Hikyuu is an open-source high-performance quantitative trading engine under the Apache-2.0 license. The core framework, backtesting, indicators, and trading models are completely free and unlimited for all users.

    To support maintenance, the project offers independent value-added plugins via a voluntary donation program. These plugins are decoupled from the core framework, meaning they do not modify or restrict the core code, allowing users to continue compiling and developing the core framework independently.

    Note on Privacy: Plugin authorization requires collecting a unique hardware identifier (e.g., CPU serial number) for device binding. This information is used strictly for authorization verification and is not used for other purposes.

  5. Enable IDE autocompletion for Hikyuu

    master

    If your IDE is not providing proper code hints/autocompletion for Hikyuu, you can generate stubs using pybind11-stubgen.

    1. Install the tool:
      pip install pybind11-stubgen
    2. Generate stubs:
       ```bash
    pybind11-stubgen hikyuu -o .
    pip install pybind11-stubgen
    pybind11-stubgen hikyuu -o .
  6. Use the Hikyuu interactive tool

    master

    Hikyuu provides a built-in interactive tool designed for exploration within a Python Shell environment. It provides matplotlib-based plotting functions to easily visualize K-lines, indicators, and signals. To use these tools, import the interactive module:

    from hikyuu.interactive import *
  7. Configure score filtering for SE_MultiFactor2

    master

    For SE_MultiFactor2 instances, you can customize how cross-sectional scores are filtered using set_scores_filter. Filters can be combined using the bitwise OR operator (|).

    Example filter chain:

    • SCFilter_IgnoreNan(): Ignore NaN values.
    • SCFilter_Group(10, 0): Divide into 10 groups and take the 0th group.
    • SCFilter_Price(10.): Price must be $\ge 10$.
    • SCFilter_AmountLimit(0.2): Transaction amount must not be in the bottom 20% of the day.
    • SCFilter_TopN(10): Take the top 10.
  8. Use Hikyuu plugins

    master

    To use Hikyuu plugins with a self-compiled version, you must install the independent plugin package:

    pip install hikyuu-plugin

    Warning: Ensure the plugin version matches your Hikyuu version. It is highly recommended to use the release branch or specific tags for compilation to avoid version mismatch crashes.

    pip install hikyuu-plugin
  9. Configure Python environment for Hikyuu

    master

    Before installing, ensure your environment meets the following requirements:

    • Python Version: Requires Python >= 3.10. It is recommended to use mainstream versions like 3.11 or 3.12 to avoid dependency compatibility issues.
    • Distribution: Using a distribution like Anaconda is recommended as it includes common data science packages.
    • Git: Install git if you plan to use hub.

    Linux Compatibility Note (Conda Users)

    On Linux, Conda often uses its own libstdc++.so instead of the system default, which can cause errors such as ImportError: /lib/x86_64-linux-gnu/libstdc++.so.6: cannot allocate memory in static TLS block.

    To resolve this, ensure you are consistently using either the system or the Conda version of libstdc++.so. You can prioritize the Conda library path by setting the LD_LIBRARY_PATH environment variable before starting your program:

    export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH"
  10. Install Hikyuu via pip

    master

    You can install Hikyuu directly using pip. This method is supported on Windows (x86 CPU), Ubuntu 24.04 and above, and macOS (arm CPU).

    Note: Versions 2.6.8 and 2.6.9 may crash on older x86 machines that do not support the AVX instruction set. It is recommended to use version 2.7.0 or higher.

  11. Prepare environment for Hikyuu compilation

    master

    Before compiling Hikyuu, ensure your system meets the following requirements:

    C++ Compiler (C++20 support required)

    • Windows: Visual C++ 2022
    • Linux: g++ >= 13 or clang >= 15
    • macOS: Install Xcode and its command line tools

    Build Tools

    Source Code

    Clone the repository using git (do not download source zip files directly, as line endings may cause compilation issues on Windows):

    git clone https://github.com/fasiondog/hikyuu.git

    OS-Specific Dependencies

    • Linux (Ubuntu): Install libsqlite3-dev via:
      sudo apt-get install -y libsqlite3-dev
  12. Register and control OrderBrokers in TradeManager

    master

    You can register multiple OrderBroker instances with a TradeManager using TradeManager.regBroker. These brokers can perform additional actions (like sending emails) when buy/sell instructions are issued.

    Crucial for Live Trading: To prevent brokers from executing orders on historical data during signal calculation/backtesting, set the TradeManager.broke_last_datetime attribute. The broker will only execute orders for timestamps strictly greater than this value.