统计已实现盈亏扣手续费,并按平仓时指数冻结为 U。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+48
-9
@@ -359,16 +359,30 @@ class SimBroker:
|
||||
side: str,
|
||||
sheets: float,
|
||||
avg_px: float,
|
||||
fee: float = 0.0,
|
||||
fee_ccy: str | None = None,
|
||||
) -> None:
|
||||
conn = self.get_db()
|
||||
try:
|
||||
ccy = str(fee_ccy or _premium_ccy_for_inst(inst_id) or "").strip().upper()
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO sim_option_orders(
|
||||
ord_id, inst_id, side, sheets, avg_px, state, acc_fill_sz, created_at
|
||||
) VALUES (?,?,?,?,?,'filled',?,?)
|
||||
ord_id, inst_id, side, sheets, avg_px, state, acc_fill_sz,
|
||||
fee, fee_ccy, created_at
|
||||
) VALUES (?,?,?,?,?,'filled',?,?,?,?)
|
||||
""",
|
||||
(ord_id, inst_id, side, float(sheets), float(avg_px), float(sheets), _now()),
|
||||
(
|
||||
ord_id,
|
||||
inst_id,
|
||||
side,
|
||||
float(sheets),
|
||||
float(avg_px),
|
||||
float(sheets),
|
||||
float(fee or 0),
|
||||
ccy,
|
||||
_now(),
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
@@ -383,6 +397,11 @@ class SimBroker:
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
keys = set(row.keys()) if hasattr(row, "keys") else set()
|
||||
fee = float(row["fee"]) if "fee" in keys and row["fee"] is not None else 0.0
|
||||
fee_ccy = str(row["fee_ccy"] or "").strip().upper() if "fee_ccy" in keys else ""
|
||||
if not fee_ccy:
|
||||
fee_ccy = _premium_ccy_for_inst(row["inst_id"])
|
||||
return {
|
||||
"ok": True,
|
||||
"ord_id": row["ord_id"],
|
||||
@@ -391,7 +410,10 @@ class SimBroker:
|
||||
"state": row["state"],
|
||||
"acc_fill_sz": float(row["acc_fill_sz"]),
|
||||
"avg_px": float(row["avg_px"]),
|
||||
"fee": -abs(fee) if fee else 0.0,
|
||||
"fee_ccy": fee_ccy,
|
||||
"sim": True,
|
||||
"raw": {"fee": -abs(fee) if fee else 0.0, "feeCcy": fee_ccy, "sim": True},
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -469,15 +491,23 @@ class SimBroker:
|
||||
|
||||
ord_id = f"sim-opt-{uuid.uuid4().hex[:16]}"
|
||||
self._store_option_order(
|
||||
ord_id=ord_id, inst_id=inst_id, side="buy", sheets=n, avg_px=pr.fill_px
|
||||
ord_id=ord_id,
|
||||
inst_id=inst_id,
|
||||
side="buy",
|
||||
sheets=n,
|
||||
avg_px=pr.fill_px,
|
||||
fee=float(pr.fee or 0),
|
||||
fee_ccy=prem_ccy,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"data": {"ordId": ord_id, "sCode": "0", "sMsg": "sim filled"},
|
||||
"raw": {"sim": True},
|
||||
"raw": {"sim": True, "fee": pr.fee, "feeCcy": prem_ccy},
|
||||
"px": pr.fill_px,
|
||||
"ord_type": "ioc",
|
||||
"info": {"sim": True, "fee": pr.fee, "fill": pr.to_dict()},
|
||||
"fee": float(pr.fee or 0),
|
||||
"fee_ccy": prem_ccy,
|
||||
"info": {"sim": True, "fee": pr.fee, "fee_ccy": prem_ccy, "fill": pr.to_dict()},
|
||||
}
|
||||
|
||||
def sell_option_close(
|
||||
@@ -540,16 +570,25 @@ class SimBroker:
|
||||
note=f"sell {inst_id} x{close_n}@{pr.fill_px}",
|
||||
)
|
||||
ord_id = f"sim-opt-close-{uuid.uuid4().hex[:16]}"
|
||||
prem_ccy = _premium_ccy_for_inst(inst_id)
|
||||
self._store_option_order(
|
||||
ord_id=ord_id, inst_id=inst_id, side="sell", sheets=close_n, avg_px=pr.fill_px
|
||||
ord_id=ord_id,
|
||||
inst_id=inst_id,
|
||||
side="sell",
|
||||
sheets=close_n,
|
||||
avg_px=pr.fill_px,
|
||||
fee=float(pr.fee or 0),
|
||||
fee_ccy=prem_ccy,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"data": {"ordId": ord_id, "sCode": "0", "sMsg": "sim filled"},
|
||||
"raw": {"sim": True},
|
||||
"raw": {"sim": True, "fee": pr.fee, "feeCcy": prem_ccy},
|
||||
"px": pr.fill_px,
|
||||
"ord_type": "ioc",
|
||||
"info": {"sim": True, "fee": pr.fee, "fill": pr.to_dict()},
|
||||
"fee": float(pr.fee or 0),
|
||||
"fee_ccy": prem_ccy,
|
||||
"info": {"sim": True, "fee": pr.fee, "fee_ccy": prem_ccy, "fill": pr.to_dict()},
|
||||
}
|
||||
|
||||
def convert_usdt_usdc(
|
||||
|
||||
@@ -103,6 +103,14 @@ def init_sim_tables(conn: sqlite3.Connection) -> None:
|
||||
)
|
||||
"""
|
||||
)
|
||||
try:
|
||||
conn.execute("ALTER TABLE sim_option_orders ADD COLUMN fee REAL DEFAULT 0")
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
conn.execute("ALTER TABLE sim_option_orders ADD COLUMN fee_ccy TEXT")
|
||||
except Exception:
|
||||
pass
|
||||
row = conn.execute("SELECT id FROM sim_wallets WHERE id=1").fetchone()
|
||||
if row is None:
|
||||
equity = max(0.0, _env_float("SIM_INITIAL_EQUITY_USDT", 10000.0))
|
||||
|
||||
Reference in New Issue
Block a user