introduce
KTrader Python Basic is the basic version of KTrader Python Trading API. (Only includes strategy backtesting function, for quantitative investment and research use) Users can write quantitative strategies according to the KTrader Python API development manual. Currently targeting the Chinese futures market.
Notice: For the latest documents, please log in to the KTrader official website of Yiliang Technology.
configuration item
Download KTrader Python Example Strategy
git clone git@gitee.com:ktrader-user/ktrader-python-basic.git
run
git submodule update --init --recursive
update submodulean examination
strategy_configs
path:global_config.json
is the global policy management configuration,turtle.json
is the turtle sample policy parameter configurationglobal_config.json
{ "python": { "identifier": 101, "python_path": "python/python_turtle_strategy.py", "python_strategy": "PythonTurtleStrategy", "config": "strategy_configs/turtle.json" } }
turtle.json
{ "context":"turtle", "params": { "symbol":"sc2204", "start_time": "21:01:00", "end_time" : "15:00:00", "unit":1, "HHigh": 550.6, "LLow": 504.2, "ATR":15.6447 } }
open in browserthird_party/ktrader-python-backtest/docs/index.html
View all API documentation
Configure the Python environment
KTrader Python uses Python 3.8 version, you can use pip or conda to configure the python environment
Notice: Don’t mix pip and conda
Configure Python with pip
Run the following commands to install Python3.8 and pip
> sudo apt install -y python3.8 python3.8-dev python3-pip
Install pandas and numpy using pip
> pip3 install pandas numpy
Configuring Python with conda
install conda
> wget https://repo.anaconda.com/miniconda/Miniconda3-py38_4.11.0-Linux-x86_64.sh -O ~/miniconda.sh
> bash miniconda.sh -b -p ~/miniconda
Whether the popup is runningconda init
chooseyes
then open a new terminal to enter the conda environment and install pandas and numpy
> conda create -n py38 python=3.8
> conda activate py38
> conda install pandas numpy
KTrader backtesting framework configuration and use
The operating environment of KTrader is Ubuntu 20.04LTS, and it does not support running directly on Windows or MacOS. It is recommended to run backtests under Docker. Running on Windows or MacOS requires Linux to be installed in a virtual machine.
Windows 10 install Ubuntu 20.04 virtual machine
MacOS install Ubuntu 20.04 virtual machine
Run the backtest locally
Use klib to download market data to the local, for example
/prodticks
Table of contents:> python download_data.py --user=$username --passwd=$password --start_date=20220101 \ --end_date=20220228 --data_type=raw_data --dir_path=/prodticks
In the Ubuntu 20.04 environment, run the backtest on the path where the strategy development is located:
> bin/run_backtest --tick_path=/prodticks/raw_data --enabled_strategy=strategy_configs \ --start_date=20220106 --end_date=20220228 --log_root=log --test_name=mytest
Backtest parameters
start_date
andend_date
are the backtest start and end dates,tick_path
It is the path where the market data is located, and it needs to include the data from the beginning to the end of each day.enabled_strategy
forglobal_config.json
directory,log_root
is the path where the running log is located.exist
ktrader-python-basic
under the pathktrader_license.json
File configuration KTrader account informationktrader_license.json
{ "license":"BASIC", "email": "user-email@test.com", "password": "user-password" }
license
Fill in the KTrader version used by the user, such asBASIC
,SOLO
,PRO
Wait.email
andpassword
Fill in the user’shttps://ktrader-official.com
The registered email and password are sufficient.The backtest results are stored in a format such as
log/run_backtest/mytest-20220209-20220209-123456
directory, where the last level format is$test_name-$start_date-$end_date-$process_id
,E.glog/run_backtest/mytest-20220106-20220228-738719/ ├── backtest ├── logs ├── orders ├── positions ├── summary └── trades
The logs directory is the system log, the orders directory is the order and transaction log of each trading day, the summary directory is the daily profit and loss summary of each strategy, and backtest/account_settlement-20220106-20220228.csv is the daily account comprehensive profit and loss summary.
#KTraderPython #KTrader #Python #quantitative #strategy #development #platform #futures #CTA