This issue recommends hikyuu, an open source quantitative trading research framework based on C++Python.
Hikyuu is a high performance open source quantitative trading research framework based on C++/Python for strategy analysis and backtesting (currently used in the domestic securities market). Its core idea is based on the current mature systematic trading method, the entire systematic trading is abstracted into seven components: market environment judgment strategy, system effective condition, signal indicator, stop loss/stop profit strategy, fund management strategy, profit target strategy, slip spread algorithm. You can build the strategic asset library of these components respectively. In practical research, they are combined freely to observe the effectiveness, stability and effect of a single type of strategy.
Advantages of Hikyuu
1. The combination is flexible and the strategic asset library is constructed by classification : Hikyuu has carried out a good abstraction of the systematic trading method, including nine strategic components: Market environment judgment strategy, system effective condition, signal indicator, stop loss/stop profit strategy, fund management strategy, profit target strategy, sliding spread algorithm, trading object selection strategy, fund allocation strategy. On this basis, you can build your own policy library, and perform flexible combination and testing. When conducting strategy exploration, we can pay more attention to the performance and impact of a certain aspect of the strategy. Its main functional modules are as follows:
2. Performance guarantee, create your own exclusive application : The current project contains three main components: core library based on C++, Python library wrapped in C++ (hikyuu), interactive tools based on Python.
- C++ core library, provides the overall policy framework, while ensuring performance, has considered the support for multi-threading and multi-core processing, in the future to pursue higher computing speed to provide convenience. C++ core library, you can separate use, build their own client tools.
- Python library (hikyuu), provides the packaging of C++ library, while integrating talib library (such as TA_SMA, corresponding to talib.sma), can be converted with numpy, pandas data structure. It is convenient to use other mature python data analysis tools.
- hikyuu.interactive Interactive exploration tool, which provides basic mapping functions of K-lines, indicators, system signals, etc., for exploration and backtesting of quantitative strategies.
3. multi-paradigm support, more convenient and free to explore : support both object-oriented and command line programming paradigm. Among them, the command line is more simple, convenient, and free for policy exploration.
4. data storage is extensible : Currently supports local HDF5 format and MySQL storage. HDF5 is used by default, and the data file size is small, the speed is faster, and the backup is convenient.
Example
# Create a mock trading account for backtesting, initial capital of 300,000
my_tm = crtTM(init_cash = 300000)
# Create a signal indicator (the 5-day EMA is the fast line, the 5-day EMA itself is the 10-day EMA slowest line, buy when the fast line crosses the slow line upward, sell vice versa)
my_sg = SG_Flex(OP(EMA(n=5)), slow_n=10)
# Fixed buying 1000 shares at a time
my_mm = MM_FixedCount(1000)
# Create trading system and run
sys = SYS_Simple(tm = my_tm, sg = my_sg, mm = my_mm)
sys.run(sm['sz000001'], Query(-150))
Complete reference:
https://nbviewer.org/github/fasiondog/hikyuu/blob/master/hikyuu/examples/notebook/000-Index.ipynb?flush_cache=True
Installation
- Preparation before installation
Supported operating system: 64-bit Windows7 and above version, Linux please use the source code to compile and install.
Python environment: > = Python3.5
< p data – track = “34” > note: it is recommended to install package integrates common data science Python release: Anaconda (https://www.anaconda.com/). Domestic users advice from tsinghua mirror site < a class = “PGC – link” href = “https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/” target = “_blank” rel=”noopener noreferrer” data-content=”mp” data-source=”outerLink”>anaconda | The mirror station help| The mirror station | Tsinghua Open Source Mirror Download.
- pip installation
Installation:
pip install hikyuu
Version upgrade:
pip install hikyuu -U
Note:
1. The talib package cannot be installed in Windows by pip
Download the corresponding wheel package from https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib/ and install it manually.
2. Linux needs to install dependent libraries: libhdf5, libmysqlclient, libsqlite3
Getting started
- Download data to start
Note: The data download tool needs to use the unrar command. In Linux, install unrar.
pip After Hikyuu is installed, run the hikyuutdx command in the command line terminal to start the data download tool, and download the data as prompted:
Note: If the HikyuuTDX command cannot be executed in the command line terminal, go to the Scripts subdirectory in the python installation directory and select the execution file (hikyuutdx.exe).
If you do not want to download the file using the GUI, run the importdata command on the CLI, as shown in the following figure:
Note: Because the importdata command uses the configuration file generated by HikyuuTDX, you need to run HikyuuTDX at least once before you execute importdata for the first time.
- Learn through code examples
You can access the latest code examples from the following URL, which will help you gradually understand and master the use of Hikyuu and the related concepts of systematic trading. During the daily use of Hikyuu, you can find detailed descriptions of the corresponding built-in indicators, policies, and functions in this help to obtain more information.
https://nbviewer.jupyter.org/github/fasiondog/hikyuu/blob/master/hikyuu/examples/notebook/000-Index.ipyn b?flush_cache=True
The above sample code is edited and run using a Jupyter notebook, you can download the sample code from the top right corner of each sample URL and run it directly in the Jupyter notebook open.
or find all the examples from “examples/notebook” in the installation directory.
If you are not used to using Jupyter notebook, you can also use your favorite Python client tools (such as shell, Spyder, PyCharm, idle, etc.) and learn to run according to the sample code.
- Run from Python Shell
You can run Hikyuu using any Python client tool. As shown in the following figure, run hikyuu in a Python shell environment in cmd. When copying code from the above example, should be careful to remove code that starts with a “%”, such as “%time” , these are ipython magic code that only works in ipython environments, not normal shells.
To run the Hikyuu interactive tool, you need to introduce it first, as follows:
# To use hikyuu in an interactive environment, the first need to introduce hikyuu interactive tools
from hikyuu.interactive.interactive import *
Note: Hikyuu itself is a normal Python package, while
Hikyuu. Interactive. Interactive for hikyuu package contains an interactive tool. If you want to develop your own other programs based on the hikyuu package instead of using it as an interactive program, you can refer to it
Hikyuu/interactive/interactive. Py hikyuu normal initialization.
-
- Edit and run
with Jupyter notebook
To start Jupyter notebook, just enter the desired working directory in cmd and type the Jupyter notebook command, as shown below:
The above command will start the local web service. At this time, you can open a browser (Chrome or Firefox is recommended) and enter the following address:
http://127.0.0.1:8888/tree according to the menu commands in the interface can be like normal code editor editing and running the code.
You can read more on your own.