Align hedge-plan perp contract precision with exchange and show TP/SL USDT PnL.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -69,6 +69,23 @@ def suggest_contracts_from_notional(
|
||||
return float(notional) / (float(entry) * float(contract_size))
|
||||
|
||||
|
||||
def floor_contracts_to_precision(contracts: float, decimals: int) -> float:
|
||||
"""按交易所张数精度向下取整,避免建议张数超过可用保证金."""
|
||||
import math
|
||||
|
||||
raw = float(contracts or 0.0)
|
||||
if raw <= 0:
|
||||
return 0.0
|
||||
try:
|
||||
d = int(decimals)
|
||||
except (TypeError, ValueError):
|
||||
d = 0
|
||||
if d <= 0:
|
||||
return float(math.floor(raw + 1e-12))
|
||||
scale = 10**d
|
||||
return math.floor(raw * scale + 1e-12) / scale
|
||||
|
||||
|
||||
def build_perp_options_preview(
|
||||
*,
|
||||
direction: str,
|
||||
|
||||
@@ -10,10 +10,12 @@ from jinja2 import ChoiceLoader, FileSystemLoader
|
||||
from lib.hedge_plan.hedge_plan_calc_lib import (
|
||||
build_options_options_preview,
|
||||
build_perp_options_preview,
|
||||
floor_contracts_to_precision,
|
||||
gate_status,
|
||||
option_premium_total,
|
||||
suggest_contracts_from_notional,
|
||||
)
|
||||
from lib.hub.hub_calculator_market_lib import amount_decimals_from_exchange
|
||||
from lib.trade.position_sizing_lib import (
|
||||
compute_full_margin_sizing,
|
||||
load_position_sizing_mode,
|
||||
@@ -305,6 +307,11 @@ def _fetch_perp_market(cfg: dict[str, Any], base: str) -> tuple[dict[str, Any],
|
||||
entry = float(mark or last or 0)
|
||||
sizing = None
|
||||
suggest_contracts = None
|
||||
amount_precision = 4
|
||||
try:
|
||||
amount_precision = int(amount_decimals_from_exchange(ex, sym))
|
||||
except Exception:
|
||||
amount_precision = 4
|
||||
if available is not None and entry > 0:
|
||||
sizing, _serr = compute_full_margin_sizing(
|
||||
symbol=sym,
|
||||
@@ -316,11 +323,20 @@ def _fetch_perp_market(cfg: dict[str, Any], base: str) -> tuple[dict[str, Any],
|
||||
funds_decimals=int(cfg.get("funds_decimals") or 2),
|
||||
)
|
||||
if sizing:
|
||||
suggest_contracts = suggest_contracts_from_notional(
|
||||
raw_contracts = suggest_contracts_from_notional(
|
||||
notional=float(sizing["notional_value"]),
|
||||
entry=entry,
|
||||
contract_size=cs,
|
||||
)
|
||||
# 优先走交易所 amount_to_precision;失败则按精度位数向下取整
|
||||
suggest_contracts = None
|
||||
try:
|
||||
precise = float(ex.amount_to_precision(sym, raw_contracts))
|
||||
if precise > raw_contracts + 1e-12:
|
||||
precise = floor_contracts_to_precision(raw_contracts, amount_precision)
|
||||
suggest_contracts = precise
|
||||
except Exception:
|
||||
suggest_contracts = floor_contracts_to_precision(raw_contracts, amount_precision)
|
||||
|
||||
return {
|
||||
"exchange_symbol": sym,
|
||||
@@ -331,7 +347,11 @@ def _fetch_perp_market(cfg: dict[str, Any], base: str) -> tuple[dict[str, Any],
|
||||
"contract_size": cs,
|
||||
"available_usdt": available,
|
||||
"full_margin_sizing": sizing,
|
||||
"suggest_contracts": round(suggest_contracts, 6) if suggest_contracts is not None else None,
|
||||
"suggest_contracts": suggest_contracts,
|
||||
"amount_precision": amount_precision,
|
||||
"unit_quote": "USDT",
|
||||
"unit_contracts": "合约张",
|
||||
"unit_note": "价格单位 USDT;张数=交易所永续合约张(与下单精度一致);名义≈张数×面值×价格",
|
||||
"entry_ref": entry or None,
|
||||
}, None
|
||||
|
||||
|
||||
@@ -39,12 +39,14 @@
|
||||
</select>
|
||||
</div>
|
||||
<div id="hp-perp-quote" class="muted hp-quote-line">加载中…</div>
|
||||
<p class="muted hp-unit-hint">单位说明:价格=USDT · 张数=交易所<strong>永续合约张</strong>(精度与 OKX 下单一致) · 盈亏=USDT</p>
|
||||
<div class="form-row" style="flex-wrap:wrap">
|
||||
<label>开仓价 <input type="number" step="any" id="hp-entry" /></label>
|
||||
<label>止盈 <input type="number" step="any" id="hp-tp" /></label>
|
||||
<label>止损 <input type="number" step="any" id="hp-sl" /></label>
|
||||
<label>张数 <input type="number" step="any" id="hp-contracts" /></label>
|
||||
<label>开仓价 <span class="hp-unit">USDT</span> <input type="number" step="any" id="hp-entry" /></label>
|
||||
<label>止盈 <span class="hp-unit">USDT</span> <input type="number" step="any" id="hp-tp" /></label>
|
||||
<label>止损 <span class="hp-unit">USDT</span> <input type="number" step="any" id="hp-sl" /></label>
|
||||
<label>张数 <span class="hp-unit">合约张</span> <input type="number" step="any" id="hp-contracts" /></label>
|
||||
</div>
|
||||
<p class="muted" id="hp-perp-pnl-line"></p>
|
||||
<p class="muted" id="hp-sizing-line"></p>
|
||||
</div>
|
||||
<div class="card hp-opt-card">
|
||||
@@ -75,9 +77,10 @@
|
||||
</div>
|
||||
<div class="form-row hp-pick-row">
|
||||
<label>已选 <code id="hp-sel-inst">—</code></label>
|
||||
<label>张数 <input type="number" step="1" min="1" id="hp-sheets" value="1" /></label>
|
||||
<label>张数 <span class="hp-unit">期权张</span> <input type="number" step="1" min="1" id="hp-sheets" value="1" /></label>
|
||||
<span class="muted" id="hp-premium-line"></span>
|
||||
</div>
|
||||
<p class="muted hp-unit-hint">单位说明:权利金结算币=<strong>USDC</strong> · 张数=期权张(整张) · 卖一/买一=价格/张</p>
|
||||
<div id="hp-opt-bal-line" class="muted hp-quote-line hp-opt-bal-line"></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -192,4 +195,4 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="/static/hedge_plan.js?v=6"></script>
|
||||
<script src="/static/hedge_plan.js?v=7"></script>
|
||||
|
||||
Reference in New Issue
Block a user