bifitnex-trading/memory/project_cost_basis_sync.md
kroutony 84093929d1 Backup Claude memory files to project repo
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 11:23:22 +00:00

30 lines
1.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
name: cost_basis_sync
description: sync_cost_basis.py architecture — order history cost calculation, wallet sync, Bitfinex API quirks
type: project
---
## sync_cost_basis.py每30分鐘 cron :02, :32
獨立腳本,從 Bitfinex 訂單歷史重算 entry_price同時從錢包同步 amount 和 USDT 餘額。
**職責分離:**
- `sync_cost_basis.py`:撈訂單 + 錢包,更新 entry_price / amount / 餘額
- `main.py`每5分鐘 :01, :06...):只撈錢包同步餘額,不重算成本
**Why:** `update_position()` 本地計算的 amount 用 `amount_usdt / price`,跟交易所實際成交量有差異,累積後總值偏差可達 10%+。
## Bitfinex 訂單 API 注意事項
1. **SELL 訂單 status 不一定是 "EXECUTED"**:可能是 `"INSUFFICIENT BALANCE (U1) was: PARTIALLY FILLED @ ..."`。必須同時檢查 `"EXECUTED"``"PARTIALLY FILLED"`
2. **排序不可靠**`/v2/auth/r/orders/{symbol}/hist` 的 `sort` 參數不保證正確排序。必須在 Python 端按 `mts_create`index 4手動排序。
3. **實際成交量 = amount_orig - amount_remaining**`amount_orig`index 7是原始下單量`amount`index 6是剩餘量。不能直接用 `amount_orig`
4. **nonce: small 錯誤**API 請求間隔太短會觸發。幣種間至少 `time.sleep(1)`
5. **Bitfinex 不收手續費**:計算出的 amount 應與錢包完全一致0% deviation
## cost_tracking.json
記錄每個幣種的訂單撈取起始時間ms。完全賣出設為 null重新買入時更新為新的第一筆 BUY 時間。