Skip to content

Use Python to generate configuration

When trading multiple symbols in an instance, manually creating json configuration files becomes quite tedious and error-prone. Python is a very convenient tool to get the job done in this situation. Below is an example:

import os, sys, copy, json, glob
import pandas as pd
import numpy as np
from apifiny.smart.security_master.SymbolInfo import create_symbol_info_cfg
from apifiny.smart.utils.CommonUtils import *

"""
This base demo is used to generate the config file for CCEventMakerStrategy strategy
"""


def gen_config_files():
    symbols = ["BTCUSDT.H"]
    instname = "huobi_event_maker"

    curpath = os.path.abspath(os.path.dirname(__file__))
    _x = lambda f: [f(x) for x in symbols]
    ports = {x : parse_symbol(x) for x in symbols}    
    cfg = {        
        "includes": [
            f"{curpath}/../config/common/cc_settings.json"
        ],

        "instance": {
            "log_path": f"{curpath}/../logs/{instname}", 
            "name": instname, 
            "trade_binance": False, 
            "trade_huobi": True
        },

        "risk_formulas": [
            *_x(lambda x: ["Port_Risk", ["RiskFormula", {"components": [[ports[x], 1.0]]}]])
        ],

        "accounts": [
            [10001, ["Account", {"risk_formulas": ["Port_Risk"],"id": 10001}]]
        ],

        "symbols": create_symbols_cfg(symbols),

        "symbol_info": create_symbol_info_cfg(symbols),

        "players": create_new_players_cfg(symbols),

        "samplers": [["ts_basis", ["TimeSampler", {"halflife": 1, "msecs": 100}]]],   

        "pricing_models": [
            *_x(lambda x: [x+"_midpx", ["MidPx", {"port": ports[x]}]]),
            *_x(lambda x: [x+"_Vwap", ["Vwap", {"port": ports[x], 'sampler': "ts_basis"}]])
        ],

        "variables": [
            ["Zero", ["Const", {"value": 0.0}]]
        ],

        "models": [
            ["Zero_m", ["LinearModel", {"variable": "Zero"}]]
        ],

        "strategies": [
            [sym, ["CCEventMakerStrategy", {
                    "symbol": ports[sym][0],
                    "trade_market": ports[sym][1],
                    "account": 10001,
                    "dep_pm": f"{sym}_midpx",
                    "model": "Zero_m",
                    "VWap":"BTCUSDT.H_Vwap",

                    "max_spread_bps":5,
                    "max_quote_frombbo_bps":0.001,   
                    "update_quote_bps":2,      
                    "quote_bps":3,
                    "allowed_bps":5,   

                    "order_notional": 50,
                    "max_notional": 250,
                    "max_risk": 250,

                    "cooloff": 100,
                    "start_time": "00:30:00",
                    "end_time": "23:59:59",
                    "use_separate_logs": True,
                    "close_mode": "none"
                }]]
                for sym in symbols]
    }

    out_dir = f"{curpath}/../config/gen/{instname}_live.json"
    write_concise_json(out_dir, cfg)
    print("file generated:", out_dir)


if __name__ == "__main__":
    gen_config_files()

Besides generating configuration, python can also play important role in trading strategy research. We will discuss this more in the research section.