feat(crypto策略): 支持 AI 信号止盈止损事件与多字段渲染

Motivation:
区分止盈与止损事件,并为 AI crypto signals 策略提供更丰富的推送内容渲染能力(杠杆、止盈档位、收益、持仓周期等)。

Changes:

* 开仓(多/空,含原 isSale 空单)统一映射为 trade.open,不再发 trade.sell
* 新增 trade.gain 事件用于止盈,止损仍映射为 trade.close;HLSS 策略保持原有事件映射
* 新增 leverageText、tp1~tp5、closeAction、revenueDisplay、holdPeriod 等渲染字段
* 新增收益符号、止盈档位、持仓周期等格式化逻辑
* 补充 AI crypto signals 的模板与规则文档及单元测试
This commit is contained in:
2026-08-17 00:54:01 +08:00
parent 0f9f154969
commit f5f64f7653
4 changed files with 412 additions and 6 deletions
+136
View File
@@ -429,3 +429,139 @@ curl -X PATCH 'http://localhost:8080/api/v1/rules/1/channels/1/disable' \
curl -X GET 'http://localhost:8080/api/v1/message-logs?source=trading-system&event=trade.open&status=success&page=1&page_size=20' \
-H 'Authorization: Bearer admin-sk-change-me'
```
---
## AI crypto signalscrypto-strategy
Source`crypto-strategy`。渠道:`safew_ai_crypto_signals`。条件:`strategyCode = ai-crypto-signals`
事件:开仓(多/空)→ `trade.open`;止盈 → `trade.gain`;止损 → `trade.close`。空单开仓不再发 `trade.sell`
开仓渲染示例:
```
预警时间:2026-08-16 03:20:13
预警币种:APE
交易方向:做多
建议杠杆:31x
入场区域:0.1235
风险控制(止损):0.1223
止盈目标:
TP10.1241
TP20.1247
TP30.1253
TP40.1259
TP50.1265
推送时间:2026-08-16 03:20:13
```
止盈渲染示例:
```
止盈时间:2026-08-16 16:29:23
预警币种:LTC
执行操作:到达第一止盈 (TP1)
平仓点位:44.63
预警收益:+14.2793%
预警周期:53分钟
```
止损渲染示例:
```
止损时间:2026-08-16 16:17:15
预警币种:ETH
执行操作:触发止损
平仓点位:1887
最终损益:-30.1557%
```
### 创建开仓模板
```bash
curl -X POST 'http://localhost:8080/api/v1/templates' \
-H 'Authorization: Bearer admin-sk-change-me' \
-H 'Content-Type: application/json' \
-d '{
"name": "ai_crypto_signals_open",
"content": "预警时间:{{.pushedAt}}\n预警币种:{{.symbol}}\n交易方向:{{case .side \"LONG\" \"做多\" \"SHORT\" \"做空\"}}\n{{line \"建议杠杆\" .leverageText}}入场区域:{{.entryRange}}\n{{line \"风险控制(止损)\" .stopLossPrice}}止盈目标:\n{{with .tp1}}TP1{{.}}\n{{end}}{{with .tp2}}TP2{{.}}\n{{end}}{{with .tp3}}TP3{{.}}\n{{end}}{{with .tp4}}TP4{{.}}\n{{end}}{{with .tp5}}TP5{{.}}\n{{end}}推送时间:{{.pushedAt}}"
}'
```
### 创建止盈模板
```bash
curl -X POST 'http://localhost:8080/api/v1/templates' \
-H 'Authorization: Bearer admin-sk-change-me' \
-H 'Content-Type: application/json' \
-d '{
"name": "ai_crypto_signals_gain",
"content": "止盈时间:{{.pushedAt}}\n预警币种:{{.symbol}}\n执行操作:{{.closeAction}}\n平仓点位:{{.price}}\n预警收益:{{.revenueDisplay}}\n{{line \"预警周期\" .holdPeriod}}"
}'
```
### 创建止损模板
```bash
curl -X POST 'http://localhost:8080/api/v1/templates' \
-H 'Authorization: Bearer admin-sk-change-me' \
-H 'Content-Type: application/json' \
-d '{
"name": "ai_crypto_signals_close",
"content": "止损时间:{{.pushedAt}}\n预警币种:{{.symbol}}\n执行操作:触发止损\n平仓点位:{{.price}}\n最终损益:{{.revenueDisplay}}\n{{line \"预警周期\" .holdPeriod}}"
}'
```
### 创建开仓 / 止盈 / 止损规则
```bash
curl -X POST 'http://localhost:8080/api/v1/rules' \
-H 'Authorization: Bearer admin-sk-change-me' \
-H 'Content-Type: application/json' \
-d '{
"name": "AI crypto signals 开仓",
"source_name": "crypto-strategy",
"event": "trade.open",
"template_name": "ai_crypto_signals_open",
"channels": ["safew_ai_crypto_signals"],
"conditions": [
{"field": "strategyCode", "op": "eq", "value": "ai-crypto-signals"}
],
"enabled": 1
}'
```
```bash
curl -X POST 'http://localhost:8080/api/v1/rules' \
-H 'Authorization: Bearer admin-sk-change-me' \
-H 'Content-Type: application/json' \
-d '{
"name": "AI crypto signals 止盈",
"source_name": "crypto-strategy",
"event": "trade.gain",
"template_name": "ai_crypto_signals_gain",
"channels": ["safew_ai_crypto_signals"],
"conditions": [
{"field": "strategyCode", "op": "eq", "value": "ai-crypto-signals"}
],
"enabled": 1
}'
```
```bash
curl -X POST 'http://localhost:8080/api/v1/rules' \
-H 'Authorization: Bearer admin-sk-change-me' \
-H 'Content-Type: application/json' \
-d '{
"name": "AI crypto signals 止损",
"source_name": "crypto-strategy",
"event": "trade.close",
"template_name": "ai_crypto_signals_close",
"channels": ["safew_ai_crypto_signals"],
"conditions": [
{"field": "strategyCode", "op": "eq", "value": "ai-crypto-signals"}
],
"enabled": 1
}'
```