This commit is contained in:
aaron 2026-02-23 00:29:30 +08:00
parent 3c8d422b85
commit 1de64df2dc
3 changed files with 134 additions and 84 deletions

View File

@ -24,22 +24,20 @@ class OrderResponse(BaseModel):
@router.get("/orders")
async def get_orders(
symbol: Optional[str] = Query(None, description="交易对筛选"),
status: Optional[str] = Query(None, description="状态筛选: active, closed, exchange"),
status: Optional[str] = Query(None, description="数据源: trades=成交记录, orders=历史订单, exchange=历史订单"),
limit: int = Query(100, description="返回数量限制")
):
"""
获取实盘订单列表
获取实盘交易历史数据
- symbol: 可选按交易对筛选
- status: 可选
- active: 本地数据库的活跃订单
- closed: 本地数据库的历史订单
- exchange: 交易所的历史订单推荐
- trades: 交易所成交记录包含每笔成交和手续费
- orders: 交易所历史订单包含订单状态
- exchange: 交易所历史订单 orders
- limit: 返回数量限制默认100
"""
try:
# 如果请求交易所历史订单,直接从交易所获取
if status == "exchange":
trading_api = get_bitget_trading_api()
if not trading_api:
@ -50,62 +48,51 @@ async def get_orders(
"orders": []
}
# 获取成交记录(推荐,包含盈亏信息)
if status == "trades":
orders = trading_api.get_closed_orders(symbol, limit)
return {
"success": True,
"count": len(orders),
"orders": orders,
"source": "trades"
}
# 获取历史订单
if status in ["orders", "exchange"]:
try:
if symbol:
ccxt_symbol = trading_api._standardize_symbol(symbol)
orders = trading_api.exchange.fetch_closed_orders(ccxt_symbol, limit=limit)
else:
orders = trading_api.exchange.fetch_closed_orders(limit=limit)
return {
"success": True,
"count": len(orders),
"orders": orders,
"source": "exchange"
"source": "orders"
}
# 否则从本地数据库获取
service = get_real_trading_service()
if not service:
except Exception as e:
logger.error(f"获取历史订单失败: {e}")
return {
"success": False,
"message": "实盘交易服务未启用",
"message": f"获取历史订单失败: {str(e)}",
"count": 0,
"orders": []
}
if status == "active":
orders = service.get_active_orders()
elif status == "closed":
# 从数据库获取历史订单
from app.services.db_service import db_service
from app.models.real_trading import RealOrder
from app.models.paper_trading import OrderStatus
db = db_service.get_session()
try:
query = db.query(RealOrder).filter(
RealOrder.status.in_([OrderStatus.CLOSED, OrderStatus.CANCELLED])
)
if symbol:
query = query.filter(RealOrder.symbol == symbol)
orders = [order.to_dict() for order in query.order_by(
RealOrder.created_at.desc()
).limit(limit).all()]
finally:
db.close()
else:
# 返回所有订单
active = service.get_active_orders()
# TODO: 获取历史订单
orders = active
# 默认返回成交记录
orders = trading_api.get_closed_orders(symbol, limit)
return {
"success": True,
"count": len(orders),
"orders": orders,
"source": "database"
"source": "trades"
}
except Exception as e:
logger.error(f"获取实盘订单列表失败: {e}")
logger.error(f"获取实盘交易历史失败: {e}")
raise HTTPException(status_code=500, detail=str(e))

View File

