-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathsignal_market_mapper.py
More file actions
241 lines (200 loc) · 7.31 KB
/
Copy pathsignal_market_mapper.py
File metadata and controls
241 lines (200 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
信號→市場映射器
將川普密碼信號(TARIFF, DEAL, RELIEF, ACTION, THREAT)
映射到 Polymarket 上對應的預測市場。
支援模糊匹配與信心度評估。
"""
from __future__ import annotations
from typing import Any
# =====================================================================
# 信號類型定義
# =====================================================================
# 每個信號類型包含:
# - keywords: 用來搜尋 Polymarket 市場的關鍵字列表
# - default_direction: 信號出現時的預設操作方向
# - base_confidence: 基礎信心度(0.0 ~ 1.0)
# - description: 信號說明
SIGNAL_DEFINITIONS: dict[str, dict[str, Any]] = {
"TARIFF": {
"keywords": ["tariff", "trade", "import", "duty", "customs"],
"default_direction": "LONG",
"base_confidence": 0.7,
"description": "關稅/貿易相關信號 — 川普傾向加徵關稅時觸發",
},
"DEAL": {
"keywords": ["deal", "agreement", "negotiate", "trade deal", "summit"],
"default_direction": "LONG",
"base_confidence": 0.65,
"description": "交易/協議信號 — 暗示即將達成某種協議",
},
"RELIEF": {
"keywords": ["relief", "exemption", "waiver", "reduce", "cut"],
"default_direction": "SHORT",
"base_confidence": 0.6,
"description": "寬減信號 — 減免關稅或放寬限制",
},
"ACTION": {
"keywords": ["executive order", "action", "sign", "announce", "decree"],
"default_direction": "LONG",
"base_confidence": 0.75,
"description": "行政行動信號 — 即將簽署行政命令或採取具體措施",
},
"THREAT": {
"keywords": ["threat", "warn", "sanction", "punish", "retaliate", "ban"],
"default_direction": "LONG",
"base_confidence": 0.55,
"description": "威脅信號 — 口頭威脅但未必落實,信心度較低",
},
}
# 同義詞/模糊匹配表:當輸入的信號文字不完全匹配時使用
SIGNAL_ALIASES: dict[str, str] = {
# TARIFF 系列
"tariff": "TARIFF",
"trade_war": "TARIFF",
"import_tax": "TARIFF",
"customs": "TARIFF",
"duty": "TARIFF",
# DEAL 系列
"deal": "DEAL",
"agreement": "DEAL",
"negotiate": "DEAL",
"summit": "DEAL",
"handshake": "DEAL",
# RELIEF 系列
"relief": "RELIEF",
"exemption": "RELIEF",
"waiver": "RELIEF",
"reduce": "RELIEF",
"cut": "RELIEF",
# ACTION 系列
"action": "ACTION",
"executive_order": "ACTION",
"eo": "ACTION",
"sign": "ACTION",
"decree": "ACTION",
# THREAT 系列
"threat": "THREAT",
"warn": "THREAT",
"sanction": "THREAT",
"ban": "THREAT",
"retaliate": "THREAT",
}
def _normalize_signal(signal: str) -> str | None:
"""
正規化信號名稱:先嘗試精確匹配,再嘗試模糊匹配。
Args:
signal: 原始信號字串。
Returns:
正規化後的信號類型(大寫),或 None 表示無法辨識。
"""
upper = signal.strip().upper()
# 精確匹配
if upper in SIGNAL_DEFINITIONS:
return upper
# 模糊匹配(透過別名表)
lower = signal.strip().lower().replace(" ", "_").replace("-", "_")
if lower in SIGNAL_ALIASES:
return SIGNAL_ALIASES[lower]
# 子字串匹配:檢查信號文字是否包含任何已知關鍵字
for alias_key, signal_type in SIGNAL_ALIASES.items():
if alias_key in lower or lower in alias_key:
return signal_type
return None
def _build_market_queries(signal_type: str) -> list[str]:
"""
根據信號類型產生用於搜尋 Polymarket 的查詢字串列表。
Args:
signal_type: 正規化後的信號類型。
Returns:
搜尋關鍵字列表。
"""
definition = SIGNAL_DEFINITIONS.get(signal_type)
if not definition:
return []
return list(definition["keywords"])
def match_signals_to_markets(signals: list[str]) -> list[dict[str, Any]]:
"""
將一組信號映射到可能的 Polymarket 市場搜尋指令。
對每個輸入信號:
1. 正規化信號名稱(支援模糊匹配)
2. 查找對應的市場搜尋關鍵字
3. 產出映射結果,包含方向與信心度
Args:
signals: 信號字串列表,例如 ["TARIFF", "deal", "executive_order"]。
Returns:
映射結果列表,每個元素為 dict:
{
"signal_type": "TARIFF",
"original_input": "tariff",
"market_queries": ["tariff", "trade", ...],
"direction": "LONG",
"confidence": 0.7,
"description": "...",
}
無法辨識的信號會標記 signal_type 為 "UNKNOWN"。
"""
results: list[dict[str, Any]] = []
for raw_signal in signals:
signal_type = _normalize_signal(raw_signal)
if signal_type is None:
# 無法辨識 → 回傳 UNKNOWN 讓呼叫方知道
results.append({
"signal_type": "UNKNOWN",
"original_input": raw_signal,
"market_queries": [],
"direction": "NEUTRAL",
"confidence": 0.0,
"description": f"無法辨識的信號: {raw_signal}",
})
continue
definition = SIGNAL_DEFINITIONS[signal_type]
queries = _build_market_queries(signal_type)
results.append({
"signal_type": signal_type,
"original_input": raw_signal,
"market_queries": queries,
"direction": definition["default_direction"],
"confidence": definition["base_confidence"],
"description": definition["description"],
})
return results
def get_supported_signals() -> list[dict[str, Any]]:
"""
列出所有支援的信號類型及其定義。
Returns:
信號定義列表。
"""
return [
{
"signal_type": sig_type,
"keywords": defn["keywords"],
"direction": defn["default_direction"],
"confidence": defn["base_confidence"],
"description": defn["description"],
}
for sig_type, defn in SIGNAL_DEFINITIONS.items()
]
# =====================================================================
# Demo
# =====================================================================
if __name__ == "__main__":
print("=== 信號→市場映射器 Demo ===\n")
# 列出所有支援的信號
print("[支援的信號類型]")
for sig in get_supported_signals():
print(f" {sig['signal_type']:10s} | 方向: {sig['direction']:5s} | "
f"信心度: {sig['confidence']:.0%} | {sig['description']}")
print()
# 測試映射
test_signals = ["TARIFF", "deal", "executive_order", "ban", "some_random_thing"]
print(f"[測試信號] {test_signals}\n")
mappings = match_signals_to_markets(test_signals)
for m in mappings:
print(f" 輸入: {m['original_input']!r}")
print(f" → 信號類型: {m['signal_type']}")
print(f" → 方向: {m['direction']}")
print(f" → 信心度: {m['confidence']:.0%}")
print(f" → 搜尋關鍵字: {m['market_queries']}")
print(f" → 說明: {m['description']}")
print()
print("=== Demo 結束 ===")