fix: unify order/key focus K-line theme, PnL, RR and exchange price tick

Share focus_chart templates and APIs across four instances; align chart Y-axis, price lines and meta bar with exchange symbol precision and live unrealized PnL.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-04 16:45:51 +08:00
parent 3d55aa0975
commit 93e148a3e7
18 changed files with 1366 additions and 2128 deletions
+57 -21
View File
@@ -6290,35 +6290,51 @@ def api_order_kline():
"volume": float(bar[5]),
})
from focus_chart_lib import (
build_order_kline_order_payload,
load_swap_positions_for_order_kline,
metrics_for_order_item,
)
current_price = get_price(order_item["symbol"])
margin = float(order_item.get("margin_capital") or 0)
leverage = float(order_item.get("leverage") or 0)
entry = float(order_item.get("trigger_price") or 0)
float_pnl = calc_pnl(order_item.get("direction") or "long", entry, current_price, margin, leverage) if current_price else 0
float_pct = round((float_pnl / margin * 100), 4) if margin > 0 else 0
positions = load_swap_positions_for_order_kline(
exchange,
private_configured=exchange_private_api_configured(),
ensure_markets_fn=ensure_markets_loaded,
)
ex_metrics = metrics_for_order_item(
order_item,
positions,
resolve_ex_sym_fn=resolve_monitor_exchange_symbol,
select_live_fn=_select_live_position_row,
parse_metrics_fn=parse_ccxt_position_metrics,
)
order_payload = build_order_kline_order_payload(
order_item,
ticker_price=current_price,
format_price_fn=format_price_for_symbol,
calc_pnl_fn=calc_pnl,
calc_rr_ratio_fn=calc_rr_ratio,
ex_metrics=ex_metrics,
)
from focus_chart_lib import kline_api_price_fields
price_fields = kline_api_price_fields(
exchange,
exchange_symbol,
candles,
ensure_markets_fn=ensure_markets_loaded,
)
return jsonify({
"ok": True,
"timeframe": timeframe,
"limit": limit,
"order": {
"id": order_item["id"],
"symbol": order_item["symbol"],
"direction": order_item.get("direction") or "long",
"trigger_price": order_item.get("trigger_price"),
"stop_loss": order_item.get("stop_loss"),
"take_profit": order_item.get("take_profit"),
"margin_capital": order_item.get("margin_capital"),
"leverage": order_item.get("leverage"),
"position_ratio": order_item.get("position_ratio"),
"rr_ratio": order_item.get("rr_ratio"),
"breakeven_enabled": bool(int(order_item.get("breakeven_enabled") or 0)),
"current_price": round(float(current_price), 8) if current_price else None,
"float_pnl": round(float(float_pnl), 6),
"float_pct": float_pct,
},
"order": order_payload,
"candles": candles,
"updated_at": app_now_str(),
**price_fields,
})
@@ -6419,15 +6435,35 @@ def api_key_kline():
"lower_pct": lower_pct,
}
from focus_chart_lib import enrich_key_kline_response
price_display, key_info = enrich_key_kline_response(
symbol=symbol,
current_price=current_price,
key_info=key_info,
format_price_fn=format_price_for_symbol,
)
from focus_chart_lib import kline_api_price_fields
price_fields = kline_api_price_fields(
exchange,
exchange_symbol,
candles,
ensure_markets_fn=ensure_markets_loaded,
)
return jsonify({
"ok": True,
"symbol": symbol,
"timeframe": timeframe,
"limit": limit,
"current_price": round(float(current_price), 8) if current_price is not None else None,
"current_price_display": price_display,
"key_monitor": key_info,
"candles": candles,
"updated_at": app_now_str(),
**price_fields,
})