wqbkit 0.2.2__tar.gz → 0.2.5__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.
- wqbkit-0.2.5/.github/workflows/publish.yml +35 -0
- wqbkit-0.2.5/LICENSE +21 -0
- wqbkit-0.2.5/PKG-INFO +267 -0
- wqbkit-0.2.5/README.md +212 -0
- wqbkit-0.2.5/__init__.py +57 -0
- wqbkit-0.2.5/app/__init__.py +0 -0
- wqbkit-0.2.5/app/config.py +54 -0
- wqbkit-0.2.5/app/core/__init__.py +5 -0
- wqbkit-0.2.5/app/core/alpha_base_core.py +241 -0
- wqbkit-0.2.5/app/core/alpha_db_core.py +210 -0
- wqbkit-0.2.5/app/core/decorators.py +58 -0
- wqbkit-0.2.5/app/core/logger.py +65 -0
- wqbkit-0.2.5/app/core/wqb_urls.py +44 -0
- wqbkit-0.2.5/app/database/__init__.py +2 -0
- wqbkit-0.2.5/app/database/alpha_db_manager.py +923 -0
- wqbkit-0.2.5/app/database/db_models.py +198 -0
- wqbkit-0.2.5/app/database/schemas.py +96 -0
- wqbkit-0.2.5/app/utils/alpha_utils.py +34 -0
- wqbkit-0.2.5/modules/__init__.py +0 -0
- wqbkit-0.2.5/modules/competitions/Osmosis/osmosis_allocator_v3.py +607 -0
- wqbkit-0.2.5/modules/competitions/Osmosis/osmosis_clear_v3.py +201 -0
- wqbkit-0.2.5/modules/competitions/Osmosis/osmosis_runner_v3.py +291 -0
- wqbkit-0.2.5/modules/competitions/Osmosis/osmosis_selector_v3.py +1560 -0
- wqbkit-0.2.5/modules/competitions/__init__.py +0 -0
- wqbkit-0.2.5/modules/correlation/__init__.py +1 -0
- wqbkit-0.2.5/modules/correlation/alpha_calc_corr.py +354 -0
- wqbkit-0.2.5/modules/message/__init__.py +1 -0
- wqbkit-0.2.5/modules/message/alpha_message_sender.py +61 -0
- wqbkit-0.2.5/modules/regular_alpha/__init__.py +2 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/__init__.py +2 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/alpha_generator.py +138 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/alpha_machine.py +349 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/config/constants.py +11 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/config/events.py +92 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/config/groups.py +207 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_machine/config/operators.py +83 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_simulator/__init__.py +1 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_simulator/alpha_simulator.py +715 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_simulator/neutralization.json +25 -0
- wqbkit-0.2.5/modules/regular_alpha/alpha_simulator/setting.json +5 -0
- wqbkit-0.2.5/modules/super_alpha/__init__.py +3 -0
- wqbkit-0.2.5/modules/super_alpha/alpha_dyeing.py +127 -0
- wqbkit-0.2.5/modules/super_alpha/super_alpha_creator.py +152 -0
- wqbkit-0.2.5/modules/super_alpha/super_alpha_simulator.py +216 -0
- {wqbkit-0.2.2 → wqbkit-0.2.5}/pyproject.toml +3 -9
- wqbkit-0.2.2/PKG-INFO +0 -89
- wqbkit-0.2.2/README.md +0 -54
- {wqbkit-0.2.2 → wqbkit-0.2.5}/.gitignore +0 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*'
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build-and-publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
permissions:
|
|
12
|
+
contents: read
|
|
13
|
+
id-token: write # for trusted publishing (optional, if configured)
|
|
14
|
+
steps:
|
|
15
|
+
- name: Checkout
|
|
16
|
+
uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Set up Python
|
|
19
|
+
uses: actions/setup-python@v5
|
|
20
|
+
with:
|
|
21
|
+
python-version: '3.12'
|
|
22
|
+
|
|
23
|
+
- name: Install build tools
|
|
24
|
+
run: |
|
|
25
|
+
python -m pip install --upgrade pip
|
|
26
|
+
pip install build twine
|
|
27
|
+
|
|
28
|
+
- name: Build package
|
|
29
|
+
run: python -m build
|
|
30
|
+
|
|
31
|
+
- name: Publish to PyPI
|
|
32
|
+
env:
|
|
33
|
+
TWINE_USERNAME: __token__
|
|
34
|
+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
|
|
35
|
+
run: twine upload dist/*
|
wqbkit-0.2.5/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Crotes
|
|
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.
|
wqbkit-0.2.5/PKG-INFO
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wqbkit
|
|
3
|
+
Version: 0.2.5
|
|
4
|
+
Summary: WorldQuant Brain Alpha Research Automation Toolkit
|
|
5
|
+
Project-URL: Homepage, https://github.com/Crotes/wqbkit
|
|
6
|
+
Project-URL: Issues, https://github.com/Crotes/wqbkit/issues
|
|
7
|
+
Author-email: Crotes <Crotes@yeah.net>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2025 Crotes
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: alpha,finance,quantitative,research,trading,worldquant
|
|
31
|
+
Classifier: Development Status :: 3 - Alpha
|
|
32
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Programming Language :: Python :: 3
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
39
|
+
Requires-Python: >=3.10
|
|
40
|
+
Requires-Dist: numpy
|
|
41
|
+
Requires-Dist: pandas
|
|
42
|
+
Requires-Dist: psycopg2-binary
|
|
43
|
+
Requires-Dist: python-dotenv
|
|
44
|
+
Requires-Dist: pytz
|
|
45
|
+
Requires-Dist: requests
|
|
46
|
+
Requires-Dist: scipy
|
|
47
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
48
|
+
Requires-Dist: tqdm
|
|
49
|
+
Requires-Dist: wqb
|
|
50
|
+
Provides-Extra: dev
|
|
51
|
+
Requires-Dist: jupyter; extra == 'dev'
|
|
52
|
+
Requires-Dist: matplotlib; extra == 'dev'
|
|
53
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
54
|
+
Description-Content-Type: text/markdown
|
|
55
|
+
|
|
56
|
+
# wqbkit
|
|
57
|
+
|
|
58
|
+
WorldQuant Brain (WQB) Alpha Research Automation Toolkit
|
|
59
|
+
|
|
60
|
+
**[English](#overview) | [中文](#概述)**
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Overview
|
|
65
|
+
|
|
66
|
+
`wqbkit` is a Python toolkit that automates alpha research workflows on the [WorldQuant Brain](https://www.worldquantbrain.com) (WQB) platform. It covers the entire alpha lifecycle — from simulation and scoring to correlation analysis and genetic expression evolution.
|
|
67
|
+
|
|
68
|
+
### Key Features
|
|
69
|
+
|
|
70
|
+
| Module | Description |
|
|
71
|
+
|--------|-------------|
|
|
72
|
+
| `AlphaDbCore` | WQB authentication, HTTP request wrapper with exponential backoff retry |
|
|
73
|
+
| `AlphaSimulator` | Multi-threaded batch simulation scheduler with queue-based result handling |
|
|
74
|
+
| `AlphaMachine` | Genetic iteration pipeline — pruning, deduplication, and next-generation expression factories |
|
|
75
|
+
| `AlphaCalcCorr` | Multi-metric correlation engine (self / ppac / prod / self_web) with greedy max-independent-set |
|
|
76
|
+
| `SuperAlphaSimulator` | Dedicated simulator for SUPER-type alphas |
|
|
77
|
+
| `AlphaDyeing` | SUPER alpha construction and combo template management |
|
|
78
|
+
| `sc_send` | Bark (iOS) push notification for long-running jobs |
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Installation
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pip install wqbkit
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Prerequisites
|
|
89
|
+
|
|
90
|
+
- Python >= 3.10
|
|
91
|
+
- PostgreSQL (optional, for local alpha metadata caching)
|
|
92
|
+
- `wqb` SDK — WorldQuant Brain official Python client
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pip install wqb
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Quick Start
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from wqbkit import AlphaDbCore, AlphaCalcCorr, AlphaGenerator
|
|
104
|
+
|
|
105
|
+
# Initialize core (auto-loads .env if present)
|
|
106
|
+
core = AlphaDbCore()
|
|
107
|
+
|
|
108
|
+
# Run correlation analysis
|
|
109
|
+
calcor = AlphaCalcCorr()
|
|
110
|
+
self_corr = calcor.calculate(alpha_ids, 'self')
|
|
111
|
+
|
|
112
|
+
# Generate next-generation expressions
|
|
113
|
+
generator = AlphaGenerator()
|
|
114
|
+
new_exprs = generator.second_order_factory(
|
|
115
|
+
expression="ts_mean(close, 20)",
|
|
116
|
+
region="USA",
|
|
117
|
+
atom=True
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## Configuration
|
|
124
|
+
|
|
125
|
+
Create a `.env` file in your working directory:
|
|
126
|
+
|
|
127
|
+
```env
|
|
128
|
+
# Database (optional)
|
|
129
|
+
DB_HOST=localhost
|
|
130
|
+
DB_PORT=5432
|
|
131
|
+
DB_NAME=WorldQuant
|
|
132
|
+
DB_USER=your_db_user
|
|
133
|
+
DB_PASSWORD=your_db_password
|
|
134
|
+
|
|
135
|
+
# WQB Credentials
|
|
136
|
+
WQB_USERNAME=your_wqb_username
|
|
137
|
+
WQB_PASSWORD=your_wqb_password
|
|
138
|
+
WQB_API_BASE_URL=https://www.worldquantbrain.com
|
|
139
|
+
|
|
140
|
+
# Logging & Retry
|
|
141
|
+
LOG_LEVEL=INFO
|
|
142
|
+
MAX_RETRIES=5
|
|
143
|
+
RETRY_DELAY_BASE=2
|
|
144
|
+
|
|
145
|
+
# Bark Push (optional)
|
|
146
|
+
BARK_KEY=your_bark_device_key
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`wqbkit` automatically loads `.env` on import via `python-dotenv`.
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## API Reference
|
|
154
|
+
|
|
155
|
+
### Core Classes
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
from wqbkit import (
|
|
159
|
+
AlphaBaseCore, # WQB session + HTTP + retry
|
|
160
|
+
AlphaDbCore, # AlphaBaseCore + DB + PnL cache
|
|
161
|
+
AlphaCalcCorr, # Correlation analysis engine
|
|
162
|
+
AlphaGenerator, # Expression factory (0/1/2/3-order)
|
|
163
|
+
AlphaSimulator, # Multi-threaded simulation scheduler
|
|
164
|
+
AlphaMachine, # Genetic iteration pipeline
|
|
165
|
+
AlphaDyeing, # SUPER alpha builder
|
|
166
|
+
SuperAlphaSimulator, # SUPER alpha simulator
|
|
167
|
+
sc_send, # Push notification helper
|
|
168
|
+
schemas, # Data models: FactorData, TaskData, SimulationData, FieldDate
|
|
169
|
+
)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Correlation Analysis
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from wqbkit import AlphaCalcCorr
|
|
176
|
+
|
|
177
|
+
calcor = AlphaCalcCorr()
|
|
178
|
+
|
|
179
|
+
# Compute correlations
|
|
180
|
+
corrs = calcor.calculate(alpha_ids, metric='self') # self-correlation
|
|
181
|
+
corrs = calcor.calculate(alpha_ids, metric='ppac') # ppac correlation
|
|
182
|
+
corrs = calcor.calculate(alpha_ids, metric='prod') # prod correlation
|
|
183
|
+
|
|
184
|
+
# Max independent alpha set (greedy approximation)
|
|
185
|
+
independent_alphas = calcor.max_independent_alphas(alpha_ids, threshold=0.7)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Alpha Simulation
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from wqbkit import AlphaSimulator
|
|
192
|
+
from wqbkit.schemas import FactorData
|
|
193
|
+
|
|
194
|
+
sim = AlphaSimulator()
|
|
195
|
+
|
|
196
|
+
# Single alpha simulation
|
|
197
|
+
result = sim.simulate(expression="rank(close)", region="USA", universe="TOP3000")
|
|
198
|
+
|
|
199
|
+
# Batch simulation from database queue
|
|
200
|
+
sim.run_batch_simulation(task_id=123)
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Database Models
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
from wqbkit import schemas
|
|
207
|
+
|
|
208
|
+
factor = schemas.FactorData(
|
|
209
|
+
expression="rank(close)",
|
|
210
|
+
region="USA",
|
|
211
|
+
universe="TOP3000",
|
|
212
|
+
neutralization="SUBINDUSTRY",
|
|
213
|
+
decay=4,
|
|
214
|
+
)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Project Structure
|
|
220
|
+
|
|
221
|
+
```
|
|
222
|
+
wqbkit/
|
|
223
|
+
├── app/
|
|
224
|
+
│ ├── core/ # AlphaBaseCore, AlphaDbCore, decorators, logger, URLs
|
|
225
|
+
│ ├── database/ # SQLAlchemy models, AlphaDBManager, schemas
|
|
226
|
+
│ └── utils/ # Token extraction helpers
|
|
227
|
+
└── modules/
|
|
228
|
+
├── regular_alpha/ # AlphaSimulator, AlphaMachine, AlphaGenerator
|
|
229
|
+
├── super_alpha/ # SuperAlphaSimulator, AlphaDyeing, SuperAlphaCreator
|
|
230
|
+
├── correlation/ # AlphaCalcCorr (self/ppac/prod/self_web)
|
|
231
|
+
├── message/ # Bark push notifications
|
|
232
|
+
└── competitions/ # Osmosis V1/V2/V3 toolkit
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
MIT
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## 概述
|
|
244
|
+
|
|
245
|
+
`wqbkit` 是一个用于自动化 [WorldQuant Brain](https://www.worldquantbrain.com) 平台 Alpha 研究的 Python 工具包,覆盖 Alpha 全生命周期:模拟、评分、去相关、遗传迭代进化。
|
|
246
|
+
|
|
247
|
+
### 核心功能
|
|
248
|
+
|
|
249
|
+
| 模块 | 说明 |
|
|
250
|
+
|------|------|
|
|
251
|
+
| `AlphaDbCore` | WQB 认证、HTTP 封装、指数退避重试 |
|
|
252
|
+
| `AlphaSimulator` | 多线程批量模拟调度器,队列式结果处理 |
|
|
253
|
+
| `AlphaMachine` | 遗传迭代管线:剪枝、去重、下一代表达式工厂 |
|
|
254
|
+
| `AlphaCalcCorr` | 多指标相关性引擎(self/ppac/prod/self_web),贪心最大独立集 |
|
|
255
|
+
| `SuperAlphaSimulator` | SUPER 类型 Alpha 专用模拟器 |
|
|
256
|
+
| `AlphaDyeing` | SUPER Alpha 构造与 combo 模板管理 |
|
|
257
|
+
| `sc_send` | Bark (iOS) 推送通知,用于长时任务提醒 |
|
|
258
|
+
|
|
259
|
+
### 快速开始
|
|
260
|
+
|
|
261
|
+
```python
|
|
262
|
+
from wqbkit import AlphaDbCore, AlphaCalcCorr, AlphaGenerator
|
|
263
|
+
|
|
264
|
+
core = AlphaDbCore()
|
|
265
|
+
calcor = AlphaCalcCorr()
|
|
266
|
+
new_exprs = AlphaGenerator().second_order_factory("ts_mean(close, 20)", region="USA")
|
|
267
|
+
```
|
wqbkit-0.2.5/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# wqbkit
|
|
2
|
+
|
|
3
|
+
WorldQuant Brain (WQB) Alpha Research Automation Toolkit
|
|
4
|
+
|
|
5
|
+
**[English](#overview) | [中文](#概述)**
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
`wqbkit` is a Python toolkit that automates alpha research workflows on the [WorldQuant Brain](https://www.worldquantbrain.com) (WQB) platform. It covers the entire alpha lifecycle — from simulation and scoring to correlation analysis and genetic expression evolution.
|
|
12
|
+
|
|
13
|
+
### Key Features
|
|
14
|
+
|
|
15
|
+
| Module | Description |
|
|
16
|
+
|--------|-------------|
|
|
17
|
+
| `AlphaDbCore` | WQB authentication, HTTP request wrapper with exponential backoff retry |
|
|
18
|
+
| `AlphaSimulator` | Multi-threaded batch simulation scheduler with queue-based result handling |
|
|
19
|
+
| `AlphaMachine` | Genetic iteration pipeline — pruning, deduplication, and next-generation expression factories |
|
|
20
|
+
| `AlphaCalcCorr` | Multi-metric correlation engine (self / ppac / prod / self_web) with greedy max-independent-set |
|
|
21
|
+
| `SuperAlphaSimulator` | Dedicated simulator for SUPER-type alphas |
|
|
22
|
+
| `AlphaDyeing` | SUPER alpha construction and combo template management |
|
|
23
|
+
| `sc_send` | Bark (iOS) push notification for long-running jobs |
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install wqbkit
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Prerequisites
|
|
34
|
+
|
|
35
|
+
- Python >= 3.10
|
|
36
|
+
- PostgreSQL (optional, for local alpha metadata caching)
|
|
37
|
+
- `wqb` SDK — WorldQuant Brain official Python client
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install wqb
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from wqbkit import AlphaDbCore, AlphaCalcCorr, AlphaGenerator
|
|
49
|
+
|
|
50
|
+
# Initialize core (auto-loads .env if present)
|
|
51
|
+
core = AlphaDbCore()
|
|
52
|
+
|
|
53
|
+
# Run correlation analysis
|
|
54
|
+
calcor = AlphaCalcCorr()
|
|
55
|
+
self_corr = calcor.calculate(alpha_ids, 'self')
|
|
56
|
+
|
|
57
|
+
# Generate next-generation expressions
|
|
58
|
+
generator = AlphaGenerator()
|
|
59
|
+
new_exprs = generator.second_order_factory(
|
|
60
|
+
expression="ts_mean(close, 20)",
|
|
61
|
+
region="USA",
|
|
62
|
+
atom=True
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Configuration
|
|
69
|
+
|
|
70
|
+
Create a `.env` file in your working directory:
|
|
71
|
+
|
|
72
|
+
```env
|
|
73
|
+
# Database (optional)
|
|
74
|
+
DB_HOST=localhost
|
|
75
|
+
DB_PORT=5432
|
|
76
|
+
DB_NAME=WorldQuant
|
|
77
|
+
DB_USER=your_db_user
|
|
78
|
+
DB_PASSWORD=your_db_password
|
|
79
|
+
|
|
80
|
+
# WQB Credentials
|
|
81
|
+
WQB_USERNAME=your_wqb_username
|
|
82
|
+
WQB_PASSWORD=your_wqb_password
|
|
83
|
+
WQB_API_BASE_URL=https://www.worldquantbrain.com
|
|
84
|
+
|
|
85
|
+
# Logging & Retry
|
|
86
|
+
LOG_LEVEL=INFO
|
|
87
|
+
MAX_RETRIES=5
|
|
88
|
+
RETRY_DELAY_BASE=2
|
|
89
|
+
|
|
90
|
+
# Bark Push (optional)
|
|
91
|
+
BARK_KEY=your_bark_device_key
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`wqbkit` automatically loads `.env` on import via `python-dotenv`.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## API Reference
|
|
99
|
+
|
|
100
|
+
### Core Classes
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from wqbkit import (
|
|
104
|
+
AlphaBaseCore, # WQB session + HTTP + retry
|
|
105
|
+
AlphaDbCore, # AlphaBaseCore + DB + PnL cache
|
|
106
|
+
AlphaCalcCorr, # Correlation analysis engine
|
|
107
|
+
AlphaGenerator, # Expression factory (0/1/2/3-order)
|
|
108
|
+
AlphaSimulator, # Multi-threaded simulation scheduler
|
|
109
|
+
AlphaMachine, # Genetic iteration pipeline
|
|
110
|
+
AlphaDyeing, # SUPER alpha builder
|
|
111
|
+
SuperAlphaSimulator, # SUPER alpha simulator
|
|
112
|
+
sc_send, # Push notification helper
|
|
113
|
+
schemas, # Data models: FactorData, TaskData, SimulationData, FieldDate
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Correlation Analysis
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from wqbkit import AlphaCalcCorr
|
|
121
|
+
|
|
122
|
+
calcor = AlphaCalcCorr()
|
|
123
|
+
|
|
124
|
+
# Compute correlations
|
|
125
|
+
corrs = calcor.calculate(alpha_ids, metric='self') # self-correlation
|
|
126
|
+
corrs = calcor.calculate(alpha_ids, metric='ppac') # ppac correlation
|
|
127
|
+
corrs = calcor.calculate(alpha_ids, metric='prod') # prod correlation
|
|
128
|
+
|
|
129
|
+
# Max independent alpha set (greedy approximation)
|
|
130
|
+
independent_alphas = calcor.max_independent_alphas(alpha_ids, threshold=0.7)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Alpha Simulation
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from wqbkit import AlphaSimulator
|
|
137
|
+
from wqbkit.schemas import FactorData
|
|
138
|
+
|
|
139
|
+
sim = AlphaSimulator()
|
|
140
|
+
|
|
141
|
+
# Single alpha simulation
|
|
142
|
+
result = sim.simulate(expression="rank(close)", region="USA", universe="TOP3000")
|
|
143
|
+
|
|
144
|
+
# Batch simulation from database queue
|
|
145
|
+
sim.run_batch_simulation(task_id=123)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Database Models
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from wqbkit import schemas
|
|
152
|
+
|
|
153
|
+
factor = schemas.FactorData(
|
|
154
|
+
expression="rank(close)",
|
|
155
|
+
region="USA",
|
|
156
|
+
universe="TOP3000",
|
|
157
|
+
neutralization="SUBINDUSTRY",
|
|
158
|
+
decay=4,
|
|
159
|
+
)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Project Structure
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
wqbkit/
|
|
168
|
+
├── app/
|
|
169
|
+
│ ├── core/ # AlphaBaseCore, AlphaDbCore, decorators, logger, URLs
|
|
170
|
+
│ ├── database/ # SQLAlchemy models, AlphaDBManager, schemas
|
|
171
|
+
│ └── utils/ # Token extraction helpers
|
|
172
|
+
└── modules/
|
|
173
|
+
├── regular_alpha/ # AlphaSimulator, AlphaMachine, AlphaGenerator
|
|
174
|
+
├── super_alpha/ # SuperAlphaSimulator, AlphaDyeing, SuperAlphaCreator
|
|
175
|
+
├── correlation/ # AlphaCalcCorr (self/ppac/prod/self_web)
|
|
176
|
+
├── message/ # Bark push notifications
|
|
177
|
+
└── competitions/ # Osmosis V1/V2/V3 toolkit
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
MIT
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## 概述
|
|
189
|
+
|
|
190
|
+
`wqbkit` 是一个用于自动化 [WorldQuant Brain](https://www.worldquantbrain.com) 平台 Alpha 研究的 Python 工具包,覆盖 Alpha 全生命周期:模拟、评分、去相关、遗传迭代进化。
|
|
191
|
+
|
|
192
|
+
### 核心功能
|
|
193
|
+
|
|
194
|
+
| 模块 | 说明 |
|
|
195
|
+
|------|------|
|
|
196
|
+
| `AlphaDbCore` | WQB 认证、HTTP 封装、指数退避重试 |
|
|
197
|
+
| `AlphaSimulator` | 多线程批量模拟调度器,队列式结果处理 |
|
|
198
|
+
| `AlphaMachine` | 遗传迭代管线:剪枝、去重、下一代表达式工厂 |
|
|
199
|
+
| `AlphaCalcCorr` | 多指标相关性引擎(self/ppac/prod/self_web),贪心最大独立集 |
|
|
200
|
+
| `SuperAlphaSimulator` | SUPER 类型 Alpha 专用模拟器 |
|
|
201
|
+
| `AlphaDyeing` | SUPER Alpha 构造与 combo 模板管理 |
|
|
202
|
+
| `sc_send` | Bark (iOS) 推送通知,用于长时任务提醒 |
|
|
203
|
+
|
|
204
|
+
### 快速开始
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
from wqbkit import AlphaDbCore, AlphaCalcCorr, AlphaGenerator
|
|
208
|
+
|
|
209
|
+
core = AlphaDbCore()
|
|
210
|
+
calcor = AlphaCalcCorr()
|
|
211
|
+
new_exprs = AlphaGenerator().second_order_factory("ts_mean(close, 20)", region="USA")
|
|
212
|
+
```
|
wqbkit-0.2.5/__init__.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""WorldQuant Brain Alpha Research Toolkit"""
|
|
2
|
+
|
|
3
|
+
from wqbkit.app.config import PROJECT_ROOT
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
|
|
6
|
+
# 显式加载项目根目录的 .env(适用于 editable install 模式)
|
|
7
|
+
# load_dotenv 默认 override=False,不会覆盖已存在的环境变量
|
|
8
|
+
_env_path = PROJECT_ROOT / ".env"
|
|
9
|
+
if _env_path.exists():
|
|
10
|
+
load_dotenv(_env_path)
|
|
11
|
+
else:
|
|
12
|
+
load_dotenv()
|
|
13
|
+
|
|
14
|
+
__version__ = "0.2.5"
|
|
15
|
+
|
|
16
|
+
# ---------- Public API ----------
|
|
17
|
+
# 核心基础设施
|
|
18
|
+
from wqbkit.app.core.alpha_base_core import AlphaBaseCore
|
|
19
|
+
from wqbkit.app.core.alpha_db_core import AlphaDbCore
|
|
20
|
+
|
|
21
|
+
# 数据模型
|
|
22
|
+
from wqbkit.app.database import schemas
|
|
23
|
+
|
|
24
|
+
# 业务模块
|
|
25
|
+
from wqbkit.modules.correlation.alpha_calc_corr import AlphaCalcCorr
|
|
26
|
+
from wqbkit.modules.regular_alpha.alpha_machine.alpha_generator import AlphaGenerator
|
|
27
|
+
from wqbkit.modules.regular_alpha.alpha_machine.alpha_machine import AlphaMachine
|
|
28
|
+
from wqbkit.modules.regular_alpha.alpha_simulator.alpha_simulator import AlphaSimulator
|
|
29
|
+
from wqbkit.modules.super_alpha.alpha_dyeing import AlphaDyeing
|
|
30
|
+
from wqbkit.modules.super_alpha.super_alpha_simulator import SuperAlphaSimulator
|
|
31
|
+
from wqbkit.modules.message.alpha_message_sender import sc_send
|
|
32
|
+
from wqbkit.modules.competitions.Osmosis.osmosis_selector_v3 import OsmosisAlphaSelectorV3
|
|
33
|
+
from wqbkit.modules.competitions.Osmosis.osmosis_allocator_v3 import OsmosisAllocatorV3
|
|
34
|
+
from wqbkit.modules.competitions.Osmosis.osmosis_clear_v3 import OsmosisClearV3
|
|
35
|
+
from wqbkit.modules.competitions.Osmosis.osmosis_runner_v3 import OsmosisRunnerV3
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"__version__",
|
|
39
|
+
# 核心
|
|
40
|
+
"AlphaBaseCore",
|
|
41
|
+
"AlphaDbCore",
|
|
42
|
+
# 数据模型
|
|
43
|
+
"schemas",
|
|
44
|
+
# 业务模块
|
|
45
|
+
"AlphaCalcCorr",
|
|
46
|
+
"AlphaGenerator",
|
|
47
|
+
"AlphaMachine",
|
|
48
|
+
"AlphaSimulator",
|
|
49
|
+
"AlphaDyeing",
|
|
50
|
+
"SuperAlphaSimulator",
|
|
51
|
+
"sc_send",
|
|
52
|
+
# Osmosis V3
|
|
53
|
+
"OsmosisAlphaSelectorV3",
|
|
54
|
+
"OsmosisAllocatorV3",
|
|
55
|
+
"OsmosisClearV3",
|
|
56
|
+
"OsmosisRunnerV3",
|
|
57
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
|
|
6
|
+
load_dotenv()
|
|
7
|
+
|
|
8
|
+
# 项目根目录(wqbkit/app/config.py -> app -> wqbkit -> root)
|
|
9
|
+
PROJECT_ROOT: Path = Path(__file__).resolve().parents[2]
|
|
10
|
+
DATA_DIR: Path = PROJECT_ROOT / "data"
|
|
11
|
+
LOGS_DIR: Path = PROJECT_ROOT / "logs"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Config:
|
|
15
|
+
"""应用配置,统一读取环境变量。"""
|
|
16
|
+
|
|
17
|
+
DB_HOST: str = os.getenv("DB_HOST", "localhost")
|
|
18
|
+
DB_PORT: str = os.getenv("DB_PORT", "5432")
|
|
19
|
+
DB_NAME: str = os.getenv("DB_NAME", "WorldQuant")
|
|
20
|
+
DB_USER: str = os.getenv("DB_USER", "postgres")
|
|
21
|
+
DB_PASSWORD: str = os.getenv("DB_PASSWORD", "")
|
|
22
|
+
|
|
23
|
+
WQB_USERNAME: str = os.getenv("WQB_USERNAME", "")
|
|
24
|
+
WQB_PASSWORD: str = os.getenv("WQB_PASSWORD", "")
|
|
25
|
+
WQB_API_BASE_URL: str = os.getenv("WQB_API_BASE_URL", "https://api.worldquantbrain.com")
|
|
26
|
+
|
|
27
|
+
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
|
|
28
|
+
|
|
29
|
+
MAX_RETRIES: int = int(os.getenv("MAX_RETRIES", "5"))
|
|
30
|
+
RETRY_DELAY_BASE: int = int(os.getenv("RETRY_DELAY_BASE", "2"))
|
|
31
|
+
|
|
32
|
+
BARK_KEY: str = os.getenv("BARK_KEY", "")
|
|
33
|
+
BARK_BASE_URL: str = os.getenv("BARK_BASE_URL", "https://api.day.app")
|
|
34
|
+
|
|
35
|
+
DEFAULT_CONSULTANT_DAY: str = os.getenv("DEFAULT_CONSULTANT_DAY", "2025-04-19")
|
|
36
|
+
|
|
37
|
+
# -------------------------------------------------------------------------
|
|
38
|
+
# 应用级统一常量(非 env,集中管理以避免跨模块重复硬编码)
|
|
39
|
+
# -------------------------------------------------------------------------
|
|
40
|
+
MAX_WORKERS: int = 10
|
|
41
|
+
TOTAL_SCORE: int = 100_000
|
|
42
|
+
DEFAULT_PAGE_LIMIT: int = 100
|
|
43
|
+
DEFAULT_PAGE_OFFSET: int = 0
|
|
44
|
+
DEFAULT_DYEING_WORKERS: int = 3
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def DATABASE_URI(self) -> str:
|
|
48
|
+
return (
|
|
49
|
+
f"postgresql://{self.DB_USER}:{self.DB_PASSWORD}"
|
|
50
|
+
f"@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
config = Config()
|