[2026-05-18] 포지션 DB 동기화 + pnl 계산 수정

- order_executor: _update_trade_exit에 pnl 계산 저장 추가
- main: 매수 시 positions DB INSERT, 매도 시 DELETE
- main: 재시작 시 DB에서 positions 복원 (_restore_positions_from_db)
This commit is contained in:
2026-05-18 13:32:43 +09:00
parent a3832dd5a8
commit bf041e4d18
2 changed files with 67 additions and 8 deletions
+13 -7
View File
@@ -88,15 +88,21 @@ class OrderExecutor:
def _update_trade_exit(self, ticker, exit_price,
qty, reason, fee):
with get_conn() as conn:
row = conn.execute("""
SELECT id, entry_price, quantity FROM trades
WHERE ticker=? AND exit_time IS NULL
ORDER BY id DESC LIMIT 1
""", (ticker,)).fetchone()
if not row:
return
trade_id, entry_price, trade_qty = row
actual_qty = qty if qty else trade_qty
pnl = (exit_price - entry_price) * actual_qty - fee
conn.execute("""
UPDATE trades
SET exit_time=?, exit_price=?, exit_reason=?, fee=fee+?
WHERE id = (
SELECT id FROM trades
WHERE ticker=? AND exit_time IS NULL
ORDER BY id DESC LIMIT 1
)
SET exit_time=?, exit_price=?, exit_reason=?, fee=fee+?, pnl=?
WHERE id=?
""", (
datetime.now().strftime("%H:%M:%S"),
exit_price, reason, fee, ticker,
exit_price, reason, fee, pnl, trade_id,
))