quant1x-trader 0.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- quant1x_trader-0.0.0/LICENSE +21 -0
- quant1x_trader-0.0.0/PKG-INFO +43 -0
- quant1x_trader-0.0.0/README.md +4 -0
- quant1x_trader-0.0.0/pyproject.toml +61 -0
- quant1x_trader-0.0.0/quant1x_trader.egg-info/PKG-INFO +43 -0
- quant1x_trader-0.0.0/quant1x_trader.egg-info/SOURCES.txt +17 -0
- quant1x_trader-0.0.0/quant1x_trader.egg-info/dependency_links.txt +1 -0
- quant1x_trader-0.0.0/quant1x_trader.egg-info/entry_points.txt +2 -0
- quant1x_trader-0.0.0/quant1x_trader.egg-info/requires.txt +16 -0
- quant1x_trader-0.0.0/quant1x_trader.egg-info/top_level.txt +1 -0
- quant1x_trader-0.0.0/setup.cfg +4 -0
- quant1x_trader-0.0.0/trader1x/__init__.py +5 -0
- quant1x_trader-0.0.0/trader1x/config.py +251 -0
- quant1x_trader-0.0.0/trader1x/context.py +260 -0
- quant1x_trader-0.0.0/trader1x/log4py.py +36 -0
- quant1x_trader-0.0.0/trader1x/proxy.py +583 -0
- quant1x_trader-0.0.0/trader1x/thinktrader.py +523 -0
- quant1x_trader-0.0.0/trader1x/utils.py +44 -0
- quant1x_trader-0.0.0/trader1x/win32serviceutil.py +1071 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 王布衣
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: quant1x-trader
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: Quant1X programmatic automated trading
|
|
5
|
+
Author-email: Wang Feng <wangfengxy@sina.cn>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitee.com/quant1x/trader
|
|
8
|
+
Keywords: quant1x,auto,trader
|
|
9
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: future~=1.0.0
|
|
23
|
+
Requires-Dist: numpy~=2.3.0
|
|
24
|
+
Requires-Dist: pandas~=2.3.0
|
|
25
|
+
Requires-Dist: PyYAML~=6.0.1
|
|
26
|
+
Requires-Dist: pywin32==311
|
|
27
|
+
Requires-Dist: uvicorn~=0.29.0
|
|
28
|
+
Requires-Dist: fastapi~=0.110.2
|
|
29
|
+
Requires-Dist: path~=16.14.0
|
|
30
|
+
Requires-Dist: requests~=2.32.0
|
|
31
|
+
Requires-Dist: loguru~=0.7.2
|
|
32
|
+
Requires-Dist: quant1x-xtquant~=2025.5.26
|
|
33
|
+
Requires-Dist: quant1x-base~=0.6.29
|
|
34
|
+
Requires-Dist: aiohttp~=3.12.15
|
|
35
|
+
Requires-Dist: sanic~=23.12.1
|
|
36
|
+
Requires-Dist: python-multipart~=0.0.19
|
|
37
|
+
Requires-Dist: APScheduler~=3.10.4
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# trader
|
|
41
|
+
|
|
42
|
+
#### 介绍
|
|
43
|
+
python实现的自动化交易员
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "quant1x-trader"
|
|
7
|
+
version = "0.0.0"
|
|
8
|
+
description = "Quant1X programmatic automated trading"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
authors = [ { name = "Wang Feng", email = "wangfengxy@sina.cn" } ]
|
|
12
|
+
license = { text = "MIT" }
|
|
13
|
+
keywords = ["quant1x", "auto", "trader"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Natural Language :: English",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.8",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"future~=1.0.0",
|
|
28
|
+
"numpy~=2.3.0",
|
|
29
|
+
"pandas~=2.3.0",
|
|
30
|
+
"PyYAML~=6.0.1",
|
|
31
|
+
"pywin32==311",
|
|
32
|
+
"uvicorn~=0.29.0",
|
|
33
|
+
"fastapi~=0.110.2",
|
|
34
|
+
"path~=16.14.0",
|
|
35
|
+
"requests~=2.32.0",
|
|
36
|
+
"loguru~=0.7.2",
|
|
37
|
+
"quant1x-xtquant~=2025.5.26",
|
|
38
|
+
"quant1x-base~=0.6.29",
|
|
39
|
+
"aiohttp~=3.12.15",
|
|
40
|
+
"sanic~=23.12.1",
|
|
41
|
+
"python-multipart~=0.0.19",
|
|
42
|
+
"APScheduler~=3.10.4",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://gitee.com/quant1x/trader"
|
|
47
|
+
|
|
48
|
+
[project.scripts]
|
|
49
|
+
quant1x-qmt-proxy = "trader1x.proxy:main"
|
|
50
|
+
|
|
51
|
+
[tool.setuptools]
|
|
52
|
+
# find packages the same way as the original setup.py
|
|
53
|
+
[tool.setuptools.packages.find]
|
|
54
|
+
include = ["trader1x", "trader1x.*"]
|
|
55
|
+
|
|
56
|
+
[tool.setuptools.package-data]
|
|
57
|
+
"*" = ["*.dll", "*.pyd", "*.ini", "*.log4cxx"]
|
|
58
|
+
|
|
59
|
+
# Note: original project computed version dynamically via q1x.base.version.project_version()
|
|
60
|
+
# The static version above is a placeholder; update `version` or switch to a dynamic
|
|
61
|
+
# version scheme if you want the build to read the package's version automatically.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: quant1x-trader
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: Quant1X programmatic automated trading
|
|
5
|
+
Author-email: Wang Feng <wangfengxy@sina.cn>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitee.com/quant1x/trader
|
|
8
|
+
Keywords: quant1x,auto,trader
|
|
9
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: future~=1.0.0
|
|
23
|
+
Requires-Dist: numpy~=2.3.0
|
|
24
|
+
Requires-Dist: pandas~=2.3.0
|
|
25
|
+
Requires-Dist: PyYAML~=6.0.1
|
|
26
|
+
Requires-Dist: pywin32==311
|
|
27
|
+
Requires-Dist: uvicorn~=0.29.0
|
|
28
|
+
Requires-Dist: fastapi~=0.110.2
|
|
29
|
+
Requires-Dist: path~=16.14.0
|
|
30
|
+
Requires-Dist: requests~=2.32.0
|
|
31
|
+
Requires-Dist: loguru~=0.7.2
|
|
32
|
+
Requires-Dist: quant1x-xtquant~=2025.5.26
|
|
33
|
+
Requires-Dist: quant1x-base~=0.6.29
|
|
34
|
+
Requires-Dist: aiohttp~=3.12.15
|
|
35
|
+
Requires-Dist: sanic~=23.12.1
|
|
36
|
+
Requires-Dist: python-multipart~=0.0.19
|
|
37
|
+
Requires-Dist: APScheduler~=3.10.4
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# trader
|
|
41
|
+
|
|
42
|
+
#### 介绍
|
|
43
|
+
python实现的自动化交易员
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
quant1x_trader.egg-info/PKG-INFO
|
|
5
|
+
quant1x_trader.egg-info/SOURCES.txt
|
|
6
|
+
quant1x_trader.egg-info/dependency_links.txt
|
|
7
|
+
quant1x_trader.egg-info/entry_points.txt
|
|
8
|
+
quant1x_trader.egg-info/requires.txt
|
|
9
|
+
quant1x_trader.egg-info/top_level.txt
|
|
10
|
+
trader1x/__init__.py
|
|
11
|
+
trader1x/config.py
|
|
12
|
+
trader1x/context.py
|
|
13
|
+
trader1x/log4py.py
|
|
14
|
+
trader1x/proxy.py
|
|
15
|
+
trader1x/thinktrader.py
|
|
16
|
+
trader1x/utils.py
|
|
17
|
+
trader1x/win32serviceutil.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
future~=1.0.0
|
|
2
|
+
numpy~=2.3.0
|
|
3
|
+
pandas~=2.3.0
|
|
4
|
+
PyYAML~=6.0.1
|
|
5
|
+
pywin32==311
|
|
6
|
+
uvicorn~=0.29.0
|
|
7
|
+
fastapi~=0.110.2
|
|
8
|
+
path~=16.14.0
|
|
9
|
+
requests~=2.32.0
|
|
10
|
+
loguru~=0.7.2
|
|
11
|
+
quant1x-xtquant~=2025.5.26
|
|
12
|
+
quant1x-base~=0.6.29
|
|
13
|
+
aiohttp~=3.12.15
|
|
14
|
+
sanic~=23.12.1
|
|
15
|
+
python-multipart~=0.0.19
|
|
16
|
+
APScheduler~=3.10.4
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
trader1x
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import warnings
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
import q1x.base
|
|
8
|
+
import yaml
|
|
9
|
+
from q1x.base import TradingSession
|
|
10
|
+
|
|
11
|
+
from trader1x import utils
|
|
12
|
+
from trader1x.log4py import logger
|
|
13
|
+
|
|
14
|
+
warnings.filterwarnings('ignore')
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def list_default_factory():
|
|
18
|
+
return []
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class TradeRule:
|
|
23
|
+
"""
|
|
24
|
+
交易规则
|
|
25
|
+
"""
|
|
26
|
+
id: int = -1 # 策略ID, -1无效
|
|
27
|
+
auto: bool = False # 是否自动执行
|
|
28
|
+
name: str = None # 策略名称
|
|
29
|
+
flag: str = '' # 订单标识,分早盘,尾盘和盘中
|
|
30
|
+
time: TradingSession = field(default_factory=lambda: TradingSession("09:30:00~11:30:00,13:00:00~14:56:30"))
|
|
31
|
+
weight: int = 0 # 策略权重, 默认0, 由系统自动分配
|
|
32
|
+
total: int = 3 # 订单总数, 默认是3
|
|
33
|
+
fee_max: float = 20000.00 # 可投入资金-最大
|
|
34
|
+
fee_min: float = 10000.00 # 可投入资金-最小
|
|
35
|
+
sectors: list[str] = None # 板块, 策略适用的板块列表, 默认板块为空, 即全部个股
|
|
36
|
+
ignore_margin_trading: bool = True # 剔除两融标的, 默认是剔除
|
|
37
|
+
|
|
38
|
+
def __init__(self, params: dict = None):
|
|
39
|
+
"""
|
|
40
|
+
初始化
|
|
41
|
+
"""
|
|
42
|
+
# 先让 dataclass 初始化默认值
|
|
43
|
+
# 手动初始化所有字段
|
|
44
|
+
self.time = TradingSession("09:30:00~11:30:00,13:00:00~14:56:30")
|
|
45
|
+
if params is None:
|
|
46
|
+
return
|
|
47
|
+
for key, value in params.items():
|
|
48
|
+
if not hasattr(self, key):
|
|
49
|
+
continue
|
|
50
|
+
tmp_value = getattr(self, key)
|
|
51
|
+
if isinstance(tmp_value, TradingSession):
|
|
52
|
+
new_value = TradingSession(value)
|
|
53
|
+
setattr(self, key, new_value)
|
|
54
|
+
else:
|
|
55
|
+
setattr(self, key, value)
|
|
56
|
+
|
|
57
|
+
def enable(self) -> bool:
|
|
58
|
+
"""
|
|
59
|
+
是否有效
|
|
60
|
+
:return:
|
|
61
|
+
"""
|
|
62
|
+
return self.auto and self.id >= 0
|
|
63
|
+
|
|
64
|
+
def buy_enable(self) -> bool:
|
|
65
|
+
"""
|
|
66
|
+
买入有效
|
|
67
|
+
:return:
|
|
68
|
+
"""
|
|
69
|
+
return self.enable() and self.total > 0
|
|
70
|
+
|
|
71
|
+
def sell_enable(self) -> bool:
|
|
72
|
+
"""
|
|
73
|
+
卖出有效
|
|
74
|
+
:return:
|
|
75
|
+
"""
|
|
76
|
+
return self.enable()
|
|
77
|
+
|
|
78
|
+
def is_cookie_cutter_for_sell(self) -> bool:
|
|
79
|
+
"""
|
|
80
|
+
是否一刀切卖出
|
|
81
|
+
:return:
|
|
82
|
+
"""
|
|
83
|
+
return self.sell_enable() and self.total == 0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@dataclass
|
|
87
|
+
class TraderConfig:
|
|
88
|
+
"""
|
|
89
|
+
配置信息
|
|
90
|
+
"""
|
|
91
|
+
# 账号ID
|
|
92
|
+
account_id: str = ''
|
|
93
|
+
# 运行路径
|
|
94
|
+
order_path: str = ''
|
|
95
|
+
# 时间范围 - 早盘策略
|
|
96
|
+
head_time: TradingSession = field(default_factory=lambda: TradingSession("09:27:00~14:57:00"))
|
|
97
|
+
# 时间范围 - 尾盘策略
|
|
98
|
+
tail_time: TradingSession = field(default_factory=lambda: TradingSession("14:45:00~14:59:50"))
|
|
99
|
+
# 时间范围 - 盘中订单
|
|
100
|
+
tick_time: TradingSession = field(default_factory=lambda: TradingSession("09:39:00-14:57:00"))
|
|
101
|
+
# 时间范围 - 持仓卖出
|
|
102
|
+
ask_time: TradingSession = field(default_factory=lambda: TradingSession("09:50:00~14:59:30"))
|
|
103
|
+
# 时间范围 - 撤销订单
|
|
104
|
+
cancel_time: TradingSession = field(default_factory=lambda: TradingSession("09:15:00~09:19:59, 09:30:00~14:56:59"))
|
|
105
|
+
# 时间范围 - 盘后复盘
|
|
106
|
+
review_time: TradingSession = field(default_factory=lambda: TradingSession("00:00:00~08:30:00, 15:01:00~23:59:59"))
|
|
107
|
+
# 买入持仓率, 资金控制阀值
|
|
108
|
+
position_ratio: float = 0.5000
|
|
109
|
+
# 印花税 - 买入, 按照成交金额, 买入0.0%
|
|
110
|
+
stamp_duty_rate_for_buy: float = 0.0000
|
|
111
|
+
# 印花税 - 卖出, 按照成交金额, 卖出0.1%
|
|
112
|
+
stamp_duty_rate_for_sell: float = 0.0010
|
|
113
|
+
# 过户费 - 双向, 按照数量收取, 默认万分之六, 0.06%
|
|
114
|
+
transfer_rate: float = 0.0006
|
|
115
|
+
# 券商佣金 - 双向, 按成交金额计算, 默认万分之二点五, 0.025%
|
|
116
|
+
commission_rate: float = 0.00025
|
|
117
|
+
# 券商佣金最低, 默认5.00
|
|
118
|
+
commission_min: float = 5.00
|
|
119
|
+
# 保留现金
|
|
120
|
+
keep_cash: float = 10000.00
|
|
121
|
+
# tick订单最大金额, 默认20000.00
|
|
122
|
+
tick_order_max_amount: float = 20000.00
|
|
123
|
+
# tick订单最小金额, 默认10000.00
|
|
124
|
+
tick_order_min_amount: float = 10000.00
|
|
125
|
+
# 买入最大金额
|
|
126
|
+
buy_amount_max: float = 250000.00
|
|
127
|
+
# 买入最小金额
|
|
128
|
+
buy_amount_min: float = 1000.00
|
|
129
|
+
# 每策略最多可买股票数量, 这里默认3
|
|
130
|
+
max_stock_quantity_for_strategy: int = 3
|
|
131
|
+
# 自动卖出, 默认为True
|
|
132
|
+
sell_order_auto: bool = False
|
|
133
|
+
# 自动交易head订单, 默认为True
|
|
134
|
+
head_order_auto: bool = False
|
|
135
|
+
# 自动交易tick订单, 默认为True
|
|
136
|
+
tick_order_auto: bool = False
|
|
137
|
+
# 自动交易tail订单, 默认为True
|
|
138
|
+
tail_order_auto: bool = False
|
|
139
|
+
|
|
140
|
+
# 启动了mimiQMT机器的代理服务的监听地址, 默认为回环地址127.0.0.1, 强烈不推荐使用0.0.0.0
|
|
141
|
+
proxy_address: str = q1x.base.lan_address()
|
|
142
|
+
# 代理服务的监听端口, 默认18168
|
|
143
|
+
proxy_port: int = 18168
|
|
144
|
+
# 代理服务默认工作线程数, 默认为cpu核数的二分之一
|
|
145
|
+
proxy_workers: int = q1x.base.max_procs()
|
|
146
|
+
# qmt proxy地址
|
|
147
|
+
proxy_url: str = ""
|
|
148
|
+
# 策略集合
|
|
149
|
+
strategies: list[TradeRule] = list_default_factory
|
|
150
|
+
# 可撤单的交易时段
|
|
151
|
+
cancel: TradingSession = field(
|
|
152
|
+
default_factory=lambda: TradingSession("09:15:00~09:19:59,09:25:00~11:29:59,13:00:00~14:59:59"))
|
|
153
|
+
|
|
154
|
+
def __fix_instance(self):
|
|
155
|
+
"""
|
|
156
|
+
加载后修复
|
|
157
|
+
:return:
|
|
158
|
+
"""
|
|
159
|
+
if isinstance(self.ask_time, str):
|
|
160
|
+
ts = TradingSession(self.ask_time)
|
|
161
|
+
if ts.is_valid():
|
|
162
|
+
self.ask_time = ts
|
|
163
|
+
if isinstance(self.cancel_time, str):
|
|
164
|
+
ts = TradingSession(self.cancel_time)
|
|
165
|
+
if ts.is_valid():
|
|
166
|
+
self.cancel_time = ts
|
|
167
|
+
if isinstance(self.tick_time, str):
|
|
168
|
+
ts = TradingSession(self.tick_time)
|
|
169
|
+
if ts.is_valid():
|
|
170
|
+
self.tick_time = ts
|
|
171
|
+
|
|
172
|
+
def __post_init__(self):
|
|
173
|
+
"""
|
|
174
|
+
__init__()后调用, 调整类型
|
|
175
|
+
:return:
|
|
176
|
+
"""
|
|
177
|
+
self.__fix_instance()
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# 全局变量 - 配置
|
|
181
|
+
__global_config = TraderConfig()
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def load(config_filename: str = '') -> TraderConfig:
|
|
185
|
+
"""
|
|
186
|
+
加载配置文件
|
|
187
|
+
:return:
|
|
188
|
+
"""
|
|
189
|
+
global __global_config
|
|
190
|
+
config = TraderConfig()
|
|
191
|
+
config_filename = config_filename.strip()
|
|
192
|
+
if len(config_filename) == 0:
|
|
193
|
+
config_filename = utils.get_quant1x_config_filename()
|
|
194
|
+
config_filename = os.path.expanduser(config_filename)
|
|
195
|
+
logger.info(config_filename)
|
|
196
|
+
if not os.path.isfile(config_filename):
|
|
197
|
+
logger.error('QMT config {}: 不存在', config_filename)
|
|
198
|
+
sys.exit(utils.errno_config_not_exist)
|
|
199
|
+
try:
|
|
200
|
+
with open(config_filename, 'r', encoding='utf-8') as f:
|
|
201
|
+
result = yaml.load(f, Loader=yaml.FullLoader)
|
|
202
|
+
key_trader = "trader"
|
|
203
|
+
if isinstance(result, dict) and key_trader in result:
|
|
204
|
+
trader = result[key_trader]
|
|
205
|
+
for key, value in trader.items():
|
|
206
|
+
if not hasattr(config, key):
|
|
207
|
+
continue
|
|
208
|
+
tmp_value = getattr(config, key)
|
|
209
|
+
if isinstance(tmp_value, TradingSession):
|
|
210
|
+
new_value = TradingSession(value)
|
|
211
|
+
setattr(config, key, new_value)
|
|
212
|
+
elif tmp_value is list_default_factory:
|
|
213
|
+
if key == 'strategies':
|
|
214
|
+
tmp_list = []
|
|
215
|
+
if isinstance(value, list):
|
|
216
|
+
for d in value:
|
|
217
|
+
rule = TradeRule(d)
|
|
218
|
+
tmp_list.append(rule)
|
|
219
|
+
setattr(config, key, tmp_list)
|
|
220
|
+
elif key == 'account_id':
|
|
221
|
+
setattr(config, 'account_id', str(value))
|
|
222
|
+
elif key == 'top_n':
|
|
223
|
+
setattr(config, 'max_stock_quantity_for_strategy', value)
|
|
224
|
+
else:
|
|
225
|
+
setattr(config, key, value)
|
|
226
|
+
except Exception as e:
|
|
227
|
+
logger.error(f"发生了一个错误:{config_filename}\n错误信息:{e}")
|
|
228
|
+
logger.warning('系统将使用默认配置')
|
|
229
|
+
config = TraderConfig()
|
|
230
|
+
# finally:
|
|
231
|
+
# logger.warning('系统将使用默认配置')
|
|
232
|
+
# 检查重点配置
|
|
233
|
+
if config.account_id == '':
|
|
234
|
+
logger.error('配置缺少账户id')
|
|
235
|
+
sys.exit(utils.errno_not_found_account_id)
|
|
236
|
+
if config.order_path == '':
|
|
237
|
+
logger.error('配置缺少订单路径')
|
|
238
|
+
sys.exit(utils.errno_not_found_order_path)
|
|
239
|
+
__global_config = config
|
|
240
|
+
return config
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def get() -> TraderConfig:
|
|
244
|
+
global __global_config
|
|
245
|
+
return __global_config
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
if __name__ == '__main__':
|
|
249
|
+
filename = '~/.q1x/quant1x.yaml'
|
|
250
|
+
config = load(filename)
|
|
251
|
+
print(config)
|