feat(trades): 添加可对账记录筛选功能以确保与币安一致
在获取交易记录和统计时,新增 `reconciled_only` 参数,默认值为 true,确保仅返回可对账的交易记录(包含 entry_order_id 和 exit_order_id)。此改动有助于提高统计的准确性,确保系统盈亏与币安一致。
This commit is contained in:
parent
c7e39ec1a4
commit
225cb436d1
|
|
@ -80,6 +80,7 @@ async def get_trades(
|
|||
exit_reason: Optional[str] = Query(None, description="平仓原因筛选: 'stop_loss', 'take_profit', 'trailing_stop', 'manual', 'sync'"),
|
||||
status: Optional[str] = Query(None, description="状态筛选: 'open', 'closed', 'cancelled'"),
|
||||
include_sync: bool = Query(False, description="是否包含 entry_reason 为 sync_recovered 的历史同步单"),
|
||||
reconciled_only: bool = Query(True, description="仅返回可对账记录(有 entry_order_id,已平仓的还有 exit_order_id),与币安一致,统计真实"),
|
||||
limit: int = Query(100, ge=1, le=1000, description="返回记录数限制"),
|
||||
):
|
||||
"""
|
||||
|
|
@ -133,7 +134,22 @@ async def get_trades(
|
|||
if (t.get("entry_reason") or "") != "sync_recovered"
|
||||
and (t.get("exit_reason") or "") != "sync"
|
||||
]
|
||||
logger.info(f"查询到 {len(trades)} 条交易记录(include_sync={include_sync})")
|
||||
# 仅可对账:有开仓订单号,已平仓的还须有平仓订单号,保证与币安可一一对应、统计真实
|
||||
def _has_entry_order_id(t):
|
||||
eid = t.get("entry_order_id")
|
||||
return eid is not None and eid != "" and (eid != 0 if isinstance(eid, (int, float)) else True)
|
||||
def _has_exit_order_id(t):
|
||||
xid = t.get("exit_order_id")
|
||||
return xid is not None and xid != "" and (xid != 0 if isinstance(xid, (int, float)) else True)
|
||||
if reconciled_only:
|
||||
before = len(trades)
|
||||
trades = [
|
||||
t for t in trades
|
||||
if _has_entry_order_id(t)
|
||||
and (t.get("status") != "closed" or _has_exit_order_id(t))
|
||||
]
|
||||
logger.info(f"可对账过滤: {before} -> {len(trades)} 条(reconciled_only=True)")
|
||||
logger.info(f"查询到 {len(trades)} 条交易记录(include_sync={include_sync}, reconciled_only={reconciled_only})")
|
||||
|
||||
# 格式化交易记录,添加平仓类型的中文显示
|
||||
formatted_trades = []
|
||||
|
|
@ -173,7 +189,8 @@ async def get_trades(
|
|||
"end_date": datetime.fromtimestamp(end_timestamp).strftime('%Y-%m-%d %H:%M:%S') if end_timestamp else None,
|
||||
"period": period,
|
||||
"symbol": symbol,
|
||||
"status": status
|
||||
"status": status,
|
||||
"reconciled_only": reconciled_only,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -192,8 +209,9 @@ async def get_trade_stats(
|
|||
period: Optional[str] = Query(None, description="快速时间段筛选: '1d', '7d', '30d', 'today', 'week', 'month'"),
|
||||
symbol: Optional[str] = Query(None, description="交易对筛选"),
|
||||
include_sync: bool = Query(False, description="是否包含 entry_reason 为 sync_recovered 的历史同步单"),
|
||||
reconciled_only: bool = Query(True, description="仅统计可对账记录,与币安一致,避免系统盈利/币安亏损偏差"),
|
||||
):
|
||||
"""获取交易统计"""
|
||||
"""获取交易统计(默认仅统计可对账记录,保证与币安一致)"""
|
||||
try:
|
||||
logger.info(f"获取交易统计请求: start_date={start_date}, end_date={end_date}, period={period}, symbol={symbol}")
|
||||
|
||||
|
|
@ -235,6 +253,19 @@ async def get_trade_stats(
|
|||
if (t.get("entry_reason") or "") != "sync_recovered"
|
||||
and (t.get("exit_reason") or "") != "sync"
|
||||
]
|
||||
if reconciled_only:
|
||||
before = len(trades)
|
||||
def _has_eid(t):
|
||||
eid = t.get("entry_order_id")
|
||||
return eid is not None and eid != "" and (eid != 0 if isinstance(eid, (int, float)) else True)
|
||||
def _has_xid(t):
|
||||
xid = t.get("exit_order_id")
|
||||
return xid is not None and xid != "" and (xid != 0 if isinstance(xid, (int, float)) else True)
|
||||
trades = [
|
||||
t for t in trades
|
||||
if _has_eid(t) and (t.get("status") != "closed" or _has_xid(t))
|
||||
]
|
||||
logger.info(f"统计可对账过滤: {before} -> {len(trades)} 条(reconciled_only=True)")
|
||||
closed_trades = [t for t in trades if t['status'] == 'closed']
|
||||
|
||||
# 辅助函数:计算净盈亏(优先使用 realized_pnl - commission)
|
||||
|
|
@ -341,7 +372,8 @@ async def get_trade_stats(
|
|||
"start_date": datetime.fromtimestamp(start_timestamp).strftime('%Y-%m-%d %H:%M:%S') if start_timestamp else None,
|
||||
"end_date": datetime.fromtimestamp(end_timestamp).strftime('%Y-%m-%d %H:%M:%S') if end_timestamp else None,
|
||||
"period": period,
|
||||
"symbol": symbol
|
||||
"symbol": symbol,
|
||||
"reconciled_only": reconciled_only,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
59
docs/INDEX.md
Normal file
59
docs/INDEX.md
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
# 文档索引(优先阅读顺序,便于 AI/人快速定位)
|
||||
|
||||
## 一、先看这些(当前策略与使用)
|
||||
|
||||
| 文档 | 说明 |
|
||||
|------|------|
|
||||
| **当前策略方案总结_2026-02-15.md** | 当前策略与参数汇总,与盈利期对齐的配置与逻辑 |
|
||||
| **快速使用_盈利期对齐配置.md** | 如何一键应用「山寨币策略(推荐)」、全局配置 |
|
||||
| **risk_profile_2026-02-15.md** | 风险参数调整记录(止损/杠杆/移动止损等) |
|
||||
| **CURRENT_STRATEGY.md** | 策略概述、信号逻辑、时间段策略(偏概念) |
|
||||
| **CONFIG_GUIDE.md** | 配置项说明与推荐值 |
|
||||
|
||||
## 二、止盈/止损与风控
|
||||
|
||||
| 文档 | 说明 |
|
||||
|------|------|
|
||||
| **止损止盈双通道说明.md** | 交易所条件单 + 本机 WebSocket 双通道,移动止损同步 |
|
||||
| **止损止盈计算说明.md** | 止损/止盈价格计算方式 |
|
||||
| **止盈止损与盈利优化_2026-02-15.md** | 止盈 28%~30%、止损约 10% 等建议 |
|
||||
| **STOP_LOSS_TAKE_PROFIT_EXPLANATION.md** | 英文版止盈止损说明 |
|
||||
| **盈利提升方案.md** | 仓位数量、单笔风险、移动止损等优化建议 |
|
||||
| **订单与统计一致性说明.md** | 订单与 DB 对账、统计口径 |
|
||||
|
||||
## 三、近期分析(可选)
|
||||
|
||||
| 文档 | 说明 |
|
||||
|------|------|
|
||||
| **交易对比分析_2026-02-14_盈利期vs亏损期.md** | 10–11 号盈利 vs 亏损期原因(RSI 反向等) |
|
||||
| **持仓分析_2026-02-15_与参数建议.md** | 当时持仓 SL/TP 与预设调整 |
|
||||
| **交易表现分析_20260214.md** / **交易分析_2026-02-14_策略执行与优化建议.md** | 2 月 14 日表现与执行建议 |
|
||||
|
||||
## 四、架构与运维
|
||||
|
||||
| 文档 | 说明 |
|
||||
|------|------|
|
||||
| **README.md** | 项目说明、快速开始 |
|
||||
| **README_ARCHITECTURE.md** | 架构与模块说明 |
|
||||
| **QUICK_START.md** | 快速启动步骤 |
|
||||
| **INSTALL.md** | 安装与依赖 |
|
||||
| **DEPLOYMENT.md** | 部署说明 |
|
||||
| **STRUCTURE.md** | 目录与代码结构 |
|
||||
| **PROJECT_SUMMARY.md** | 项目摘要 |
|
||||
| **全局配置与数据库同步.md** | 全局配置表与同步方式 |
|
||||
| **MULTI_USER_ARCHITECTURE.md** | 多账号/多用户架构 |
|
||||
| **SUPERVISOR_TROUBLESHOOTING.md** | Supervisor 排查 |
|
||||
| **API_KEY_SETUP.md** | API 密钥配置 |
|
||||
| **ENTRY_CONTEXT_入场思路记录.md** | 入场思路 JSON 字段说明 |
|
||||
|
||||
## 五、策略与 ATR
|
||||
|
||||
| 文档 | 说明 |
|
||||
|------|------|
|
||||
| **ATR.md** | ATR 指标与用法 |
|
||||
| **ATR_STRATEGY_IMPLEMENTATION.md** | ATR 策略实现 |
|
||||
| **山寨币策略快速应用完整指南.md** | 山寨币预设使用指南 |
|
||||
|
||||
---
|
||||
|
||||
**archive/** 目录下为历史分析、一次性修复总结、旧方案(2026-01 居多),需要查历史再进 archive;日常整理与问答以本索引优先。
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
基于币安API的Python自动交易系统,实现自动发现涨跌幅最大的货币对并执行交易策略。
|
||||
|
||||
**文档导航**:详细文档列表与优先阅读顺序见 [INDEX.md](INDEX.md)。历史/一次性分析已移至 [archive/](archive/)。
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
|
|
|
|||
20
docs/archive/README.md
Normal file
20
docs/archive/README.md
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# 归档文档说明
|
||||
|
||||
本目录为 **历史/一次性** 文档,已从 `docs/` 根目录移入,避免日常阅读与 AI 整理时干扰。
|
||||
|
||||
## 归档内容概览
|
||||
|
||||
- **按日期的一次性分析**:如 2026-01-23 ~ 2026-02-04 的交易分析、亏损分析、策略执行分析等
|
||||
- **已完成实施的总结**:如「配置优化实施完成总结」「分步止盈状态细分实施完成总结」「ATR 配置优化完成总结」等
|
||||
- **多版本文案只保留最终版后**:如「配置值格式统一」多个版本、「分步止盈」多篇分析
|
||||
- **单次修复/单币种分析**:如止损失效修复、某币种止损价错误分析、WebSocket/Redis 修复说明等
|
||||
- **旧计划与建议**:如 newplan20260115、策略优化建议评估与实施方案等
|
||||
|
||||
## 使用方式
|
||||
|
||||
- 需要查 **当时为什么这样改** 或 **某次问题结论** 时,可在此目录按文件名或日期查找
|
||||
- 当前策略与配置以 **docs/当前策略方案总结_2026-02-15.md** 和 **docs/INDEX.md** 为准
|
||||
|
||||
## 归档时间
|
||||
|
||||
2026-02-15
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user