tradepose-client 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl
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.
Potentially problematic release.
This version of tradepose-client might be problematic. Click here for more details.
- tradepose_client/__init__.py +24 -4
- tradepose_client/builder/__init__.py +22 -0
- tradepose_client/builder/blueprint_builder.py +276 -0
- tradepose_client/builder/indicator_wrapper.py +126 -0
- tradepose_client/builder/strategy_builder.py +312 -0
- tradepose_client/builder/trading_context.py +140 -0
- tradepose_client/enums.py +176 -0
- tradepose_client/indicator_models.py +300 -0
- tradepose_client/models.py +266 -261
- {tradepose_client-0.1.0.dist-info → tradepose_client-0.1.2.dist-info}/METADATA +1 -1
- tradepose_client-0.1.2.dist-info/RECORD +22 -0
- tradepose_client-0.1.0.dist-info/RECORD +0 -15
- {tradepose_client-0.1.0.dist-info → tradepose_client-0.1.2.dist-info}/WHEEL +0 -0
- {tradepose_client-0.1.0.dist-info → tradepose_client-0.1.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""
|
|
2
|
+
策略构建器(主入口类)
|
|
3
|
+
|
|
4
|
+
管理整体策略构建流程:
|
|
5
|
+
1. 自动继承 base_instrument 和 base_freq
|
|
6
|
+
2. 添加指标(返回 IndicatorSpecWrapper)
|
|
7
|
+
3. 设置 base blueprint
|
|
8
|
+
4. 添加 advanced blueprints(链式)
|
|
9
|
+
5. 构建最终 StrategyConfig
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
from tradepose_client.builder import StrategyBuilder, BlueprintBuilder
|
|
13
|
+
|
|
14
|
+
# 1. 创建 builder
|
|
15
|
+
builder = StrategyBuilder(
|
|
16
|
+
name="KOG_US100_15T_ST_21_3",
|
|
17
|
+
base_instrument="US100.cash_M15_FTMO_FUTURE",
|
|
18
|
+
base_freq="15min"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
# 2. 添加指标(自动继承 instrument_id)
|
|
22
|
+
atr = builder.add_indicator("atr", period=21, freq="1D", shift=1)
|
|
23
|
+
st = builder.add_indicator(
|
|
24
|
+
"supertrend",
|
|
25
|
+
multiplier=3.0,
|
|
26
|
+
volatility_column=atr.display_name,
|
|
27
|
+
freq="1D",
|
|
28
|
+
shift=1
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
# 3. 创建并设置 base blueprint
|
|
32
|
+
base_bp = BlueprintBuilder("base", "Long", "Trend")\
|
|
33
|
+
.add_entry_trigger(...)\
|
|
34
|
+
.add_exit_trigger(...)\
|
|
35
|
+
.build()
|
|
36
|
+
|
|
37
|
+
builder.set_base_blueprint(base_bp)
|
|
38
|
+
|
|
39
|
+
# 4. 添加 advanced blueprints
|
|
40
|
+
adv_bp = BlueprintBuilder("risk_mgmt", "Long", "Trend")\
|
|
41
|
+
.add_exit_trigger(...)\
|
|
42
|
+
.build()
|
|
43
|
+
|
|
44
|
+
builder.add_advanced_blueprint(adv_bp)
|
|
45
|
+
|
|
46
|
+
# 5. 构建最终策略
|
|
47
|
+
strategy = builder.build(
|
|
48
|
+
volatility_indicator=atr,
|
|
49
|
+
note="US100 顺势策略"
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# 6. 保存或注册
|
|
53
|
+
strategy.save("strategy.json")
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
from typing import List, Optional, Union, TYPE_CHECKING
|
|
57
|
+
|
|
58
|
+
if TYPE_CHECKING:
|
|
59
|
+
from tradepose_client.models import StrategyConfig, Blueprint, Freq
|
|
60
|
+
from tradepose_client.enums import IndicatorType
|
|
61
|
+
|
|
62
|
+
from .indicator_wrapper import IndicatorSpecWrapper
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class StrategyBuilder:
|
|
66
|
+
"""
|
|
67
|
+
策略构建器(主入口类)
|
|
68
|
+
|
|
69
|
+
管理整体策略构建流程,自动继承 base_instrument 和 base_freq。
|
|
70
|
+
|
|
71
|
+
Attributes:
|
|
72
|
+
name (str): 策略名称
|
|
73
|
+
base_instrument (str): 基准商品
|
|
74
|
+
base_freq (Freq): 基准频率
|
|
75
|
+
|
|
76
|
+
Methods:
|
|
77
|
+
add_indicator(indicator_type, **kwargs) -> IndicatorSpecWrapper:
|
|
78
|
+
添加指标(自动继承 instrument_id)
|
|
79
|
+
set_base_blueprint(blueprint) -> StrategyBuilder:
|
|
80
|
+
设置 Base Blueprint
|
|
81
|
+
add_advanced_blueprint(blueprint) -> StrategyBuilder:
|
|
82
|
+
添加 Advanced Blueprint(链式)
|
|
83
|
+
build(volatility_indicator, note) -> StrategyConfig:
|
|
84
|
+
构建最终 StrategyConfig
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
name: str,
|
|
90
|
+
base_instrument: str,
|
|
91
|
+
base_freq: str,
|
|
92
|
+
):
|
|
93
|
+
"""
|
|
94
|
+
初始化策略构建器
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
name: 策略名称(唯一标识)
|
|
98
|
+
base_instrument: 基准商品 ID(所有指标自动继承此值)
|
|
99
|
+
base_freq: 基准频率(支持字符串,如 "15min", "1D")
|
|
100
|
+
|
|
101
|
+
Examples:
|
|
102
|
+
>>> builder = StrategyBuilder(
|
|
103
|
+
... name="KOG_US100_15T_ST_21_3",
|
|
104
|
+
... base_instrument="US100.cash_M15_FTMO_FUTURE",
|
|
105
|
+
... base_freq="15min"
|
|
106
|
+
... )
|
|
107
|
+
"""
|
|
108
|
+
self.name = name
|
|
109
|
+
self.base_instrument = base_instrument
|
|
110
|
+
self.base_freq = base_freq # 字符串形式,稍后转换
|
|
111
|
+
|
|
112
|
+
self._indicators: List[IndicatorSpecWrapper] = []
|
|
113
|
+
self._base_blueprint: Optional["Blueprint"] = None
|
|
114
|
+
self._advanced_blueprints: List["Blueprint"] = []
|
|
115
|
+
|
|
116
|
+
def add_indicator(
|
|
117
|
+
self,
|
|
118
|
+
indicator_type: Union["IndicatorType", str],
|
|
119
|
+
freq: Union["Freq", str],
|
|
120
|
+
shift: int = 1,
|
|
121
|
+
instrument_id: Optional[str] = None,
|
|
122
|
+
**kwargs,
|
|
123
|
+
) -> IndicatorSpecWrapper:
|
|
124
|
+
"""
|
|
125
|
+
添加指标(支持可选 instrument_id 覆盖)
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
indicator_type: 指标类型(支持 IndicatorType enum 或字符串)
|
|
129
|
+
- IndicatorType.ATR 或 "atr"
|
|
130
|
+
- IndicatorType.SMA 或 "sma"
|
|
131
|
+
- IndicatorType.SUPERTREND 或 "supertrend"
|
|
132
|
+
freq: 指标频率(支持 Freq enum 或字符串)
|
|
133
|
+
- Freq.DAY_1 或 "1D"
|
|
134
|
+
- Freq.HOUR_1 或 "1h"
|
|
135
|
+
- Freq.MIN_15 或 "15min"
|
|
136
|
+
shift: 位移(默认 1,依赖指标通常使用 0)
|
|
137
|
+
instrument_id: 商品 ID(默认 None,使用 base_instrument)
|
|
138
|
+
- None: 使用 self.base_instrument(常见用法)
|
|
139
|
+
- "OTHER_INSTRUMENT": 使用其他商品(跨商品引用)
|
|
140
|
+
**kwargs: 指标参数(传递给 Indicator 工厂方法)
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
IndicatorSpecWrapper: 指标包装器,提供 .col() 和 .display_name
|
|
144
|
+
|
|
145
|
+
Examples:
|
|
146
|
+
>>> # 使用 enum(推荐,类型安全)
|
|
147
|
+
>>> atr = builder.add_indicator(
|
|
148
|
+
... IndicatorType.ATR,
|
|
149
|
+
... period=21,
|
|
150
|
+
... freq=Freq.DAY_1,
|
|
151
|
+
... shift=1
|
|
152
|
+
... )
|
|
153
|
+
>>>
|
|
154
|
+
>>> # 使用字符串(向后兼容)
|
|
155
|
+
>>> atr = builder.add_indicator("atr", period=21, freq="1D", shift=1)
|
|
156
|
+
>>>
|
|
157
|
+
>>> # 跨商品引用(使用其他商品的指标)
|
|
158
|
+
>>> vix_atr = builder.add_indicator(
|
|
159
|
+
... IndicatorType.ATR,
|
|
160
|
+
... period=21,
|
|
161
|
+
... freq=Freq.DAY_1,
|
|
162
|
+
... shift=1,
|
|
163
|
+
... instrument_id="VIX.cash" # 引用 VIX 的 ATR
|
|
164
|
+
... )
|
|
165
|
+
>>>
|
|
166
|
+
>>> # 依赖指标(SuperTrend 引用 ATR)
|
|
167
|
+
>>> st = builder.add_indicator(
|
|
168
|
+
... IndicatorType.SUPERTREND,
|
|
169
|
+
... multiplier=3.0,
|
|
170
|
+
... volatility_column=atr.display_name, # 引用依赖
|
|
171
|
+
... freq=Freq.DAY_1,
|
|
172
|
+
... shift=1
|
|
173
|
+
... )
|
|
174
|
+
"""
|
|
175
|
+
from tradepose_client.models import Indicator, create_indicator_spec, Freq
|
|
176
|
+
from tradepose_client.enums import IndicatorType
|
|
177
|
+
|
|
178
|
+
# 转换 indicator_type 为字符串
|
|
179
|
+
if isinstance(indicator_type, IndicatorType):
|
|
180
|
+
indicator_type_str = indicator_type.value
|
|
181
|
+
else:
|
|
182
|
+
indicator_type_str = indicator_type
|
|
183
|
+
|
|
184
|
+
# 动态调用 Indicator 工厂方法
|
|
185
|
+
if not hasattr(Indicator, indicator_type_str):
|
|
186
|
+
raise ValueError(
|
|
187
|
+
f"Unknown indicator type: '{indicator_type_str}'. "
|
|
188
|
+
f"Available: {', '.join([e.value for e in IndicatorType])}"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
indicator_factory = getattr(Indicator, indicator_type_str)
|
|
192
|
+
indicator_config = indicator_factory(**kwargs)
|
|
193
|
+
|
|
194
|
+
# 转换 freq 字符串/enum 为 Freq enum
|
|
195
|
+
freq_enum = Freq(freq) if isinstance(freq, str) else freq
|
|
196
|
+
|
|
197
|
+
# 使用 instrument_id(如果提供),否则使用 base_instrument
|
|
198
|
+
final_instrument_id = instrument_id if instrument_id is not None else self.base_instrument
|
|
199
|
+
|
|
200
|
+
# 创建 IndicatorSpec
|
|
201
|
+
spec = create_indicator_spec(
|
|
202
|
+
freq=freq_enum,
|
|
203
|
+
indicator=indicator_config,
|
|
204
|
+
instrument_id=final_instrument_id,
|
|
205
|
+
shift=shift,
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
wrapper = IndicatorSpecWrapper(spec)
|
|
209
|
+
self._indicators.append(wrapper)
|
|
210
|
+
return wrapper
|
|
211
|
+
|
|
212
|
+
def set_base_blueprint(self, blueprint: "Blueprint") -> "StrategyBuilder":
|
|
213
|
+
"""
|
|
214
|
+
设置 Base Blueprint
|
|
215
|
+
|
|
216
|
+
Args:
|
|
217
|
+
blueprint: Base Blueprint 对象(通常由 BlueprintBuilder 创建)
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
StrategyBuilder: 返回自身,支持链式调用
|
|
221
|
+
|
|
222
|
+
Examples:
|
|
223
|
+
>>> base_bp = BlueprintBuilder("base", "Long", "Trend")\
|
|
224
|
+
... .add_entry_trigger(...)\
|
|
225
|
+
... .add_exit_trigger(...)\
|
|
226
|
+
... .build()
|
|
227
|
+
>>>
|
|
228
|
+
>>> builder.set_base_blueprint(base_bp)
|
|
229
|
+
"""
|
|
230
|
+
self._base_blueprint = blueprint
|
|
231
|
+
return self
|
|
232
|
+
|
|
233
|
+
def add_advanced_blueprint(self, blueprint: "Blueprint") -> "StrategyBuilder":
|
|
234
|
+
"""
|
|
235
|
+
添加 Advanced Blueprint(链式调用)
|
|
236
|
+
|
|
237
|
+
Args:
|
|
238
|
+
blueprint: Advanced Blueprint 对象(通常由 BlueprintBuilder 创建)
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
StrategyBuilder: 返回自身,支持链式调用
|
|
242
|
+
|
|
243
|
+
Examples:
|
|
244
|
+
>>> adv_bp = BlueprintBuilder("risk_mgmt", "Long", "Trend")\
|
|
245
|
+
... .add_exit_trigger(...)\
|
|
246
|
+
... .build()
|
|
247
|
+
>>>
|
|
248
|
+
>>> builder.add_advanced_blueprint(adv_bp)
|
|
249
|
+
"""
|
|
250
|
+
self._advanced_blueprints.append(blueprint)
|
|
251
|
+
return self
|
|
252
|
+
|
|
253
|
+
def build(
|
|
254
|
+
self,
|
|
255
|
+
volatility_indicator: IndicatorSpecWrapper,
|
|
256
|
+
note: str = "",
|
|
257
|
+
) -> "StrategyConfig":
|
|
258
|
+
"""
|
|
259
|
+
构建最终 StrategyConfig
|
|
260
|
+
|
|
261
|
+
Args:
|
|
262
|
+
volatility_indicator: 波动率指标(通常是 ATR)
|
|
263
|
+
note: 策略说明
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
StrategyConfig: 完整的策略配置对象
|
|
267
|
+
|
|
268
|
+
Raises:
|
|
269
|
+
ValueError: 如果未设置 base_blueprint
|
|
270
|
+
|
|
271
|
+
Examples:
|
|
272
|
+
>>> strategy = builder.build(
|
|
273
|
+
... volatility_indicator=atr,
|
|
274
|
+
... note="US100 顺势策略 - SuperTrend(21,3)"
|
|
275
|
+
... )
|
|
276
|
+
>>>
|
|
277
|
+
>>> # 保存为 JSON
|
|
278
|
+
>>> strategy.save("strategy.json")
|
|
279
|
+
>>>
|
|
280
|
+
>>> # 或注册到 API
|
|
281
|
+
>>> client.register_strategy(strategy.to_json())
|
|
282
|
+
"""
|
|
283
|
+
from tradepose_client.models import StrategyConfig, Freq
|
|
284
|
+
|
|
285
|
+
if not self._base_blueprint:
|
|
286
|
+
raise ValueError(
|
|
287
|
+
"Base blueprint is required. Use .set_base_blueprint() first."
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
# 转换 base_freq 为 Freq enum
|
|
291
|
+
base_freq_enum = Freq(self.base_freq) if isinstance(self.base_freq, str) else self.base_freq
|
|
292
|
+
|
|
293
|
+
return StrategyConfig(
|
|
294
|
+
name=self.name,
|
|
295
|
+
base_instrument=self.base_instrument,
|
|
296
|
+
base_freq=base_freq_enum,
|
|
297
|
+
volatility_indicator=volatility_indicator.spec,
|
|
298
|
+
indicators=[ind.spec for ind in self._indicators],
|
|
299
|
+
base_blueprint=self._base_blueprint,
|
|
300
|
+
advanced_blueprints=self._advanced_blueprints,
|
|
301
|
+
note=note,
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
def __repr__(self) -> str:
|
|
305
|
+
"""字符串表示"""
|
|
306
|
+
return (
|
|
307
|
+
f"StrategyBuilder(name='{self.name}', "
|
|
308
|
+
f"base_instrument='{self.base_instrument}', "
|
|
309
|
+
f"base_freq='{self.base_freq}', "
|
|
310
|
+
f"indicators={len(self._indicators)}, "
|
|
311
|
+
f"advanced_blueprints={len(self._advanced_blueprints)})"
|
|
312
|
+
)
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Trading Context 固定字段访问器
|
|
3
|
+
|
|
4
|
+
基于 schema.py 中定义的 struct 字段,提供简洁的属性访问方式,
|
|
5
|
+
避免繁琐的 `.struct.field()` 调用。
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
from tradepose_client.builder import TradingContext
|
|
9
|
+
import polars as pl
|
|
10
|
+
|
|
11
|
+
# 简洁访问(返回 pl.Expr)
|
|
12
|
+
entry_price = TradingContext.base.entry_price
|
|
13
|
+
highest = TradingContext.advanced_entry.highest_since_entry
|
|
14
|
+
bars = TradingContext.advanced_exit.bars_since_entry
|
|
15
|
+
|
|
16
|
+
# 用于策略条件表达式
|
|
17
|
+
condition = TradingContext.base.bars_in_position > 50
|
|
18
|
+
stop_loss_price = TradingContext.advanced_entry.entry_price - pl.col("atr") * 2
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import polars as pl
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TradingContextProxy:
|
|
25
|
+
"""
|
|
26
|
+
Trading Context 字段访问代理
|
|
27
|
+
|
|
28
|
+
为 base_trading_context, advanced_entry_trading_context,
|
|
29
|
+
advanced_exit_trading_context 提供统一的属性访问接口。
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
context_name: 上下文字段名称(如 "base_trading_context")
|
|
33
|
+
context_type: 上下文类型("base" 或 "advanced")
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
context_name: str,
|
|
39
|
+
):
|
|
40
|
+
self._context_name = context_name
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def entry_price(self) -> pl.Expr:
|
|
44
|
+
"""
|
|
45
|
+
进场价格
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
pl.Expr: Polars 表达式,可直接用于条件或价格计算
|
|
49
|
+
"""
|
|
50
|
+
return pl.col(self._context_name).struct.field("position_entry_price")
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def bars_in_position(self) -> pl.Expr:
|
|
54
|
+
"""
|
|
55
|
+
持仓 K 线数(仅适用于 base_trading_context)
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
pl.Expr: Polars 表达式
|
|
59
|
+
|
|
60
|
+
Note:
|
|
61
|
+
- base_trading_context 使用 "bars_in_position"
|
|
62
|
+
- advanced 使用 "bars_since_advanced_entry"(请用 .bars_since_entry)
|
|
63
|
+
"""
|
|
64
|
+
return pl.col(self._context_name).struct.field("bars_in_position")
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def highest_since_entry(self) -> pl.Expr:
|
|
68
|
+
"""
|
|
69
|
+
进场以来最高价
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
pl.Expr: Polars 表达式,可用于 trailing stop 计算
|
|
73
|
+
"""
|
|
74
|
+
return pl.col(self._context_name).struct.field("highest_since_entry")
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def lowest_since_entry(self) -> pl.Expr:
|
|
78
|
+
"""
|
|
79
|
+
进场以来最低价
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
pl.Expr: Polars 表达式,可用于 short position trailing stop 计算
|
|
83
|
+
"""
|
|
84
|
+
return pl.col(self._context_name).struct.field("lowest_since_entry")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class TradingContext:
|
|
88
|
+
"""
|
|
89
|
+
Trading Context 统一访问器
|
|
90
|
+
|
|
91
|
+
提供三种 trading context 的属性访问:
|
|
92
|
+
- base: base_trading_context
|
|
93
|
+
- advanced_entry: advanced_entry_trading_context
|
|
94
|
+
- advanced_exit: advanced_exit_trading_context
|
|
95
|
+
|
|
96
|
+
Usage:
|
|
97
|
+
# Base context(Base Blueprint 生成)
|
|
98
|
+
TradingContext.base.entry_price
|
|
99
|
+
TradingContext.base.bars_in_position
|
|
100
|
+
TradingContext.base.highest_since_entry
|
|
101
|
+
TradingContext.base.lowest_since_entry
|
|
102
|
+
|
|
103
|
+
# Advanced Entry context(Advanced Entry Triggers 使用)
|
|
104
|
+
TradingContext.advanced_entry.entry_price
|
|
105
|
+
TradingContext.advanced_entry.bars_since_entry
|
|
106
|
+
TradingContext.advanced_entry.highest_since_entry
|
|
107
|
+
TradingContext.advanced_entry.lowest_since_entry
|
|
108
|
+
|
|
109
|
+
# Advanced Exit context(Advanced Exit Triggers 使用)
|
|
110
|
+
TradingContext.advanced_exit.entry_price
|
|
111
|
+
TradingContext.advanced_exit.bars_since_entry
|
|
112
|
+
TradingContext.advanced_exit.highest_since_entry
|
|
113
|
+
TradingContext.advanced_exit.lowest_since_entry
|
|
114
|
+
|
|
115
|
+
Examples:
|
|
116
|
+
# Stop Loss(Long)
|
|
117
|
+
stop_loss_price = (
|
|
118
|
+
TradingContext.advanced_entry.entry_price -
|
|
119
|
+
pl.col("atr") * 2
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Trailing Stop(Long)
|
|
123
|
+
trailing_stop_price = (
|
|
124
|
+
TradingContext.advanced_entry.highest_since_entry -
|
|
125
|
+
pl.col("atr") * 2
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# 持倉時間過濾
|
|
129
|
+
timeout_condition = TradingContext.advanced_entry.bars_since_entry > 100
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
base = TradingContextProxy("base_trading_context")
|
|
133
|
+
advanced_entry = TradingContextProxy("advanced_entry_trading_context")
|
|
134
|
+
advanced_exit = TradingContextProxy("advanced_exit_trading_context")
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# 向后兼容的别名
|
|
138
|
+
BaseContext = TradingContext.base
|
|
139
|
+
AdvancedEntryContext = TradingContext.advanced_entry
|
|
140
|
+
AdvancedExitContext = TradingContext.advanced_exit
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Enumerations for Tradepose strategy configuration
|
|
3
|
+
|
|
4
|
+
All enums are aligned with Rust backend types for JSON serialization.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from enum import Enum
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Freq(str, Enum):
|
|
11
|
+
"""
|
|
12
|
+
Time frequency enum (aligned with Rust Freq enum)
|
|
13
|
+
|
|
14
|
+
Values:
|
|
15
|
+
MIN_1: 1 minute
|
|
16
|
+
MIN_5: 5 minutes
|
|
17
|
+
MIN_15: 15 minutes
|
|
18
|
+
MIN_30: 30 minutes
|
|
19
|
+
HOUR_1: 1 hour
|
|
20
|
+
HOUR_4: 4 hours
|
|
21
|
+
DAY_1: 1 day
|
|
22
|
+
WEEK_1: 1 week
|
|
23
|
+
MONTH_1: 1 month
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
MIN_1 = "1min"
|
|
27
|
+
MIN_5 = "5min"
|
|
28
|
+
MIN_15 = "15min"
|
|
29
|
+
MIN_30 = "30min"
|
|
30
|
+
HOUR_1 = "1h"
|
|
31
|
+
HOUR_4 = "4h"
|
|
32
|
+
DAY_1 = "1D"
|
|
33
|
+
WEEK_1 = "1W"
|
|
34
|
+
MONTH_1 = "1M"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class OrderStrategy(str, Enum):
|
|
38
|
+
"""
|
|
39
|
+
Order strategy enum (aligned with Rust OrderStrategy enum)
|
|
40
|
+
|
|
41
|
+
Used to specify the execution strategy for entry/exit triggers.
|
|
42
|
+
|
|
43
|
+
Rust mapping:
|
|
44
|
+
- Rust: OrderStrategy::ImmediateEntry → Python: OrderStrategy.IMMEDIATE_ENTRY (u32: 0)
|
|
45
|
+
- Rust: OrderStrategy::FavorableDelayEntry → Python: OrderStrategy.FAVORABLE_DELAY_ENTRY (u32: 1)
|
|
46
|
+
- Rust: OrderStrategy::AdverseDelayEntry → Python: OrderStrategy.ADVERSE_DELAY_ENTRY (u32: 2)
|
|
47
|
+
- Rust: OrderStrategy::ImmediateExit → Python: OrderStrategy.IMMEDIATE_EXIT (u32: 3)
|
|
48
|
+
- Rust: OrderStrategy::StopLoss → Python: OrderStrategy.STOP_LOSS (u32: 4)
|
|
49
|
+
- Rust: OrderStrategy::TakeProfit → Python: OrderStrategy.TAKE_PROFIT (u32: 5)
|
|
50
|
+
- Rust: OrderStrategy::TrailingStop → Python: OrderStrategy.TRAILING_STOP (u32: 6)
|
|
51
|
+
- Rust: OrderStrategy::Breakeven → Python: OrderStrategy.BREAKEVEN (u32: 7)
|
|
52
|
+
- Rust: OrderStrategy::TimeoutExit → Python: OrderStrategy.TIMEOUT_EXIT (u32: 8)
|
|
53
|
+
|
|
54
|
+
Entry Strategies:
|
|
55
|
+
IMMEDIATE_ENTRY: Immediate entry on signal (required for Base Blueprint)
|
|
56
|
+
FAVORABLE_DELAY_ENTRY: Wait for favorable price (pullback/retracement)
|
|
57
|
+
ADVERSE_DELAY_ENTRY: Wait for breakout/aggressive entry
|
|
58
|
+
|
|
59
|
+
Exit Strategies:
|
|
60
|
+
IMMEDIATE_EXIT: Immediate exit on signal (required for Base Blueprint)
|
|
61
|
+
STOP_LOSS: Fixed stop loss
|
|
62
|
+
TAKE_PROFIT: Fixed take profit
|
|
63
|
+
TRAILING_STOP: Dynamic trailing stop
|
|
64
|
+
BREAKEVEN: Move stop to breakeven after profit
|
|
65
|
+
TIMEOUT_EXIT: Exit after time limit
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
IMMEDIATE_ENTRY = "ImmediateEntry"
|
|
69
|
+
FAVORABLE_DELAY_ENTRY = "FavorableDelayEntry"
|
|
70
|
+
ADVERSE_DELAY_ENTRY = "AdverseDelayEntry"
|
|
71
|
+
IMMEDIATE_EXIT = "ImmediateExit"
|
|
72
|
+
STOP_LOSS = "StopLoss"
|
|
73
|
+
TAKE_PROFIT = "TakeProfit"
|
|
74
|
+
TRAILING_STOP = "TrailingStop"
|
|
75
|
+
BREAKEVEN = "Breakeven"
|
|
76
|
+
TIMEOUT_EXIT = "TimeoutExit"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class Weekday(str, Enum):
|
|
80
|
+
"""
|
|
81
|
+
Weekday enum (aligned with Rust Weekday enum)
|
|
82
|
+
|
|
83
|
+
Used for Market Profile WeeklyTime configuration.
|
|
84
|
+
|
|
85
|
+
Mapping:
|
|
86
|
+
MON (Monday) = 0
|
|
87
|
+
TUE (Tuesday) = 1
|
|
88
|
+
WED (Wednesday) = 2
|
|
89
|
+
THU (Thursday) = 3
|
|
90
|
+
FRI (Friday) = 4
|
|
91
|
+
SAT (Saturday) = 5
|
|
92
|
+
SUN (Sunday) = 6
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
MON = "Mon"
|
|
96
|
+
TUE = "Tue"
|
|
97
|
+
WED = "Wed"
|
|
98
|
+
THU = "Thu"
|
|
99
|
+
FRI = "Fri"
|
|
100
|
+
SAT = "Sat"
|
|
101
|
+
SUN = "Sun"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class IndicatorType(str, Enum):
|
|
105
|
+
"""
|
|
106
|
+
Indicator type enum
|
|
107
|
+
|
|
108
|
+
Maps to Indicator factory methods in models.py.
|
|
109
|
+
|
|
110
|
+
Values:
|
|
111
|
+
SMA: Simple Moving Average
|
|
112
|
+
EMA: Exponential Moving Average
|
|
113
|
+
SMMA: Smoothed Moving Average
|
|
114
|
+
WMA: Weighted Moving Average
|
|
115
|
+
ATR: Average True Range
|
|
116
|
+
ATR_QUANTILE: ATR Rolling Quantile
|
|
117
|
+
SUPERTREND: SuperTrend
|
|
118
|
+
MARKET_PROFILE: Market Profile
|
|
119
|
+
CCI: Commodity Channel Index
|
|
120
|
+
RSI: Relative Strength Index
|
|
121
|
+
BOLLINGER_BANDS: Bollinger Bands
|
|
122
|
+
MACD: Moving Average Convergence Divergence
|
|
123
|
+
STOCHASTIC: Stochastic Oscillator
|
|
124
|
+
ADX: Average Directional Index
|
|
125
|
+
RAW_OHLCV: Raw OHLCV column
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
SMA = "sma"
|
|
129
|
+
EMA = "ema"
|
|
130
|
+
SMMA = "smma"
|
|
131
|
+
WMA = "wma"
|
|
132
|
+
ATR = "atr"
|
|
133
|
+
ATR_QUANTILE = "atr_quantile"
|
|
134
|
+
SUPERTREND = "supertrend"
|
|
135
|
+
MARKET_PROFILE = "market_profile"
|
|
136
|
+
CCI = "cci"
|
|
137
|
+
RSI = "rsi"
|
|
138
|
+
BOLLINGER_BANDS = "bollinger_bands"
|
|
139
|
+
MACD = "macd"
|
|
140
|
+
STOCHASTIC = "stochastic"
|
|
141
|
+
ADX = "adx"
|
|
142
|
+
RAW_OHLCV = "raw_ohlcv"
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class TradeDirection(str, Enum):
|
|
146
|
+
"""
|
|
147
|
+
Trade direction enum
|
|
148
|
+
|
|
149
|
+
Used to specify the trading direction in Blueprint.
|
|
150
|
+
|
|
151
|
+
Values:
|
|
152
|
+
LONG: Long trades only
|
|
153
|
+
SHORT: Short trades only
|
|
154
|
+
BOTH: Both long and short trades (currently not fully supported)
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
LONG = "Long"
|
|
158
|
+
SHORT = "Short"
|
|
159
|
+
BOTH = "Both"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class TrendType(str, Enum):
|
|
163
|
+
"""
|
|
164
|
+
Trend type enum
|
|
165
|
+
|
|
166
|
+
Used to categorize strategy trading style.
|
|
167
|
+
|
|
168
|
+
Values:
|
|
169
|
+
TREND: Trend-following strategies
|
|
170
|
+
RANGE: Range-bound/mean-reversion strategies
|
|
171
|
+
REVERSAL: Reversal/counter-trend strategies
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
TREND = "Trend"
|
|
175
|
+
RANGE = "Range"
|
|
176
|
+
REVERSAL = "Reversal"
|