Skip to content

Commit 07b3dc9

Browse files
authored
feat: 网格日志能力补齐 (#89)
* feat: 网格日志能力补齐 * fix: 修正error标记
1 parent 54628e4 commit 07b3dc9

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

grid_main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ def run_cycle(self):
125125
current_grid_summary,
126126
)
127127

128+
# 记录网格决策到本地日志 + 云端
129+
action = ai_decision.get("action", "UNKNOWN")
130+
reason = ai_decision.get("reason", "")
131+
confidence = float(ai_decision.get("confidence", 0.0))
132+
self.logger.log_decision(
133+
symbol=self.config.symbols[0],
134+
market_data=market_data,
135+
prompt="[GridAgent]",
136+
ai_response=reason,
137+
decision=action,
138+
action_details=ai_decision,
139+
status="SUCCESS" if action != "ERROR" else "ERROR",
140+
confidence=confidence,
141+
)
142+
128143
self.grid_manager.sync_grid(self.config.symbols[0], ai_decision)
129144
self.logger.print_header("✅ 网格交易周期完成")
130145

src/trading/grid_manager.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ def sync_grid(self, symbol: str, ai_config: dict[str, Any]):
235235
if oid:
236236
buy_orders.append({"oid": oid, "px": p})
237237
self.logger.print_info(f" [Grid] ✅ 买单挂载: ${p}")
238+
self.logger.log_trade(
239+
symbol=symbol,
240+
action="GRID_BUY",
241+
amount=new_amount,
242+
price=p,
243+
order_id=str(oid),
244+
status="PLACED",
245+
)
238246
elif res and not res.get("success"):
239247
self.logger.print_warning(
240248
f" [Grid] ⚠️ 买单跳过 @ ${p}: {res.get('message', 'unknown')}"
@@ -254,6 +262,14 @@ def sync_grid(self, symbol: str, ai_config: dict[str, Any]):
254262
if oid:
255263
sell_orders.append({"oid": oid, "px": p})
256264
self.logger.print_info(f" [Grid] ✅ 卖单挂载: ${p}")
265+
self.logger.log_trade(
266+
symbol=symbol,
267+
action="GRID_SELL",
268+
amount=new_amount,
269+
price=p,
270+
order_id=str(oid),
271+
status="PLACED",
272+
)
257273
elif res and not res.get("success"):
258274
self.logger.print_warning(
259275
f" [Grid] ⚠️ 卖单跳过 @ ${p}: {res.get('message', 'unknown')}"
@@ -669,6 +685,14 @@ def _ensure_position_exit_orders(
669685
self.logger.print_warning(
670686
f" [Grid] 🛟 补减仓{side_name}单: {order_size:.6f} @ ${limit_price} (reduce_only)"
671687
)
688+
self.logger.log_trade(
689+
symbol=symbol,
690+
action="GRID_REDUCE_BUY" if close_with_buy else "GRID_REDUCE_SELL",
691+
amount=order_size,
692+
price=float(limit_price),
693+
order_id=str(oid) if oid is not None else "",
694+
status="PLACED",
695+
)
672696
else:
673697
self.logger.print_warning(
674698
f" [Grid] ⚠️ 减仓{side_name}单失败 @ ${limit_price}: {result}"

src/utils/logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def log_decision(
341341
action_details: dict[str, Any] = None,
342342
status: str = "SUCCESS",
343343
error_message: str = None,
344+
confidence: float = 0.0,
344345
):
345346
"""
346347
记录决策日志到文件
@@ -384,6 +385,7 @@ def log_decision(
384385
decision=decision,
385386
status=status,
386387
ai_response=ai_response or "",
388+
confidence=confidence,
387389
current_price=float(market_data.get("current_price", 0)),
388390
error_message=error_message or "",
389391
)

tests/test_grid_manager_exit_orders.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ def print_warning(self, *args, **kwargs):
2020
def print_error(self, *args, **kwargs):
2121
pass
2222

23+
def log_trade(self, *args, **kwargs):
24+
pass
25+
26+
def log_decision(self, *args, **kwargs):
27+
pass
28+
2329

2430
class FakeClient:
2531
def __init__(self):

0 commit comments

Comments
 (0)