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

  1. Download KTrader Python Example Strategygit clone git@gitee.com:ktrader-user/ktrader-python-basic.git

  2. rungit submodule update --init --recursiveupdate submodule

  3. an examinationstrategy_configspath: global_config.jsonis the global policy management configuration,turtle.jsonis the turtle sample policy parameter configuration

    global_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.htmlView 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 initchooseyesthen 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/prodticksTable 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 parametersstart_dateandend_dateare the backtest start and end dates,tick_pathIt 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_strategyforglobal_config.jsondirectory,log_rootis the path where the running log is located.

  • existktrader-python-basicunder the pathktrader_license.jsonFile configuration KTrader account information

    ktrader_license.json

    {
        "license":"BASIC",
        "email": "user-email@test.com",
        "password": "user-password"
    }

    licenseFill in the KTrader version used by the user, such asBASIC, SOLO, PROWait.emailandpasswordFill in the user’shttps://ktrader-official.comThe registered email and password are sufficient.

  • The backtest results are stored in a format such aslog/run_backtest/mytest-20220209-20220209-123456directory, where the last level format is$test_name-$start_date-$end_date-$process_id,E.g

    log/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

Leave a Comment

Your email address will not be published. Required fields are marked *