fix: persist positions to DB before goroutine so restarts don't lose open trades

This commit is contained in:
jackyu66git
2026-05-04 20:16:44 +08:00
parent 411365b66e
commit 06a7e37836
2 changed files with 95 additions and 51 deletions
+8 -2
View File
@@ -105,14 +105,20 @@ func (d *DB) UpdateTradeStatus(id int64, t *TradeRecord) error {
return err
}
// GetOpenTrades returns all trades with status='open'.
// SetTradeStatus updates only the status field of a trade.
func (d *DB) SetTradeStatus(id int64, status string) error {
_, err := d.Exec("UPDATE trades SET status=? WHERE id=?", status, id)
return err
}
// GetOpenTrades returns all non-closed trades (status='open' or status='entering').
func (d *DB) GetOpenTrades() ([]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,
pnl_long_usd, pnl_short_usd, fee_long_usd, fee_short_usd
FROM trades WHERE status='open'`)
FROM trades WHERE status IN ('open','entering')`)
if err != nil {
return nil, err
}