fix: use actual fill price from exchange API for position records

- Remove trader_orders table and OrderSyncManager (never worked correctly)
- Poll GetOrderStatus to get actual avgPrice, executedQty, and commission
- Get entry price from exchange GetPositions API when closing positions
- Pass fee to trader_positions table on close
- Move TraderStats type to position.go
This commit is contained in:
tinkle-community
2025-12-08 12:15:41 +08:00
parent f39fc8af23
commit 8a5744e0a0
9 changed files with 135 additions and 904 deletions

View File

@@ -287,16 +287,15 @@ func (s *DecisionStore) GetStatistics(traderID string) (*Statistics, error) {
}
stats.FailedCycles = stats.TotalCycles - stats.SuccessfulCycles
// Count open positions from trader_orders table
// Count from trader_positions table
s.db.QueryRow(`
SELECT COUNT(*) FROM trader_orders
WHERE trader_id = ? AND status = 'FILLED' AND action IN ('open_long', 'open_short')
SELECT COUNT(*) FROM trader_positions
WHERE trader_id = ?
`, traderID).Scan(&stats.TotalOpenPositions)
// Count close positions from trader_orders table
s.db.QueryRow(`
SELECT COUNT(*) FROM trader_orders
WHERE trader_id = ? AND status = 'FILLED' AND action IN ('close_long', 'close_short', 'auto_close_long', 'auto_close_short')
SELECT COUNT(*) FROM trader_positions
WHERE trader_id = ? AND status = 'CLOSED'
`, traderID).Scan(&stats.TotalClosePositions)
return stats, nil
@@ -310,15 +309,14 @@ func (s *DecisionStore) GetAllStatistics() (*Statistics, error) {
s.db.QueryRow(`SELECT COUNT(*) FROM decision_records WHERE success = 1`).Scan(&stats.SuccessfulCycles)
stats.FailedCycles = stats.TotalCycles - stats.SuccessfulCycles
// Count from trader_orders table
// Count from trader_positions table
s.db.QueryRow(`
SELECT COUNT(*) FROM trader_orders
WHERE status = 'FILLED' AND action IN ('open_long', 'open_short')
SELECT COUNT(*) FROM trader_positions
`).Scan(&stats.TotalOpenPositions)
s.db.QueryRow(`
SELECT COUNT(*) FROM trader_orders
WHERE status = 'FILLED' AND action IN ('close_long', 'close_short', 'auto_close_long', 'auto_close_short')
SELECT COUNT(*) FROM trader_positions
WHERE status = 'CLOSED'
`).Scan(&stats.TotalClosePositions)
return stats, nil