fix: 重启后总PnL回到0的问题 + 前端PnL图表公式统一
- trader.go: NewTrader 启动时从DB GetAllClosedTrades() 加载所有已平仓交易到 closedTrades - trader.go: 新增 safeFloat/safeStr 辅助函数处理DB空指针 - db/trade_repo.go: 新增 GetAllClosedTrades() 方法 - frontend: PnL图表标题和曲线改用 2*AmountUSD*NetPnl/100 公式,与后端Stats一致
This commit is contained in:
@@ -260,6 +260,20 @@ func (d *DB) GetScalePrices(tradeID int64) (longPrices, shortPrices []float64, e
|
||||
return longPrices, shortPrices, rows.Err()
|
||||
}
|
||||
|
||||
// GetAllClosedTrades returns all closed trades for PnL history restoration.
|
||||
func (d *DB) GetAllClosedTrades() ([]TradeRecord, error) {
|
||||
rows, err := d.Query(`SELECT id, coin, direction, status, entry_spread, exit_spread,
|
||||
long_exchange, short_exchange, long_entry, long_exit, short_entry, short_exit,
|
||||
long_pnl, short_pnl, fee_entry, fee_exit, net_pnl,
|
||||
amount_usd, scale_count, exit_reason, convergence, opened_at, closed_at
|
||||
FROM trades WHERE status='closed' ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanTrades(rows)
|
||||
}
|
||||
|
||||
// GetClosedStats returns convergence counts from the database.
|
||||
func (d *DB) GetClosedStats() (converged, diverged, flat, total int, err error) {
|
||||
if err = d.QueryRow("SELECT COUNT(*) FROM trades WHERE status='closed'").Scan(&total); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user