使用 Python 生成配置
当在一个实例中交易多个品种时,手动创建 json 配置文件变得非常繁琐且容易出错。 Python 是在这种情况下完成工作的非常方便的工具。示例
下面 _ 是 _ _ :
_ 一个基本演示用于生成 CCEventMakerStrategy 策略的配置文件
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()
除了生成配置,python 还可以在交易策略研究中发挥重要作用。我们将在研究部分对此进行更多讨论。