Fix float PnL to match displayed entry price and contract multiplier.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+15
-10
@@ -990,14 +990,16 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
lots = int(p.get("lots") or 0)
|
lots = int(p.get("lots") or 0)
|
||||||
entry = float(p.get("avg_price") or 0)
|
entry = float(p.get("avg_price") or 0)
|
||||||
ctp_margin = float(p.get("margin") or 0)
|
ctp_margin = float(p.get("margin") or 0)
|
||||||
float_pnl = p.get("pnl")
|
|
||||||
if float_pnl is not None:
|
|
||||||
float_pnl = round(float(float_pnl), 2)
|
|
||||||
mark = None
|
mark = None
|
||||||
if ctp_status(mode).get("connected"):
|
if ctp_status(mode).get("connected"):
|
||||||
mark = ctp_get_tick_price(mode, sym)
|
mark = ctp_get_tick_price(mode, sym)
|
||||||
if mark is None or mark <= 0:
|
if mark is None or mark <= 0:
|
||||||
mark = entry if entry else None
|
mark = entry if entry else None
|
||||||
|
float_pnl = None
|
||||||
|
if mark and entry and lots > 0:
|
||||||
|
float_pnl = calc_position_metrics(
|
||||||
|
direction, entry, entry, entry, lots, mark, capital, sym,
|
||||||
|
).get("float_pnl")
|
||||||
est = calc_position_metrics(
|
est = calc_position_metrics(
|
||||||
direction, entry, entry, entry, lots, mark or entry, capital, sym,
|
direction, entry, entry, entry, lots, mark or entry, capital, sym,
|
||||||
).get("margin")
|
).get("margin")
|
||||||
@@ -1085,16 +1087,12 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
margin = None
|
margin = None
|
||||||
position_pct = None
|
position_pct = None
|
||||||
mark = None
|
mark = None
|
||||||
float_pnl = ctp.get("pnl")
|
float_pnl = None
|
||||||
if float_pnl is not None:
|
|
||||||
float_pnl = round(float(float_pnl), 2)
|
|
||||||
|
|
||||||
if lots <= 0:
|
if lots <= 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if ctp:
|
if ctp:
|
||||||
if ctp.get("pnl") is not None:
|
|
||||||
float_pnl = round(float(ctp["pnl"]), 2)
|
|
||||||
if not mon:
|
if not mon:
|
||||||
ctp_lots = int(ctp.get("lots") or 0)
|
ctp_lots = int(ctp.get("lots") or 0)
|
||||||
if ctp_lots > 0:
|
if ctp_lots > 0:
|
||||||
@@ -1124,7 +1122,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
if mark is None or mark <= 0:
|
if mark is None or mark <= 0:
|
||||||
mark = entry if entry else None
|
mark = entry if entry else None
|
||||||
close_est = float(mark) if mark and mark > 0 else entry
|
close_est = float(mark) if mark and mark > 0 else entry
|
||||||
if float_pnl is None and mark and entry:
|
if mark and entry and lots > 0:
|
||||||
pos_tmp = calc_position_metrics(
|
pos_tmp = calc_position_metrics(
|
||||||
direction, entry, sl or entry, tp or entry, lots, mark, capital, sym,
|
direction, entry, sl or entry, tp or entry, lots, mark, capital, sym,
|
||||||
)
|
)
|
||||||
@@ -1676,6 +1674,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
if not positions:
|
if not positions:
|
||||||
positions = _ctp_positions(mode, refresh_if_empty=False)
|
positions = _ctp_positions(mode, refresh_if_empty=False)
|
||||||
quotes: list[dict] = []
|
quotes: list[dict] = []
|
||||||
|
conn = get_db()
|
||||||
|
try:
|
||||||
for p in positions:
|
for p in positions:
|
||||||
lots = int(p.get("lots") or 0)
|
lots = int(p.get("lots") or 0)
|
||||||
if lots <= 0:
|
if lots <= 0:
|
||||||
@@ -1683,10 +1683,13 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
ths = _ctp_pos_to_ths_code(p) or (p.get("symbol") or "")
|
ths = _ctp_pos_to_ths_code(p) or (p.get("symbol") or "")
|
||||||
if not ths:
|
if not ths:
|
||||||
continue
|
continue
|
||||||
|
direction = (p.get("direction") or "long").strip().lower()
|
||||||
entry = float(p.get("avg_price") or 0)
|
entry = float(p.get("avg_price") or 0)
|
||||||
|
mon = _find_active_monitor(conn, ths, direction)
|
||||||
|
if mon and float(mon.get("entry_price") or 0) > 0:
|
||||||
|
entry = float(mon["entry_price"])
|
||||||
if entry <= 0:
|
if entry <= 0:
|
||||||
continue
|
continue
|
||||||
direction = (p.get("direction") or "long").strip().lower()
|
|
||||||
mark = ctp_get_tick_price(mode, ths)
|
mark = ctp_get_tick_price(mode, ths)
|
||||||
if not mark or mark <= 0:
|
if not mark or mark <= 0:
|
||||||
continue
|
continue
|
||||||
@@ -1705,6 +1708,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
"current_price": mark,
|
"current_price": mark,
|
||||||
"float_pnl": float_pnl,
|
"float_pnl": float_pnl,
|
||||||
})
|
})
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
return {"ok": True, "quotes": quotes}
|
return {"ok": True, "quotes": quotes}
|
||||||
|
|
||||||
def _push_position_quotes_async() -> None:
|
def _push_position_quotes_async() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user