@ -376,21 +376,21 @@ class BitgetTradingAPI:
历史订单列表
"""
try:
# 使用 CCXT 的 fetch_closed_orders 或 fetch_my_trades
# 使用 CCXT 的 fetch_my_trades 获取历史成交记录(包含盈亏信息)
if symbol:
ccxt_symbol = self._standardize_symbol(symbol)
orders = self.exchange.fetch_closed_orders(ccxt_symbol, limit=limit)
trades = self.exchange.fetch_my_trades(ccxt_symbol, limit=limit)
else:
orders = self.exchange.fetch_closed_orders(limit=limit)
trades = self.exchange.fetch_my_trades(limit=limit)
logger.debug(f"查询到 {len(orders)} 条历史订单")
return orders
logger.debug(f"查询到 {len(trades)} 条历史成交记录")
return trades
except ccxt.BaseError as e:
logger.error(f"❌ 查询历史订单失败: {e}")
logger.error(f"❌ 查询历史成交失败: {e}")
return []
except Exception as e:
logger.error(f"❌ 查询历史订单异常: {e}")
logger.error(f"❌ 查询历史成交异常: {e}")
return []
# ==================== 账户操作 ====================

View File

@ -462,8 +462,14 @@
</button>
<button
class="tab"
:class="{ active: currentTab === 'history' }"
@click="currentTab = 'history'">
:class="{ active: currentTab === 'trades' }"
@click="currentTab = 'trades'">
成交记录
</button>
<button
class="tab"
:class="{ active: currentTab === 'orders' }"
@click="currentTab = 'orders'">
历史订单
</button>
</div>
@ -482,7 +488,14 @@
<p>暂无持仓</p>
</div>
<div v-else-if="currentTab === 'history' && historyOrders.length === 0" class="empty-state">
<div v-else-if="currentTab === 'trades' && tradeHistory.length === 0" class="empty-state">
<svg fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<p>暂无成交记录</p>
</div>
<div v-else-if="currentTab === 'orders' && orderHistory.length === 0" class="empty-state">
<svg fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
@ -503,14 +516,23 @@
<th>盈亏比例</th>
<th>强平价格</th>
</tr>
<tr v-else-if="currentTab === 'trades'">
<th>交易对</th>
<th>方向</th>
<th>价格</th>
<th>数量</th>
<th>成交金额</th>
<th>手续费</th>
<th>盈亏</th>
<th>时间</th>
</tr>
<tr v-else>
<th>交易对</th>
<th>方向</th>
<th>类型</th>
<th>价格</th>
<th>数量</th>
<th>成交金额</th>
<th>手续费</th>
<th>成交数量</th>
<th>状态</th>
<th>时间</th>
</tr>
@ -539,9 +561,35 @@
<td>${{ pos.liquidationPrice ? parseFloat(pos.liquidationPrice).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}) : '-' }}</td>
</tr>
</template>
<!-- 订单表格 -->
<template v-else>
<tr v-for="order in displayOrders" :key="order.id || order.order_id">
<!-- 成交记录表格 -->
<template v-if="currentTab === 'trades'">
<tr v-for="trade in displayOrders" :key="trade.id">
<td><strong>{{ formatSymbol(trade.symbol) }}</strong></td>
<td>
<span class="side-badge" :class="trade.side === 'buy' ? 'side-long' : 'side-short'">
{{ trade.side === 'buy' ? '买入' : '卖出' }}
</span>
</td>
<td>${{ trade.price ? parseFloat(trade.price).toLocaleString() : '-' }}</td>
<td>{{ trade.amount || trade.filled || '0' }}</td>
<td>${{ trade.cost ? parseFloat(trade.cost).toFixed(2) : '-' }}</td>
<td>
<span :class="getFeeClass(trade)">
{{ trade.fee && parseFloat(trade.fee) !== 0 ? (trade.fee > 0 ? '+' : '') + parseFloat(trade.fee).toFixed(4) : '-' }}
</span>
</td>
<td>
<span v-if="trade.return !== undefined" :class="parseFloat(trade.return || 0) >= 0 ? 'pnl-positive' : 'pnl-negative'">
{{ parseFloat(trade.return || 0) >= 0 ? '+' : '' }}${{ parseFloat(trade.return || 0).toFixed(2) }}
</span>
<span v-else>-</span>
</td>
<td>{{ formatTime(trade.datetime || trade.timestamp) }}</td>
</tr>
</template>
<!-- 历史订单表格 -->
<template v-else-if="currentTab === 'orders'">
<tr v-for="order in displayOrders" :key="order.id">
<td><strong>{{ formatSymbol(order.symbol) }}</strong></td>
<td>
<span class="side-badge" :class="order.side === 'buy' ? 'side-long' : 'side-short'">
@ -550,13 +598,8 @@
</td>
<td>{{ order.type || 'market' }}</td>
<td>${{ order.price ? parseFloat(order.price).toLocaleString() : '市价' }}</td>
<td>{{ order.amount || order.filled || '0' }}</td>
<td>${{ order.cost ? parseFloat(order.cost).toFixed(2) : '-' }}</td>
<td>
<span :class="getFeeClass(order)">
{{ order.fee && parseFloat(order.fee) !== 0 ? (order.fee > 0 ? '+' : '') + parseFloat(order.fee).toFixed(4) : '-' }}
</span>
</td>
<td>{{ order.amount || '0' }}</td>
<td>{{ order.filled || '0' }}</td>
<td>
<span class="status-badge" :class="'status-' + order.status">
{{ formatOrderStatus(order.status) }}
@ -592,14 +635,16 @@
total_position_value: 0
},
stats: null,
historyOrders: [],
tradeHistory: [],
orderHistory: [],
exchangePositions: [],
autoRefreshInterval: null
};
},
computed: {
displayOrders() {
if (this.currentTab === 'history') return this.historyOrders;
if (this.currentTab === 'trades') return this.tradeHistory;
if (this.currentTab === 'orders') return this.orderHistory;
if (this.currentTab === 'positions') return this.exchangePositions;
return [];
}
@ -612,9 +657,15 @@
this.fetchServiceStatus(),
this.fetchAccountStatus(),
this.fetchStats(),
this.fetchHistoryOrders(),
this.fetchExchangePositions()
]);
// 根据当前标签页获取对应数据
if (this.currentTab === 'trades') {
await this.fetchTradeHistory();
} else if (this.currentTab === 'orders') {
await this.fetchOrderHistory();
}
} finally {
this.loading = false;
}
@ -659,12 +710,24 @@
}
},
async fetchHistoryOrders() {
async fetchTradeHistory() {
try {
// 从交易所获取历史订单
const response = await axios.get('/api/real-trading/orders?status=exchange&limit=50');
// 获取成交记录(包含盈亏)
const response = await axios.get('/api/real-trading/orders?status=trades&limit=100');
if (response.data.success) {
this.historyOrders = response.data.orders;
this.tradeHistory = response.data.orders;
}
} catch (error) {
console.error('获取成交记录失败:', error);
}
},
async fetchOrderHistory() {
try {
// 获取历史订单
const response = await axios.get('/api/real-trading/orders?status=orders&limit=50');
if (response.data.success) {
this.orderHistory = response.data.orders;
}
} catch (error) {
console.error('获取历史订单失败:', error);